我想通过web3js使用合同的属性和方法:
版本:
"ganache-cli": "^6.2.3",
"mocha": "^6.0.1",
"solc": "^0.5.0",
"web3": "^1.0.0-beta.37"
Imooc.sol:
pragma solidity ^0.5.0;
contract Course {
string public name;
constructor(string memory _name) public {
name = _name;
}
function getName() public view returns (string memory) {
return name;
}
}
此合同在混音中效果很好。
在我的项目中,我使用摩卡咖啡来测试合同。
course.spec.js:
const path = require('path');
const assert = require('assert');
const Web3 = require('web3');
const web3 = new Web3('ws://localhost:8545');
const Imooc = require(path.resolve(__dirname, '../src/compiled/Imooc.json'));
let accounts;
describe('test', () => {
before(async () => {
accounts = await web3.eth.getAccounts();
myCourse = await new web3.eth.Contract(Imooc.Course.abi, accounts[0]);
myCourse.deploy({
data: Imooc.Course.evm.bytecode.object,
arguments: ['vue course']
}).send({
from: accounts[0]
});
});
it('test 1', async () => {
const name = await myCourse.methods.name().call();
// const name = await myCourse.methods.getName().call();
console.log(name);
})
});
我不知道为什么这样的简单代码无法正常工作,我查看了web3js文档并发出了问题,但无法解决。我尝试将ganache更新为6.3.0,将web3js更新为beta.47,它没有任何变化。 / p>