简体   繁体   English

Web3.js 和 MetaMask ethereum.request:是否可以指定用于支付的特定加密/令牌?

[英]Web3.js and MetaMask ethereum.request: is it possible to specify specific crypto/token used to pay?

I will have a few "Click to Pay" buttons on a website that accepts crypto, using web3.js and MetaMask.我将使用 web3.js 和 MetaMask 在接受加密的网站上设置一些“点击支付”按钮。 I want to make sure different buttons automatically direct the user to use different currencies, such as ETH, MATIC, and other ERC-20 tokens, etc.我想确保不同的按钮自动引导用户使用不同的货币,例如 ETH、MATIC 和其他 ERC-20 代币等。

I'm not sure how to do this with the following code, which seems to work with whatever network the user is connected to:我不确定如何使用以下代码执行此操作,该代码似乎适用于用户连接到的任何网络:

const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
const account = accounts[0];
console.log(account);   
var hex_value = '0x'+parseInt(my_input['amount'], 10).toString(16); // gets me the right value in hex

const transactionParameters = {
    from: account,
    to: MyContractAddr,
    data: MyContract.methods.processPayment(
        my_input['name'],
        my_input['amount'],
        my_input['date']
    ).encodeABI(),
    value: hex_value,
};
// popup - request the user to sign and broadcast the transaction
await ethereum.request({
    method: 'eth_sendTransaction',
    params: [transactionParameters],
});

How can I make the above specific to a particular cryptocurrency, eg ETH or MATIC?我怎样才能使上述特定于特定的加密货币,例如 ETH 或 MATIC? It seems to work fine with either, but as the user on the website, I manually select which network I am and which currency I'm using via MetaMask, so I'm confused.似乎两者都可以正常工作,但是作为网站上的用户,我手动 select 我是哪个网络以及我通过 MetaMask 使用哪种货币,所以我很困惑。 I want this to be decided automatically rather than the user being able to pay "20 units" of "whatever" crypto.我希望这是自动决定的,而不是用户能够支付“20 单位”的“任何”加密货币。

How can I make the above specific to a particular cryptocurrency, eg ETH or MATIC?我怎样才能使上述特定于特定的加密货币,例如 ETH 或 MATIC? I

ERC-20 tokens are identified by their smart contract address. ERC-20 代币由其智能合约地址识别。 You can use a token search to find the smart contract address of a token you want to make payment with .您可以使用代币搜索来查找您要用于支付的代币的智能合约地址 Then the user makes transfer() or transferFrom() call to the token smart contract to complete the payment.然后用户对代币智能合约进行transfer()transferFrom()调用以完成支付。

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

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