GoG*_*eme 33 solidity erc20 openzeppelin
尝试运行:
\n1.) Dappuniversity 项目 (dappuniversity/dbank)\n2.) pet-shop-tutorial
\nTruffle v5.3.3(核心:5.3.3)\nNode v14.15.5
\n当 Truffle 要求 it\xe2\x80\x99s 编译器/solc 匹配 5.3.3 时,ser 如何在 0.8.4 编译代码以导入 OpenZeppelin\xe2\x80\x99s ERC20.sol 模板?
\n// SPDX-License-Identifier: MIT\n\npragma solidity ^0.8.4;\n\nimport "@openzeppelin/contracts/token/ERC20/ERC20.sol";\n\ncontract Token is ERC20 {\n\n //add minter variable\n\n //add minter changed event\n\n constructor() public payable ERC20("Name", "Symbol") {\n\n //assign initial minter\n\n }\n\n //Add pass minter role function\n\n function mint(address account, uint256 amount) public {\n\n //check if msg.sender has a minter role\n\n _mint(account, amount);\n\n }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n未找到源 \xe2\x80\x9c@openzeppelin/contracts/token/ERC20/ERC20.sol\xe2\x80\x9d:不支持文件导入回调
\nAso*_*ace 65
错误:
Source "@openzeppelin/contracts/token/ERC20/ERC20.sol" not found: File import callback not supported

选择localNodeModule
可能需要重新启动 IDE

小智 9
对于我(运行 Win 10)来说,当我清除 VSCode Solidity 扩展中的设置时,此错误得到解决。
扩展菜单
--> 右键单击 Juan Blanco 的 Solidity
--> 扩展设置
--> 滚动到“Solidity:Package Default Dependency Contracts Directory”
--> 删除默认值
默认值将事物指向错误的路径。
https://github.com/juanfranblanco/vscode-solidity/issues/178
小智 8
如果node_modules包含要导入的脚本的目录不在 VSCode 工作区的根目录下,您可以手动将 Solidity 扩展指向它,如下所示.vscode/settings.json:
{
"solidity.packageDefaultDependenciesDirectory": "path/to/sub/dir/node_modules"
}
Run Code Online (Sandbox Code Playgroud)
该错误是由 solc-js 编译器引起的。GitHub 页面是https://github.com/ethereum/solc-js
您需要使用导入回调进行编译,我不知道 Truffle 如何处理这个问题,但如果您以编程方式编译自己,则必须使用导入回调,如以下代码所示(示例取自 GitHub 页面,findImports功能更改为它对我的工作方式):
const solc = require('solc');
const input = {
language: 'Solidity',
sources: {
'test.sol': {
content: 'import "lib.sol"; contract C { function f() public {
L.f(); } }'
}
},
settings: {
outputSelection: {
'*': {
'*': ['*']
}
}
}
};
function findImports(relativePath) {
//my imported sources are stored under the node_modules folder!
const absolutePath = path.resolve(__dirname, 'node_modules', relativePath);
const source = fs.readFileSync(absolutePath, 'utf8');
return { contents: source };
}
// New syntax (supported from 0.5.12, mandatory from 0.6.0)
var output = JSON.parse(
solc.compile(JSON.stringify(input), { import: findImports })
);
Run Code Online (Sandbox Code Playgroud)
如果您使用 VSCode,则当您的 IDE 无法解析导入路径时会导致此错误。
某些合同包在contracts文件夹中包含合同,而其他合同包可能包含包含以下内容的子文件夹:contracts中可能包含包含文件夹的子文件夹,这会导致路径错误。
如果您使用 Juan 的 Solidity 扩展,请确保您的重新映射正确:
settings.json这是如果您选择修改重新映射则会弹出的文件示例。请注意,重新映射模板是NAME_OF_PACKAGE/=node_modules/PATH_TO_PACKAGE:
{
...,
"solidity.remappingsUnix": [
"@uniswap/=node_modules/@uniswap/",
"@openzeppelin/=node_modules/@openzeppelin/"
]
}
Run Code Online (Sandbox Code Playgroud)
小智 0
ERC20文件需要其他文件
pragma solidity ^0.8.0;
import "./IERC20.sol";
import "./extensions/IERC20Metadata.sol";
import "../../utils/Context.sol";
Run Code Online (Sandbox Code Playgroud)
目前尚不清楚您是否已正确安装 OpenZeppelin。