我正在尝试创建一个稳定的、0.5.10、松露和web3的ETH智能合同。一切似乎都很顺利,除了我得到:
ParserError:预期的实用化、导入指令或契约/接口/库定义。const web3 =需要量(‘web3’);
当我试图加载web3时。
我已经安装了web3 (dir {project文件夹} npm安装web3)和我的package.json (位于我的项目文件夹中):
“依赖项”:{ "web3":"^1.3.4“}
我尝试过这两种方法:从‘Web3’导入Web3;
和const Web3 = require('web3');
但是它仍然不能加载web3,我做错了什么?
导致错误的合同
pragma solidity 0.5.10;
const web3 = require('web3');
contract UserRepository {
struct User {
uint id;
bytes32 firstName;
bytes32 lastName;
}
mapping(uint => User) public users;
uint public latestUserId = 0;
address private owner;
constructor() public {
owner = msg.sender;
}
}
package.json
{
"name": "helloworld",
"version": "1.0.0",
"main": "truffle-config.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"web3": "^1.3.4"
}
}
发布于 2021-04-02 14:38:32
您正在将node.js混入到您的可靠代码中。
pragma solidity 0.5.10;
const web3 = require('web3'); // this is node.js
contract UserRepository {
删除const web3 = require('web3');
行,它将成功编译(在Remix中测试)。
在这里,我只是猜测您希望使用web3
来启用JS代码与您的智能契约进行通信(例如,用于测试目的)。
如果是这样的话,您需要创建一个单独的JS文件,该文件导入web3
并使用web3.eth.Contract实例化这个契约。
或者,由于您已经在使用Tru显编译,您可以使用他们的测试工具进行智能合同的JS测试。
https://stackoverflow.com/questions/66920255
复制相似问题