Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Tests/QyvoraCoreTests/EVMChainsAuditRegressionTests.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 Testing43import QyvoraMath44@testable import QyvoraCore4546/// Regression tests of the multi-chain security audit, area "EVM networks and limits table" (finding QYV-SVC-082):47/// the plausibility floor of the prices that convert a dollar ceiling into coins.48@Suite struct EVMChainsAuditRegressionTests {49 /// QYV-SVC-082. A dollar ceiling divided by a price close to zero is no ceiling. Each native coin whose ceiling is a50 /// dollar figure has a floor, and a price under it cannot be used.51 @Test func priceFloorsBoundTheDollarCeilings() {52 let floors = MainnetPolicy.minimumPlausibleUSDPrice53 // every EVM native coin that is not ether, and every UTXO coin without a ceiling in its own unit54 for network in EthereumNetwork.allCases where !network.nativeIsEther { #expect(floors[network.priceID] != nil, "\(network)") }55 for network in BitcoinNetwork.allCases where !network.isTest {56 if case .usd? = MainnetPolicy.utxoLimits(network)?.value {57 #expect(network.priceID.flatMap { floors[$0] } != nil, "\(network)")58 }59 }60 #expect(floors["solana"] != nil && floors["tron"] != nil)61 for (id, floor) in floors {62 #expect(floor.isFinite && floor > 0, "\(id)")63 #expect(MainnetPolicy.isPlausibleUSDPrice(floor, priceID: id) && MainnetPolicy.isPlausibleUSDPrice(floor * 1_000, priceID: id), "\(id)")64 for bad in [floor * 0.999_999, floor / 1_000, 1e-300, 0, -1, -floor, .nan, .infinity, -.infinity, 1e12, 1e300, .leastNonzeroMagnitude] as [Double] {65 #expect(!MainnetPolicy.isPlausibleUSDPrice(bad, priceID: id), "\(id) at \(bad)")66 }67 // what the ceiling becomes at the floor, in coins: finite, and what one transaction can move at most68 let bound = MainnetPolicy.absoluteNativeCeiling(usd: MainnetPolicy.limits.nativeUSDPerTransaction, priceID: id)69 #expect(bound == 150 / floor && (bound ?? .infinity).isFinite, "\(id)")70 }71 // an asset without a floor keeps the general bounds only72 #expect(MainnetPolicy.isPlausibleUSDPrice(0.000_001, priceID: "usd-coin") && !MainnetPolicy.isPlausibleUSDPrice(0, priceID: "usd-coin"))73 #expect(!MainnetPolicy.isPlausibleUSDPrice(.nan, priceID: "usd-coin") && !MainnetPolicy.isPlausibleUSDPrice(1e12, priceID: "usd-coin"))74 #expect(MainnetPolicy.absoluteNativeCeiling(usd: 150, priceID: "usd-coin") == nil)75 }7677 /// `Ceiling.allows` and `nativeCeiling`: no trap and no "allowed" on a price or an amount that cannot be valued.78 @Test func ceilingsNeverAllowWhatCannotBeValued() {79 let usd = MainnetPolicy.Ceiling.usd(150), native = MainnetPolicy.Ceiling.native(UInt256(50_000_000_000_000_000))80 for price in [nil, 0, -1, .nan, .infinity, -.infinity] as [Double?] {81 #expect(usd.allows(wei: UInt256(1), usdPrice: price) == nil)82 #expect(native.allows(wei: UInt256(1), usdPrice: price) == true)83 #expect(native.allows(wei: .max, usdPrice: price) == false)84 }85 // 2^256 − 1 wei is about 1.16e59 coins: at 1e-50 USD it is far above 150 USD, at 1e-60 USD it is 12 cents.86 // The rule itself cannot tell a wrong price from a right one, which is why a price has a floor.87 #expect(usd.allows(wei: .max, usdPrice: 1e-50) == false)88 #expect(usd.allows(wei: .max, usdPrice: 1e-60) == true)89 #expect(usd.allows(wei: .max, usdPrice: 1e300) == false)90 #expect(usd.allows(wei: .zero, usdPrice: 1e300) == true)91 #expect(usd.allows(wei: UInt256(1_500_000_000_000_000_000), usdPrice: 100) == true)92 #expect(usd.allows(wei: UInt256(1_500_000_000_000_000_001), usdPrice: 100.000_001) == false)93 #expect(native.allows(wei: UInt256(50_000_000_000_000_000), usdPrice: nil) == true)94 #expect(native.allows(wei: UInt256(50_000_000_000_000_001), usdPrice: nil) == false)95 for decimals in [-1, 19, Int.max, Int.min] { #expect(MainnetPolicy.nativeCeiling(usd: 150, priceUSD: 1, decimals: decimals) == nil) }96 for price in [0, -1, .nan, .infinity] as [Double] { #expect(MainnetPolicy.nativeCeiling(usd: 150, priceUSD: price, decimals: 8) == nil) }97 for value in [-1, .nan, .infinity] as [Double] { #expect(MainnetPolicy.nativeCeiling(usd: value, priceUSD: 1, decimals: 8) == nil) }98 #expect(MainnetPolicy.nativeCeiling(usd: 150, priceUSD: 1e-30, decimals: 18) == UInt64(Int64.max))99 #expect(MainnetPolicy.nativeCeiling(usd: 150, priceUSD: 1e-300, decimals: 18) == nil)100 #expect(MainnetPolicy.nativeCeiling(usd: 150, priceUSD: 100, decimals: 9) == 1_500_000_000)101 #expect(MainnetPolicy.nativeCeiling(usd: 0, priceUSD: 100, decimals: 9) == 0)102 }103}104