使用Web3js发送BNB会赢得日志错误
我有此代码,允许我使用metAmask使用Web3.j发送BNB 我想在用户拒绝付款或存在任何其他错误时显示一条消息,如果成功付款,请显示成功消息。我找不到解决方案。
我的代码是
$(".pay-button").click(async () => {
await initWeb3('https://bsc-dataseed1.binance.org:443');
// paymentAddress is where funds will be send to
const web3 = new Web3('https://bsc-dataseed1.binance.org:443');
const reciever = '0x5c03906C1c6f737eF982BDdA481D2E04D0089155';
ethereum.request({
method: 'eth_sendTransaction',
params: [
{
from: ethereum.selectedAddress,
to: reciever,
value: web3.toHex(web3.toWei('0.001', 'ether')),
gas: '0x76c0', // 30400
gasPrice: '', // 10000000000000
},
],
})
.then((txHash) => console.log(txHash))
.catch((error) => console.error);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试检查以下错误代码,并相应地调整UI。
https://docs.metamask.io/guide/guide/ethereum-provider.html#events
如果您的交易成功,即收到TXHASH时,您可以显示,您可以显示,您可以显示UI中的成功消息
这里的交易状态正在做一些类似的工作
You can try to check for the following error codes that are emitted by Metamask and tweak you UI accordingly.
https://docs.metamask.io/guide/ethereum-provider.html#events
If your transaction is success, i.e. when you receive the txHash, you can show success message in the UI
https://github.com/skiran017/sol-tip-jar/blob/main/src/modules/TipWidget/TipWidget.tsx
transaction status here is doing some similar job