Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Implementing EIP-1559
- Add required constants to Web3
- Add public Web3 extension with `verifyEip1559Block` and `calcBaseFee` methods
- Add draft ChainVersion enum to differ Blockchain versions
  • Loading branch information
yaroslavyaroslav committed Mar 14, 2022
commit 27a6e9e98b3dca884308ca687c701e589a079094
7 changes: 7 additions & 0 deletions Sources/web3swift/Web3/Web3+Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@
//

import Foundation
import BigInt

struct Constants {
static let infuraHttpScheme = ".infura.io/v3/"
static let infuraWsScheme = ".infura.io/ws/v3/"
static let infuraToken = "4406c3acf862426c83991f1752c46dd8"
}

extension Web3 {
static let BaseFeeChangeDenominator: BigUInt = 8 // Bounds the amount the base fee can change between blocks.
static let ElasticityMultiplier: BigUInt = 2 // Bounds the maximum gas limit an EIP-1559 block may have.
static let InitialBaseFee: BigUInt = 1000000000 // Initial base fee for EIP-1559 blocks.
}
69 changes: 69 additions & 0 deletions Sources/web3swift/Web3/Web3+EIP1559.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// File.swift
//
//
// Created by Yaroslav on 14.03.2022.
//

import Foundation
import BigInt

/// EIP-1559 Base fee extension
public extension Web3 {
func verifyEip1559Block(chain version: ChainVersion, parent: Block, current: Block) -> Bool {
return true
}

func calcBaseFee(chain version: ChainVersion, parent: Block) -> BigUInt {
// If the current block is the first EIP-1559 block, return the InitialBaseFee.
guard .London == version else { return Web3.InitialBaseFee }

let parentGasTarget = parent.gasLimit / Web3.ElasticityMultiplier

// If the parent gasUsed is the same as the target, the baseFee remains unchanged.
if parent.gasUsed == parentGasTarget {
return parent.baseFeePerGas

} else if parent.gasUsed > parentGasTarget {
// If the parent block used more gas than its target, the baseFee should increase.
let gasUsedDelta = parent.gasUsed - parentGasTarget
let baseFeePerGasDelta = max(parent.baseFeePerGas * gasUsedDelta / parentGasTarget / Web3.BaseFeeChangeDenominator, 1)
let expectedBaseFeePerGas = parent.baseFeePerGas + baseFeePerGasDelta

return expectedBaseFeePerGas
} else {
// Otherwise if the parent block used less gas than its target, the baseFee should decrease.
let gasUsedDelta = parentGasTarget - parent.gasUsed
let baseFeePerGasDelta = parent.baseFeePerGas * gasUsedDelta / parentGasTarget / Web3.BaseFeeChangeDenominator
let expectedBaseFeePerGas = parent.baseFeePerGas - baseFeePerGasDelta

return expectedBaseFeePerGas
}
}
}


public extension Web3 {
enum ChainVersion {
case Byzantium
case Petersburg
case Istanbul
case MuirGlacier
case Berlin
case London
case ArrowGlacier
case MergeFork
/*
ByzantiumBlock // Byzantium switch block
ConstantinopleBlock // Constantinople switch block
PetersburgBlock // Petersburg switch block
IstanbulBlock // Istanbul switch block
MuirGlacierBlock // Eip-2384 (bomb delay) switch block
BerlinBlock // Berlin switch block
LondonBlock // London switch block
ArrowGlacierBlock // Eip-4345 (bomb delay) switch block
MergeForkBlock
*/
}
}