Skip to content
This repository has been archived by the owner on Aug 15, 2023. It is now read-only.

Commit

Permalink
perf: update getWalletsBalances query (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
dolcalmi authored Jul 7, 2022
1 parent 23fa92e commit 8e6b213
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions dealer/src/DealerRemoteWalletV2.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
import { ErrorLevel, Result } from "./Result"
import { GaloyWallet, WalletsBalances } from "./GaloyWalletTypes"
import {
ApolloClient,
NormalizedCacheObject,
createHttpLink,
InMemoryCache,
ApolloLink,
gql,
} from "@apollo/client/core"
import { setContext } from "@apollo/client/link/context"
import fetch from "node-fetch"
import { pino } from "pino"
import { cents2usd, sat2btc } from "./utils"

import { QUERIES, MUTATIONS } from "@galoymoney/client"

import { cents2usd, sat2btc } from "./utils"

import { GaloyWallet, WalletsBalances } from "./GaloyWalletTypes"
import { ErrorLevel, Result } from "./Result"
import {
addAttributesToCurrentSpan,
asyncRunInSpan,
recordExceptionInCurrentSpan,
SemanticAttributes,
} from "./services/tracing"

const BALANCE_QUERY = gql`
query walletsBalance {
me {
defaultAccount {
wallets {
id
balance
walletCurrency
}
}
}
}
`

export class DealerRemoteWalletV2 implements GaloyWallet {
client: ApolloClient<NormalizedCacheObject>
httpLink: ApolloLink
Expand Down Expand Up @@ -90,29 +107,22 @@ export class DealerRemoteWalletV2 implements GaloyWallet {
const usdBalanceOffset = Number(process.env["DEALER_USD_BAL_OFFSET"] ?? 0)

const logger = this.logger.child({ method: "getWalletsBalances()" })
const variables = {
isAuthenticated: this.isAuthenticated(),
recentTransactions: 0,
}
try {
const result = await this.client.query({
query: QUERIES.main,
variables: variables,
})
const result = await this.client.query({ query: BALANCE_QUERY })
logger.debug(
{ query: QUERIES.main, variables, result },
"{query} with {variables} to galoy graphql api successful with {result}",
{ query: BALANCE_QUERY, result },
"{query} to galoy graphql api successful with {result}",
)

const me = result.data?.me
const btcWallet = me?.defaultAccount?.wallets?.find(
(wallet) => wallet?.__typename === "BTCWallet",
(wallet) => wallet.walletCurrency === "BTC",
)
const btcWalletId = btcWallet?.id
const btcWalletBalance: number = (btcWallet?.balance ?? NaN) - btcBalanceOffset

const usdWallet = me?.defaultAccount?.wallets?.find(
(wallet) => wallet?.__typename === "UsdWallet",
(wallet) => wallet.walletCurrency === "USD",
)
const usdWalletId = usdWallet?.id
const usdWalletBalance: number = (usdWallet?.balance ?? NaN) - usdBalanceOffset
Expand Down Expand Up @@ -142,8 +152,8 @@ export class DealerRemoteWalletV2 implements GaloyWallet {
} catch (error) {
recordExceptionInCurrentSpan({ error, level: ErrorLevel.Warn })
logger.error(
{ query: QUERIES.main, variables, error },
"{query} with {variables} to galoy graphql api failed with {error}",
{ query: BALANCE_QUERY, error },
"{query} to galoy graphql api failed with {error}",
)
return { ok: false, error }
}
Expand Down

0 comments on commit 8e6b213

Please sign in to comment.