SPB Git

Repository Public

ERC20.swift 23.1 KB 341 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : Sources/QyvoraEthereum/ERC20.swift8// Module           : Qyvora Ethereum9// Repository       : https://git.spboucher.ai/qyvora10// Project Website  : Not specified11// Created          : 2026-12-0412// 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 QyvoraMath42import QyvoraKeccak43import QyvoraCore4445/// ABI encoding and decoding for the ERC-20 calls QYVORA makes.46public enum ERC20 {47    /// First four bytes of Keccak-256 of the canonical signature.48    public static func selector(_ signature: String) -> Bytes {49        Bytes(SHA3.keccak256(Bytes(signature.utf8)).prefix(4))50    }5152    static func word(_ address: EthereumAddress) -> Bytes { Bytes(repeating: 0, count: 12) + address.bytes }5354    public static let transferSelector = selector("transfer(address,uint256)")55    public static let approveSelector = selector("approve(address,uint256)")56    public static let balanceOfSelector = selector("balanceOf(address)")57    public static let allowanceSelector = selector("allowance(address,address)")58    public static let decimalsSelector = selector("decimals()")59    public static let symbolSelector = selector("symbol()")60    public static let nameSelector = selector("name()")61    /// keccak256("Transfer(address,address,uint256)")62    public static let transferTopic = SHA3.keccak256(Bytes("Transfer(address,address,uint256)".utf8))63    /// keccak256("Approval(address,address,uint256)")64    public static let approvalTopic = SHA3.keccak256(Bytes("Approval(address,address,uint256)".utf8))6566    public static func transfer(to: EthereumAddress, amount: UInt256) -> Bytes {67        transferSelector + word(to) + amount.bytes3268    }6970    public static func approve(spender: EthereumAddress, amount: UInt256) -> Bytes {71        approveSelector + word(spender) + amount.bytes3272    }7374    public static func balanceOf(_ owner: EthereumAddress) -> Bytes { balanceOfSelector + word(owner) }7576    public static func allowance(owner: EthereumAddress, spender: EthereumAddress) -> Bytes {77        allowanceSelector + word(owner) + word(spender)78    }7980    /// A decoded call found in transaction data, for the confirmation screen and the cold-mode review.81    public enum Call: Equatable, Sendable {82        case transfer(to: EthereumAddress, amount: UInt256)83        case approve(spender: EthereumAddress, amount: UInt256)84    }8586    /// Decodes transfer / approve calldata; nil for anything else or for a malformed encoding87    /// (wrong length, non-zero bytes above the address).88    public static func decodeCall(_ data: Bytes) -> Call? {89        guard data.count == 68 else { return nil }90        let sel = Bytes(data[0..<4])91        guard data[4..<16].allSatisfy({ $0 == 0 }), let address = EthereumAddress(bytes: Bytes(data[16..<36])),92              let amount = UInt256(bigEndian: Bytes(data[36..<68])) else { return nil }93        if sel == transferSelector { return .transfer(to: address, amount: amount) }94        if sel == approveSelector { return .approve(spender: address, amount: amount) }95        return nil96    }9798    /// A uint256 return value: exactly 32 bytes.99    public static func decodeUInt(_ returned: Bytes) -> UInt256? {100        returned.count == 32 ? UInt256(bigEndian: returned) : nil101    }102103    /// `symbol()` / `name()` return data: an ABI string, or a bytes32 for some old tokens (e.g. MKR).104    public static func decodeString(_ returned: Bytes) -> String? {105        func clean(_ b: Bytes) -> String? {106            guard let s = String(validatingUTF8: b.map { CChar(bitPattern: $0) } + [0]), !s.isEmpty, s.count <= 64,107                  s.unicodeScalars.allSatisfy({ $0.value >= 0x20 && $0.value != 0x7f }) else { return nil }108            return s109        }110        if returned.count == 32 {111            return clean(Bytes(returned.prefix(while: { $0 != 0 })))112        }113        guard returned.count >= 64, let offset = UInt256(bigEndian: Bytes(returned[0..<32]))?.uint64, offset == 32,114              let length = UInt256(bigEndian: Bytes(returned[32..<64]))?.uint64, length <= 256,115              returned.count >= 64 + Int(length) else { return nil }116        return clean(Bytes(returned[64..<(64 + Int(length))]))117    }118119    /// The unlimited allowance value (2^256 − 1) that many applications request.120    public static func isUnlimited(_ amount: UInt256) -> Bool {121        amount >= UInt256(bigEndian: [0x7f] + Bytes(repeating: 0xff, count: 31))!122    }123}124125/// A token contract known to the wallet.126public struct TokenInfo: Equatable, Hashable, Sendable, Codable, Identifiable {127    public let network: EthereumNetwork128    public let address: EthereumAddress129    public let symbol: String130    public let name: String131    public let decimals: Int132    /// CoinGecko identifier for prices, when known.133    public let priceID: String?134    /// True for tokens added by the user rather than taken from the built-in list.135    public let custom: Bool136137    public var id: String { "\(network.rawValue):\(address.checksummed)" }138139    public init(network: EthereumNetwork, address: EthereumAddress, symbol: String, name: String, decimals: Int, priceID: String? = nil, custom: Bool = false) {140        self.network = network141        self.address = address142        self.symbol = symbol143        self.name = name144        self.decimals = decimals145        self.priceID = priceID146        self.custom = custom147    }148}149150/// Built-in lists of widely used ERC-20 contracts on each main network (addresses from each issuer's documentation,151/// carried over from SP Wallet's verified table). On-chain `decimals()` is re-checked before a token is used.152public enum BuiltInTokens {153    /// One row: symbol, name, decimals, contract address (EIP-55), CoinGecko identifier, and `pegUSD`.154    ///155    /// `pegUSD` is true for a token whose issuer or bridge redeems it one for one against the US dollar or against such156    /// a token (USDC, USDT, DAI, PYUSD, USDS and their bridged representations USDC.e, USDT0, Binance-Peg, Standard157    /// Bridge). It decides how the dollar ceiling of an unaudited mainnet build is converted into units158    /// (`MainnetPolicy.stablecoinCeilingPrice`, QYV-SVC-087): a pegged token is valued at no less than 0.90 USD159    /// whatever the price source answers. EURC is pegged to the euro, not to the dollar: its flag is false and it is160    /// valued like any other token, with two agreeing price sources. The flag is read from these tables only, never161    /// from a stored profile: a token added by the user is never treated as pegged.162    typealias Row = (String, String, Int, String, String?, pegUSD: Bool)163164    private static let mainnetTable: [Row] = [165        ("USDC", "USD Coin", 6, "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", "usd-coin", pegUSD: true),166        ("USDT", "Tether USD", 6, "0xdAC17F958D2ee523a2206206994597C13D831ec7", "tether", pegUSD: true),167        ("DAI", "Dai", 18, "0x6B175474E89094C44Da98b954EedeAC495271d0F", "dai", pegUSD: true),168        ("PYUSD", "PayPal USD", 6, "0x6c3ea9036406852006290770BEdFcAbA0e23A0e8", "paypal-usd", pegUSD: true),169        ("USDS", "USDS", 18, "0xdC035D45d973E3EC169d2276DDab16f1e407384F", "usds", pegUSD: true),170        ("EURC", "Euro Coin", 6, "0x1aBaEA1f7C830bD89Acc67eC4af516284b1bC33c", "euro-coin", pegUSD: false),171        ("WETH", "Wrapped Ether", 18, "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", "weth", pegUSD: false),172        ("WBTC", "Wrapped Bitcoin", 8, "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599", "wrapped-bitcoin", pegUSD: false),173        ("cbBTC", "Coinbase Wrapped BTC", 8, "0xcbB7C0000aB88B473b1f5aFd9ef808440eed33Bf", "coinbase-wrapped-btc", pegUSD: false),174        ("wstETH", "Wrapped stETH", 18, "0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0", "wrapped-steth", pegUSD: false),175        ("LINK", "Chainlink", 18, "0x514910771AF9Ca656af840dff83E8264EcF986CA", "chainlink", pegUSD: false),176        ("UNI", "Uniswap", 18, "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984", "uniswap", pegUSD: false),177        ("AAVE", "Aave", 18, "0x7Fc66500c84A76Ad7e9c93437bFc5Ac33E2DDaE9", "aave", pegUSD: false),178        ("MKR", "Maker", 18, "0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2", "maker", pegUSD: false),179        ("LDO", "Lido DAO", 18, "0x5A98FcBEA516Cf06857215779Fd812CA3beF1B32", "lido-dao", pegUSD: false),180        ("PAXG", "PAX Gold", 18, "0x45804880De22913dAFE09f4980848ECE6EcbAf78", "pax-gold", pegUSD: false),181        ("ARB", "Arbitrum", 18, "0xB50721BCf8d664c30412Cfbc6cf7a15145234ad1", "arbitrum", pegUSD: false),182        ("POL", "Polygon Ecosystem Token", 18, "0x455e53CBB86018Ac2B8092FdCd39d8444aFFC3F6", "polygon-ecosystem-token", pegUSD: false),183        ("SHIB", "Shiba Inu", 18, "0x95aD61b0a150d79219dCF64E1E6Cc01f0B64C4cE", "shiba-inu", pegUSD: false),184        ("PEPE", "Pepe", 18, "0x6982508145454Ce325dDbE47a25d4ec3d2311933", "pepe", pegUSD: false),185        ("CRV", "Curve DAO", 18, "0xD533a949740bb3306d119CC777fa900bA034cd52", "curve-dao-token", pegUSD: false),186        ("COMP", "Compound", 18, "0xc00e94Cb662C3520282E6f5717214004A7f26888", "compound-governance-token", pegUSD: false),187        ("ONDO", "Ondo", 18, "0xfAbA6f8e4a5E8Ab82F62fe7C39859FA577269BE3", "ondo-finance", pegUSD: false),188        ("ENA", "Ethena", 18, "0x57e114B691Db790C35207b2e685D4A43181e6061", "ethena", pegUSD: false),189    ]190191    // The six tables below were generated on 2026-12-05 from SP Wallet's accepted registry (research/accepted.json,192    // registry v5 of 2026-11-10; rule in its docs/TOKENS.md: the issuer's or the bridge's own document states the193    // address, CoinGecko lists the same address for the coin identifier kept here, and the contract answers the194    // expected symbol(), decimals() and a non-zero supply on two endpoints). A subset was kept: stablecoins, wrapped195    // ether and bitcoin, the wrapped native coin and a few widely held tokens.196    //197    // One symbol is one contract per network, and a representation keeps its own symbol and name:198    // "USDC" is the token issued by Circle on that network, "USDC.e" a bridged representation of Ethereum USDC that199    // Circle does not issue; on BNB Smart Chain USDC, USDT, DAI and ETH are Binance-Peg tokens with 18 decimals.200    // USDT0 answers "USD₮0" to symbol(); UNI on Base has no price identifier (CoinGecko lists no address for it), so an201    // unaudited build cannot send it on mainnet.202    private static let baseTable: [Row] = [203        ("USDC", "USD Coin", 6, "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913", "usd-coin", pegUSD: true),204        ("USDT", "Bridged USDT (Base Standard Bridge)", 6, "0xfde4C96c8593536E31F229EA8f37b2ADa2699bb2", "l2-standard-bridged-usdt-base", pegUSD: true),205        ("DAI", "Dai (Base Standard Bridge)", 18, "0x50c5725949A6F0c72E6C4a641F24049A917DB0Cb", "l2-standard-bridged-dai-base", pegUSD: true),206        ("USDS", "USDS", 18, "0x820C137fa70C8691f0e44Dc420a5e53c168921Dc", "usds", pegUSD: true),207        ("EURC", "EURC", 6, "0x60a3E35Cc302bFA44Cb288Bc5a4F316Fdb1adb42", "euro-coin", pegUSD: false),208        ("WETH", "Wrapped Ether", 18, "0x4200000000000000000000000000000000000006", "l2-standard-bridged-weth-base", pegUSD: false),209        ("WBTC", "Wrapped Bitcoin", 8, "0x0555E30da8f98308EdB960aa94C0Db47230d2B9c", "wrapped-bitcoin", pegUSD: false),210        ("cbBTC", "Coinbase Wrapped BTC", 8, "0xcbB7C0000aB88B473b1f5aFd9ef808440eed33Bf", "coinbase-wrapped-btc", pegUSD: false),211        ("wstETH", "Lido wstETH (Base bridge)", 18, "0xc1CBa3fCea344f92D9239c08C0568f6F2F0ee452", "superbridge-bridged-wsteth-base", pegUSD: false),212        ("LINK", "Chainlink", 18, "0x88Fb150BDc53A65fe94Dea0c9BA0a6dAf8C6e196", "chainlink", pegUSD: false),213        ("UNI", "Uniswap (Base Standard Bridge)", 18, "0xc3De830EA07524a0761646a6a4e4be0e114a3C83", nil, pegUSD: false),214        ("AAVE", "Aave", 18, "0x63706e401c06ac8513145b7687A14804d17f814b", "aave", pegUSD: false),215        ("AERO", "Aerodrome", 18, "0x940181a94A35A4569E4529A3CDfB74e38FD98631", "aerodrome-finance", pegUSD: false),216        ("CAKE", "PancakeSwap", 18, "0x3055913c90Fcc1A6CE9a358911721eEb942013A1", "pancakeswap-token", pegUSD: false),217    ]218219    private static let arbitrumTable: [Row] = [220        ("USDC", "USD Coin", 6, "0xaf88d065e77c8cC2239327C5EDb3A432268e5831", "usd-coin", pegUSD: true),221        ("USDC.e", "Bridged USDC (Arbitrum bridge)", 6, "0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8", "usd-coin-ethereum-bridged", pegUSD: true),222        ("USDT0", "USDT0 (Tether omnichain, LayerZero OFT)", 6, "0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9", "usdt0", pegUSD: true),223        ("DAI", "Dai (Sky Arbitrum bridge)", 18, "0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1", "makerdao-arbitrum-bridged-dai-arbitrum-one", pegUSD: true),224        ("USDS", "USDS", 18, "0x6491c05A82219b8D1479057361ff1654749b876b", "usds", pegUSD: true),225        ("PYUSD", "PayPal USD", 6, "0x46850aD61C2B7d64d08c9C754F45254596696984", "paypal-usd", pegUSD: true),226        ("WETH", "Wrapped Ether", 18, "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1", "arbitrum-bridged-weth-arbitrum-one", pegUSD: false),227        ("WBTC", "Wrapped BTC (Arbitrum bridge)", 8, "0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f", "arbitrum-bridged-wbtc-arbitrum-one", pegUSD: false),228        ("cbBTC", "Coinbase Wrapped BTC", 8, "0xcbB7C0000aB88B473b1f5aFd9ef808440eed33Bf", "coinbase-wrapped-btc", pegUSD: false),229        ("wstETH", "Lido wstETH (Arbitrum bridge)", 18, "0x5979D7b546E38E414F7E9822514be443A4800529", "arbitrum-bridged-wsteth-arbitrum", pegUSD: false),230        ("LINK", "Chainlink", 18, "0xf97f4df75117a78c1A5a0DBb814Af92458539FB4", "chainlink", pegUSD: false),231        ("UNI", "Uniswap", 18, "0xFa7F8980b0f1E64A2062791cc3b0871572f1F7f0", "uniswap", pegUSD: false),232        ("AAVE", "Aave", 18, "0xba5DdD1f9d7F570dc94a51479a000E3BCE967196", "aave", pegUSD: false),233        ("ARB", "Arbitrum", 18, "0x912CE59144191C1204E64559FE8253a0e49E6548", "arbitrum", pegUSD: false),234        ("CAKE", "PancakeSwap", 18, "0x1b896893dfc86bb67Cf57767298b9073D2c1bA2c", "pancakeswap-token", pegUSD: false),235        ("GMX", "GMX", 18, "0xfc5A1A6EB076a2C7aD06eD22C90d7E710E35ad0a", "gmx", pegUSD: false),236    ]237238    private static let optimismTable: [Row] = [239        ("USDC", "USD Coin", 6, "0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85", "usd-coin", pegUSD: true),240        ("USDC.e", "Bridged USDC (Optimism Standard Bridge)", 6, "0x7F5c764cBc14f9669B88837ca1490cCa17c31607", "bridged-usd-coin-optimism", pegUSD: true),241        ("USDT0", "USDT0 (Tether omnichain, LayerZero OFT)", 6, "0x01bFF41798a0BcF287b996046Ca68b395DbC1071", "usdt0", pegUSD: true),242        ("DAI", "Dai (Sky Optimism bridge)", 18, "0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1", "makerdao-optimism-bridged-dai-optimism", pegUSD: true),243        ("WETH", "Wrapped Ether", 18, "0x4200000000000000000000000000000000000006", "l2-standard-bridged-weth-optimism", pegUSD: false),244        ("WBTC", "Wrapped BTC (Optimism Standard Bridge)", 8, "0x68f180fcCe6836688e9084f035309E29Bf0A2095", "wrapped-bitcoin", pegUSD: false),245        ("wstETH", "Lido wstETH (Optimism bridge)", 18, "0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb", "superbridge-bridged-wsteth-optimism", pegUSD: false),246        ("LINK", "Chainlink", 18, "0x350a791Bfc2C21F9Ed5d10980Dad2e2638ffa7f6", "chainlink", pegUSD: false),247        ("UNI", "Uniswap", 18, "0x6fd9d7AD17242c41f7131d257212c54A0e816691", "uniswap", pegUSD: false),248        ("AAVE", "Aave", 18, "0x76FB31fb4af56892A25e32cFC43De717950c9278", "aave", pegUSD: false),249        ("OP", "Optimism", 18, "0x4200000000000000000000000000000000000042", "optimism", pegUSD: false),250    ]251252    private static let polygonTable: [Row] = [253        ("USDC", "USD Coin", 6, "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359", "usd-coin", pegUSD: true),254        ("USDC.e", "Bridged USDC (Polygon PoS Bridge)", 6, "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174", "bridged-usdc-polygon-pos-bridge", pegUSD: true),255        ("USDT0", "USDT0 (Tether omnichain, LayerZero OFT)", 6, "0xc2132D05D31c914a87C6611C10748AEb04B58e8F", "usdt0", pegUSD: true),256        ("DAI", "Dai (Polygon PoS Bridge)", 18, "0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063", "polygon-pos-bridged-dai-polygon-pos", pegUSD: true),257        ("PYUSD", "PayPal USD", 6, "0x99aF3EeA856556646C98c8B9b2548Fe815240750", "paypal-usd", pegUSD: true),258        ("WETH", "Wrapped Ether (Polygon PoS Bridge)", 18, "0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619", "polygon-pos-bridged-weth-polygon-pos", pegUSD: false),259        ("WBTC", "Wrapped BTC (Polygon PoS Bridge)", 8, "0x1BFD67037B42Cf73acF2047067bd4F2C47D9BfD6", "polygon-bridged-wbtc-polygon-pos", pegUSD: false),260        ("wstETH", "Lido wstETH (Polygon PoS Bridge)", 18, "0x03b54A6e9a984069379fae1a4fC4dBAE93B3bCCD", "polygon-bridged-wsteth-polygon", pegUSD: false),261        ("WPOL", "Wrapped POL", 18, "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270", "wmatic", pegUSD: false),262        ("LINK", "Chainlink (Polygon PoS Bridge)", 18, "0x53E0bca35eC356BD5ddDFebbD1Fc0fD03FaBad39", "chainlink", pegUSD: false),263        ("UNI", "Uniswap (Polygon PoS Bridge)", 18, "0xb33EaAd8d922B1083446DC23f610c2567fB5180f", "uniswap", pegUSD: false),264        ("AAVE", "Aave", 18, "0xD6DF932A45C0f255f85145f286eA0b292B21C90B", "aave", pegUSD: false),265    ]266267    private static let bscTable: [Row] = [268        ("USDC", "Binance-Peg USD Coin", 18, "0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d", "binance-bridged-usdc-bnb-smart-chain", pegUSD: true),269        ("USDT", "Binance-Peg BSC-USD (USDT)", 18, "0x55d398326f99059fF775485246999027B3197955", "binance-bridged-usdt-bnb-smart-chain", pegUSD: true),270        ("DAI", "Binance-Peg Dai", 18, "0x1AF3F329e8BE154074D8769D1FFa4eE058B1DBc3", "binance-peg-dai", pegUSD: true),271        ("ETH", "Binance-Peg Ethereum", 18, "0x2170Ed0880ac9A755fd29B2688956BD959F933F8", "binance-peg-weth", pegUSD: false),272        ("WBTC", "Wrapped Bitcoin", 8, "0x0555E30da8f98308EdB960aa94C0Db47230d2B9c", "wrapped-bitcoin", pegUSD: false),273        ("BTCB", "Binance-Peg BTCB", 18, "0x7130d2A12B9BCbFAe4f2634d864A1Ee1Ce3Ead9c", "binance-bitcoin", pegUSD: false),274        ("wstETH", "Lido wstETH (Wormhole + Axelar bridge)", 18, "0x26c5e01524d2E6280A48F2c50fF6De7e52E9611C", "wormhole-bridged-wsteth-bsc", pegUSD: false),275        ("WBNB", "Wrapped BNB", 18, "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c", "wbnb", pegUSD: false),276        ("LINK", "Binance-Peg ChainLink", 18, "0xF8A0BF9cF54Bb92F17374d9e9A321E6a111a51bD", "chainlink", pegUSD: false),277        ("UNI", "Binance-Peg Uniswap", 18, "0xBf5140A22578168FD562DCcF235E5D43A02ce9B1", "uniswap", pegUSD: false),278        ("AAVE", "Binance-Peg Aave", 18, "0xfb6115445Bff7b52FeB98650C87f44907E58f802", "aave", pegUSD: false),279        ("CAKE", "PancakeSwap", 18, "0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82", "pancakeswap-token", pegUSD: false),280    ]281282    private static let avalancheTable: [Row] = [283        ("USDC", "USD Coin", 6, "0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E", "usd-coin", pegUSD: true),284        ("USDC.e", "Bridged USDC (Avalanche Bridge)", 6, "0xA7D7079b0FEaD91F3e65f86E8915Cb59c1a4C664", "usd-coin-avalanche-bridged-usdc-e", pegUSD: true),285        ("USDT", "Tether USD", 6, "0x9702230A8Ea53601f5cD2dc00fDBc13d4dF4A8c7", "tether", pegUSD: true),286        ("EURC", "EURC", 6, "0xC891EB4cbdEFf6e073e859e987815Ed1505c2ACD", "euro-coin", pegUSD: false),287        ("WETH.e", "Wrapped Ether (Avalanche Bridge)", 18, "0x49D5c2BdFfac6CE2BFdB6640F4F80f226bc10bAB", "avalanche-bridged-weth-avalanche", pegUSD: false),288        ("WBTC", "Wrapped Bitcoin", 8, "0x0555E30da8f98308EdB960aa94C0Db47230d2B9c", "wrapped-bitcoin", pegUSD: false),289        ("BTC.b", "Bitcoin (BTC.b)", 8, "0x152b9d0FdC40C096757F570A51E494bd4b943E50", "bitcoin-avalanche-bridged-btc-b", pegUSD: false),290        ("WAVAX", "Wrapped AVAX", 18, "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7", "wrapped-avax", pegUSD: false),291        ("GMX", "GMX", 18, "0x62edc0692BD897D2295872a9FFCac5425011c661", "gmx", pegUSD: false),292    ]293294    /// The raw rows of a network, as written above (the tests check each address against its EIP-55 checksum).295    static func table(for network: EthereumNetwork) -> [Row] {296        switch network {297        case .mainnet: return mainnetTable298        case .base: return baseTable299        case .arbitrum: return arbitrumTable300        case .optimism: return optimismTable301        case .polygon: return polygonTable302        case .bsc: return bscTable303        case .avalanche: return avalancheTable304        case .sepolia, .baseSepolia, .arbitrumSepolia, .optimismSepolia, .polygonAmoy, .bscTestnet, .avalancheFuji: return []305        }306    }307308    private static func build(_ network: EthereumNetwork) -> [TokenInfo] {309        table(for: network).compactMap { row in310            // only an address written with its exact EIP-55 checksum is accepted in a built-in list311            guard let a = EthereumAddress(row.3), a.checksummed == row.3 else { return nil }312            return TokenInfo(network: network, address: a, symbol: row.0, name: row.1, decimals: row.2, priceID: row.4)313        }314    }315316    public static let mainnet: [TokenInfo] = build(.mainnet)317318    private static let byNetwork: [EthereumNetwork: [TokenInfo]] =319        Dictionary(uniqueKeysWithValues: EthereumNetwork.mainNetworks.map { ($0, $0 == .mainnet ? mainnet : build($0)) })320321    private static let peggedByNetwork: [EthereumNetwork: Set<EthereumAddress>] =322        Dictionary(uniqueKeysWithValues: EthereumNetwork.mainNetworks.map { network in323            (network, Set(table(for: network).filter(\.pegUSD).compactMap { EthereumAddress($0.3) }))324        })325326    /// True when `address` is, on `network`, a built-in token marked `pegUSD` in the tables above.327    public static func isDollarPegged(_ address: EthereumAddress, network: EthereumNetwork) -> Bool {328        peggedByNetwork[network]?.contains(address) ?? false329    }330331    /// The built-in token at this contract address, if any (never a token added by the user).332    public static func token(_ address: EthereumAddress, network: EthereumNetwork) -> TokenInfo? {333        tokens(for: network).first { $0.address == address }334    }335336    /// Built-in tokens of a network. Test networks have none: a token there is added by its address.337    public static func tokens(for network: EthereumNetwork) -> [TokenInfo] {338        byNetwork[network] ?? []339    }340}341
Files main
Menu

Theme