web3swift-team / web3swift

Elegant Web3js functionality in Swift. Native ABI parsing and smart contract interactions.
Apache License 2.0
819 stars 439 forks source link

PolicyResolver incorrect maxFeePerGas for eip1559 transaction #834

Open penachett opened 1 year ago

penachett commented 1 year ago

What happened?

When using PolicyResolver (manually or in case of calling writeToChain operation function) for sending eip1559 transaction the maxFeePerGas field is set to value of baseFeePerGas returned by the resolveGasBaseFee resolver method. According to eip1559 maxFeePerGas should't be lower than baseFeePerGas + maxPriorityFeePerGas, so when I am trying to send a transaction and getting maxPriorityFeePerGas higher than maxFeePerGas node returns an error: nodeError(desc: "Server error. Error code: -32000. max priority fee per gas higher than max fee per gas") I assume this is because in this code of Web3+Resolver

public func resolveAll(for tx: inout CodableTransaction, with policies: Policies = .auto) async throws {
    ...
    if case .eip1559 = tx.type {
        tx.maxFeePerGas = await resolveGasBaseFee(for: policies.maxFeePerGasPolicy)
        tx.maxPriorityFeePerGas = await resolveGasPriorityFee(for: policies.maxPriorityFeePerGasPolicy)
    } else {
        ...
    }
}

we are setting tx.maxFeePerGas as baseFee and we should set it as a sum of resolveGasBaseFee() and resolveGasPriorityFee()

What are the steps to reproduce?

We need to set up simple eip1559 tx and try to send it. I tested it on eth goerli network (chainId=5) with quicknode RPC provider.

let chainId = BigUInt(5)
let value = BigUInt(10000000000000)
let from = "0x04c0bdbf60aff56ab1fcb4bfaf774b14506435c3"
let to = "0x5C885b7882277e40c67cA4DEF5f11beCAf1A604D"

let web3 = await web3swift.Web3(
    provider: Web3HttpProvider(
        URL(string: "...")!,
        network: Networks.Custom(networkID: chainId)
    )!
)
do {
    var tx = CodableTransaction(
        type: .eip1559,
        to: EthereumAddress(to)!,
        value: value
    )
    tx.from = EthereumAddress(from)!
    tx.chainID = chainId

    let resolver = PolicyResolver(provider: web3.provider)
    try await resolver.resolveAll(for: &tx)
    print("tx: \n\(tx)")

    try tx.sign(privateKey: ...)
    guard let transactionData = tx.encode(for: .transaction) else { throw Web3Error.dataError }
    let res = try await web3.eth.send(raw: transactionData)
} catch {
    print("error: \(error)")
}

Final transaction I get before send:

Type: EIP-1559
chainID: Optional(5)
Nonce: 32
Gas limit: 21000
Max priority fee per gas: Optional(1023750147)
Max fee per gas: Optional(374)
To: 0x5C885b7882277e40c67cA4DEF5f11beCAf1A604D
Value: 10000000000000
Data: 0x
...

What is the expected behavior?

EIP1559 gas fees set correct and transaction is sent

What is the error thrown?

nodeError(desc: "Server error. Error code: -32000. max priority fee per gas higher than max fee per gas")

What's the stack trace said?

-

OS version

macOS Ventura 13.3.1

Library version

3.1.0

vanijain3108 commented 7 months ago

I am creating crypto wallet application for iOS and using this web3swift package . I need to find gas limit for native and custom token both using Abi file while transfer token but unable to find that can anyone help me out for this issue . I can't provide the static gas limit I need to find for every transaction gas limit

vanijain3108 commented 7 months ago

I am creating crypto wallet application for iOS and using this web3swift package . I need to find gas limit for native and custom token both using Abi file while transfer token but unable to find that can anyone help me out for this issue . I can't provide the static gas limit I need to find for every transaction gas limit

I need the result of this issue as soon as possible . if anybody knows please give a reply