Skip to content

Commit

Permalink
Add ChainIDFetchable protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
gemcoder21 committed Jun 12, 2024
1 parent 9ec97d3 commit 04e299d
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 4 deletions.
8 changes: 8 additions & 0 deletions Packages/Blockchain/Sources/Aptos/AptosService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,11 @@ extension AptosService: ChainTokenable {
false
}
}

// MARK: - ChainIDFetchable

extension AptosService: ChainIDFetchable {
public func getChainID() async throws -> String {
fatalError()
}
}
8 changes: 8 additions & 0 deletions Packages/Blockchain/Sources/Bitcoin/BitcoinService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions Packages/Blockchain/Sources/ChainService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
9 changes: 7 additions & 2 deletions Packages/Blockchain/Sources/Cosmos/CosmosProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: "")!
Expand All @@ -29,7 +30,8 @@ public enum CosmosProvider: TargetType {
.delegations,
.undelegations,
.rewards,
.validators:
.validators,
.nodeInfo:
return .GET
case .broadcast:
return .POST
Expand Down Expand Up @@ -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"
}
}

Expand All @@ -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))
Expand Down
10 changes: 10 additions & 0 deletions Packages/Blockchain/Sources/Cosmos/CosmosService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
16 changes: 16 additions & 0 deletions Packages/Blockchain/Sources/Cosmos/Models/CosmosNodeInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
12 changes: 12 additions & 0 deletions Packages/Blockchain/Sources/Ethereum/EthereumService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<BigIntable>.self).result.value.description
}
}
8 changes: 8 additions & 0 deletions Packages/Blockchain/Sources/Near/NearService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
7 changes: 6 additions & 1 deletion Packages/Blockchain/Sources/Protocols/ChainServiceable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down
8 changes: 8 additions & 0 deletions Packages/Blockchain/Sources/Solana/SolanaService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
8 changes: 8 additions & 0 deletions Packages/Blockchain/Sources/Sui/SuiService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions Packages/Blockchain/Sources/Ton/TonService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
}
Expand Down
8 changes: 8 additions & 0 deletions Packages/Blockchain/Sources/Tron/TronService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions Packages/Blockchain/Sources/Xrp/XRPService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,11 @@ extension XRPService: ChainTokenable {
false
}
}

// MARK: - ChainIDFetchable

extension XRPService: ChainIDFetchable {
public func getChainID() async throws -> String {
fatalError()
}
}

0 comments on commit 04e299d

Please sign in to comment.