Skip to content

Estimate Gas incorrectly every other transaction #882

Open
@nigelfang

Description

@nigelfang

What happened?

For every other transaction, the estimate gas price is incorrect, usually by 50-80% of what the previous correct gas price was. What's even stranger is that setting the gasLimit manually seems to have no effect on what gasLimit is being used on the network, maybe the network is using estimateGasPrice internally and overriding the manual?

func createTransaction(functions: [Int], dataHash: String) async throws -> String {
        guard let contract = contract else {
            throw NSError(domain: "", code: -1, userInfo: [NSLocalizedDescriptionKey: "Contract not initialized"])
        }
        let functionFees: [Int: BigUInt] = [
            5: 1 // function 5 (F)= 1
        ]
        // Calculate the total ETH value required
        var totalValue = BigUInt(0)
        for function in functions {
            if let fee = functionFees[function] {
                totalValue += fee
            }
        }
        // Prepare transaction parameters
        let parameters: [AnyObject] = [functions as AnyObject, dataHash as AnyObject]
        // Create write operation
        let method = "createTransaction"
        
        guard let writeOp = contract.createWriteOperation(method, parameters: parameters) else {
            throw NSError(domain: "", code: -1, userInfo: [NSLocalizedDescriptionKey: "Failed to create write operation"])
        }
        // Set transaction parameters
        let ethValue = BigUInt((Double(totalValue))*pow(10, 18))
        writeOp.transaction.value = ethValue
        writeOp.transaction.from = EthereumAddress(myAddress)
        writeOp.transaction.chainID = BigUInt(chainID)
        
        var estimatedGas = try await web3.eth.estimateGas(for: writeOp.transaction) * BigUInt(1.2)
        print("Estimated gas: \(estimatedGas)")
        estimatedGas += 100000
        writeOp.transaction.gasLimit = 1000000
        let gasPrice = try await web3.eth.gasPrice() * BigUInt(1.2)
        writeOp.transaction.gasPrice = gasPrice
        print("Estimated gas: \(estimatedGas)")
        print("gas price: \(gasPrice)")
        // Send transaction
        let result = try await writeOp.writeToChain(password: password)
        return result.hash
    }

What are the steps to reproduce?

Using hardhat for local dev
send transaction 1, will be correct and no problems
send transaction 2, will underestimate gas cost and fail

What is the expected behavior?

both transactions should act the same with similar gas costs

What is the error thrown?

TransactionExecutionError: Transaction ran out of gas

What's the stack trace said?

Sources/web3swift/EthereumAPICalls/Ethereum/IEth+Defaults.swift/estimateGas

OS version

macOS 14.6.1, iOS 18.1, XCode 16.2

Library version

3.3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions