Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Tests/QyvoraCoreTests/EVMNetworksTests.swift8// Module : Tests9// Repository : https://git.spboucher.ai/qyvora10// Project Website : Not specified11// Created : 2026-12-0512// Last Modified : 2026-12-0513//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation42import Testing43@testable import QyvoraCore4445/// The EVM network table: identifiers that sign transactions, the properties the fee rules read, and the ceilings of46/// an unaudited build per class of network.47@Suite struct EVMNetworksTests {48 @Test func chainIdentifiersAreTheOfficialOnes() {49 let expected: [EthereumNetwork: UInt64] = [50 .mainnet: 1, .sepolia: 11_155_111, .base: 8_453, .baseSepolia: 84_532, .arbitrum: 42_161, .arbitrumSepolia: 421_614,51 .optimism: 10, .optimismSepolia: 11_155_420, .polygon: 137, .polygonAmoy: 80_002, .bsc: 56, .bscTestnet: 97,52 .avalanche: 43_114, .avalancheFuji: 43_113,53 ]54 #expect(EthereumNetwork.allCases.count == 14 && expected.count == 14)55 for network in EthereumNetwork.allCases {56 #expect(network.chainID == expected[network], "\(network)")57 #expect(EthereumNetwork(chainID: network.chainID) == network)58 }59 #expect(Set(EthereumNetwork.allCases.map(\.chainID)).count == 14)60 #expect(EthereumNetwork(chainID: 999) == nil && EthereumNetwork(chainID: 0) == nil)61 }6263 /// Profiles written before the multi-chain upgrade hold "mainnet" or "sepolia": both must decode to the same cases.64 @Test func storedRawValuesStillDecode() throws {65 #expect(EthereumNetwork.mainnet.rawValue == "mainnet" && EthereumNetwork.sepolia.rawValue == "sepolia")66 let decoded = try JSONDecoder().decode([EthereumNetwork].self, from: Data(#"["mainnet","sepolia"]"#.utf8))67 #expect(decoded == [.mainnet, .sepolia])68 #expect(decoded[0].chainID == 1 && decoded[1].chainID == 11_155_111)69 #expect(decoded[0].displayName == "Ethereum" && decoded[1].displayName == "Ethereum Sepolia")70 for network in EthereumNetwork.allCases {71 let data = try JSONEncoder().encode([network])72 #expect(try JSONDecoder().decode([EthereumNetwork].self, from: data) == [network])73 }74 #expect(throws: (any Error).self) { try JSONDecoder().decode([EthereumNetwork].self, from: Data(#"["goerli"]"#.utf8)) }75 }7677 @Test func familiesAndCounterparts() {78 #expect(EthereumNetwork.mainNetworks.count == 7 && EthereumNetwork.testNetworks.count == 7)79 #expect(Set(EthereumNetwork.mainNetworks + EthereumNetwork.testNetworks) == Set(EthereumNetwork.allCases))80 #expect(EthereumNetwork.mainNetworks.allSatisfy { !$0.isTest } && EthereumNetwork.testNetworks.allSatisfy(\.isTest))81 for (main, test) in zip(EthereumNetwork.mainNetworks, EthereumNetwork.testNetworks) {82 #expect(main.family == test.family)83 #expect(main.testCounterpart == test && test.mainCounterpart == main)84 #expect(main.mainCounterpart == main && test.testCounterpart == test)85 #expect(main.nativeSymbol == test.nativeSymbol && main.priceID == test.priceID)86 #expect(main.isOPStack == test.isOPStack && main.isArbitrum == test.isArbitrum && main.hasZeroBaseFee == test.hasZeroBaseFee)87 #expect(EthereumNetwork.networks(of: main.family) == (main, test))88 }89 #expect(Set(EthereumNetwork.mainNetworks.map(\.family)) == Set(EVMFamily.allCases))90 #expect(EthereumNetwork.mainnet.testCounterpart == .sepolia)91 #expect(EthereumNetwork.polygon.testCounterpart == .polygonAmoy && EthereumNetwork.avalancheFuji.mainCounterpart == .avalanche)92 }9394 @Test func nativeCoinsAndPriceIdentifiers() {95 let ether: [EthereumNetwork] = [.mainnet, .base, .arbitrum, .optimism]96 for network in EthereumNetwork.allCases {97 #expect(network.nativeDecimals == 18)98 #expect(network.supportsEIP1559)99 #expect(!network.displayName.isEmpty && !network.shortName.isEmpty && !network.nativeName.isEmpty)100 #expect(network.nativeIsEther == ether.contains(network.mainCounterpart))101 #expect(network.nativeIsEther == (network.nativeSymbol == "ETH"))102 #expect(network.nativeIsEther == (network.priceID == "ethereum"))103 }104 #expect(EthereumNetwork.polygon.nativeSymbol == "POL" && EthereumNetwork.polygon.priceID == "polygon-ecosystem-token")105 #expect(EthereumNetwork.bsc.nativeSymbol == "BNB" && EthereumNetwork.bsc.priceID == "binancecoin")106 #expect(EthereumNetwork.avalanche.nativeSymbol == "AVAX" && EthereumNetwork.avalanche.priceID == "avalanche-2")107 #expect(Set(EthereumNetwork.allCases.map(\.displayName)).count == 14)108 #expect(Set(EthereumNetwork.allCases.map(\.shortName)).count == 14)109 }110111 @Test func feeAndFinalityProperties() {112 #expect(EthereumNetwork.allCases.filter(\.isOPStack) == [.base, .baseSepolia, .optimism, .optimismSepolia])113 #expect(EthereumNetwork.allCases.filter(\.isArbitrum) == [.arbitrum, .arbitrumSepolia])114 #expect(EthereumNetwork.allCases.filter(\.hasZeroBaseFee) == [.bsc, .bscTestnet])115 // 21 000 gas is a constant everywhere but on Arbitrum116 #expect(EthereumNetwork.allCases.filter { !$0.nativeTransferGasIsFixed } == [.arbitrum, .arbitrumSepolia])117 // Polygon is the only network with a minimum tip: 30 gwei118 for network in EthereumNetwork.allCases {119 #expect(network.minimumPriorityFee == (network.family == .polygon ? UInt256(30_000_000_000) : .zero))120 #expect(network.blockTimeSeconds > 0 && network.blockTimeSeconds <= 15)121 #expect(network.finalityConfirmations >= 1)122 // the fallback never claims finality in less than a second, nor waits more than twenty minutes123 let seconds = Double(network.finalityConfirmations) * network.blockTimeSeconds124 #expect(seconds >= 1 && seconds <= 1_200, "\(network): \(seconds) s")125 }126 #expect(EthereumNetwork.mainnet.finalityConfirmations == 64)127 #expect(EVMPredeploys.opStackGasPriceOracle == "0x420000000000000000000000000000000000000F")128 }129130 @Test func explorerLinks() {131 let hash = "0x" + String(repeating: "ab", count: 32), address = "0x" + String(repeating: "cd", count: 20)132 for network in EthereumNetwork.allCases {133 #expect(network.explorerBaseURL.hasPrefix("https://") && !network.explorerBaseURL.hasSuffix("/"))134 #expect(network.explorerTransactionURL(hash: hash)?.absoluteString == network.explorerBaseURL + "/tx/" + hash)135 #expect(network.explorerAddressURL(address: address)?.absoluteString == network.explorerBaseURL + "/address/" + address)136 }137 #expect(Set(EthereumNetwork.allCases.map(\.explorerBaseURL)).count == 14)138 // nothing but a well-formed hash or address ends up in a link139 #expect(EthereumNetwork.base.explorerTransactionURL(hash: "0x12") == nil)140 #expect(EthereumNetwork.base.explorerTransactionURL(hash: hash + "?x=1") == nil)141 #expect(EthereumNetwork.base.explorerTransactionURL(hash: "0x" + String(repeating: "zz", count: 32)) == nil)142 #expect(EthereumNetwork.base.explorerAddressURL(address: address + "/../x") == nil)143 #expect(EthereumNetwork.base.explorerAddressURL(address: String(repeating: "cd", count: 21)) == nil)144 }145146 // MARK: Limits of an unaudited build147148 @Test func everyMainNetworkIsAMainnetForTheGate() {149 for network in EthereumNetwork.allCases {150 #expect(MainnetPolicy.isMainnet(network) == !network.isTest)151 }152 #expect(EthereumNetwork.allCases.filter(MainnetPolicy.isMainnet) == [.mainnet, .base, .arbitrum, .optimism, .polygon, .bsc, .avalanche])153 }154155 /// The existing figures are unchanged; the new ones are the ones the owner's brief gives.156 @Test func limitValues() {157 #expect(MainnetPolicy.limits.etherWeiPerTransaction == UInt256(50_000_000_000_000_000))158 #expect(MainnetPolicy.limits.etherFeeWeiPerTransaction == UInt256(10_000_000_000_000_000))159 #expect(MainnetPolicy.limits.tokenUSDPerTransaction == 150)160 #expect(MainnetPolicy.limits.bitcoinSatoshisPerTransaction == 200_000)161 #expect(MainnetPolicy.limits.bitcoinFeeSatoshisPerTransaction == 20_000 && MainnetPolicy.limits.bitcoinFeeRateSatPerVByte == 200)162 #expect(MainnetPolicy.evmLimits.nativeUSDPerTransaction == 150)163 #expect(MainnetPolicy.evmLimits.nativeUSDPerTransaction == MainnetPolicy.limits.tokenUSDPerTransaction)164 #expect(MainnetPolicy.evmLimits.nativeFeeUSDPerTransaction == 5)165 }166167 @Test func ceilingsPerNetworkClass() {168 for network in EthereumNetwork.allCases {169 if network.nativeIsEther {170 // 0.05 ETH and 0.01 ETH on Ethereum, Base, Arbitrum and OP Mainnet alike171 #expect(MainnetPolicy.nativeValueCeiling(for: network) == .native(UInt256(50_000_000_000_000_000)))172 #expect(MainnetPolicy.feeCeiling(for: network) == .native(UInt256(10_000_000_000_000_000)))173 } else {174 #expect(MainnetPolicy.nativeValueCeiling(for: network) == .usd(150))175 #expect(MainnetPolicy.feeCeiling(for: network) == .usd(5))176 }177 #expect(MainnetPolicy.nativeValueCeiling(for: network).needsPrice == !network.nativeIsEther)178 }179 #expect(MainnetPolicy.describe(MainnetPolicy.nativeValueCeiling(for: .base), network: .base) == "0.05 ETH")180 #expect(MainnetPolicy.describe(MainnetPolicy.feeCeiling(for: .arbitrum), network: .arbitrum) == "0.01 ETH")181 #expect(MainnetPolicy.describe(MainnetPolicy.nativeValueCeiling(for: .polygon), network: .polygon) == "150 USD of POL")182 #expect(MainnetPolicy.describe(MainnetPolicy.feeCeiling(for: .bsc), network: .bsc) == "5 USD of BNB")183 }184185 @Test func etherCeilingIsExactAndNeedsNoPrice() {186 let ceiling = MainnetPolicy.nativeValueCeiling(for: .optimism)187 let limit = UInt256(50_000_000_000_000_000)188 #expect(ceiling.allows(wei: .zero, usdPrice: nil) == true)189 #expect(ceiling.allows(wei: limit, usdPrice: nil) == true)190 #expect(ceiling.allows(wei: limit + UInt256(1), usdPrice: nil) == false)191 #expect(ceiling.allows(wei: .max, usdPrice: 0.000001) == false)192 }193194 @Test func dollarCeilingNeedsAUsablePrice() {195 let value = MainnetPolicy.nativeValueCeiling(for: .avalanche), fee = MainnetPolicy.feeCeiling(for: .polygon)196 let one = UInt256(1_000_000_000_000_000_000)197 // 1 coin at 100 USD is under 150 USD, 2 coins are above198 #expect(value.allows(wei: one, usdPrice: 100) == true)199 #expect(value.allows(wei: one + one, usdPrice: 100) == false)200 // exactly 150 USD passes, one wei more at a round price does not201 #expect(value.allows(wei: one, usdPrice: 150) == true)202 #expect(value.allows(wei: one + one, usdPrice: 75.01) == false)203 // a fee of 0.01 coin: 2 USD at 200 USD, 6 USD at 600 USD204 let hundredth = UInt256(10_000_000_000_000_000)205 #expect(fee.allows(wei: hundredth, usdPrice: 200) == true)206 #expect(fee.allows(wei: hundredth, usdPrice: 600) == false)207 // without a price, or with one that is not a positive finite number, nothing can be verified208 for bad in [nil, 0, -1, Double.nan, Double.infinity] as [Double?] {209 #expect(value.allows(wei: one, usdPrice: bad) == nil)210 #expect(fee.allows(wei: hundredth, usdPrice: bad) == nil)211 }212 // an absurd amount is refused, not wrapped213 #expect(value.allows(wei: .max, usdPrice: 0.5) == false)214 }215}216