0

I use tx.gasprice and block.basefee in my Smart Contract to calculate how much eth the User spent, to send the spent amount back to the Relayer, because I am using meta Transactions.

Now I wanted to set the gasLimit of my contract to the estimated Gas amount(to make the calculation in my smart contract as perfect as possible), but now I ran into a problem:

I can't estimate Gas when using tx.gasprice, because it only shows me that it spent ~92k gas, but in reality, the tx spent 190k-200k.

enter image description here

So when i set test = true, the below code shows me only ~92k gas spent:

let estGas = await forwarder.estimateGas.execute(request, signature);

Also, when I set test = false it shows me a closer estimated gas amount of ~225k.

enter image description here

enter image description here

Here is one example tx on goerli, everything works fine when I set the gasLimit to 300k besides that I get a wrong estimateGas() amount in javascript.


The same problem happens with transfer function which calls the modifier payGasFees(): when I set test = true, I get an error message and if I set it test = false everything works besides estimatedGas()

let estimatedGas = await myContract.estimateGas.transfer(chosenToken, amountInMaximum, fee, true, token, transferTo, _amount,{from: from, gasLimit: 300000});

enter image description here


So to Summarize everything:

The estimateGas function from ethers or web3 are not working when I use tx.gasprice in my smart contract.

I don't know why it's not working and idk how to solve this issue. It would be nice if someone have any idea or the same issue, I have no clue how to solve this problem. If you need more code to help me please let me know.


Code:


export async function signMetaTxRequest(signer, forwarder, input) {

    let request = await buildRequest( forwarder, input);
    const toSign = await buildTypedData(forwarder, request);
    const signature = await signTypedData2(signer, input.from, toSign);
    let estGas = await forwarder.estimateGas.execute(request, signature);
    console.log("estGas: ", estGas);
    console.log("estGas as Number: ", estGas.toNumber());
    return { signature, request };
}

export async function buildRequest(forwarder, input) {

    const nonce = await forwarder.getNonce(input.from).then(nonce => nonce.toString());
    return { value: 0, gas: 300000, nonce, ...input };
}

1 Answer 1

0

I was connected to another network(matic) and it works perfectly, so the problem is probably goerli or maybe in general all testnets.

I will post a upadte as soon I can estimate the issue correctly

2
  • ok, i made a mistake, it still doesn't work, i can't estimate the gas at all, even when i try to estimate the gas for the below function i get an "unpredicatable gas fee" error: function getPrice() external { gasprice = tx.gasprice; } Jan 4, 2023 at 10:01
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jan 4, 2023 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.