我正在尝试使用solc version 0.5.1
和web3 version 1.0.0-beta.36
. Number
合约被部署并contractAddress
在收到时返回receipt
。
虽然看起来很麻烦,但为了更容易重现,我已经在这里编写了所有代码。我遇到的问题是在最后一个块中(与合同交互)。
const solc = require('solc');
const Web3 = require('web3');
const input = {
language: 'Solidity',
sources: {
solContract: {
content: `
pragma solidity ^0.5.1;
contract Number {
uint private num = 100;
function getNum() public view returns(uint) {
return num;
}
function setNum(uint _num) public returns(bool success) {
num = _num;
return true;
}
}`
}
},
settings: {
outputSelection: {
'*': {
'*': ['*']
}
}
}
}
const output = JSON.parse(solc.compile(JSON.stringify(input)))
const abi = output.contracts.solContract.Number.abi
const bytecode = `0x${output.contracts.solContract.Number.evm.bytecode.object}`
const web3 = new Web3('http://127.0.0.1:8555');
const privateKey = '0x82f74b773d7f948153d7eb8c192bd9819e3e94073d8bdc0e03d659aa42cd34ba';
// const account = web3.eth.accounts.privateKeyToAccount(privateKey);
const contract = new web3.eth.Contract(abi);
web3.eth.personal.unlockAccount('0x27f4cD26d7e4eAde2052ec5B61f6594D1481C4A2', 'passwordstring', 600)
.then(
contract.deploy({
data: bytecode,
arguments: []
})
.send({
from: '0x27f4cD26d7e4eAde2052ec5B61f6594D1481C4A2',
gas: 0x47b760
}, (error, transactionHash) => {
if (error) {
console.log(error)
} else if (transactionHash) {
web3.eth.getTransaction(transactionHash)
.then((data) => {
console.log(data)
})
}
})
.on('receipt', (receipt) => {
console.log(receipt.contractAddress)
}))
// Interacting with deployed contract
// contract address returned from above
// 0x101ea6703717Fa3CF70e96FD3C84FE74ddca50eB
contract.options.address = '0x101ea6703717Fa3CF70e96FD3C84FE74ddca50eB'
contract.methods.getNum().call({
from: 0x27f4cD26d7e4eAde2052ec5B61f6594D1481C4A2,
gas: 0x47b760
}, (err, data) => {
err ? console.log(err) : console.log(data)
})