这是我的代码示例:
const userAccount = '0x...';
// Call the contract method helloWorld()
const transaction = this.contract.methods
.helloWrold('some parameter 1', 'some parameter 2')
.send({ from: userAccount });
// Handle errors from the transaction
transaction.on('error', (error: any) => {
console.error('Transaction error:', error);
});
// Handle success
transaction.on('receipt', (receipt: any) => {
console.log('Transaction success:', receipt);
});
当执行
send()
方法时,会出现 MetaMask
的弹出窗口。
问题是,如果用户通过在弹出窗口中按取消来取消交易。
我的错误处理程序未执行。我尝试过使用不同的事件,我尝试过使用
try...catch
块,但这似乎不适用于异步函数。
这个问题有解决办法吗?
我所能看到的就是控制台中的错误,来自 MetaMask,用户拒绝签署交易。
我使用发送功能遇到了同样的问题。 我解决了使用回调函数发送函数的问题。像这样。
ethereum.send({
method: 'eth_sendTransaction',
params: [{"from": accounts[0],
"to": smartContractaddress,
"gas": "0x2DC6C0", // 30400
"gasPrice": "0x2540BE400",
"value": weitohex, // 2441406250
"data": inputhex}],
from: accounts[0],
},function(err, transactionHash) {
if (!err){
console.log(transactionHash);
if(transactionHash.result !== undefined){
document.getElementById('span_metalink').innerText="https://kovan.etherscan.io/tx/"+transactionHash.result;
document.getElementById('span_metalink').href="https://kovan.etherscan.io/tx/"+transactionHash.result;
document.getElementById('span_success').innerText = "Transaction Successfully Done!!!";
}
else{
document.getElementById('span_success').innerText = "User denied transaction signature.";
}
}
})