Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When calling the contract transferFrom method, gas cannot be calculated #4770

Open
liuchengts opened this issue Jun 24, 2024 · 1 comment
Open
Assignees
Labels
investigate Under investigation and may be a bug. v6 Issues regarding v6

Comments

@liuchengts
Copy link

Ethers Version

6.13.1

Search Terms

transferFrom、estimateGas

Describe the Problem

Before calling the contract transferFrom method, I want to calculate the gas estimate for it, but the actual result is that it cannot be calculated. The amazing thing is that the gas estimate can be performed normally when calling the contract transfer

Code Snippet

import {BrowserProvider, Contract, ethers, JsonRpcSigner, parseEther, parseUnits} from "ethers";
// @ts-ignore
import {InfuraProvider} from "ethers/src.ts/providers/provider-infura";
import {jsonToString} from "@/web3/web3";

export const USDT_ADDRESS = '0x55d398326f99059fF775485246999027B3197955'
export const CUSTOM_ADDRESS = '0x4cd16D0a8AecBd2e4e3816043B29C27f45783a74'

export class Web3Service {
    provider: BrowserProvider;
    signer: JsonRpcSigner;
    address: string;

    constructor(windowEthereum: any) {
        this.provider = new ethers.BrowserProvider(windowEthereum)
        this.provider.getSigner().then(signer => {
            this.signer = signer;
            this.signer.getAddress().then((address: string) => {
                this.address = address
                console.log("address:", address)
            });
        });
    }

    getContract(readOnly: Boolean): Contract {
        if (readOnly) {
            return new ethers.Contract(USDT_ADDRESS,
                ["function balanceOf(address account) external view returns (uint256)",
                    "function allowance(address _owner, address spender) external view returns (uint256)"],
                this.provider)
        } else {
            return new ethers.Contract(USDT_ADDRESS,
                ["function approve(address spender, uint256 amount) external returns (bool)",
                    "function transfer(address recipient, uint256 amount) external returns (bool)",
                    "function transferFrom(address sender, address recipient, uint256 amount) external returns (bool)"],
                this.signer)
        }
    }
}

// initialization
const web3Service = new Web3Service(window.ethereum);
const account=web3Service.address

// Gas cannot be estimated normally
async function transferFrom() {
    const contract: Contract = web3Service.getContract(false)
    // 将以太币转为 wei
    const amount = parseEther("0.01");
    console.log("-----:", account, "  ", CUSTOM_ADDRESS, "   ", amount)
    // Unable to call estimated gas
    const estimateGas = await contract.getFunction("transferFrom").estimateGas(account, CUSTOM_ADDRESS, amount);
    // const estimateGas = await contract.transferFrom.estimateGas(getAccount(), CUSTOM_ADDRESS, amount);
    console.log("estimateGas:", estimateGas)
    let overrides = {
        gasLimit: estimateGas,
        gasPrice: parseUnits('1', 'gwei')
    };
    contract.getFunction("transferFrom").send(account, CUSTOM_ADDRESS, amount, overrides)
        .then((result) => {
            console.log(result)
        }).catch((error) => {
        console.error('transferFrom error:', error);
    });
}
// Gas can be estimated normally
async function transfer() {
    // 将以太币转为 wei
    const amount = parseEther("0.01");
    const contract: Contract = web3Service.getContract(false)
    const estimateGas = await contract.getFunction("transfer").estimateGas(CUSTOM_ADDRESS, amount);
    let overrides = {
        gasLimit: estimateGas,
        gasPrice: parseUnits('1', 'gwei')
    };
    contract.getFunction("transfer").send(CUSTOM_ADDRESS, amount, overrides)
        .then((result) => {
            console.log(result)
        }).catch((error) => {
        console.error('transfer error:', error);
    });

}

Contract ABI

["function approve(address spender, uint256 amount) external returns (bool)",
                    "function transfer(address recipient, uint256 amount) external returns (bool)",
                    "function transferFrom(address sender, address recipient, uint256 amount) external returns (bool)"]

Errors

errors.js:124 Uncaught (in promise) Error: missing revert data (action="estimateGas", data=null, reason=null, transaction={ "data": "0x23b872dd000000000000000000000000068207376ede7da8784986899e8138370d5533840000000000000000000000004cd16d0a8aecbd2e4e3816043b29c27f45783a740000000000000000000000000000000000000000000000000de0b6b3a7640000", "from": "0x068207376eDe7Da8784986899E8138370d553384", "to": "0x55d398326f99059fF775485246999027B3197955" }, invocation=null, revert=null, code=CALL_EXCEPTION, version=6.13.1)
    at makeError (errors.js:124:1)
    at getBuiltinCallException (abi-coder.js:102:21)
    at AbiCoder.getBuiltinCallException (abi-coder.js:203:1)
    at BrowserProvider.getRpcError (provider-jsonrpc.js:672:31)
    at BrowserProvider.getRpcError (provider-browser.js:67:1)
    at eval (provider-jsonrpc.js:298:1)

Environment

Other (please specify)

Environment (Other)

next 14.2.3;react 18

@liuchengts liuchengts added investigate Under investigation and may be a bug. v6 Issues regarding v6 labels Jun 24, 2024
@liuchengts liuchengts changed the title When calling the contract transferFrom method, gas cannot be calculatedAdd Bug Title Here When calling the contract transferFrom method, gas cannot be calculatedAdd Bug Jun 24, 2024
@liuchengts liuchengts changed the title When calling the contract transferFrom method, gas cannot be calculatedAdd Bug When calling the contract transferFrom method, gas cannot be calculated Jun 24, 2024
@hilmanshini
Copy link

is it solved yet?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigate Under investigation and may be a bug. v6 Issues regarding v6
Projects
None yet
Development

No branches or pull requests

3 participants