[英]Web3.js and MetaMask ethereum.request: is it possible to specify specific crypto/token used to pay?
我将使用 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? 似乎两者都可以正常工作,但是作为网站上的用户,我手动 select 我是哪个网络以及我通过 MetaMask 使用哪种货币,所以我很困惑。 我希望这是自动决定的,而不是用户能够支付“20 单位”的“任何”加密货币。
我怎样才能使上述特定于特定的加密货币,例如 ETH 或 MATIC? 我
ERC-20 代币由其智能合约地址识别。 您可以使用代币搜索来查找您要用于支付的代币的智能合约地址。 然后用户对代币智能合约进行transfer()
或transferFrom()
调用以完成支付。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.