使用web3.js发送以太坊今天不起作用(结果是失败)



这是我的web3.js函数,用于发送ETH。上个月效果很好。但今天它运行不好。它花费了1到5分钟以上,然后返回失败。

有时它会发送,但完成交易也需要很长时间。

这是我当前的代码:

var web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/GfgWbe8c2O82N18RRSuJ'));
// Who holds the token now?
var myAddress = address;
// This file is just JSON stolen from the contract page on etherscan.io under "Contract ABI"
return await web3.eth.getBalance(myAddress);
}
const sendETHCoin = async (from_addr, to_addr, amount, private_key, fee) => {
var content = fs.readFileSync(base_path + 'abiDefinitions/ethAbiContract.json');
content = JSON.parse(content);
///////////////////////////////////

// connect to Infura node
var web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/GfgWbe8c2O82N18RRSuJ'));  
// the address that will send the test transaction
const addressFrom = from_addr;
const privKey = private_key;
// the destination address
const addressTo = to_addr;

var gasPrice = "0x02540BE400";
var gasLimit = "0x250CA";

if(fee == ''){
fee = parseInt(gasPrice, 16) * parseInt(gasLimit, 16);  
}else{
gasPrice = parseInt(parseInt(fee)/parseInt(gasLimit, 16))+1;
if(gasPrice < 1){
gasPrice = 1;
}
gasPrice = "0x"+gasPrice.toString(16);
}
//gasPrice = "0x03540BE400";

var txCount = await web3.eth.getTransactionCount(addressFrom);

const txData = {
nonce: web3.utils.toHex(txCount),
gasLimit: web3.utils.toHex(25000),
gasPrice: web3.utils.toHex(10e9), // 10 Gwei
to: addressTo,
from: addressFrom,
value: web3.utils.toHex(web3.utils.toWei(amount, 'wei'))
}

// Signs the given transaction data and sends it. Abstracts some of the details 
// of buffering and serializing the transaction for web3.
const privateKey = new Buffer(privKey, 'hex')
const transaction = new Tx(txData)
transaction.sign(privateKey)
const serializedTx = transaction.serialize().toString('hex')

try {
return await web3.eth.sendSignedTransaction('0x' + serializedTx)    
}catch(err) {
console.log(err.message);
return err.message;
}
//////////////////////////////
}

您可以在etherscan中检查事务失败的原因。如果上个月交易成功,问题可能出在天然气价格上。上周天然气价格太高(安全低点为50 gwei(,我看到你发送的是10 gwei,这很可能是你的交易失败的原因。试着提高天然气价格,看看它是否再次起作用

最新更新