mantlenetworkio / mantlenetworkio/mantle-tutorial

Error in gasEstimation

Open
#13 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
33
Forks
16
PR merge metrics
No merged PRs in 30d

Description

Followed the gas estimation tutorial here: https://docs.mantle.xyz/network/for-devs/tutorials/estimating-transaction-fees

Warpped the gas estimation into:

`
const ethers = require("ethers")
const mantleSDK = require("@mantleio/sdk");

module.exports.getEstimateGas = async (tx) => {
const l2RpcProvider = new ethers.providers.JsonRpcProvider("https://rpc.testnet.mantle.xyz")
try{
// By calling the BVM_GasPriceOracle contract method l1basefee()
const gasPrice = await mantleSDK.getL1GasPrice(l2RpcProvider);
const decimals = await mantleSDK.decimals(l2RpcProvider);
const scalar = await mantleSDK.scalar(l2RpcProvider);
const gasUsed = await mantleSDK.overhead(l2RpcProvider);
// L1RollupFee
const l1RollupFee = gasPrice.mul(gasUsed).mul(scalar).div(10**decimals)

    // L2TxnFee
    const l2Gas = await l2RpcProvider.estimateGas(tx)
    const l2GasPrice = await l2RpcProvider.getGasPrice()
    const l2TxnFee = l2GasPrice.mul(l2Gas);
    
    // Total estimated Gas Fee
    const totalEstimatedGasFee = l1RollupFee.add(l2TxnFee);
    console.log('Total estimated Gas Fee:', totalEstimatedGasFee.toString());
    return { success: true, estimatedGas: totalEstimatedGasFee }
} catch (error) {
    console.error('Error estimating gas:', error);
    return { success: false }
}

}
`

If using a fake tx, the getEstimateGas function can be successfully invoked:

`const { getEstimateGas } = require('./gas_helper');
const ethers = require("ethers")

const tx = {
to: "0xdEc3262D5EADbbD946AdbB48a2A07ABe15B0AB83",
value: ethers.utils.parseEther("0.01"),

};

(async () => {
try {
const result = await getEstimateGas(tx);
console.log('Gas Estimate:', result);
} catch (error) {
console.error('Error:', error);
}
})();
Output: Total estimated Gas Fee: 1050780143553
Gas Estimate: {
success: true,
estimatedGas: BigNumber { _hex: '0xf4a7608fc1', _isBigNumber: true }
}`.

But when user call a specific contract method (mint NFT), the getEstimateGas will meet error:

Cotract address: https://explorer.testnet.mantle.xyz/address/0xF0681C0ae6eA87dbF8D7D2fe56d81D05C7f4eCa6/contracts#address-tabs

Script that cause error:
`const Web3 = require("web3");
const web3 = new Web3(new Web3.providers.HttpProvider("https://rpc.testnet.mantle.xyz"));
var fs = require('fs');
const { sleep } = require('./sleep.helper.js');
const { getEstimateGas } = require("./estimateGas.helper.js");
require('dotenv').config();

var contract_address = process.env.CONTRACT_ADDRESS;
var contract_jsonFile = "./fan_card_abi.json";
var contract_parsed = JSON.parse(fs.readFileSync(contract_jsonFile));
var contract_abi = contract_parsed.abi;
var contract_router = new web3.eth.Contract(contract_abi, web3.utils.toChecksumAddress(contract_address));

const walletGeneration = async () => {

const privateKey = "";
const publicKey = "";
const gasPrice = await web3.eth.getGasPrice();
try {
    var gas_balance = await web3.eth.getBalance(publicKey) / (10 ** 18);
    console.log("gas_balance", gas_balance)

    const messageHash = web3.utils.soliditySha3({ t: 'address', v: publicKey }); // userAddress is the address of the user you're whitelisting
    const signature = signerAccount.sign(messageHash).signature;

    // const expectedGas = await contract_router.methods.mintNFT(signature).estimateGas({ from: publicKey });
    const tx = await contract_router.methods.mintNFT(signature);
    const _data = tx.encodeABI();

    const pureTx = {
        to: web3.utils.toChecksumAddress(contract_address),
        data: _data,
    }

    const estimatedGasData = await getEstimateGas(pureTx);
    if (estimatedGasData.success) {
        console.log("Estimated Gas Fee ", estimatedGasData.estimatedGas)
        const signedTx = await web3.eth.accounts.signTransaction(
            {
                to: web3.utils.toChecksumAddress(contract_address),
                data: _data,
                gasLimit: parseInt(estimatedGasData.estimatedGas * 2),
                gasPrice: parseInt(estimatedGasData.estimatedGas),
            },
            privateKey,
        );

        const transaction = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
        const hash = await transaction.transactionHash;
        console.log("NFT Mint Transaction Success", hash);
    } else {
        console.log("Error ocurred to get estimated gas")
    }
} catch (error) {
    console.log("Error ", error);
}

}

walletGeneration();`

Error looks like:
Error estimating gas: Error: cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ] (reason="execution reverted: Invalid signature", method="estimateGas", transaction={"to":"0xF0681C0ae6eA87dbF8D7D2fe56d81D05C7f4eCa6","data":"0x52c253c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000419c745e4f1316fea37908c08e2f77c5efff909c4eddca4164138519fc619cfea06e6214eb8215ad583d1d7ca1db0cb732d8c2238a21e2875869c2e81bcef064661c00000000000000000000000000000000000000000000000000000000000000","accessList":null}, error={"reason":"processing response error","code":"SERVER_ERROR","body":"{\"jsonrpc\":\"2.0\",\"error\":{\"code\":3,\"message\":\"execution reverted: Invalid signature\",\"data\":\"0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000011496e76616c6964207369676e6174757265000000000000000000000000000000\"},\"id\":52}\n","error":{"code":3,"data":"0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000011496e76616c6964207369676e6174757265000000000000000000000000000000"},"requestBody":"{\"method\":\"eth_estimateGas\",\"params\":[{\"to\":\"0xf0681c0ae6ea87dbf8d7d2fe56d81d05c7f4eca6\",\"data\":\"0x52c253c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000419c745e4f1316fea37908c08e2f77c5efff909c4eddca4164138519fc619cfea06e6214eb8215ad583d1d7ca1db0cb732d8c2238a21e2875869c2e81bcef064661c00000000000000000000000000000000000000000000000000000000000000\"}],\"id\":52,\"jsonrpc\":\"2.0\"}","requestMethod":"POST","url":"https://rpc.testnet.mantle.xyz"}, code=UNPREDICTABLE_GAS_LIMIT, version=providers/5.7.2) at Logger.makeError (/Users/boreaswu/Desktop/Crypto/FT_alpha/Axelar_bridge/node_modules/@ethersproject/logger/lib/index.js:238:21) at Logger.throwError (/Users/boreaswu/Desktop/Crypto/FT_alpha/Axelar_bridge/node_modules/@ethersproject/logger/lib/index.js:247:20) at checkError (/Users/boreaswu/Desktop/Crypto/FT_alpha/Axelar_bridge/node_modules/@ethersproject/providers/lib/json-rpc-provider.js:122:20) at JsonRpcProvider.<anonymous> (/Users/boreaswu/Desktop/Crypto/FT_alpha/Axelar_bridge/node_modules/@ethersproject/providers/lib/json-rpc-provider.js:751:47) at step (/Users/boreaswu/Desktop/Crypto/FT_alpha/Axelar_bridge/node_modules/@ethersproject/providers/lib/json-rpc-provider.js:48:23) at Object.throw (/Users/boreaswu/Desktop/Crypto/FT_alpha/Axelar_bridge/node_modules/@ethersproject/providers/lib/json-rpc-provider.js:29:53) at rejected (/Users/boreaswu/Desktop/Crypto/FT_alpha/Axelar_bridge/node_modules/@ethersproject/providers/lib/json-rpc-provider.js:21:65) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { reason: 'execution reverted: Invalid signature', code: 'UNPREDICTABLE_GAS_LIMIT', method: 'estimateGas', transaction: { to: '0xF0681C0ae6eA87dbF8D7D2fe56d81D05C7f4eCa6', data: '0x52c253c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000419c745e4f1316fea37908c08e2f77c5efff909c4eddca4164138519fc619cfea06e6214eb8215ad583d1d7ca1db0cb732d8c2238a21e2875869c2e81bcef064661c00000000000000000000000000000000000000000000000000000000000000', accessList: null }, error: Error: processing response error (body="{\"jsonrpc\":\"2.0\",\"error\":{\"code\":3,\"message\":\"execution reverted: Invalid signature\",\"data\":\"0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000011496e76616c6964207369676e6174757265000000000000000000000000000000\"},\"id\":52}\n", error={"code":3,"data":"0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000011496e76616c6964207369676e6174757265000000000000000000000000000000"}, requestBody="{\"method\":\"eth_estimateGas\",\"params\":[{\"to\":\"0xf0681c0ae6ea87dbf8d7d2fe56d81d05c7f4eca6\",\"data\":\"0x52c253c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000419c745e4f1316fea37908c08e2f77c5efff909c4eddca4164138519fc619cfea06e6214eb8215ad583d1d7ca1db0cb732d8c2238a21e2875869c2e81bcef064661c00000000000000000000000000000000000000000000000000000000000000\"}],\"id\":52,\"jsonrpc\":\"2.0\"}", requestMethod="POST", url="https://rpc.testnet.mantle.xyz", code=SERVER_ERROR, version=web/5.7.1) at Logger.makeError (/Users/boreaswu/Desktop/Crypto/FT_alpha/Axelar_bridge/node_modules/@ethersproject/logger/lib/index.js:238:21) at Logger.throwError (/Users/boreaswu/Desktop/Crypto/FT_alpha/Axelar_bridge/node_modules/@ethersproject/logger/lib/index.js:247:20) at /Users/boreaswu/Desktop/Crypto/FT_alpha/Axelar_bridge/node_modules/@ethersproject/web/lib/index.js:313:32 at step (/Users/boreaswu/Desktop/Crypto/FT_alpha/Axelar_bridge/node_modules/@ethersproject/web/lib/index.js:33:23) at Object.next (/Users/boreaswu/Desktop/Crypto/FT_alpha/Axelar_bridge/node_modules/@ethersproject/web/lib/index.js:14:53) at fulfilled (/Users/boreaswu/Desktop/Crypto/FT_alpha/Axelar_bridge/node_modules/@ethersproject/web/lib/index.js:5:58) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { reason: 'processing response error', code: 'SERVER_ERROR', body: '{"jsonrpc":"2.0","error":{"code":3,"message":"execution reverted: Invalid signature","data":"0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000011496e76616c6964207369676e6174757265000000000000000000000000000000"},"id":52}\n',

You can use publickey: 0xdEc3262D5EADbbD946AdbB48a2A07ABe15B0AB83 and singature: 0x9c745e4f1316fea37908c08e2f77c5efff909c4eddca4164138519fc619cfea06e6214eb8215ad583d1d7ca1db0cb732d8c2238a21e2875869c2e81bcef064661c when reproduce the error.

I guess probably something wrong with the singature or the mintNFT method in their contract!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure against https://rpc.testnet.mantle.xyz using the supplied public key, signature, and mintNFT calldata. Start by comparing the mintNFT ABI in fan_card_abi.json with the signature construction and the gas-estimation path in estimateGas.helper.js. Done means identifying whether the invalid signature comes from the caller data or the contract method and recording a verified resolution.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
blockchain
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.