Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Sources/QyvoraSolana/SolanaTokens.swift8// Module : Qyvora Solana9// 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 QyvoraCore42import QyvoraMath4344/// A token the wallet lists without the user adding it. The decimals are a hint for display before the first45/// connection only: `SolanaRPC.mintInfo` reads the mint on chain before any transfer, and a mint whose on-chain46/// decimals or owner program differ from this entry must be treated as a different token.47public struct SolanaToken: Equatable, Hashable, Sendable {48 public let mint: SolanaPublicKey49 public let symbol: String50 public let name: String51 public let decimals: UInt852 public let program: SolanaTokenProgram.Kind53 /// True for a built-in token redeemable one for one against the US dollar (USDC, USDT): its dollar ceiling on an54 /// unaudited mainnet build is converted at no less than 0.90 USD (`MainnetPolicy.stablecoinCeilingPrice`,55 /// QYV-SVC-087). Set in `builtIn` only.56 public let pegUSD: Bool5758 public init(mint: SolanaPublicKey, symbol: String, name: String, decimals: UInt8, program: SolanaTokenProgram.Kind, pegUSD: Bool = false) {59 self.mint = mint60 self.symbol = symbol61 self.name = name62 self.decimals = decimals63 self.program = program64 self.pegUSD = pegUSD65 }6667 /// Built-in list: USDC and USDT on mainnet-beta (mints published by Circle and Tether); nothing on the test68 /// clusters, where the user adds a mint by address.69 public static func builtIn(_ network: SolanaNetwork) -> [SolanaToken] {70 guard network == .mainnetBeta else { return [] }71 return [72 SolanaToken(mint: SolanaPublicKey("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"), symbol: "USDC", name: "USD Coin", decimals: 6, program: .token, pegUSD: true),73 SolanaToken(mint: SolanaPublicKey("Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB"), symbol: "USDT", name: "Tether USD", decimals: 6, program: .token, pegUSD: true),74 ]75 }76}7778/// A mint as read on chain (`getAccountInfo`, `jsonParsed`).79public struct SolanaMintInfo: Equatable, Sendable {80 public let address: SolanaPublicKey81 /// The program that owns the mint account: it decides which program the transfer instruction is sent to and82 /// which associated token accounts are used.83 public let program: SolanaTokenProgram.Kind84 public let decimals: UInt885 /// Total supply in base units.86 public let supply: UInt6487 public let isInitialized: Bool88 /// Names of the Token-2022 extensions of the mint, as the RPC node reports them (empty for the Token program).89 public let extensions: [String]90 /// False when the node could not parse the account (it then returns raw data): the extensions are unknown.91 public let extensionsKnown: Bool9293 public init(address: SolanaPublicKey, program: SolanaTokenProgram.Kind, decimals: UInt8, supply: UInt64, isInitialized: Bool,94 extensions: [String], extensionsKnown: Bool = true) {95 self.address = address96 self.program = program97 self.decimals = decimals98 self.supply = supply99 self.isInitialized = isInitialized100 self.extensions = extensions101 self.extensionsKnown = extensionsKnown102 }103104 /// Token-2022 mint extensions that do not change what a plain `TransferChecked` does or costs: metadata and105 /// group bookkeeping, and the authority to close the mint. A `permanentDelegate` does not affect the transfer106 /// itself either (it lets the issuer move or burn anyone's tokens): it is accepted with a warning.107 public static let harmlessExtensions: Set<String> = [108 "metadataPointer", "tokenMetadata", "groupPointer", "tokenGroup", "groupMemberPointer", "tokenGroupMember", "mintCloseAuthority",109 "permanentDelegate",110 ]111112 /// Why this module refuses to build a transfer of this token, or nil when it accepts.113 ///114 /// **Rule.** A mint owned by the Token program is accepted. A mint owned by Token-2022 is accepted only when115 /// every one of its extensions is in `harmlessExtensions`. Any other extension is refused, by name, because the116 /// transfer would not do what the review screen says or would need accounts this module cannot derive:117 /// `transferFeeConfig` (the recipient receives less than the amount shown), `transferHook` (an arbitrary program118 /// runs during the transfer and needs extra accounts), `confidentialTransferMint`, `nonTransferable`,119 /// `defaultAccountState` (a new recipient account may be frozen), `pausable`, `interestBearingConfig` and120 /// `scaledUiAmountConfig` (the displayed amount differs from base units ÷ 10^decimals), and any extension unknown121 /// to this list. A mint the node could not parse is refused too.122 public var transferRefusal: String? {123 guard isInitialized else { return "the mint is not initialized" }124 guard program == .token2022 else { return nil }125 guard extensionsKnown else { return "the extensions of this Token-2022 mint could not be read" }126 for name in extensions where !Self.harmlessExtensions.contains(name) {127 return "unsupported Token-2022 extension: \(name)"128 }129 return nil130 }131132 /// True when the issuer can move or burn tokens from any account (Token-2022 `permanentDelegate`).133 public var hasPermanentDelegate: Bool { extensions.contains("permanentDelegate") }134}135136/// A token account as read on chain (`getTokenAccountsByOwner`, `jsonParsed`).137public struct SolanaTokenAccount: Equatable, Sendable {138 public let address: SolanaPublicKey139 public let mint: SolanaPublicKey140 public let owner: SolanaPublicKey141 public let program: SolanaTokenProgram.Kind142 /// Balance in base units.143 public let amount: UInt64144 public let decimals: UInt8145 /// "initialized" or "frozen".146 public let state: String147 /// Lamports held by the account (its rent-exempt reserve).148 public let lamports: UInt64149150 public var isFrozen: Bool { state == "frozen" }151152 public init(address: SolanaPublicKey, mint: SolanaPublicKey, owner: SolanaPublicKey, program: SolanaTokenProgram.Kind, amount: UInt64,153 decimals: UInt8, state: String, lamports: UInt64) {154 self.address = address155 self.mint = mint156 self.owner = owner157 self.program = program158 self.amount = amount159 self.decimals = decimals160 self.state = state161 self.lamports = lamports162 }163}164165/// An amount with a sign, in base units (a balance change).166public struct SolanaSignedAmount: Equatable, Hashable, Sendable {167 public let isNegative: Bool168 public let magnitude: UInt64169170 public static let zero = SolanaSignedAmount(isNegative: false, magnitude: 0)171172 public init(isNegative: Bool, magnitude: UInt64) {173 self.isNegative = isNegative && magnitude != 0174 self.magnitude = magnitude175 }176177 /// after − before.178 public init(from before: UInt64, to after: UInt64) {179 self.init(isNegative: after < before, magnitude: after < before ? before - after : after - before)180 }181182 public var isZero: Bool { magnitude == 0 }183}184185/// Exact decimal formatting and parsing of amounts (never through `Double`).186public enum SolanaAmount {187 /// "1.5" for 1_500_000_000 lamports.188 public static func formatLamports(_ lamports: UInt64, maxFractionDigits: Int? = nil) -> String {189 BaseUnits.format(UInt256(lamports), decimals: SolanaNetwork.decimals, maxFractionDigits: maxFractionDigits)190 }191192 public static func format(_ baseUnits: UInt64, decimals: UInt8, maxFractionDigits: Int? = nil) -> String {193 BaseUnits.format(UInt256(baseUnits), decimals: Int(decimals), maxFractionDigits: maxFractionDigits)194 }195196 /// Base units of a decimal text ("0.25"); nil when it is not a number, has too many decimals or exceeds 64 bits.197 public static func parse(_ text: String, decimals: UInt8) -> UInt64? {198 BaseUnits.parse(text, decimals: Int(decimals))?.uint64199 }200201 public static func parseLamports(_ text: String) -> UInt64? { parse(text, decimals: UInt8(SolanaNetwork.decimals)) }202}203