0

What do I need to change in order to have a successful transaction on the 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. Below is how I attempted to do this but my transactions are never successful.

Initing my token web3 and ABI code

//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

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

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

Another thing. I have noticed that no matter how high I set my gas limit, Etherscan says it has used 100% of it. I have set a gas limit to very high numbers and still, usage is always 100%.

TylerH
  • 20,799
  • 66
  • 75
  • 101

1 Answers1

0

I managed to get the TUSDT token to send from one address to another via the following solution

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

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

When creating the transaction object I increased the amount of wei I sent. 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. It seems that in web3js there are many apparent ways to do the same thing but only a few of them may be correct. 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.

TylerH
  • 20,799
  • 66
  • 75
  • 101