简体   繁体   English

使用 web3 + 元掩码发送自定义令牌

[英]Sending a custom token with web3 + metamask

I'm trying to send USDT (custom token) with the metamask API using web3.我正在尝试使用 web3 发送带有元掩码 API 的 USDT(自定义令牌)。 I'm developing on ReactJS and I could detected correctly the user account from metamask.我正在 ReactJS 上开发,我可以从元掩码中正确检测到用户帐户。

My code:我的代码:

const web3 = new Web3(window.web3.currentProvider);
const contractInstance = new web3.eth.Contract(abiUSDT, addressUSDT);
const amount = 200;
const tx = {
from: 'PERSON_SENDER',
to: contractInstance._address,
data: contractInstance.methods.transfer('PERSON_RECIPIENT', web3.utils.toWei( amount.toString() ) ).encodeABI(),
        }
        web3.eth.sendTransaction(tx).then(res => {
            console.log("res",res)
        }).catch(err => {
            console.log("err",err)
        });

obviously, I replace the fields called: PERSON_SENDER and PERSON_RECIPIENT with:显然,我将名为PERSON_SENDERPERSON_RECIPIENT的字段替换为:

PERSON_SENDER : The user account PERSON_SENDER : 用户帐户

PERSON_RECIPIENT : My personal account (I want to deposit in this account) PERSON_RECIPIENT : 我的个人账户(我想存入这个账户)

Using this config, the transaction doesn't send to my account when I check in etherscan (and the amount was sent to the address of the contract), and a user communicate that if he uses real values for him real account, metamask doesn't touch his amount.使用此配置,当我签入 etherscan 时,交易不会发送到我的帐户(并且金额已发送到合约的地址),并且用户传达说,如果他为真实帐户使用真实值,metamask 不会不要碰他的金额。

I made the same code and it works.我制作了相同的代码并且它有效。 But check the correct ABI, and adrresses format.但请检查正确的 ABI 和地址格式。 The address "FROM" must be tha same address that your metamask is conected.地址“FROM”必须与连接元掩码的地址相同。

My code:我的代码:

 var contractABI = [{"YOUR TOKEN ABI HERE"}]

addressUSDT = "0xf80e1C5e28226cAfc7B0Ee6729E553ef54A7774F"; //IT'S OTHER CONTRACT              
const web3 = new Web3(window.web3.currentProvider);
const contractInstance = new web3.eth.Contract(contractABI, addressUSDT);
const amount = 200;
const tx = {
from: '0x2acd4F50ee814d5092d9FC892d2BDEab91f5594d',
to: contractInstance._address,
data: contractInstance.methods.transfer('0xe54356F4e1aD4215973E7180BeBb4644a07DF508', web3.utils.toWei( amount.toString() ) ).encodeABI(),
        }
        web3.eth.sendTransaction(tx).then(res => {
            console.log("res",res)
        }).catch(err => {
            console.log("err",err)
        });

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

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