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

Commit cd67be5

Browse files
authored
Fix currency rate not applied on first price insert (#1835)
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 3c88c7d commit cd67be5

3 files changed

Lines changed: 36 additions & 6 deletions

File tree

Packages/Store/Sources/Models/PriceRecord.swift

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

9191
extension AssetPrice {
92-
var record: PriceRecord {
93-
return PriceRecord(
92+
func record(fiatPrice: Double) -> PriceRecord {
93+
PriceRecord(
9494
assetId: assetId,
95-
price: price,
96-
priceUsd: price,
95+
price: fiatPrice,
96+
priceUsd: self.price,
9797
priceChangePercentage24h: priceChangePercentage24h
9898
)
9999
}

Packages/Store/Sources/Stores/PriceStore.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ 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 convertedPrice = assetPrice.price * rate.rate
33+
let _ = try assetPrice.record(fiatPrice: convertedPrice).upsertAndFetch(
3334
db,
3435
onConflict: [],
3536
doUpdate: { _ in [
36-
PriceRecord.Columns.price.set(to: assetPrice.price * rate.rate),
37+
PriceRecord.Columns.price.set(to: convertedPrice),
3738
PriceRecord.Columns.priceUsd.set(to: assetPrice.price),
3839
PriceRecord.Columns.priceChangePercentage24h.set(to: assetPrice.priceChangePercentage24h),
3940
PriceRecord.Columns.updatedAt.set(to: Date()),
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)