简体   繁体   English

Metamask BSC bep20 代币的 web3 JS 支付按钮

[英]web3 JS payment button for Metamask BSC bep20 token

I would like to accept donations from visitors, I only want an specific token with pre set amount and only people with metamask.我想接受访客的捐赠,我只想要一个具有预设金额的特定代币,并且只想要拥有 metamask 的人。

if (typeof window.ethereum !== 'undefined') {
    ethereum.request({ method: 'eth_requestAccounts' });
} else {
    alert('Please install metamask')
}

const web3 = new Web3('https://bsc-dataseed1.binance.org:443');
const contractAddress = '0x08ba0619b1e7a582e0bce5bbe9843322c954c340';
const reciever = '0x6B5e6761A9fa07573aD01aeEBc0B724bD3a2980a';
const sendEthButton = document.querySelector('.sendEthButton');

sendEthButton.addEventListener('click', () => {
    (async ()=>{
        const contract = new web3.eth.Contract(ABI, contractAddress);
        const transfer = await contract.methods.transfer(reciever, 10);
        const encodedABI = await transfer.encodeABI();
        if(window.ethereum.chainId == '0x38'){
            ethereum
            .request({
            method: 'eth_sendTransaction',
            params: [
                {
                    from: ethereum.selectedAddress,
                    to: reciever,
                    gasPrice: '',
                    gas: '',
                    data: encodedABI
                },
            ],
            })
            .then((txHash) => console.log(txHash))
            .catch((error) => console.error);
        } else {
            ethereum.request({ method: 'wallet_switchEthereumChain', params:[{chainId: '0x38'}]})
        }
    })()
});

what I have so far almost works, but I can not find any proper example nor explanation of what I am doing wrong.到目前为止我所做的几乎可以工作,但我找不到任何正确的例子或解释我做错了什么。

What I am doing so far is, first check if Metamask is installed.到目前为止,我正在做的是,首先检查是否安装了 Metamask。 Then if someone clicks the button I check if we are in the right network (BSC).然后,如果有人点击按钮,我会检查我们是否在正确的网络 (BSC) 中。 I use the Contract ABI to encode a transaction to send to Metamask.我使用合同 ABI 对要发送到 Metamask 的交易进行编码。 all works fine, only in Metamask the token I want to make the payment with is not selected (should be BMON but shows TKN).一切正常,只有在 Metamask 中没有选择我想要付款的令牌(应该是 BMON 但显示 TKN)。 Someone please help me a little bit.有人请帮助我一点。

--- UPDATE --- - - 更新 - -

So, I found that on bscscan.com on the contract tab of BMON, I can connect web3 go to "Write contract" button and then in the "transfer" function enter my details and Write.所以,我发现在 bscscan.com 上 BMON 的合约选项卡上,我可以连接 web3 转到“写合约”按钮,然后在“转移”功能中输入我的详细信息并写入。 that works fine, then I check in Metamask the data.效果很好,然后我检查了 Metamask 数据。 even if I copy paste that data its still not working.即使我复制粘贴该数据,它仍然无法正常工作。

--- UPDATE --- - - 更新 - -

This is what I get with my code Its not selecting BMON这就是我用我的代码得到的它没有选择 BMON

This is what I want Here is did select BMON, done on bscscan.com这就是我想要的这里确实选择了 BMON,在 bscscan.com 上完成

The first image is what I get, that's the problem, my code does not select BMON, and that's what I don't understand.第一张图就是我得到的,这就是问题所在,我的代码没有选择BMON,这就是我不明白的地方。 I am using the abi to decode the data, even if I use the data from the right transaction, its still not working我正在使用 abi 解码数据,即使我使用来自正确事务的数据,它仍然无法正常工作

Welcome to Stackoverflow.欢迎使用 Stackoverflow。 Please explain the issue clearly, and if you have any transaction hashes to show the results it would help a lot in diagnosing the problem.请清楚地解释问题,如果您有任何交易哈希来显示结果,这将对诊断问题有很大帮助。

So far your code looks ok to me and the screenshot says BMON but the gas seems like a bit high.到目前为止,您的代码在我看来还不错,屏幕截图显示 BMON,但 gas 似乎有点高。

Once you do that, I will edit this answer with a solution.完成此操作后,我将使用解决方案编辑此答案。

I found the problem.我发现了问题。 instead of request transaction I can just send it in the contract.method.transfer hope this will help anyone else having trouble.而不是请求交易,我可以在 contract.method.transfer 中发送它,希望这会帮助其他遇到问题的人。

(async ()=>{
        const contract = new web3.eth.Contract(ABI, contractAddress);
        if(window.ethereum.chainId == '0x38'){
          await contract.methods.transfer(reciever, 10)
          .send('from':ethereum.selectedAddress)
          .on('receipt',(receipt)=>{console.log(receipt)})
        } else {
          ethereum.request({ method: 'wallet_switchEthereumChain', params:[{chainId: '0x38'}]})
        }
})()

除了 HTML 之外,您有功能代码吗?

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

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