Skip to content

Commit

Permalink
[NotificationService] Change exchange rate stored format (number time…
Browse files Browse the repository at this point in the history
…stamp and pair) (#1945)
  • Loading branch information
jeanregisser authored and celo-ci-bot-user committed Nov 28, 2019
1 parent 8abb8b7 commit 4b75656
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
16 changes: 13 additions & 3 deletions packages/notification-service/src/exchange/exchangeQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,24 @@ const SELL_AMOUNTS = {

export async function handleExchangeQuery() {
const contractKitInstance = await getContractKit()
const fetchTime = Date.now().toString()
const fetchTime = Date.now()
const [dollarMakerRate, goldMakerRate] = await Promise.all([
getExchangeRate(CURRENCY_ENUM.DOLLAR, contractKitInstance),
getExchangeRate(CURRENCY_ENUM.GOLD, contractKitInstance),
])

writeExchangeRatePair(CURRENCY_ENUM.DOLLAR, dollarMakerRate.toString(), fetchTime)
writeExchangeRatePair(CURRENCY_ENUM.GOLD, goldMakerRate.toString(), fetchTime)
writeExchangeRatePair(
CURRENCY_ENUM.GOLD,
CURRENCY_ENUM.DOLLAR,
dollarMakerRate.toString(),
fetchTime
)
writeExchangeRatePair(
CURRENCY_ENUM.DOLLAR,
CURRENCY_ENUM.GOLD,
goldMakerRate.toString(),
fetchTime
)
}

async function getExchangeRate(makerToken: CURRENCY_ENUM, contractKitInstance: ContractKit) {
Expand Down
15 changes: 10 additions & 5 deletions packages/notification-service/src/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CURRENCY_ENUM } from '@celo/utils'
import { CURRENCIES, CURRENCY_ENUM } from '@celo/utils'
import * as admin from 'firebase-admin'
import i18next from 'i18next'
import { Currencies } from './blockscout/transfers'
Expand Down Expand Up @@ -44,9 +44,9 @@ interface PendingRequests {
}

interface ExchangeRateObject {
makerToken: CURRENCY_ENUM
pair: string
exchangeRate: string
timestamp: string
timestamp: number // timestamp in milliseconds
}

let registrations: Registrations = {}
Expand Down Expand Up @@ -149,11 +149,16 @@ export function setPaymentRequestNotified(uid: string): Promise<void> {
}

export function writeExchangeRatePair(
takerToken: CURRENCY_ENUM,
makerToken: CURRENCY_ENUM,
exchangeRate: string,
timestamp: string
timestamp: number
) {
const exchangeRateRecord: ExchangeRateObject = { makerToken, exchangeRate, timestamp }
const exchangeRateRecord: ExchangeRateObject = {
pair: `${CURRENCIES[takerToken].code}/${CURRENCIES[makerToken].code}`,
exchangeRate,
timestamp,
}
exchangeRatesRef.push(exchangeRateRecord)
console.debug('Recorded exchange rate ', exchangeRateRecord)
}
Expand Down

0 comments on commit 4b75656

Please sign in to comment.