简体   繁体   English

如何收听 Metamask 的 web3 的“确认”/“取消”合同事件?

[英]How to listen to Metamask's web3's "confirm"/"cancel" event of a contract?

Since metamask's injected web3 follows https://github.com/ethereum/wiki/wiki/JavaScript-API由于 metamask 注入的 web3 遵循https://github.com/ethereum/wiki/wiki/JavaScript-API

But there seems to be no coding to catch the confirm/cancel button clicking event (img below) when calling contract.new()但是在调用 contract.new() 时似乎没有编码来捕捉确认/取消按钮点击事件(如下图)

元掩码弹出供用户确认/取消

Is there a way to catch the event?有没有办法捕捉事件? thanks.谢谢。

Found an answer myself自己找到了答案

if the user clicked the 'cancel' button, the 'processedContract' variable below will be undefined.如果用户单击“取消”按钮,则下面的“已处理合同”变量将是未定义的。 If confirmed, it will be the contract and if it has an address ( processedContract.address ) then it is being mined.如果确认,它将是合约,如果它有一个地址(processedContract.address),那么它正在被挖掘。

newContract.new({...}, function(e, processedContract) {...});

To handle Confirm/Cancel response of user on your contract functions, you can use the transactionHash event & error event from send function.要处理用户对合约函数的确认/取消响应,您可以使用 send 函数中的transactionHash事件和错误事件。

"transactionHash" returns transactionHash: String: Fired when the transaction hash is available. "transactionHash" 返回 transactionHash: String: 当交易哈希可用时触发。

"error" returns error: Error: Fired if an error occurs during sending. “错误”返回错误:错误:如果在发送过程中发生错误则触发。 If the transaction was rejected by the network with a receipt, the receipt will be available as a property on the error object.如果交易被网络拒绝并带有收据,则收据将作为错误对象的属性可用。 在此处输入图片说明

You can see detail here. 您可以在此处查看详细信息。

When you click on Confirm button then you can handle this case in the transactionHash event.当您单击Confirm按钮时,您可以在transactionHash事件中处理这种情况。

When you click on Cancel button then you can handle this case in the error event.当您单击取消按钮时,您可以在错误事件中处理这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM