这是我一直在处理的一个问题。我的临时解决方案是在我的 Contracts 目录中创建一个 SafeMath.sol 文件并直接从那里导入它。不过,我一直在寻找这样的“清晰的解决方案” ......老办法似乎是直接从GitHub的链接导入它,因为在某些回购和其他堆栈溢出的职位像看到这样的
但是,正确的方法似乎是安装相应的 oz 包(@openzeppelin/contracts-ethereum-package)并将文件直接导入到所需的合约中,即
import "@openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol";
但是,使用 VSCode,我仍然收到错误Source "@openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol" not found: File import callback not supported
也就是说,如何正确导入 SafeMath?
编辑:我正在使用 pragma solidity ^0.6.0;
每当我尝试编译我的 Solidity 合约时,ParserError: Source \"@OpenZeppelin/contracts/math/SafeMath.sol\" not found: File import callback not supported都会抛出错误。
pragma solidity ^0.7.0;
import "@OpenZeppelin/contracts/token/ERC20/ERC20.sol";
import "@OpenZeppelin/contracts/math/SafeMath.sol";
Run Code Online (Sandbox Code Playgroud)
任何想法可能导致这种情况?
我正在尝试开始使用应该是非常简单的 Solidity 合同,但是 VSCode 给我带来了困难。我正在使用 Juan Blancos solidity 插件,但 VSCode 找不到 openzepplin 导入
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
错误是:
找不到源“@openzeppelin/contracts/token/ERC20/ERC20.sol”:不支持文件导入回调
即使 vscode 显示红色波浪线,我也可以通过hardhat compile成功编译。
如果我将路径更改为
import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
VScode 停止抱怨,但随后我无法通过 hardhard 进行编译,错误是:
未找到源“node_modules/@openzeppelin/contracts/security/Pausable.sol”:允许目录之外的文件。
我的用户和工作区 Solidity 扩展的 VSCode 设置是:
"solidity.packageDefaultDependenciesContractsDirectory": "contracts",
"solidity.packageDefaultDependenciesDirectory": "node_modules"
Run Code Online (Sandbox Code Playgroud)
这对应于我的项目结构
root
|_ contracts
|_ MyToken.sol
|_ node_modules
|_ @openzepplin
Run Code Online (Sandbox Code Playgroud)
我按照此处的说明进行了广泛的研究,但不幸的是无法使其正常工作。
我看到的大多数使用 Open Zeppelin 的 ERC721 示例都要求 mint 函数具有访问控制,仅允许合约所有者调用该函数。例如,
function mint(address to) public virtual {
require(hasRole(MINTER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have minter role to mint");
_mint(to, _tokenIdTracker.current());
_tokenIdTracker.increment();
}
Run Code Online (Sandbox Code Playgroud)
或使用Ownable库进行以下操作。
function mint(address receiver) external onlyOwner returns (uint256) {
_tokenIds.increment();
uint256 newTokenId = _tokenIds.current();
_mint(receiver, newTokenId);
return newTokenId;
}
Run Code Online (Sandbox Code Playgroud)
这是否意味着每次铸造新代币时都必须部署新合约?这不仅在 Gas 费用方面显得过高,而且 ERC721 合约具有映射不同所有者和代币的属性:
// Mapping from token ID to owner address
mapping (uint256 => address) private _owners;
// Mapping owner address to token count
mapping (address => uint256) private …Run Code Online (Sandbox Code Playgroud) 我正在尝试从打开的 zeppelin 导入一些合约文件,以便我的 Solidity 智能合约可以继承它们的功能,当尝试编写在编译时在我的智能合约上运行的 chai 测试时,我在 chai 测试中遇到错误。
3 passing (2s)
1 failing
1) Contract: Color
minting
creates a new token:
TypeError: contract.totalSupply is not a function
Run Code Online (Sandbox Code Playgroud)
我的合同导入了 openzeppelin 合同
pragma solidity 0.8.7;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; //import base functionality
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol"; //import totalsupply()
contract color is ERC721 {
string[] public colors;
mapping(string => bool) _colorExists; //mappings are like json objects where value a is searched and its value is returned
constructor() ERC721("Color", "COLOR") {
}
function mint(string memory _color) public{ …Run Code Online (Sandbox Code Playgroud) 在OpenZeppelin ERC20实现中,有一个_transfer方法:
function _transfer(
address sender,
address recipient,
uint256 amount
) internal virtual {
require(sender != address(0), "ERC20: transfer from the zero address");
require(recipient != address(0), "ERC20: transfer to the zero address");
_beforeTokenTransfer(sender, recipient, amount);
uint256 senderBalance = _balances[sender];
require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");
unchecked {
_balances[sender] = senderBalance - amount;
}
_balances[recipient] += amount;
emit Transfer(sender, recipient, amount);
_afterTokenTransfer(sender, recipient, amount);
}
Run Code Online (Sandbox Code Playgroud)
为什么他们使用未经检查的算术来减少余额?我知道,如果未经检查,2-3 将返回 2**256-1 并且不会出现异常。但为什么我们需要这个?
我正在编写关于 Solidity 0.8.3 的合同,_setTokenURI()尽管该方法是在 OpenZeppelin 4.X 中定义的,但我收到了这个奇怪的错误。
pragma solidity ^0.8.3;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract NFTB is ERC721 {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
mapping(string => uint8) hashes;
constructor() public ERC721("NFTB", "NFTB") {}
function awardItem(address recipient, string memory hash, string memory metadata) public returns (uint256) {
require(hashes[hash] != 1);
hashes[hash] = 1;
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_setTokenURI(newItemId, metadata);
_mint(recipient, newItemId);
return newItemId;
} }
Run Code Online (Sandbox Code Playgroud)
以下是我的智能合约(已部署)。当我尝试验证它以将代码提交到 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) 我正在开发荷兰式拍卖风格的 ICO 合约,目前正在尝试迁移 ERC20 合约的早期阶段以测试基本功能(它是否具有正确的名称、符号和小数点)。合同可以编译,但我无法迁移它,因为它是“抽象合同”。我的代币合约继承自ERC20Detailed,即Open Zeppelin合约,而后者又继承自IERC20接口合约。我可以做什么来解决这个问题?我尝试让我的代币合约也从 ERC20 继承基础合约,但它说标识符已经声明了。我看到了 Truffle 终端输出可能的响应,但我很好奇为什么我的实现不起作用,并且希望获得更多帮助来理解 Solidity 接口和抽象合约。
我可以做什么来解决这个问题?我尝试让我的代币合约也从 ERC20 继承基础合约,但它说标识符已经声明了。
pragma solidity ^0.5.8;
import "node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20Detailed.sol";
contract Token is ERC20Detailed{
constructor(string memory _name, string memory _symbol, uint8 _decimals)
ERC20Detailed(_name, _symbol, _decimals)
public
{
}
}
Run Code Online (Sandbox Code Playgroud)
Bash 终端的输出
“Token”是抽象合约或接口,无法部署。* 将抽象导入到使用它们的“.sol”文件中,而不是单独部署它们。* 继承抽象的合约必须准确实现其所有方法签名。* 仅实现继承抽象的一部分的合约也被认为是抽象的。
openzeppelin ×9
solidity ×9
ethereum ×4
hardhat ×2
truffle ×2
blockchain ×1
erc20 ×1
nft ×1
node.js ×1