I'm fairly new to blockchain and I'm struggling with sending a transaction to a smart contract - I'm getting intrinsic gas too low error. I've tried searching online for a solution but just couldn't solve it.
From https://stackoverflow.com/a/71261434/20453413 I understand that I need to 'allocate more gas by setting a higher gasLimit', okey, but by how much? Am I calculating gasPrice correctly?
I've deployed my contract to a goerli testnet and I'm using Infura as a provider to the blockchain.
let baseNonce = await provider.getTransactionCount(wallet.getAddress());
const gasPrice = await provider.getGasPrice()
const tx = await someContract.populateTransaction.setFoo('hello');
tx.nonce = baseNonce + 1
tx.value = ethers.utils.parseUnits('0.001', 'ether')
tx.gasPrice = gasPrice
await wallet.signTransaction(tx).then(signedTransaction => {
provider.sendTransaction(signedTransaction).then(transactionResponse => {
console.log("Our transaction response: " + transactionResponse)
})
})
provider.getGasPrice() returns a BigNumber object. Something like this { BigNumber: "21971214174" }
But you need to convert this BigNumber to a string. BigNumber object has a toString() to method. you can check the docs
tx.gasPrice = gasPrice.toString()
Bit of context; this was working fine on Goerli just a few days a go but the script now works intermittently. At the moment im thinking its down to one of the following:
Node issue - currently using infura and have tried changing it across other public RPCS but didn't work any better
Web3 issue - not sure if something is now outdated
Nonce issue with wallets, not sure if there is something obvious in the code here that is causing my Txs to get stuck and as such never return a receipt
For some reason this was all working fine last night after having not worked all day. Bit stuck as to what i could be doing wrong tbh. The logic is that based on a mempool tx, the sendTx function is invoked with the 'targetAddress' param which contains all the necessary objects to replicate the tx which i am tracking.
Tx.js:
const Web3 = require('web3');
const Tx = require('ethereumjs-tx');
process.removeAllListeners('warning');
require('dotenv').config();
const INFURA_URL_TESTNET = process.env.INFURA_URL_TESTNET;
const web3 = new Web3(INFURA_URL_TESTNET);
const BN = web3.utils.BN;
// Tx details:
const sendTx = async (targetAddress) => {
const address = targetAddress.address;
const ethAmmount = targetAddress.value;
const data = targetAddress.input;
const gasLimit = targetAddress.gas;
//const gasPrice = targetAddress.gasPrice - web3.utils.toWei('1', 'gwei');
const maxFeePerGas = targetAddress.maxFeePerGas //- 1000000;
const maxPriorityFeePerGas = targetAddress.maxPriorityFeePerGas //- 1000000;
const privKey = targetAddress.privKey;
const from = targetAddress.from;
const _txCount = await web3.eth.getTransactionCount(from);
const createTransaction = await web3.eth.accounts.signTransaction(
{ to: address,
from: from,
data: data,
gas: gasLimit,
value: ethAmmount,
nonce: _txCount,
maxFeePerGas: maxFeePerGas,
maxPriorityFeePerGas: maxPriorityFeePerGas,
},
privKey,
);
// Send Tx and Wait for Receipt
const createReceipt = await web3.eth.sendSignedTransaction(createTransaction.rawTransaction);
//const transaction = await web3.eth.getTransaction(createReceipt.transactionHash);
console.log(`Tx successful with hash: ${createReceipt.transactionHash}`);
};
module.exports = {sendTx};
The code stops at the await .sendSignedTx and doesn't return a receipt. As i said, this does work exactly as expected sometimes and i can figure out why sometimes it works and sometimes it doesn't?
Thanks everyone!
Since you are using web3js, you have to handle the event promise chain when calling sendSignedTransaction and log all the payloads. That will tell you the reason for it not being broadcast. Could be a network error, gas issue, maybe nonce .e.t.c.
See https://web3js.readthedocs.io/en/v1.2.11/callbacks-promises-events.html#callbacks-promises-events
I call the contract.methods.ownerOf 2500 times then I got the MetaMask - RPC Error: Failed to fetch. But I call the method about 200 times, then there's no error. how can I fix the error?
(ownerOf is etherscan contract interface)
here's my code.
export async function list() {
const MAX_TOKEN_ID = 2500;
const web3 = new Web3(Web3.givenProvider ||'http://localhost:8080');
const contract = new web3.eth.Contract(ERC721ABI as AbiItem[], CONTRACT);
const list = new Set<number>();
let result: object;
for (let tokenId = 1; tokenId <= MAX_TOKEN_ID; tokenId++) {
const address = await contract.methods.ownerOf(tokenId).call();
list.add(address);
}
result = Array.from(list).map((item) => ({address: item}));
return result;
}
and my error
To make a large number of JSON-RPC requests you need to host your own node or purchase as a node as a service.
RPC requests are not free and someone must pay for them. If you are not paying yourself, then whoever is serving you has all their right to cut you off.
I'm using ether.js to create a dapp.I want to input something in my database when something modifies on a blockchain.So I am using this for the check const receipt = await transaction.wait(1); Once the transaction receipt is received it means it has been mined successfully. But the problem is when I am speeding up the transaction I get an error saying "TRANSACTION REPLACED" because I tried to send transaction again using the same nonce.The block is mined on the network but on frontend I'm getting this error. How to handle this error? This is the explanation of this error on docs.
This error is thrown when waiting for a transaction which has been replaced by another, by the sender submitting a second transaction with the same nonce, while the transaction was pending in the transaction pool.
Just found out what the core problem is -
https://medium.com/the-capital/ethereum-speed-up-transactions-how-they-work-and-what-they-mean-for-dapps-6d4ba1068580
But their service is paid.
error from console log-
index.ts:261
Uncaught (in promise) Error: transaction was replaced [ See: https://links.ethers.org/v5-errors-TRANSACTION_REPLACED ] (cancelled=false, reason="repriced", replacement={"hash":"0x6fb9737ee9449817cb797104e64e023641e480f01b9a76721fbbd5d4bd8a25a5","type":2,"accessList":[],"blockHash":"0x95b11ed6cb7329fd9b475d324a6eb5de54cfb3ca79eac0c2ecdb0952a0021ad6","blockNumber":27034014,"transactionIndex":4,"confirmations":1,"from":"0xB157e57e493167491c8EE69681D89873a9D68B1C","gasPrice":{"type":"BigNumber","hex":"0x08896c4c12"},"maxPriorityFeePerGas":{"type":"BigNumber","hex":"0x08896c4c0b"},"maxFeePerGas":{"type":"BigNumber","hex":"0x08896c4c16"},"gasLimit":{"type":"BigNumber","hex":"0x6e80"},"to":"0x92265B57f08EF2F30dDd6d9CdCac1BD62C1A004b","value":{"type":"BigNumber","hex":"0x038d7ea4c68000"},"nonce":38,"data":"0xf340fa01000000000000000000000000b157e57e493167491c8ee69681d89873a9d68b1c","r":"0x96346c519bd65bbe2550a1396757e92f89c4d87246bf0bd4c3a8c8d315a6e78c","s":"0x332b38044b3efcd7bbe8e7b7e860024fb13306ce47c37a7b2168765c6e82f7e1","v":0,"creates":null,"chainId":80001}, hash="0x1a7442367d14df07d598aeb0ac9479926bee663637e11d535fa698cd9fd8f113", receipt={"to":"0x92265B57f08EF2F30dDd6d9CdCac1BD62C1A004b","from":"0xB157e57e493167491c8EE69681D89873a9D68B1C","contractAddress":null,"transactionIndex":4,"gasUsed":{"type":"BigNumber","hex":"0x6e80"},"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000008000100000000000008000000000000000000000000000000000000000000000000000000000800000000000000000000100048000004000000000000000080000000010000000000000000000000080000000000000000000000000000000000000000000000000080001000080000000000000000000200000000000000000000000000000004000000400000100000000000000004000000000000000020001000000000000000000000000800000108040000000000000000000000000000000000000000000000000000000400000000000100000","blockHash":"0x95b11ed6cb7329fd9b475d324a6eb5de54cfb3ca79eac0c2ecdb0952a0021ad6","transactionHash":"0x6fb9737ee9449817cb797104e64e023641e480f01b9a76721fbbd5d4bd8a25a5","logs":[{"transactionIndex":4,"blockNumber":27034014,"transactionHash":"0x6fb9737ee9449817cb797104e64e023641e480f01b9a76721fbbd5d4bd8a25a5","address":"0x0000000000000000000000000000000000001010","topics":["0xe6497e3ee548a3372136af2fcb0696db31fc6cf20260707645068bd3fe97f3c4","0x0000000000000000000000000000000000000000000000000000000000001010","0x000000000000000000000000b157e57e493167491c8ee69681d89873a9d68b1c","0x00000000000000000000000092265b57f08ef2f30ddd6d9cdcac1bd62c1a004b"],"data":"0x00000000000000000000000000000000000000000000000000038d7ea4c680000000000000000000000000000000000000000000000000004a10eca800d2aa93000000000000000000000000000000000000000000000000035801792bb0d0000000000000000000000000000000000000000000000000004a0d5f295c0c2a93000000000000000000000000000000000000000000000000035b8ef7d0775000","logIndex":10,"blockHash":"0x95b11ed6cb7329fd9b475d324a6eb5de54cfb3ca79eac0c2ecdb0952a0021ad6"},{"transactionIndex":4,"blockNumber":27034014,"transactionHash":"0x6fb9737ee9449817cb797104e64e023641e480f01b9a76721fbbd5d4bd8a25a5","address":"0x92265B57f08EF2F30dDd6d9CdCac1BD62C1A004b","topics":["0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c"],"data":"0x000000000000000000000000b157e57e493167491c8ee69681d89873a9d68b1c00000000000000000000000000000000000000000000000000038d7ea4c68000","logIndex":11,"blockHash":"0x95b11ed6cb7329fd9b475d324a6eb5de54cfb3ca79eac0c2ecdb0952a0021ad6"},{"transactionIndex":4,"blockNumber":27034014,"transactionHash":"0x6fb9737ee9449817cb797104e64e023641e480f01b9a76721fbbd5d4bd8a25a5","address":"0x0000000000000000000000000000000000001010","topics":["0x4dfe1bbbcf077ddc3e01291eea2d5c70c2b422b415d95645b9adcfd678cb1d63","0x0000000000000000000000000000000000000000000000000000000000001010","0x000000000000000000000000b157e57e493167491c8ee69681d89873a9d68b1c","0x000000000000000000000000be188d6641e8b680743a4815dfa0f6208038960f"],"data":"0x0000000000000000000000000000000000000000000000000003af513ed2bf800000000000000000000000000000000000000000000000004a149bf93fa86f93000000000000000000000000000000000000000000001d2f81f5cd2c083658f80000000000000000000000000000000000000000000000004a10eca800d5b013000000000000000000000000000000000000000000001d2f81f97c7d47091878","logIndex":12,"blockHash":"0x95b11ed6cb7329fd9b475d324a6eb5de54cfb3ca79eac0c2ecdb0952a0021ad6"}],"blockNumber":27034014,"confirmations":1,"cumulativeGasUsed":{"type":"BigNumber","hex":"0x06733e"},"effectiveGasPrice":{"type":"BigNumber","hex":"0x08896c4c12"},"status":1,"type":2,"byzantium":true}, code=TRANSACTION_REPLACED, version=providers/5.6.8)
at Logger.makeError (index.ts:261:1)
at Web3Provider.<anonymous> (base-provider.ts:1372:1)
at Generator.next (<anonymous>)
at fulfilled (base-provider.ts:1:1)
if (typeof window.ethereum !== "undefined" && logInState === true) {
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contract = new ethers.Contract(gambleAddress, Gamble.abi, signer);
const transaction = await contract.deposit(acc, {
value: ethers.utils.parseEther(matic.toString()),
});
const receipt = await transaction.wait(1);
console.log(receipt);
if (receipt) {
updateDepositInDb();
} else {
alert("transaction failed");
}
You should surround your code with try catch to handle exceptions.
You may refer to the link below to find out how to detect replacement transactions in your scenarios.
https://blog.ricmoo.com/highlights-ethers-js-may-2021-2826e858277d
I have an ethers contract that I've made a transaction with:
const randomSVG = new ethers.Contract(RandomSVG.address, RandomSVGContract.interface, signer)
let tx = await randomSVG.create()
I have an event with this transaction:
function create() public returns (bytes32 requestId) {
requestId = requestRandomness(keyHash, fee);
emit requestedRandomSVG(requestId);
}
However, I can't see the logs in the transaction receipt.](https://docs.ethers.io/v5/api/providers/types/#providers-TransactionReceipt)
// This returns undefined
console.log(tx.logs)
When you create a transaction with Ethers.js, you get back a TransactionResponse that may not be included in the blockchain yet. Therefore it doesn't know what logs will be emitted.
Instead, you want to wait until the transaction is confirmed and you get back a TransactionReceipt. At this point the transaction was included in a block, and you can see which events were emitted.
const randomSVG = new ethers.Contract(RandomSVG.address, RandomSVGContract.interface, signer)
const tx = await randomSVG.create()
// Wait until the tx has been confirmed (default is 1 confirmation)
const receipt = await tx.wait()
// Receipt should now contain the logs
console.log(receipt.logs)