【发布时间】:2018-12-28 08:46:03
【问题描述】:
目前,我已成功将智能合约部署到 Rinkeby 测试网,但使用 web3 版本 1.0 访问相关方法时遇到问题。
这是我的 web3 代码,它实例化了一个合约实例并调用了一个合约方法:
const contractInstance = new web3.eth.Contract(abiDefinition, contractAddress);
var value = web3.utils.toWei('1', 'ether')
var sentTransaction = contractInstance.methods.initiateScoreRetrieval().send({value: value, from: fromAddress})
console.log('event sent, now set listeners')
sentTransaction.on('confirmation', function(confirmationNumber, receipt){
console.log('method confirmation', confirmationNumber, receipt)
})
sentTransaction.on('error', console.error);
这是我的智能合约,或者更确切地说是它的一个版本,被剥离到相关的部分:
contract myContract {
address private txInitiator;
uint256 private amount;
function initiateScoreRetrieval() public payable returns(bool) {
require(msg.value >= coralFeeInEth);
amount = msg.value;
txInitiator = msg.sender;
return true;
}
}
我无法访问在 web3 端设置事件侦听器的 console.log,并且我没有收到任何类型的错误。我当然不会从实际的事件监听器那里得到控制台。我猜我发送交易的方式有问题,但我认为我正确地遵循了下面记录的模式:https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-send
有没有人知道如何使用 web3 1.0 来正确调用合约方法?我在传递选项等方面做错了吗?
谢谢!
【问题讨论】:
-
更改您的事件发射器以检查
transactionHash和receipt。事务哈希的回调将是您返回的第一个响应。如果你得到了,但从未得到收据,那么你的交易就没有被挖掘。我还将明确包含gasPrice和gas值。最后,您是否在 Etherscan 上确认您的合约已正确部署?
标签: blockchain ethereum solidity web3