I write API for smart contract but its shown error "Insufficient funds for gas * price + value"?
My try
I have increased my address balance but it's showing the same error.
I have changed my chain code id but it's showing same error.
I have changed gas price - tx.gasPrice = web3.utils.toHex(1000000);
but its showing same error. I have change provider as ropsten infura but its showing the same error.
My code:
async function sendSignedTransactionsForContracts(data, senderAddress, senderPrivateKey) {
let txObject = {};
let nonce = null;
let gasPrice = null;
return new Promise(async (resolve, reject) => {
await Promise.all([web3.eth.getTransactionCount(senderAddress), web3.eth.getGasPrice()])
.then(result => {
nonce = result[0];
gasPrice = result[1];
gasPrice = (parseInt(gasPrice) + parseInt(gasPrice / 2)).toString();
console.log(gasPrice);
txObject = {
from: senderAddress,
nonce: web3.utils.toHex(nonce),
data: data
};
return web3.eth.estimateGas(txObject);
})
.then(estimateGas => {
txObject.gasPrice = web3.utils.toHex(gasPrice);
txObject.gas = web3.utils.toHex(estimateGas);
const tx = new Tx.Transaction(txObject, {'chain':'ropsten'});
const privateKey = Buffer.from(senderPrivateKey, 'hex');
tx.sign(privateKey);
const serializedTx = tx.serialize();
const raw = '0x' + serializedTx.toString('hex');
web3.eth.sendSignedTransaction(raw, async (err, txHash) => {
if (err) {
reject(err)
return
}
console.log('err:', err, 'txHash:', txHash)
let getTransaction = setInterval(async () => {
web3.eth.getTransactionReceipt(txHash, async function (error, result) {
if (error) {
clearInterval(getTransaction)
console.log({ 'errrr': error })
reject(error)
}
if (result) {
clearInterval(getTransaction)
resolve(result.contractAddress)
}
});
}, 600)
})
})
})
}
async function sendSignedTransactionsForMethods(data, contract_address, senderAddress, senderPrivateKey) {
return new Promise(async (resolve, reject) => {
Promise.all([web3.eth.getTransactionCount(senderAddress), web3.eth.getGasPrice()])
.then(async result => {
nonce = result[0];
const tx = {
nonce: web3.utils.toHex(nonce),
from: senderAddress,
to: contract_address,
gas: result[1],
data: data,
chainId : 3,
chain: 'ropsten',
hardfork: 'petersburg'
};
let gasLimit = await web3.eth.estimateGas(tx);
tx.gasPrice = web3.utils.toHex(result[1]);
tx.gas = web3.utils.toHex(gasLimit);
web3.eth.accounts.signTransaction(tx, senderPrivateKey).then(signed => {
const tran = web3.eth
.sendSignedTransaction(signed.rawTransaction, async (err, txHash) => {
console.log(err);
if (err) {
reject(err)
return
}
console.log('err:', err, 'txHash:', txHash)
// Use this txHash to find the contract on Etherscan!
let getTransaction = setInterval(async () => {
await web3.eth.getTransactionReceipt(txHash, async function (error, result) {
if (error) {
clearInterval(getTransaction)
console.log({ 'errrr': error })
reject(new Error(error))
}
resolve(txHash)
});
}, 600)
});
});
})
})
}