javascript - web3.eth.contracts.method 等待函数永远不会返回
问题描述
我正在开发一个与以太坊中的智能合约进行交互的项目。一切工作正常,事务工作正常等等......但我意识到,当我尝试在一个专门的等待函数之后运行一些代码时,该代码从未运行过,我真的不知道为什么。我调用的其他合约方法运行良好,代码正常执行。
编码:
onSubmit = async (event) => {
event.preventDefault()
console.log("Submitting the form...")
var day = new Date(Date.now()).getDate()
var month = new Date(Date.now()).getMonth() + 1
var year = new Date(Date.now()).getFullYear()
var today = new Date(year, month -1, 1)
var todayTime = today.getTime()
const ipfsadded = await ipfs.add({path:this.state.fileName, content:this.state.buffer})
var ipfsHash = ipfsadded.cid.toString()
this.setState({ipfsHash: ipfsHash})
console.log('hash:', ipfsHash, " / name =", this.state.fileName )
console.log("inserindo na blockchain..")
const accounts = await web3.eth.getAccounts()
//ERROR HERE!!
//THIS FUNCTION IS EXECUTED, THE TRANSACTION IS CONFIRMED IN METAMASK BUT AFTER THAT NOTHING HAPPENS
var result = await contract.methods.add(ipfsHash, this.state.fileName, this.state.fileType, todayTime).send({from:accounts[0], gas:300000});
console.log("resultado =", result) //IS NEVER EXECUTED EVEN WITH TRANSACTION OK
console.log("File submitted on blockchain!") //IS NEVER EXECUTED
}
解决方案
这很可能是因为交易没有在 750 秒内被挖掘并.send
引发错误,但没有被捕获。您可以通过用 try/catch 块包装它并检查错误来证明这一点。
try {
var result = await contract.methods.add(ipfsHash, this.state.fileName, this.state.fileType, todayTime).send({from:accounts[0], gas:300000});
} catch(err) {
console.error(err);
}
更好的方法是订阅由 公开receipt
的transactionHash
或confirmation
事件send
。
contract.methods.add(ipfsHash, this.state.fileName, this.state.fileType, todayTime)
.send({from:accounts[0], gas:300000})
.on('transactionHash', resolve)
.on('error', reject);
在你的情况下,它会是
onSubmit = async (event) => {
event.preventDefault()
console.log("Submitting the form...")
var day = new Date(Date.now()).getDate()
var month = new Date(Date.now()).getMonth() + 1
var year = new Date(Date.now()).getFullYear()
var today = new Date(year, month -1, 1)
var todayTime = today.getTime()
const ipfsadded = await ipfs.add({path:this.state.fileName, content:this.state.buffer})
var ipfsHash = ipfsadded.cid.toString()
this.setState({ipfsHash: ipfsHash})
console.log('hash:', ipfsHash, " / name =", this.state.fileName )
console.log("inserindo na blockchain..")
const accounts = await web3.eth.getAccounts()
//ERROR HERE!!
//THIS FUNCTION IS EXECUTED, THE TRANSACTION IS CONFIRMED IN METAMASK BUT AFTER THAT NOTHING HAPPENS
const promise = new Promise()
contract.methods.add(ipfsHash, this.state.fileName, this.state.fileType, todayTime)
.send({from:accounts[0], gas:300000})
.on('receipt', (receipt) => {
console.log('this should be executed now')
console.log("resultado =", receipt)
console.log("File submitted on blockchain!")
})
.on('error', console.error)
}
这些答案中的任何一个https://ethereum.stackexchange.com/a/58936/6814和https://ethereum.stackexchange.com/a/59114/6814都是正确的。
推荐阅读
- sql - SQL:选择一系列日期范围内的所有观察?
- java - JoiningTables 使用 jpa 和休眠问题是多映射失败
- python - Python:与格式 '%Y-%m-%d %H:%M:%S' 不匹配
- kubernetes - 将 K8S 审计日志保存在 s3 存储桶中
- ios - 使用旧版构建系统时不支持包,但当前工作区已启用它
- c# - 视图模型和列表的 C# 和 ASP.NET MVC 问题
- rust - 锈溢出检查连续更多的整数操作
- javascript - 更改复选框中标签颜色的问题
- php - 将 PHP 中生成的 SHA1 密码迁移到 JavaScript 中的 Bcrypt
- android - 如何同时使用 AppCompatActivity 和 RecyclerView?