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?