0

I apologize for posting another question. Here I'm giving the full example code which is giving me the respond: "txHash undefined" when I try to send erc20. I want to fix this code. If you have even smaller ABI share it.

const Tx = require('ethereumjs-tx').Transaction;
const Web3Utils = require('web3-utils');
const Web3 = require('web3');
const web3 = new Web3('https://mainnet.infura.io/v3/your infura');

var myAddress ='put your address';
var destinationAddres='put the destination address';  
var contractAddress = "put the contracta address";  //  contract code 

privatekeyP='your private key';

const privateKey1=Buffer.from(privatekeyP, 'hex');



var ABI = [{
  "constant": true,
  "inputs": [],
  "name": "name",
  "outputs": [{
      "name": "",
      "type": "string"
  }],
  "payable": false,
  "stateMutability": "view",
  "type": "function"
}, {
  "constant": false,
  "inputs": [{
      "name": "_spender",
      "type": "address"
  }, {
      "name": "_value",
      "type": "uint256"
  }],
  "name": "approve",
  "outputs": [{
      "name": "",
      "type": "bool"
  }],
  "payable": false,
  "stateMutability": "nonpayable",
  "type": "function"
}, {
  "constant": true,
  "inputs": [],
  "name": "totalSupply",
  "outputs": [{
      "name": "",
      "type": "uint256"
  }],
  "payable": false,
  "stateMutability": "view",
  "type": "function"
}, {
  "constant": false,
  "inputs": [{
      "name": "_from",
      "type": "address"
  }, {
      "name": "_to",
      "type": "address"
  }, {
      "name": "_value",
      "type": "uint256"
  }],
  "name": "transferFrom",
  "outputs": [{
      "name": "",
      "type": "bool"
  }],
  "payable": false,
  "stateMutability": "nonpayable",
  "type": "function"
}, {
  "constant": true,
  "inputs": [],
  "name": "INITIAL_SUPPLY",
  "outputs": [{
      "name": "",
      "type": "uint256"
  }],
  "payable": false,
  "stateMutability": "view",
  "type": "function"
}, {
  "constant": true,
  "inputs": [],
  "name": "decimals",
  "outputs": [{
      "name": "",
      "type": "uint8"
  }],
  "payable": false,
  "stateMutability": "view",
  "type": "function"
}, {
  "constant": false,
  "inputs": [{
      "name": "_spender",
      "type": "address"
  }, {
      "name": "_subtractedValue",
      "type": "uint256"
  }],
  "name": "decreaseApproval",
  "outputs": [{
      "name": "",
      "type": "bool"
  }],
  "payable": false,
  "stateMutability": "nonpayable",
  "type": "function"
}, {
  "constant": true,
  "inputs": [{
      "name": "_owner",
      "type": "address"
  }],
  "name": "balanceOf",
  "outputs": [{
      "name": "balance",
      "type": "uint256"
  }],
  "payable": false,
  "stateMutability": "view",
  "type": "function"
}, {
  "constant": true,
  "inputs": [],
  "name": "symbol",
  "outputs": [{
      "name": "",
      "type": "string"
  }],
  "payable": false,
  "stateMutability": "view",
  "type": "function"
}, {
  "constant": false,
  "inputs": [{
      "name": "_to",
      "type": "address"
  }, {
      "name": "_value",
      "type": "uint256"
  }],
  "name": "transfer",
  "outputs": [{
      "name": "",
      "type": "bool"
  }],
  "payable": false,
  "stateMutability": "nonpayable",
  "type": "function"
}, {
  "constant": false,
  "inputs": [{
      "name": "_spender",
      "type": "address"
  }, {
      "name": "_addedValue",
      "type": "uint256"
  }],
  "name": "increaseApproval",
  "outputs": [{
      "name": "",
      "type": "bool"
  }],
  "payable": false,
  "stateMutability": "nonpayable",
  "type": "function"
}, {
  "constant": true,
  "inputs": [{
      "name": "_owner",
      "type": "address"
  }, {
      "name": "_spender",
      "type": "address"
  }],
  "name": "allowance",
  "outputs": [{
      "name": "",
      "type": "uint256"
  }],
  "payable": false,
  "stateMutability": "view",
  "type": "function"
}, {
  "inputs": [],
  "payable": false,
  "stateMutability": "nonpayable",
  "type": "constructor"
}, {
  "anonymous": false,
  "inputs": [{
      "indexed": true,
      "name": "owner",
      "type": "address"
  }, {
      "indexed": true,
      "name": "spender",
      "type": "address"
  }, {
      "indexed": false,
      "name": "value",
      "type": "uint256"
  }],
  "name": "Approval",
  "type": "event"
}, {
  "anonymous": false,
  "inputs": [{
      "indexed": true,
      "name": "from",
      "type": "address"
  }, {
      "indexed": true,
      "name": "to",
      "type": "address"
  }, {
      "indexed": false,
      "name": "value",
      "type": "uint256"
  }],
  "name": "Transfer",
  "type": "event"
}]

var contract = new web3.eth.Contract(ABI, contractAddress, {
    from: myAddress
});

web3.eth.getTransactionCount(myAddress, (err,txCount)=>{
  // build the transaction


     const txObject = {

      nonce: Web3Utils.toHex(txCount),
      to: contractAddress,
      value: Web3Utils.toHex(0),
      gasLimit: Web3Utils.toHex(91000),
      gasPrice: Web3Utils.toHex(Web3Utils.toWei('10','gwei')),
      data:contract.methods.transfer(destinationAddres,web3.utils.toHex(0)).encodeABI()

  }


      const tx = new Tx(txObject, { chain: 'mainnet'})
    tx.sign(privateKey1);

    const serializedTransaction =tx.serialize();
    const raw ='0x'+ serializedTransaction.toString('hex');



    // Broadcast the transaction
    web3.eth.sendSignedTransaction(raw,(err, txHash)=>{
        console.log('txHash', txHash);
    })


})
  • web3 6.9.0? Current official version is around 1.2.4. There's a 2.x alpha version, but nowhere near 6. – goodvibration Jan 22 at 14:46
  • What do you mean by "it fails" ? What is the error ? Moreover, the latest version of web3 on npm is 1.2.5 (npmjs.com/package/web3). You can check your version of web3 by using this: web3js.readthedocs.io/en/v1.2.4/web3.html#version – Florian Castelain Jan 22 at 14:46
  • "Everything I tried" - I'd expect by the least to see myContract.methods.transfer somewhere in your code. – goodvibration Jan 22 at 14:47
  • npm web3 -v gives me 6.9.0. I had troubles to install it so I used --force. I updated my code. When I launch it with data it returns txHash Null. – Mikheil misha Jan 22 at 15:00
0

You can use this function (tested with web3.js v1.2.1):

const ABI = [{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"}];

async function send(web3, contractAddress, destAddress, destAmount, privateKey, gasPrice) {
    const contract    = new web3.eth.Contract(ABI, contractAddress);
    const transaction = contract.methods.transfer(destAddress, destAmount);
    const account     = web3.eth.accounts.privateKeyToAccount(privateKey).address;

    const options = {
        to      : contractAddress,
        data    : transaction.encodeABI(),
        gas     : await transaction.estimateGas({from: account}),
        gasPrice: gasPrice
    };

    const signed  = await web3.eth.accounts.signTransaction(options, privateKey);
    const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction);
    return receipt;
}

Updating the answer according to your updating of the question:

This is wrong:

var contract = new web3.eth.Contract(ABI, contractAddress, {from: myAddress});

Either you create this object:

var contract = new web3.eth.Contract(ABI, contractAddress)

And then deploy a new contract by signing and sending this transaction:

contract.deploy({data: "0x" + BIN, arguments: [...]})

Or you create this object in order to connect to an already deployed contract:

var contract = new web3.eth.Contract(ABI, contractAddress);
| improve this answer | |
  • Thanks I'll check it! – Mikheil misha Jan 22 at 15:01
  • contractAddress='contract address'; destAddress='destination address'; destAmount='0'; privateKey='aprivatekeygohere'; gasPrice=7; send(web3, contractAddress, destAddress, destAmount, privateKey, gasPrice); When I try like this it gives me this error: UnhandledPromiseRejectionWarning: Error: You must provide the json interface of the contract when instantiating a contract object. – Mikheil misha Jan 22 at 15:09
  • Can you give me some example code about how you launch function? Maybe private key needs to start from 0x or gas price should be in hex? – Mikheil misha Jan 22 at 15:12
  • @Mikheilmisha: First of all, it's an async function. So you need to do either one of the following: 1. Resolve the Promise object that it returns. 2. await for it to complete (must be done from inside another async function). 3. Call it at the very end of your script (nothing after that line). – goodvibration Jan 22 at 15:17
  • Second, gasPrice=7??? That's extremely low. Perhaps you meant 7 followed by 9 zeros? – goodvibration Jan 22 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.