简体   繁体   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?

there is simple source code in HTML and JS about button to connecting wallet as a web3 and loading ETH transaction in Metamask.在 HTML 和 JS 中简单的源代码,关于按钮将钱包连接为 web3 并在 Metamask 中加载 ETH 交易。

javascript 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 HTML

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

I would like to ask, how to change code to determine loading of Binance smart chain network (BEP-20) instead of ETH.我想问一下,如何更改代码以确定加载 Binance 智能链网络(BEP-20)而不是 ETH。 I tried to find and change ETH contract which would be changed to BEP-20 contract but I don´t see contract there.我试图找到并更改将更改为 BEP-20 合约的 ETH 合约,但我看不到那里的合约。

Second problem there is that this code contains value of 3 ETH which are loaded for transaction.第二个问题是该代码包含为交易加载的 3 ETH 值。

I would like to ask where is value set up to 3?我想问值在哪里设置为3?

how to change code to determine loading of Binance smart chain network (BEP-20) instead of ETH如何更改代码以确定加载 Binance 智能链网络(BEP-20)而不是 ETH

You can send a wallet_switchEthereumChain request to MetaMask.您可以向 MetaMask 发送wallet_switchEthereumChain请求。 Code example in the linked documentation.链接文档中的代码示例。 BSC chain ID is 56 (decimal), which is 0x38 in hex, and the mainnet RPC URL is https://bsc-dataseed.binance.org/ ( source ). BSC链ID为56(十进制),十六进制为0x38 ,主网RPC URL为https://bsc-dataseed.binance.org/ )。

where is value set up to 3?值在哪里设置为 3?

It's the value param of the eth_sendTransaction method.它是eth_sendTransaction方法的value参数。 0x29a2241af62c0000 in hex is 3000000000000000000 in decimal, the amount of wei.十六进制的0x29a2241af62c0000是十进制的3000000000000000000 ,即 wei 的数量。

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

相关问题 如何在 React 上的 bsc 测试网上将 Metamask 钱包地址连接到 web3 - How to connect Metamask wallet address to web3 on bsc testnet on React angular uing web3中如何连接钱包MetaMask? - How to connect with wallet MetaMask in angular uing web3? Metamask BSC bep20 代币的 web3 JS 支付按钮 - web3 JS payment button for Metamask BSC bep20 token Web3:访问 MetaMask 钱包中的私钥 - Web3: accessing private key in MetaMask wallet 未捕获的类型错误:web3 不是构造函数。 区块链网站无法连接到元掩码 - Uncaught TypeError: web3 is not a constructor. the blockchain website cannot connect to metamask Web3 元掩码连接无法读取未定义的属性(读取“以太坊”) - Web3 metamask connection Cannot read properties of undefined (reading 'ethereum') 如何使用Meteor和web3在MetaMask中检测帐户更改? - How to detect an account change in MetaMask using Meteor and web3? nodejs web3 bsc bep20令牌传输无确认 - nodejs web3 bsc bep20 token transfer no confirmation 如何通过 web3 或其他人获取 Metamask 钱包中的所有代币? - How do I get all the tokens inside my Metamask wallet by web3 or others? 如何仅将我的 web3 应用程序连接到 Meta Mask 钱包 - How to connect my web3 app only to Meta Mask wallet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM