繁体   English   中英

Metamask (web3) 连接钱包并发送交易 - 如何将区块链更改为 Bianance 智能链 (BEP-20) 网络而不是以太坊?

[英]Metamask (web3) connect wallet and send transaction - how to change blockchain to Bianance smart chain (BEP-20) network instead of Ethereum?

在 HTML 和 JS 中简单的源代码,关于按钮将钱包连接为 web3 并在 Metamask 中加载 ETH 交易。

javascript

const ethereumButton = document.querySelector('.enableEthereumButton');
const sendEthButton = document.querySelector('.sendEthButton');

let accounts = [];

//Sending Ethereum to an address
sendEthButton.addEventListener('click', () => {
  ethereum
    .request({
      method: 'eth_sendTransaction',
      params: [
        {
          from: accounts[0],
          to: '0x2f318C334780961FB129D2a6c30D0763d9a5C970',
          value: '0x29a2241af62c0000',
          gasPrice: '0x09184e72a000',
          gas: '0x2710',
        },
      ],
    })
    .then((txHash) => console.log(txHash))
    .catch((error) => console.error);
});

ethereumButton.addEventListener('click', () => {
  getAccount();
});

async function getAccount() {
  accounts = await ethereum.request({ method: 'eth_requestAccounts' });
}

HTML

<button class="enableEthereumButton btn">Enable Ethereum</button>
<button class="sendEthButton btn">Send Eth</button>

我想问一下,如何更改代码以确定加载 Binance 智能链网络(BEP-20)而不是 ETH。 我试图找到并更改将更改为 BEP-20 合约的 ETH 合约,但我看不到那里的合约。

第二个问题是该代码包含为交易加载的 3 ETH 值。

我想问值在哪里设置为3?

如何更改代码以确定加载 Binance 智能链网络(BEP-20)而不是 ETH

您可以向 MetaMask 发送wallet_switchEthereumChain请求。 链接文档中的代码示例。 BSC链ID为56(十进制),十六进制为0x38 ,主网RPC URL为https://bsc-dataseed.binance.org/ )。

值在哪里设置为 3?

它是eth_sendTransaction方法的value参数。 十六进制的0x29a2241af62c0000是十进制的3000000000000000000 ,即 wei 的数量。

暂无
暂无

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

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