SPB Git

spb/os-vault Public

Self-custody, multi-chain crypto wallet for macOS. One recovery phrase, six chain families, zero API keys — nothing leaves your Mac.

Swift 96% Shell 3.4% Makefile 0.6%
1.8 KB · 72 lines swift
Raw Blame History
1import Foundation23public actor SolanaTokenListRepository: TokenRepository {4    // MARK: - Properties56    let tokenListSource: SolanaTokenListSource7    let storage: SolanaTokenListStorage89    // Hash map for address and token.10    var records: [String: TokenMetadata] = [:]1112    public init(13        tokenListSource: SolanaTokenListSource,14        storage: SolanaTokenListStorage = InMemorySolanaTokenListStorage()15    ) {16        self.storage = storage17        self.tokenListSource = tokenListSource18    }1920    public func setup() async throws {21        try await fill()22    }2324    public func get(address: String) async throws -> TokenMetadata? {25        if records.isEmpty {26            try await fill()27        }2829        return records[address]30    }3132    public func get(addresses: [String]) async throws -> [String: TokenMetadata] {33        if records.isEmpty {34            try await fill()35        }3637        var result: [String: TokenMetadata] = [:]3839        for address in addresses {40            result[address] = records[address]41        }4243        return result44    }4546    public func all() async throws -> [String: TokenMetadata] {47        if records.isEmpty {48            try await fill()49        }5051        return records52    }5354    public func reset() async throws {55        records = [:]56        await storage.save(tokens: nil)57    }5859    func fill() async throws {60        // Load from storage61        if let storageData = await storage.getTokens(), !storageData.isEmpty {62            records = Dictionary(storageData.map { ($0.mintAddress, $0) }, uniquingKeysWith: { lhs, _ in lhs })63            return64        }6566        // Load from source67        let sourceData = try await tokenListSource.download()68        records = Dictionary(sourceData.map { ($0.mintAddress, $0) }, uniquingKeysWith: { lhs, _ in lhs })69        await storage.save(tokens: sourceData)70    }71}72