diff --git a/packages/trpc/src/routers/private/exchange-accounts/check-account/handler.ts b/packages/trpc/src/routers/private/exchange-accounts/check-account/handler.ts index 3591aa3a..edb70eae 100644 --- a/packages/trpc/src/routers/private/exchange-accounts/check-account/handler.ts +++ b/packages/trpc/src/routers/private/exchange-accounts/check-account/handler.ts @@ -1,6 +1,5 @@ -import { AuthenticationError, InvalidNonce } from "ccxt"; import { xprisma } from "@opentrader/db"; -import { exchangeProvider } from "@opentrader/exchanges"; +import { checkExchangeCredentials } from "../../../../utils/exchange-account.js"; import type { Context } from "../../../../utils/context.js"; import type { TCheckExchangeAccountInputSchema } from "./schema.js"; @@ -22,13 +21,9 @@ export async function checkExchangeAccount({ input, ctx }: Options) { }, }); - const exchange = exchangeProvider.fromAccount(exchangeAccount); - - try { - // to check account credentials validity - // any private endpoint is fine - await exchange.accountAssets(); + const { valid, message } = await checkExchangeCredentials(exchangeAccount); + if (valid) { await xprisma.exchangeAccount.update({ where: { id: exchangeAccount.id, @@ -37,28 +32,24 @@ export async function checkExchangeAccount({ input, ctx }: Options) { expired: false, }, }); - } catch (err) { - if (err instanceof AuthenticationError || err instanceof InvalidNonce) { - await xprisma.exchangeAccount.update({ - where: { - id: exchangeAccount.id, - }, - data: { - expired: true, - }, - }); - return { - valid: false, - error: err.message, - }; - } + return { + valid: true, + message: "Exchange accounts credentials are valid", + }; + } else { + await xprisma.exchangeAccount.update({ + where: { + id: exchangeAccount.id, + }, + data: { + expired: true, + }, + }); - throw err; + return { + valid: false, + error: message, + }; } - - return { - valid: true, - message: "Exchange accounts credentials are valid", - }; } diff --git a/packages/trpc/src/utils/exchange-account.ts b/packages/trpc/src/utils/exchange-account.ts new file mode 100644 index 00000000..5fc30053 --- /dev/null +++ b/packages/trpc/src/utils/exchange-account.ts @@ -0,0 +1,42 @@ +import { AuthenticationError, InvalidNonce } from "ccxt"; +import { exchangeProvider } from "@opentrader/exchanges"; +import { ExchangeAccountWithCredentials } from "@opentrader/db"; +import { xprisma } from "@opentrader/db"; + +/** + * Check if the exchange account credentials are valid. + */ +export async function checkExchangeCredentials( + exchangeAccount: ExchangeAccountWithCredentials, +) { + const exchange = exchangeProvider.fromAccount(exchangeAccount); + + try { + // to check account credentials validity + // any private endpoint is fine + await exchange.accountAssets(); + + return { + valid: true, + message: undefined, + }; + } catch (err) { + if (err instanceof AuthenticationError || err instanceof InvalidNonce) { + await xprisma.exchangeAccount.update({ + where: { + id: exchangeAccount.id, + }, + data: { + expired: true, + }, + }); + + return { + valid: false, + error: err.message, + }; + } + + throw err; + } +}