【问题标题】:How to send transaction from Polygon test network using web3.js?如何使用 web3.js 从 Polygon 测试网络发送交易?
【发布时间】:2023-02-12 17:31:41
【问题描述】:

我正在尝试通过 Web3.js 使用 Polygon 测试网络来集成事务。相同的代码在以太坊上运行良好。但是如何使用 Polygon 测试网络发送交易呢?我需要修改任何代码吗?我在 Metamask 中创建了多边形孟买测试网络。

    const initPayButton = () =>{
        
            sendTransaction({
                to: paymentAddress,
                value: toWei(amountEth, 'ether')
            }, (err, transactionId)=>{
                if(err){
                    console.log("Payment Failed", err)
                    $('#status').html("Payment failed")
                }else{
                    console.log("Payment Successful", transactionId)
                    $('#status').html("Payment Successful")
                }
            }
            )
        }

        )
    }

【问题讨论】:

    标签: web3js


    【解决方案1】:

    您需要先切换钱包的网络。在发送交易之前,请执行以下操作:

    try {
        await window.ethereum.request({
           method: 'wallet_switchEthereumChain',
           params: [{ chainId: "0x13881" }],
        })
    } catch (e) {
        console.log(e)
    } 
    
    

    这将使 MetaMask 要求您将网络切换到 Polygon Testnet。在此之后,您可以进行交易。

    【讨论】:

    • 我应该在问题中给出的代码中使用上面的代码吗?
    • 谢谢它的工作。但我需要知道是否必须更改 ChainId:'0x13881'?因为我使用了“0x13881”并且代码运行良好。所以我需要知道是否必须更改 ChainId?
    • 先生,链 ID: '0x13881' 是多边形测试网络的默认 ID 吗?
    • 哦!我得到了它。 0x13881是chainId的十六进制转换。谢啦。
    • 是的,这是 Polygon 测试网的十六进制链接 ID。您需要提供要切换到的网络的链ID
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 2021-08-04
      • 2021-11-25
      • 2021-10-22
      • 2023-01-13
      • 2021-12-18
      • 2018-11-04
      • 2019-05-23
      • 1970-01-01
      • 2022-11-11
      相关资源
      最近更新 更多