我正在尝试分叉 Safemoon(或者实际上是 NotSafeMoon),并将其用作学习智能合约开发的工具。(我拥有大量您可能称之为“Web 2.0”的开发经验)。
假设我的构造函数中有类似的内容:
constructor () {
_rOwned[_msgSender()] = _rTotal;
IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x10ED43C718714eb63d5aA57B78B54704E256024E); // binance PANCAKE V2
uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory()).createPair(address(this), _uniswapV2Router.WETH());
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,npx hardhat test出现以下失败:
Compilation finished successfully
TestToken contract
Deployment
1) "before each" hook for "Has the right name"
0 passing (807ms)
1 failing
1) TestToken contract
"before each" hook for "Has the right name":
Error: Transaction reverted: function call to a non-contract account
Run Code Online (Sandbox Code Playgroud)
现在,这确实很有意义,毕竟我正在尝试调用 Pancakeswap v2 路由器合约。我如何绕过这个限制?有没有办法将路由器的合约地址作为环境变量注入?我可以使用 UniswapRouter 的模拟构造函数吗?一般来说,这种事情是如何通过智能合约开发以保持可测试的方式完成的(以及如何测试)?
在你的合约中,如果你有接收枚举类型的方法,你将如何从安全帽脚本传递参数?
contract SomeContract {
enum WinStatus {
PENDING,
LOST,
WON
}
WinStatus status;
function updateWinStatus(WinStatus _status) public {
status = _status;
}
}
Run Code Online (Sandbox Code Playgroud)
// in your hardhat script
...
await someContract.updateWinStatus() // how should i call it. bare in mind hardhat is setup using javascript not typescript in my case.
Run Code Online (Sandbox Code Playgroud)
我尝试传递一个数字,希望它能按顺序(索引)获取它。但我收到“无效的 BigNumber 值”。另外,我尝试传递像“PENDING”或“WinType.PENDING”这样的字符串:思考:
我正在 Hardhat 中开发智能合约并在RSK Testnet上进行测试。\n为了创建签名者帐户,我使用助记符种子短语\n和以下 Hardhat 配置:\n\xe2\x80\x8b
\nrequire(\'@nomicfoundation/hardhat-toolbox\');\nconst { mnemonic } = require(\'./.secret.json\');\n\xe2\x80\x8b\n/** @type import(\'hardhat/config\').HardhatUserConfig */\nmodule.exports = {\n solidity: \'0.8.16\',\n networks: {\n hardhat: {},\n rsktestnet: {\n chainId: 31,\n url: \'https://public-node.testnet.rsk.co/\',\n accounts: { // <-- here\n mnemonic,\n path: "m/44\'/60\'/0\'/0",\n },\n },\n },\n // ...\n};\nRun Code Online (Sandbox Code Playgroud)\n\xe2\x80\x8b\n在我的测试中,我通常使用ethers.js辅助函数\n在我的测试中获取签名者数组:\n\xe2\x80\x8b
const signers = await hre.ethers.getSigners();\nRun Code Online (Sandbox Code Playgroud)\n\xe2\x80\x8b\n但是这个函数返回 20 个签名者钱包。\n如果我不想要这个默认数量,有没有办法获得特定数量的钱包?
\n当尝试在测试函数中使用时,ethers.utils.parseUnits("1", "ether")会引发错误TypeError: Cannot read properties of undefined (reading 'parseUnits')。
const { deployments, ethers, getNamedAccounts } = require("hardhat")
const { assert, expect } = require("chai")
describe("FundMe", async function () {
let fundMe
let deployer
const sendValue = ethers.utils.parseUnits("1", "ether")
})
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用 parseEther 得到相同的结果。在ethers.utils.parseUnits 的文档规范中,它说只使用它。我还缺少其他功能吗?我的以太配置可能不正确吗?
以下是我的智能合约(已部署)。当我尝试验证它以将代码提交到 Etherscan 时,我收到以下错误,我真的不知道为什么。请问有人可以建议吗?
npx hardhat verify --network ropsten 0xE9abA803d6a801fce021d0074ae71256C9F24Da4
Run Code Online (Sandbox Code Playgroud)
错误信息:
Error in plugin @nomiclabs/hardhat-etherscan: More than one contract was found to match the deployed bytecode.
Please use the contract parameter with one of the following contracts:
* @openzeppelin/contracts/finance/PaymentSplitter.sol:PaymentSplitter
* contracts/MyNFTContract.sol: MyNFTContract
For example:
hardhat verify --contract contracts/Example.sol:ExampleContract <other args>
If you are running the verify subtask from within Hardhat instead:
await run("verify:verify", {
<other args>,
contract: "contracts/Example.sol:ExampleContract"
};
Run Code Online (Sandbox Code Playgroud)
MyNFTContract.sol:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/finance/PaymentSplitter.sol";
contract MyNFTContract is …Run Code Online (Sandbox Code Playgroud) 我创建了一个函数,其中一个合约实例化另一个合约,它突然使整个合约超出了大小限制。这是函数:
function createContract(E.BKLiteMeta memory meta)
public
payable
returns(address)
{
require(meta.publisherAddress == msg.sender, "only pub");
return(address(new BKopyLite(meta)));
}
Run Code Online (Sandbox Code Playgroud)
Hardhatsize-contracts报告合约大小为 24.02。当我将函数更改为:
function createContract(E.BKLiteMeta memory meta)
public
payable
returns(address)
{
require(meta.publisherAddress == msg.sender, "only pub");
return(address(0);
}
Run Code Online (Sandbox Code Playgroud)
“size-contracts”报告的大小为 4.68。那么通过调用 new 大约会增加 20K 的大小?有人可以解释一下并建议解决方法吗?(BKopyLite合约的合约规模约为17k)
当编译安全帽项目时,当它显示Nothing to Compile时。
{
"name": "HardhAtToken",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.6",
"@nomiclabs/hardhat-waffle": "^2.0.3",
"chai": "^4.3.6",
"ethereum-waffle": "^3.4.4",
"ethers": "^5.6.6",
"hardhat": "^2.9.6-dev.1"
},
"dependencies": {
"glob": "^7.2.0"
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Hardhat 验证合同源代码并将其提交到 etherscan,但遇到以下错误,并且我不明白如何解决该错误。我已经通读了代码,但我无法发现我做错了什么。请问有人可以建议吗?
我运行时遇到的错误:
npx hardhat verify --network ropsten 0xA16c8f9A5Ab944454D6404CE626E600AF0054aaa 'MyNFTPrice!
错误信息:
Error in plugin @nomiclabs/hardhat-etherscan: The constructor for contracts/MyNFTPrice.sol:MyNFTPrice has 0 parameters but 1 arguments were provided instead.
我的智能合约源文件(MyNFTPrice.sol):
//Contract based on [https://docs.openzeppelin.com/contracts/3.x/erc721](https://docs.openzeppelin.com/contracts/3.x/erc721)
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
contract MyNFTPrice is ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
constructor() public ERC721("MyNFTPrice", "NFTPRICE") {}
// Mint new NFT
function mintNFT(address recipient, string memory tokenURI) public payable {
require(msg.value …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建自己的 NFT 合约工厂,但在主网上失败了。我的钱包里有 ETH。在测试中它工作正常。这是我的合同:
//Contract based on [https://docs.openzeppelin.com/contracts/3.x/erc721](https://docs.openzeppelin.com/contracts/3.x/erc721)
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
contract NFT is ERC721URIStorage, Ownable {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
constructor() ERC721("NFT", "NFT") {}
function mintNFT(address recipient, string memory tokenURI)
public onlyOwner
returns (uint256)
{
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(recipient, newItemId);
_setTokenURI(newItemId, tokenURI);
return newItemId;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的合约部署脚本
const MyNFT = await ethers.getContractFactory("NFT");
const gasPrice = await MyNFT.signer.getGasPrice();
const estimatedGas2 = await ethers.provider.estimateGas(MyNFT.getDeployTransaction().data)
const …Run Code Online (Sandbox Code Playgroud) 我正在使用React、Hardhat、Ethers.js和Solidity制作一个简单的 dapp 。
我一直在关注 YouTube 上的教程,但我陷入了从创建的合约对象调用 Solidity 函数的部分。
每当我尝试从合约中调用函数时,它都会不断产生以下错误:
"contract runner does not support calling"
Run Code Online (Sandbox Code Playgroud)
查询合约是有效的,因为我可以获得合约的余额,但我找不到任何有关如何修复合约运行程序错误的资源。将不胜感激的帮助。下面是 React 中的代码。
"contract runner does not support calling"
Run Code Online (Sandbox Code Playgroud)
坚固性代码:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
contract Rental {
struct Bike{
string name;
uint rangePower;
uint maxSpeed;
uint batteryCapacity;
uint costPerHour;
bool isAvailable;
}
//admin variables
address owner;
uint totalHours=0;
uint totalRents=0;
uint totalEbikes;
//array of bikes
Bike[] bikes;
constructor(){
//contract deployer address
owner = msg.sender;
//initialization …Run Code Online (Sandbox Code Playgroud)