Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit 367036d

Browse files
committed
Fix currency rate not applied on first price insert
The upsert doUpdate path (with rate conversion) only runs on conflict. On first insert, prices were stored in USD regardless of selected currency. Changed AssetPrice.record to accept rate parameter so both INSERT and UPDATE paths write correctly converted prices.
1 parent e7ad53c commit 367036d

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

Packages/Store/Sources/Models/PriceRecord.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ extension PriceRecord: Identifiable {
8989
}
9090

9191
extension AssetPrice {
92-
var record: PriceRecord {
93-
return PriceRecord(
92+
func record(rate: Double) -> PriceRecord {
93+
PriceRecord(
9494
assetId: assetId,
95-
price: price,
95+
price: price * rate,
9696
priceUsd: price,
9797
priceChangePercentage24h: priceChangePercentage24h
9898
)

Packages/Store/Sources/Stores/PriceStore.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public struct PriceStore: Sendable {
2929
let rate = try getRate(currency: currency)
3030
try db.write { db in
3131
for assetPrice in prices {
32-
let _ = try assetPrice.record.upsertAndFetch(
32+
let _ = try assetPrice.record(rate: rate.rate).upsertAndFetch(
3333
db,
3434
onConflict: [],
3535
doUpdate: { _ in [
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c). Gem Wallet. All rights reserved.
2+
3+
import Testing
4+
import Store
5+
import Primitives
6+
import PrimitivesTestKit
7+
import StoreTestKit
8+
9+
struct PriceStoreTests {
10+
11+
@Test
12+
func firstInsertAppliesCurrencyRate() throws {
13+
let db = DB.mockWithChains([.ethereum])
14+
let priceStore = PriceStore(db: db)
15+
let fiatRateStore = FiatRateStore(db: db)
16+
17+
let assetId = Chain.ethereum.assetId
18+
let rate = 90.0
19+
let priceUsd = 2500.0
20+
let currency = Currency.rub.rawValue
21+
22+
try fiatRateStore.add([FiatRate(symbol: currency, rate: rate)])
23+
try priceStore.updatePrices(prices: [.mock(assetId: assetId, price: priceUsd)], currency: currency)
24+
25+
let result = try priceStore.getPrices(for: [assetId.identifier])
26+
27+
#expect(result.first?.price == priceUsd * rate)
28+
}
29+
}

0 commit comments

Comments
 (0)