Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Gem/Views/MainTabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,17 @@ extension MainTabView {
private func onReceiveNotification(notification: PushNotification) async {
do {
switch notification {
case .transaction(let walletIndex, let assetId):
//select wallet
case let .transaction(walletIndex, assetId, transactionId):
if walletIndex != model.wallet.index.asInt {
walletService.setCurrent(for: walletIndex)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should push navigationState.wallet.append(Scenes.Asset(asset: transaction.asset)) first and then do preload transaction next, in this case if transactionsService.getTransaction is slow, you are at right asset. this works only if you have an asset already

let asset = try await assetsService.getOrFetchAsset(for: assetId)
navigationState.wallet.append(Scenes.Asset(asset: asset))
if let walletId = walletService.currentWalletId {
let transaction = try await transactionsService.getTransaction(walletId: walletId, transactionId: transactionId)
navigationState.wallet.append(transaction)
}
case .priceAlert(let assetId):
let asset = try await assetsService.getOrFetchAsset(for: assetId)
navigationState.wallet.append(Scenes.Price(asset: asset))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,19 @@ public final class TransactionsService: Sendable {
try await prefetchAssets(walletId: wallet.walletId, transactions: response.transactions)
try transactionStore.addTransactions(walletId: wallet.id, transactions: response.transactions)
try addressStore.addAddressNames(response.addressNames)

store.setTransactionsForAssetTimestamp(assetId: assetId.identifier, value: newTimestamp)
}


public func getTransaction(walletId: WalletId, transactionId: String) async throws -> TransactionExtended {
let transaction = try await provider.getTransaction(transactionId: transactionId)

try await prefetchAssets(walletId: walletId, transactions: [transaction])
try transactionStore.addTransactions(walletId: walletId.id, transactions: [transaction])

return try transactionStore.getTransaction(walletId: walletId.id, transactionId: transactionId)
}

private func prefetchAssets(walletId: WalletId, transactions: [Transaction]) async throws {
let assetIds = transactions.map { $0.assetIds }.flatMap { $0 }
if assetIds.isEmpty {
Expand Down
7 changes: 6 additions & 1 deletion Packages/GemAPI/Sources/GemAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public enum GemAPI: TargetType {
case deletePriceAlerts(deviceId: String, priceAlerts: [PriceAlert])

case getTransactions(deviceId: String, options: TransactionsFetchOption)

case getTransaction(transactionId: String)

case getAsset(AssetId)
case getAssets([AssetId])
case getSearchAssets(query: String, chains: [Chain], tags: [AssetTag])
Expand Down Expand Up @@ -58,6 +59,7 @@ public enum GemAPI: TargetType {
.getSubscriptions,
.getDevice,
.getTransactions,
.getTransaction,
.getAsset,
.getSearchAssets,
.getAssetsList,
Expand Down Expand Up @@ -112,6 +114,8 @@ public enum GemAPI: TargetType {
return "/v1/devices/\(device.id)"
case .getTransactions(let deviceId, _):
return "/v2/transactions/device/\(deviceId)"
case .getTransaction(let transactionId):
return "/v1/transactions/\(transactionId)"
case .getAsset(let id):
return "/v1/assets/\(id.identifier.replacingOccurrences(of: "/", with: "%2F"))"
case .getAssets:
Expand Down Expand Up @@ -147,6 +151,7 @@ public enum GemAPI: TargetType {
.deleteDevice,
.getAssetsList,
.getAsset,
.getTransaction,
.getPriceAlerts,
.getNFTAssets,
.markets:
Expand Down
11 changes: 10 additions & 1 deletion Packages/GemAPI/Sources/GemAPIService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public protocol GemAPISubscriptionService: Sendable {
public protocol GemAPITransactionService: Sendable {
func getTransactionsAll(deviceId: String, walletIndex: Int, fromTimestamp: Int) async throws -> TransactionsResponse
func getTransactionsForAsset(deviceId: String, walletIndex: Int, asset: AssetId, fromTimestamp: Int) async throws -> TransactionsResponse
func getTransaction(transactionId: String) async throws -> Transaction
}

public protocol GemAPIPriceAlertService: Sendable {
Expand Down Expand Up @@ -176,13 +177,21 @@ extension GemAPIService: GemAPITransactionService {
.request(.getTransactions(deviceId: deviceId, options: options))
.map(as: TransactionsResponse.self)
}

public func getTransactionsAll(deviceId: String, walletIndex: Int, fromTimestamp: Int) async throws -> TransactionsResponse {
let options = TransactionsFetchOption(wallet_index: walletIndex.asInt32, asset_id: .none, from_timestamp: fromTimestamp.asUInt32)
return try await provider
.request(.getTransactions(deviceId: deviceId, options: options))
.map(as: TransactionsResponse.self)
}

public func getTransaction(transactionId: String) async throws -> Transaction {
let transaction = try await provider
.request(.getTransaction(transactionId: transactionId))
.map(as: Transaction.self)

return transaction
}
}

extension GemAPIService: GemAPIAssetsListService {
Expand Down
4 changes: 2 additions & 2 deletions Packages/Primitives/Sources/PushNotification+Primitives.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import Foundation

public enum PushNotification: Equatable, Sendable {
case transaction(walletIndex: Int, AssetId)
case transaction(walletIndex: Int, AssetId, transactionId: String)
case asset(AssetId)
case priceAlert(AssetId)
case buyAsset(AssetId)
Expand All @@ -27,7 +27,7 @@ public enum PushNotification: Equatable, Sendable {
case .transaction:
let transaction = try decoder.decode(PushNotificationTransaction.self, from: data)
let assetId = try AssetId(id: transaction.assetId)
self = .transaction(walletIndex: transaction.walletIndex.asInt, assetId)
self = .transaction(walletIndex: transaction.walletIndex.asInt, assetId, transactionId: transaction.transactionId)
case .asset:
let asset = try decoder.decode(PushNotificationAsset.self, from: data)
self = .asset(try AssetId(id: asset.assetId))
Expand Down
6 changes: 6 additions & 0 deletions Packages/Store/Sources/Stores/TransactionStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public struct TransactionStore: Sendable {
.fetchAll(db)
}
}

public func getTransaction(walletId: String, transactionId: String) throws -> TransactionExtended {
try db.read { db in
try TransactionRequest(walletId: walletId, transactionId: transactionId).fetch(db)
}
}

public func addTransactions(walletId: String, transactions: [Transaction]) throws {
if transactions.isEmpty {
Expand Down
Loading