2

I want to transfer some ERC20 tokens in ethereum. I did some preparation work and tried in ropsten, it succeed. Then I changed the used api, wallet address, httpProvicer, contract address, and ran the code, but it failed. I didn't find why. Could you help me?

Below is the main part of my code.

web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/xxxx"));
let WALLETBASE = "xxx";      
let contractAddr = "xxx";    
let gasPrice = web3.utils.toHex(41 * 10^9); //41gwei

let abi = JSON.parse(fs.readFileSync("./file.abi").toString("utf8"));
let contractInstance = new web3.eth.Contract(abi, contractAddr);
function setAllocation(_nonce, _to, _amount){
console.log('prepare for new transaction: %s %s', _to, _amount);
var transfer_amount = _amount + '000000000000000000';
let data = web3.utils.sha3("transfer(address,uint256)").substr(0,10)    
            + "000000000000000000000000"                                
            + _to.substr(2,40)                                          
            + "00000000000000000000000000000000"                        
            + toHex32(transfer_amount);                                         
var noncehex = "0x" + _nonce.toString(16);
// console.log('data: ' + data);

web3.eth.estimateGas({
    "from" : WALLETBASE,
    "nonce": noncehex,
    "to"   : contractAddr,
    "data" : data
}).then((value) => {
    console.log("gas limit: " + value);
    var signedTx = {
        "from"      : WALLETBASE,       
        "nonce"     : noncehex, 
        "gasPrice"  : gasPrice,         
        "gasLimit"  : value,
        "to"        : contractAddr,     
        "value"     : "0x00",
        "data"      : data,
        "chainId"   : 1
    }

    var privateKey = new Buffer.from("xxxx", 'hex');
    var tx = new Tx(signedTx);
    tx.sign(privateKey);
    var serializedTx = tx.serialize();

    try{
        web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'),
            function(err, hash) {
            if (!err) {
                console.log("-------------------");
                console.log("to: " + _to);
                console.log("TxHash: " + hash);
            } else {
                console.log("=====error occur=====");
                console.log(err);
            }
        });
    }catch(err){
        console.log('ERROR: ' + err);
    }
});
}

when i run the code, it takes a long time without any response. for about 5 mins, I got the error:

(node:1336) UnhandledPromiseRejectionWarning: Error: Transaction was not mined within750 seconds, please make sure your transaction was properly sent. Be aware that it might still be mined!
at /Users/.../node_modules/web3-core-method/src/index.js:392:42
at process._tickCallback (internal/process/next_tick.js:178:7)
(node:1336) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1336) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

PS: the wallet address does not belong to me. The owner gave me the private key and i transfer tokens on behalf of him. Is there any identity authorization issues?

4
  • What error are you getting? May 28, 2018 at 9:19
  • i added the error info!
    – E.W
    May 28, 2018 at 9:22
  • Do you have the transaction hash? The gas seems enough. How do you get the transaction nonce? The formatting of the transaction input seem not flexible enough, if you miss a '0' there you might end sending to the wrong address or an incorrect amount. Why don't you use a library like web3 to format the transaction? ERC20 are pretty common and you can get the ABI easily.
    – Ismael
    May 28, 2018 at 18:00
  • hi Ismael, yes i have the transaction hash. but when i searched it, it said that the txhash couldn't be located. Until now, it has been for 18 hours and those transction still could not be seen in my address.
    – E.W
    May 29, 2018 at 2:04

1 Answer 1

0

Your transactions is being sent. That is a warning message.

Also, your code doesn't end because it waits for your transaction to get mined and then return you success callback.

Instead, you could get the receipt and check tx status in a separate call.

web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('receipt', console.log);
1
  • it has been 18 hours and i still didn't get any response. and I couldn't locate the transaction by txhash. I guess there maybe some other reason.
    – E.W
    May 29, 2018 at 2:05

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.