我正在使用 Hardhat 的主网分支编写单元测试,对于测试,我想检查ownerERC20 代币(特别是 DAI)账户的初始余额。这是我到目前为止的测试:
const { ethers } = require("hardhat");
describe("Simple Test", function () {
it("Check balance of DAI", async function () {
const provider = ethers.provider;
const [owner] = await ethers.getSigners();
// Want to get initial DAI balance here so it can be compared later
});
});
Run Code Online (Sandbox Code Playgroud)
有什么简单的方法可以做到这一点?
我正在开发两个智能合约,其部署顺序很重要。首先是 ERC20 代币,然后将其部署地址传递给 ERC721 智能合约的构造函数。两个触点的交互已在Hardhat上成功测试。但是,在遵循Hardhat 教程时,我尝试使用以下脚本将这些智能合约部署到RSK regtest本地节点:\n\xe2\x80\x8b
\nconst totalSuply = 1e6;\n\xe2\x80\x8b\nasync function main() {\n \n const ERC20 = await ethers.getContractFactory(\'ERC20\');\n const erc20 = await ERC20.deploy(totalSuply);\n\xe2\x80\x8b\n const ERC721 = await ethers.getContractFactory(\'ERC721\');\n const erc721 = await ERC721.deploy(erc20.address);\n \n}\n\xe2\x80\x8b\nmain()\n .then(() => process.exit(0))\n .catch((error) => {\n console.error(error);\n process.exit(1);\n });\nRun Code Online (Sandbox Code Playgroud)\n\xe2\x80\x8b\n我收到以下错误:
\nError: \ntransaction failed \n[ See: https://links.ethers.org/v5-errors-CALL_EXCEPTION ](transactionHash="0xac83bdd94bf43eaec2e930497aa9a637883ad870749e5ee14cf8ba4ff332a810",\ntransaction {\n"hash":"0xac83bdd94bf43eaec2e930497aa9a637883ad870749e5ee14cf8ba4ff332a810",\n"type":0,\n"accessList":null,\n"blockHash":null,\n"blockNumber":null,\n"transactionIndex":null,\n"confirmations":0,\n"from":"0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",\n"gasPrice":{"type":"BigNumber","hex":"0x00"},\n"gasLimit":{"type":"BigNumber","hex":"0x1204a5"},\n"to":null,"value":{"type":"BigNumber","hex":"0x00"},\n"nonce":736,"data":...\n\xe2\x80\x8b\nRun Code Online (Sandbox Code Playgroud)\n\xe2\x80\x8b\n根据指定的链接,最可能的原因是:
\n\n\n如果您未能等到合约部署,则可能会发生这种情况。
\n
既然我已经await在进行部署了,这怎么可能呢?有没有人在通过 Hardhat 在 RSK 上部署智能合约时遇到类似的问题?\n
\n作为参考,这是hardhat.config.js我正在使用的文件:\xe2\x80\x8b
我在尝试在安全帽开发链上部署模拟合约时收到以下错误。
\n我正在关注:
\n\n\n使用 JavaScript 学习区块链、Solidity 和全栈 Web3 开发 \xe2\x80\x93 由 Patrick Collins 在 FreeCodeCamp YT 频道上提供的 32 小时课程
\n
我在上述课程的 10:48:53 时收到此错误。我几乎尝试了所有方法,但没有任何帮助,但我找到了问题发生的地方。
\n问题:我认为问题出在哪里?(12:26)
\n\n\n据我所知,问题出在我们尝试部署脚本并等待返回承诺的行。
\n
这是错误:
\nError: ERROR processing /Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/deploy/00-deploy-mocks.js:\nTypeError: Cannot read property \'length\' of undefined\n at getFrom (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat-deploy/src/helpers.ts:1713:14)\n at _deploy (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat-deploy/src/helpers.ts:533:9)\n at processTicksAndRejections (internal/process/task_queues.js:95:5)\n at _deployOne (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat-deploy/src/helpers.ts:1004:16)\n at Object.module.exports [as func] (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/deploy/00-deploy-mocks.js:12:26)\n at DeploymentsManager.executeDeployScripts (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1219:22)\n at DeploymentsManager.runDeploy (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1052:5)\n at SimpleTaskDefinition.action (/Users/rishavsharma/Documents/working/Web Dev/Blockchain/Freecodecamp/solidity&Block/blockchain-with-js/buyMeACoffee-dapp/node_modules/hardhat-deploy/src/index.ts:438:5)\n at …Run Code Online (Sandbox Code Playgroud) 这是一个非常小的存储库来显示该问题:https ://github.com/adamdry/ethers-event-issue
但我也会在这里解释一下。这是我的合同:
//SPDX-License-Identifier: UNLICENSED;
pragma solidity 0.8.4;
contract ContractA {
event TokensMinted(uint amount);
function mint(uint amount) public {
emit TokensMinted(amount);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的测试代码:
import * as chai from 'chai'
import { BigNumber, ContractTransaction } from 'ethers'
import { ethers } from 'hardhat'
import { ContractA, ContractAFactory } from '../typechain'
const expect = chai.expect
describe("Example test", function () {
it("should fire the event", async function () {
const [owner] = await ethers.getSigners();
const contractAFactory = (await ethers.getContractFactory(
'ContractA',
owner, …Run Code Online (Sandbox Code Playgroud) 我正在尝试遵循此处的NFT 教程。我已经在 Alchemy 上设置了帐户,并且 Metamask 创建了 .sol 文件。我的根目录中有一个 .env 文件,如下所示:
API_URL = "https://eth-ropsten.alchemyapi.io/v2/your-api-key"
PRIVATE_KEY = "your-metamask-private-key"
Run Code Online (Sandbox Code Playgroud)
我的安全帽配置文件如下所示:
/**
* @type import('hardhat/config').HardhatUserConfig
*/
require('dotenv').config();
require("@nomiclabs/hardhat-ethers");
const { API_URL, PRIVATE_KEY } = process.env;
module.exports = {
solidity: {
compilers: [
{
version: "0.5.7"
},
{
version: "0.8.0"
},
{
version: "0.6.12"
}
]
},
defaultNetwork: "ropsten",
networks: {
hardhat: {},
ropsten: {
url: API_KEY,
accounts: [`0x${PRIVATE_KEY}`]
}
},
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试编译时,我不断收到此错误:
HardhatConfig.networks 的值 {"url":"https://eth-ropsten.alchemyapi.io/v2/your-api-key","accounts":["0xyour-metamask-private-key"]} 无效。 ropsten - 需要 HttpNetworkConfig 类型的值。
我似乎无法弄清楚为什么这不是 …
我正在尝试遵循本指南,但我正在努力运行安全帽。按照文章中的命令操作后:
mkdir hardhat-tutorial
cd hardhat-tutorial
npm init --yes
npm install --save-dev hardhat
npx hardhat
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息,指出npm ERR! could not determine executable to run. 可以通过导航到该npm_modules/hardhat目录来运行安全帽工具,但这似乎会干扰本教程后面的命令,因为该工具与其所使用的文件位于不同的目录中。
我不确定我是否从根本上误解了安全帽的工作原理。您是否应该在每个项目中安装它,并将您的项目目录放在其中npm_modules/hardhat,还是我弄乱了安装/配置?
似乎已经有一个涉及此问题的问题,但遗憾的是它被删除了,我还没有找到其他解决方案。
fallback() external {
console.log(msg.data);
(bool result, ) = address(delegate).delegatecall(msg.data);
if (result) {
console.log("SUCESS");
this;
}
}
Run Code Online (Sandbox Code Playgroud)
回报
TypeError: Member "log" not found or not visible after argument-dependent lookup in type(library console).
console.log(msg.data);
^---------^
Run Code Online (Sandbox Code Playgroud)
console.logged 的数据类型是否有限制?参数依赖查找到底意味着什么?
我尝试部署我的 masterchef 合约。我在构造函数中指定了 2 个参数(代币地址和以秒为单位的实际块号)。我使用安全帽来执行此操作,并且实际上我试图以秒为单位获取实际块编号的值?我怎样才能做到这一点 ?
const StakingRewards = await hre.ethers.getContractFactory("Masterchef");
const stakingRewards = await StakingRewards.deploy(token.address, block.timestamp);
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建 v3 uniswap 池的实例。我正在使用安全帽和孟买测试网的分支。
当我尝试创建池实例时:
const poolExample = new Pool(
TokenA,
TokenB,
immutables.fee,
state.sqrtPriceX96.toString(),
state.liquidity.toString(),
state.tick
);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Error: Invariant failed: PRICE_BOUNDS
Run Code Online (Sandbox Code Playgroud)
堆栈是:
Error: Invariant failed: PRICE_BOUNDS
at invariant (node_modules/tiny-invariant/dist/tiny-invariant.cjs.js:14:11)
at new Pool (node_modules/@uniswap/v3-sdk/src/entities/pool.ts:71:5)
at Context.<anonymous> (test/Uniswap-test.js:134:25)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Run Code Online (Sandbox Code Playgroud)
有关追踪错误根源的任何提示吗?
池的参数具有以下值:
fee : 3000
state.sqrtPriceX96 : 0
state.liquidity: 0
state.tick: 0
Run Code Online (Sandbox Code Playgroud) 假设我有一个 ERC20 智能合约,正在Hardhat中开发和测试:
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract Erc20Token is ERC20, Ownable {
constructor(uint initialSupply) ERC20("ERC20 Token", "ETK") {
ERC20._mint(msg.sender, initialSupply);
}
}
Run Code Online (Sandbox Code Playgroud)
我打算使用以下 Hardhat脚本将其部署到RSKdeploy.js网络:
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract Erc20Token is ERC20, Ownable {
constructor(uint initialSupply) ERC20("ERC20 Token", "ETK") {
ERC20._mint(msg.sender, initialSupply);
}
}
Run Code Online (Sandbox Code Playgroud)
要运行此脚本并在RSK 主网上部署我的智能合约,我需要在终端中运行以下命令:
async function deploy() {
try {
const initialSupply = 1e6;
const ERC20TokenFactory = await ethers.getContractFactory('Erc20Token');
const erc20Token …Run Code Online (Sandbox Code Playgroud) hardhat ×10
solidity ×5
ethereum ×4
ethers.js ×3
javascript ×3
rsk ×2
blockchain ×1
erc20 ×1
uniswap ×1