使用Web3js发送BNB会赢得日志错误

发布于 2025-01-20 14:40:24 字数 1140 浏览 0 评论 0 原文

我有此代码,允许我使用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);
   
      });

我正在使用此软件包

I have this code that allows me to send bnb using web3.js using MetaMask
I want to display a message when user rejects payment or there is any other error and if it is successful, display a success message. I can not find my way around it.

My Code is

$(".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);
   
      });

I am using this package https://unpkg.com/@metamask/legacy-web3@latest/dist/metamask.web3.min.js

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

源来凯始玺欢你 2025-01-27 14:40:24

您可以尝试检查以下错误代码,并相应地调整UI。

“由metAmask给出的错误代码”

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.

Error codes given by metamask

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文