From 04e299d6402358123925ec7db8f4e137fe72b771 Mon Sep 17 00:00:00 2001 From: gemcoder21 <104884878+gemcoder21@users.noreply.github.com> Date: Wed, 12 Jun 2024 19:23:59 +0100 Subject: [PATCH] Add ChainIDFetchable protocol --- .../Blockchain/Sources/Aptos/AptosService.swift | 8 ++++++++ .../Sources/Bitcoin/BitcoinService.swift | 8 ++++++++ Packages/Blockchain/Sources/ChainService.swift | 6 ++++++ .../Sources/Cosmos/CosmosProvider.swift | 9 +++++++-- .../Sources/Cosmos/CosmosService.swift | 10 ++++++++++ .../Sources/Cosmos/Models/CosmosNodeInfo.swift | 16 ++++++++++++++++ .../Sources/Ethereum/EthereumService.swift | 12 ++++++++++++ .../Blockchain/Sources/Near/NearService.swift | 8 ++++++++ .../Sources/Protocols/ChainServiceable.swift | 7 ++++++- .../Sources/Solana/SolanaService.swift | 8 ++++++++ Packages/Blockchain/Sources/Sui/SuiService.swift | 8 ++++++++ Packages/Blockchain/Sources/Ton/TonService.swift | 8 ++++++++ .../Blockchain/Sources/Tron/TronService.swift | 8 ++++++++ Packages/Blockchain/Sources/Xrp/XRPService.swift | 8 ++++++++ core | 2 +- 15 files changed, 122 insertions(+), 4 deletions(-) diff --git a/Packages/Blockchain/Sources/Aptos/AptosService.swift b/Packages/Blockchain/Sources/Aptos/AptosService.swift index 61294a6a..89d79d11 100644 --- a/Packages/Blockchain/Sources/Aptos/AptosService.swift +++ b/Packages/Blockchain/Sources/Aptos/AptosService.swift @@ -113,3 +113,11 @@ extension AptosService: ChainTokenable { false } } + +// MARK: - ChainIDFetchable + +extension AptosService: ChainIDFetchable { + public func getChainID() async throws -> String { + fatalError() + } +} diff --git a/Packages/Blockchain/Sources/Bitcoin/BitcoinService.swift b/Packages/Blockchain/Sources/Bitcoin/BitcoinService.swift index 4660ffa2..d51b7aa0 100644 --- a/Packages/Blockchain/Sources/Bitcoin/BitcoinService.swift +++ b/Packages/Blockchain/Sources/Bitcoin/BitcoinService.swift @@ -164,6 +164,14 @@ extension BitcoinService: ChainTokenable { } } +// MARK: - ChainIDFetchable + +extension BitcoinService: ChainIDFetchable { + public func getChainID() async throws -> String { + fatalError() + } +} + public extension UTXO { func mapToUnspendTransaction(address: String, coinType: CoinType) -> BitcoinUnspentTransaction { BitcoinUnspentTransaction.with { diff --git a/Packages/Blockchain/Sources/ChainService.swift b/Packages/Blockchain/Sources/ChainService.swift index 22708328..75565621 100644 --- a/Packages/Blockchain/Sources/ChainService.swift +++ b/Packages/Blockchain/Sources/ChainService.swift @@ -122,3 +122,9 @@ extension ChainService: ChainTokenable { } } +extension ChainService: ChainIDFetchable { + public func getChainID() async throws -> String { + return try await Self.service(chain: chain, with: url) + .getChainID() + } +} diff --git a/Packages/Blockchain/Sources/Cosmos/CosmosProvider.swift b/Packages/Blockchain/Sources/Cosmos/CosmosProvider.swift index 9d38ab47..fba55d73 100644 --- a/Packages/Blockchain/Sources/Cosmos/CosmosProvider.swift +++ b/Packages/Blockchain/Sources/Cosmos/CosmosProvider.swift @@ -14,6 +14,7 @@ public enum CosmosProvider: TargetType { case broadcast(data: String) case transaction(id: String) case syncing + case nodeInfo public var baseUrl: URL { return URL(string: "")! @@ -29,7 +30,8 @@ public enum CosmosProvider: TargetType { .delegations, .undelegations, .rewards, - .validators: + .validators, + .nodeInfo: return .GET case .broadcast: return .POST @@ -58,6 +60,8 @@ public enum CosmosProvider: TargetType { return "/cosmos/tx/v1beta1/txs/\(id)" case .syncing: return "/cosmos/base/tendermint/v1beta1/syncing" + case .nodeInfo: + return "/cosmos/base/tendermint/v1beta1/node_info" } } @@ -71,7 +75,8 @@ public enum CosmosProvider: TargetType { .validators, .block, .transaction, - .syncing: + .syncing, + .nodeInfo: return .plain case .broadcast(let data): return .data(Data(data.utf8)) diff --git a/Packages/Blockchain/Sources/Cosmos/CosmosService.swift b/Packages/Blockchain/Sources/Cosmos/CosmosService.swift index 16f5a39d..b277ff14 100644 --- a/Packages/Blockchain/Sources/Cosmos/CosmosService.swift +++ b/Packages/Blockchain/Sources/Cosmos/CosmosService.swift @@ -351,3 +351,13 @@ extension CosmosService: ChainTokenable { false } } + +// MARK: - ChainIDFetchable + +extension CosmosService: ChainIDFetchable { + public func getChainID() async throws -> String { + return try await provider + .request(.nodeInfo) + .map(as: CosmosNodeInfoResponse.self).default_node_info.network + } +} diff --git a/Packages/Blockchain/Sources/Cosmos/Models/CosmosNodeInfo.swift b/Packages/Blockchain/Sources/Cosmos/Models/CosmosNodeInfo.swift index 0f291644..82822dd8 100644 --- a/Packages/Blockchain/Sources/Cosmos/Models/CosmosNodeInfo.swift +++ b/Packages/Blockchain/Sources/Cosmos/Models/CosmosNodeInfo.swift @@ -27,3 +27,19 @@ public struct CosmosBlockResponse: Codable { self.block = block } } + +public struct CosmosNodeInfo: Codable { + public let network: String + + public init(network: String) { + self.network = network + } +} + +public struct CosmosNodeInfoResponse: Codable { + public let default_node_info: CosmosNodeInfo + + public init(default_node_info: CosmosNodeInfo) { + self.default_node_info = default_node_info + } +} diff --git a/Packages/Blockchain/Sources/Ethereum/EthereumService.swift b/Packages/Blockchain/Sources/Ethereum/EthereumService.swift index c65307dd..2c35653e 100644 --- a/Packages/Blockchain/Sources/Ethereum/EthereumService.swift +++ b/Packages/Blockchain/Sources/Ethereum/EthereumService.swift @@ -249,6 +249,8 @@ extension EthereumService: ChainStakable { } } +// MARK: - ChainTokenable + extension EthereumService: ChainTokenable { public func getTokenData(tokenId: String) async throws -> Asset { guard let address = WalletCore.AnyAddress(string: tokenId, coin: chain.chain.coinType)?.description else { @@ -275,3 +277,13 @@ extension EthereumService: ChainTokenable { tokenId.hasPrefix("0x") && Data(fromHex: tokenId) != nil && tokenId.count == 42 } } + +// MARK: - ChainIDFetchable + +extension EthereumService: ChainIDFetchable { + public func getChainID() async throws -> String { + return try await provider + .request(.chainId) + .map(as: JSONRPCResponse.self).result.value.description + } +} diff --git a/Packages/Blockchain/Sources/Near/NearService.swift b/Packages/Blockchain/Sources/Near/NearService.swift index 8dc18971..969b3159 100644 --- a/Packages/Blockchain/Sources/Near/NearService.swift +++ b/Packages/Blockchain/Sources/Near/NearService.swift @@ -154,6 +154,14 @@ extension NearService: ChainTokenable { } } +// MARK: - ChainIDFetchable + +extension NearService: ChainIDFetchable { + public func getChainID() async throws -> String { + fatalError() + } +} + extension NearRPCError: LocalizedError { public var errorDescription: String? { if let data = error.data{ diff --git a/Packages/Blockchain/Sources/Protocols/ChainServiceable.swift b/Packages/Blockchain/Sources/Protocols/ChainServiceable.swift index 80c281f1..d7e91239 100644 --- a/Packages/Blockchain/Sources/Protocols/ChainServiceable.swift +++ b/Packages/Blockchain/Sources/Protocols/ChainServiceable.swift @@ -9,7 +9,8 @@ public typealias ChainServiceable = ChainBalanceable & ChainTransactionStateFetchable & ChainSyncable & ChainStakable & - ChainTokenable + ChainTokenable & + ChainIDFetchable public protocol ChainBalanceable { func coinBalance(for address: String) async throws -> AssetBalance @@ -36,6 +37,10 @@ public protocol ChainSyncable { func getInSync() async throws -> Bool } +public protocol ChainIDFetchable { + func getChainID() async throws -> String +} + public protocol ChainStakable { func getValidators(apr: Double) async throws -> [DelegationValidator] func getStakeDelegations(address: String) async throws -> [DelegationBase] diff --git a/Packages/Blockchain/Sources/Solana/SolanaService.swift b/Packages/Blockchain/Sources/Solana/SolanaService.swift index f1e7393f..0dad8279 100644 --- a/Packages/Blockchain/Sources/Solana/SolanaService.swift +++ b/Packages/Blockchain/Sources/Solana/SolanaService.swift @@ -361,3 +361,11 @@ extension SolanaService: ChainTokenable { tokenId.count.isBetween(40, and: 60) && Base58.decodeNoCheck(string: tokenId) != nil } } + +// MARK: - ChainIDFetchable + +extension SolanaService: ChainIDFetchable { + public func getChainID() async throws -> String { + fatalError() + } +} diff --git a/Packages/Blockchain/Sources/Sui/SuiService.swift b/Packages/Blockchain/Sources/Sui/SuiService.swift index 2f602c6f..85f33927 100644 --- a/Packages/Blockchain/Sources/Sui/SuiService.swift +++ b/Packages/Blockchain/Sources/Sui/SuiService.swift @@ -395,6 +395,14 @@ extension SuiService: ChainStakable { } } +// MARK: - ChainIDFetchable + +extension SuiService: ChainIDFetchable { + public func getChainID() async throws -> String { + fatalError() + } +} + extension SuiSystemState { var epochStartDate: Date { Date(timeIntervalSince1970: (TimeInterval(epochStartTimestampMs) ?? 0) / 1000) diff --git a/Packages/Blockchain/Sources/Ton/TonService.swift b/Packages/Blockchain/Sources/Ton/TonService.swift index c4c07f3a..3899f981 100644 --- a/Packages/Blockchain/Sources/Ton/TonService.swift +++ b/Packages/Blockchain/Sources/Ton/TonService.swift @@ -234,6 +234,14 @@ extension TonService: ChainTokenable { } } +// MARK: - ChainIDFetchable + +extension TonService: ChainIDFetchable { + public func getChainID() async throws -> String { + fatalError() + } +} + struct RunGetMethod: Codable { let stack: [[StackItem]] } diff --git a/Packages/Blockchain/Sources/Tron/TronService.swift b/Packages/Blockchain/Sources/Tron/TronService.swift index 625e58d1..bcf4cd3a 100644 --- a/Packages/Blockchain/Sources/Tron/TronService.swift +++ b/Packages/Blockchain/Sources/Tron/TronService.swift @@ -293,6 +293,14 @@ extension TronService: ChainStakable { } } +// MARK: - ChainIDFetchable + +extension TronService: ChainIDFetchable { + public func getChainID() async throws -> String { + fatalError() + } +} + extension TronService: ChainTokenable { public func getTokenData(tokenId: String) async throws -> Asset { guard let address = WalletCore.AnyAddress(string: tokenId, coin: chain.coinType)?.description else { diff --git a/Packages/Blockchain/Sources/Xrp/XRPService.swift b/Packages/Blockchain/Sources/Xrp/XRPService.swift index b4e90ff6..74607409 100644 --- a/Packages/Blockchain/Sources/Xrp/XRPService.swift +++ b/Packages/Blockchain/Sources/Xrp/XRPService.swift @@ -124,3 +124,11 @@ extension XRPService: ChainTokenable { false } } + +// MARK: - ChainIDFetchable + +extension XRPService: ChainIDFetchable { + public func getChainID() async throws -> String { + fatalError() + } +} diff --git a/core b/core index 20d6ec26..09777228 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit 20d6ec265a262a7a6368bd28ebe772d1c053039d +Subproject commit 09777228e5055b0bb393ce3f8b40280bf1ef45a8