Skip to content

PolicyResolver incorrect maxFeePerGas for eip1559 transaction #834

Open
@penachett

Description

@penachett

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

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