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

Estimate Gas incorrectly every other transaction #882

Open
nigelfang opened this issue Jan 21, 2025 · 0 comments
Open

Estimate Gas incorrectly every other transaction #882

nigelfang opened this issue Jan 21, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@nigelfang
Copy link

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

@nigelfang nigelfang added the bug Something isn't working label Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant