-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wal2-68: add search token by id functionality
- Loading branch information
Showing
5 changed files
with
172 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...iumWallet/Views/AccountsView/AccountDetails/TokenSelectionView/SearchTokenViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// SearchTokenViewModel.swift | ||
// ConcordiumWallet | ||
// | ||
// Created by Maksym Rachytskyy on 19.03.2024. | ||
// Copyright © 2024 concordium. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import BigInt | ||
|
||
final class SearchTokenViewModel: ObservableObject { | ||
@Published var tokens: [CIS2TokenSelectionRepresentable] = [] | ||
@Published var isSearching: Bool = false | ||
|
||
private let service: CIS2ServiceProtocol | ||
private let accountAddress: String | ||
private let contractIndex: String | ||
|
||
init( | ||
accountAdress: String, | ||
contractIndex: String, | ||
service: CIS2ServiceProtocol | ||
){ | ||
self.service = service | ||
self.accountAddress = accountAdress | ||
self.contractIndex = contractIndex | ||
} | ||
|
||
func runSearch(_ tokenIndex: String) { | ||
isSearching = true | ||
Task { | ||
do { | ||
let data = try await searchTokenData(by: tokenIndex) | ||
await MainActor.run { | ||
tokens = data | ||
isSearching = false | ||
} | ||
} catch { | ||
await MainActor.run { | ||
isSearching = false | ||
} | ||
} | ||
} | ||
} | ||
|
||
func searchTokenData(by tokenId: String) async throws -> [CIS2TokenSelectionRepresentable] { | ||
let metadata = try await service.fetchTokensMetadata(contractIndex: contractIndex, contractSubindex: "0", tokenId: tokenId) | ||
let metadataPairs = try await service.getTokenMetadataPair(metadata: metadata) | ||
let balances = try await service.fetchTokensBalance(contractIndex: contractIndex, contractSubindex: "0", accountAddress: accountAddress, tokenId: tokenId) | ||
|
||
return metadataPairs.map { (metadataItem, details) in | ||
CIS2TokenSelectionRepresentable( | ||
contractName: metadata.contractName, | ||
tokenId: metadataItem.tokenId, | ||
balance: BigInt(balances.first(where: { $0.tokenId == metadataItem.tokenId })?.balance ?? "") ?? .zero, | ||
contractIndex: contractIndex, | ||
name: details.name, | ||
symbol: details.symbol, | ||
decimals: details.decimals ?? 6, | ||
description: details.description, | ||
thumbnail: details.thumbnail?.url, | ||
display: details.display?.url, | ||
unique: details.unique ?? false, | ||
accountAddress: accountAddress) | ||
} | ||
} | ||
} |