-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tRPC): move exchange account credentials checker to utils
- Loading branch information
Showing
2 changed files
with
62 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |