Skip to content

Commit

Permalink
feat(tRPC): move exchange account credentials checker to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
bludnic committed Jul 25, 2024
1 parent 2bb63b7 commit 96cb88e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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,
Expand All @@ -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",
};
}
42 changes: 42 additions & 0 deletions packages/trpc/src/utils/exchange-account.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 96cb88e

Please sign in to comment.