Skip to content

Commit 9302114

Browse files
committed
wip: rate fetching
1 parent a3bee0a commit 9302114

File tree

4 files changed

+55
-10
lines changed

4 files changed

+55
-10
lines changed

background/services/indexing/db.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ function numberArrayCompare(arr1: number[], arr2: number[]) {
121121
return 0
122122
}
123123

124+
type FeedRate = {
125+
id: string
126+
value: bigint
127+
decimals: bigint
128+
time: number
129+
}
130+
124131
export class IndexingDatabase extends Dexie {
125132
private prices!: Dexie.Table<PriceMeasurement, number>
126133

@@ -139,6 +146,11 @@ export class IndexingDatabase extends Dexie {
139146
*/
140147
private customAssets!: Dexie.Table<CustomAsset, number>
141148

149+
/**
150+
* Currency exchange rates
151+
*/
152+
private currencyRates!: Dexie.Table<FeedRate, "id">
153+
142154
/*
143155
* Tokens whose balances should be checked periodically. It might make sense
144156
* for this to be tracked against particular accounts in the future.
@@ -256,6 +268,8 @@ export class IndexingDatabase extends Dexie {
256268
delete customAsset.metadata?.discoveryTxHash
257269
}),
258270
)
271+
272+
this.version(7).stores({ currencyRates: "&id" })
259273
}
260274

261275
async savePriceMeasurement(
@@ -429,6 +443,14 @@ export class IndexingDatabase extends Dexie {
429443
tokenList: v as TokenList,
430444
}))
431445
}
446+
447+
async getCurrencyRates(): Promise<FeedRate[]> {
448+
return this.currencyRates.toArray()
449+
}
450+
451+
async saveCurrencyRates(rates: FeedRate[]): Promise<void> {
452+
await this.currencyRates.bulkPut(rates)
453+
}
432454
}
433455

434456
export async function getOrCreateDb(

background/services/indexing/index.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import {
5757
isSameAsset,
5858
} from "../../redux-slices/utils/asset-utils"
5959
import { wrapIfEnabled } from "../../features"
60+
import fetchRatesFromPriceFeeds from "./price-feeds"
6061

6162
// Transactions seen within this many blocks of the chain tip will schedule a
6263
// token refresh sooner than the standard rate.
@@ -1108,11 +1109,19 @@ export default class IndexingService extends BaseService<Events> {
11081109
private async handleCurrencyRatesAlarm(): Promise<void> {
11091110
logger.info("Syncing currency rates...")
11101111

1111-
const rate = {
1112-
code: "EUR",
1113-
rate: { amount: BigInt("0x06f9d7c8"), decimals: 8n },
1114-
}
1112+
const rates = await fetchRatesFromPriceFeeds(this.chainService)
1113+
1114+
this.db.saveCurrencyRates(Object.values(rates))
1115+
1116+
const currencies = Object.keys(rates).map((id) => {
1117+
const currencyCode = id.split("/")[0]
1118+
1119+
return {
1120+
code: currencyCode,
1121+
rate: { amount: rates[id].value, decimals: rates[id].decimals },
1122+
}
1123+
})
11151124

1116-
this.emitter.emit("updatedCurrencyRates", [rate])
1125+
this.emitter.emit("updatedCurrencyRates", currencies)
11171126
}
11181127
}

background/services/indexing/price-feeds.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type FeedRate = {
2222
id: string
2323
value: bigint
2424
decimals: bigint
25+
time: number
2526
}
2627

2728
export default async function fetchRatesFromPriceFeeds(
@@ -44,6 +45,7 @@ export default async function fetchRatesFromPriceFeeds(
4445
id,
4546
value: (await contract.callStatic.latestRoundData()) as BigNumber,
4647
decimals: (await contract.callStatic.decimals()) as number,
48+
time: Date.now(),
4749
}
4850
}),
4951
)
@@ -55,6 +57,7 @@ export default async function fetchRatesFromPriceFeeds(
5557
id: feed.id,
5658
value: feed.value.toBigInt(),
5759
decimals: BigInt(feed.decimals),
60+
time: feed.time,
5861
},
5962
]),
6063
)

background/services/redux/index.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -947,16 +947,27 @@ export default class ReduxService extends BaseService<never> {
947947
this.store.dispatch(
948948
setDisplayCurrency({
949949
code: "USD",
950-
rate: { amount: 1000000n, decimals: 6n },
950+
rate: { amount: 1000000n, decimals: 10n },
951951
}),
952952
)
953953
}
954954

955-
this.indexingService.emitter.on("updatedCurrencyRates", async (rates) => {
956-
// const currency = await this.preferenceService.getCurrency()
955+
this.indexingService.emitter.on(
956+
"updatedCurrencyRates",
957+
async (currencies) => {
958+
const currency = await this.preferenceService.getCurrency()
957959

958-
this.store.dispatch(setDisplayCurrency(rates[0]))
959-
})
960+
const fallback = {
961+
code: currency.code,
962+
rate: { amount: 1000000n, decimals: 10n },
963+
}
964+
965+
const update =
966+
currencies.find((rate) => rate.code === currency.code) ?? fallback
967+
968+
this.store.dispatch(setDisplayCurrency(update))
969+
},
970+
)
960971

961972
this.indexingService.emitter.on("refreshAsset", (asset) => {
962973
this.store.dispatch(

0 commit comments

Comments
 (0)