简体   繁体   English

Web3.js。 无法通过 Rinkeby 测试网发送 ERC20 代币? 终端说交易已被 EVM 还原

[英]Web3js. Can't send ERC20 Token over Rinkeby Testnet? Terminal says Transaction has been reverted by the EVM

Question问题

What do I need to change in order to have a successful transaction on the EVM?为了在 EVM 上成功交易,我需要进行哪些更改? Where have I gone wrong in the below code.我在下面的代码中哪里出错了。

What I have done我做了什么

I'm trying to send tokens from my wallet address using Web3 to another wallet address.我正在尝试使用 Web3 将代币从我的钱包地址发送到另一个钱包地址。 Below is how I attempted to do this but my transactions are never successful.以下是我尝试执行此操作的方法,但我的交易从未成功。

Initing my token web3 and ABI code初始化我的令牌 web3 和 ABI 代码

//TestAccount01
const TA1 = {
   address: '0x57ece112876fb585c6a2b37114c91be752b7578c',
   privateKey:
      'privatekey',
};

const Web3 = require('web3');
const web3 = new Web3(
   'https://rinkeby.infura.io/v3/api_endpint_url_id'
);

let minABI = [
   // transfer
   {
      constant: false,
      inputs: [
         {
            name: '_to',
            type: 'address',
         },
         {
            name: '_value',
            type: 'uint256',
         },
      ],
      name: 'transfer',
      outputs: [
         {
            name: '',
            type: 'bool',
         },
      ],
      type: 'function',
   },
];

The function that sends token TXNs to address function 将令牌 TXN 发送到地址

async function sendToken() {
   let contract = new web3.eth.Contract(
      minABI,
      //contract address
      '0xD92E713d051C37EbB2561803a3b5FBAbc4962431'
   );

   let tx = await contract.methods.transfer(TA1.address, 1000);

   let data = tx.encodeABI();
   let gas = 61963;
   let gasPrice = 1649999980;

   console.log('creating transaction');
   let transaction = await web3.eth.accounts.signTransaction(
      {
         data,
         gas,
         gasPrice,
      },
      'my private key'
   );

   console.log('sending transaction');
   let recipt = await web3.eth
      .sendSignedTransaction(transaction.rawTransaction)
      .catch(err => {
         console.log('error in sending tx');
         console.error(err);
      });

   console.log(recipt);
}

await sendToken();

This gives me the error Error: Transaction has been reverted by the EVM: And a transaction hash of 0x7f0ca267163219d2c8c783e9fdc8ec3a4c1d1c0c1c52d6a405c3763cac240d9c witch you can see for your self at https://rinkeby.etherscan.io/tx/0x7f0ca267163219d2c8c783e9fdc8ec3a4c1d1c0c1c52d6a405c3763cac240d9c This gives me the error Error: Transaction has been reverted by the EVM: And a transaction hash of 0x7f0ca267163219d2c8c783e9fdc8ec3a4c1d1c0c1c52d6a405c3763cac240d9c witch you can see for your self at https://rinkeby.etherscan.io/tx/0x7f0ca267163219d2c8c783e9fdc8ec3a4c1d1c0c1c52d6a405c3763cac240d9c

I have tried changing many of the variables (especially the gas and Ga) in an attempt to get tokens to send from my address to TA1.address but the transaction keeps getting reverted by EVM我尝试更改许多变量(尤其是 gas 和 Ga),试图让代币从我的地址发送到TA1.address ,但交易不断被 EVM 还原

Another thing.另一件事。 I have noticed that no matter how high I set my gas limit, Etherscan says it has used 100% of it.我注意到,无论我设置的 gas 限制有多高,Etherscan 都表示它已经使用了 100%。 I have set a gas limit to very high numbers and still, usage is always 100%.我已经为非常高的数字设置了气体限制,但使用率始终是 100%。

Any help will be very appreciated.任何帮助将不胜感激。 Thanks.谢谢。

I managed to get the TUSDT token to send from one address to another via the following solution我设法通过以下解决方案将 TUSDT 令牌从一个地址发送到另一个地址

The mistake was something minor but still could trip you up as it did me.这个错误很小,但仍然可能像我一样绊倒你。

When creating the contract, I specified a from address in an object as the third property创建合约时,我在 object 中指定了一个发件人地址作为第三个属性

let contract = new web3.eth.Contract(
      minABI,
      '0xD92E713d051C37EbB2561803a3b5FBAbc4962431',
      {
         from: '0x28BD22dA90A884e1F7fD6bad50564Bc1646707d2',
      }
   );

When creating the transaction object I increased the amount of wei I sent.在创建交易 object 时,我增加了发送的 wei 数量。 like so.像这样。

let tx = await contract.methods.transfer(TA1.address, 1000000);

I made gas a very high amount.我做了很多气体。 (Not an optimal amount) (不是最佳数量)

let gasPrice = 1649999980;
   let gas = 61963;

And I then created the transaction like so然后我像这样创建了交易

let transaction = await web3.eth.accounts.signTransaction(
      {
         to: '0xD92E713d051C37EbB2561803a3b5FBAbc4962431',
         data, // dat is just tx.encodeABI()
         gas,
         gasPrice,
      },
      'My private key here'
   );

This answer just highlights the changes I made from my initial question.这个答案只是突出了我从最初的问题中所做的更改。 I believe by specifying a from address when creating the transaction and specifying a to address when signing it, I fixed the problem.我相信通过在创建交易时指定发件人地址并在签名时指定收件人地址,我解决了问题。 I'm not sure why this was so as the code in my question appeared to be valid web3js code according to the docs.我不确定为什么会这样,因为根据文档,我的问题中的代码似乎是有效的 web3js 代码。 It seems that in web3js there are many apparent ways to do the same thing but only a few of them may be correct.似乎在 web3js 中有许多明显的方法可以做同样的事情,但其中只有少数可能是正确的。 Anyways, if you guys know exactly what I did wrong and would like to provide a better answer than what I provided, I'm all ears.无论如何,如果你们确切地知道我做错了什么并且想提供比我提供的更好的答案,我会全神贯注。

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

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