你不需要打电话
.at()
如果您正在使用
.new()
.您使用
at()
当您想要与已部署到区块链的合同进行交互时。
如果选择部署新契约,则应使用异步版本的方法调用(web3j 1.0不再支持同步)。你的电话应该是这样的:
VotingContract.new(['Rama','Nick','Jose'],{data: byteCode, from: web3.eth.accounts[0], gas: 4700000}, (error, deployedContract) => {
if (!error) {
if (deployedContract.address) {
console.log(deployedContract.totalVotesFor.call('Rama'));
}
}
});
请注意,回调被触发两次。首次提交交易(
deployedContract.transactionHash
将被设置),并在挖掘事务后的第二次。
您还可以查看
web3js docs
(为了方便粘贴在下面)。
const fs = require("fs");
const solc = require('solc')
let source = fs.readFileSync('nameContract.sol', 'utf8');
let compiledContract = solc.compile(source, 1);
let abi = compiledContract.contracts['nameContract'].interface;
let bytecode = compiledContract.contracts['nameContract'].bytecode;
let gasEstimate = web3.eth.estimateGas({data: bytecode});
let MyContract = web3.eth.contract(JSON.parse(abi));
var myContractReturned = MyContract.new(param1, param2, {
from:mySenderAddress,
data:bytecode,
gas:gasEstimate}, function(err, myContract){
if(!err) {
// NOTE: The callback will fire twice!
// Once the contract has the transactionHash property set and once its deployed on an address.
// e.g. check tx hash on the first call (transaction send)
if(!myContract.address) {
console.log(myContract.transactionHash) // The hash of the transaction, which deploys the contract
// check address on the second call (contract deployed)
} else {
console.log(myContract.address) // the contract address
}
// Note that the returned "myContractReturned" === "myContract",
// so the returned "myContractReturned" object will also get the address set.
}
});