Web3.js和MetaMask以太坊.request:是否可以指定用于支付的特定加密货币/代币



我会有一些"点击支付";使用web3.js和MetaMask在接受加密货币的网站上点击按钮。我想确保不同的按钮自动引导用户使用不同的货币,如ETH、MATIC和其他ERC-20代币等。

我不知道如何使用以下代码来实现这一点,它似乎适用于用户连接的任何网络:

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],
});

我如何使上述内容特定于特定的加密货币,例如ETH或MATIC?这两种方法似乎都很好,但作为网站上的用户,我通过MetaMask手动选择我是哪个网络,使用哪个货币,所以我很困惑。我希望这是自动决定的,而不是用户能够支付";20个单元";的";无论什么";加密货币。

如何使上述内容特定于特定的加密货币,例如ETH或MATIC?I

ERC-20代币通过其智能合约地址进行识别。您可以使用代币搜索来查找要使用的代币的智能合约地址。然后用户对令牌智能合约进行transfer()transferFrom()调用以完成支付。

最新更新