Skip to content

Commit

Permalink
feat(exchanges): add paper trading (#38)
Browse files Browse the repository at this point in the history
* feat(exchanges): add `destroy` method

* feat(exchanges): add `isPaper` property to exchange instance
  • Loading branch information
bludnic authored Aug 14, 2024
1 parent f2163b3 commit 0fed0c5
Show file tree
Hide file tree
Showing 20 changed files with 436 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class OrderSynchronizerWsWatcher extends OrderSynchronizerWatcher {

override async disable() {
await super.disable();
await this.exchangeService.ccxt.close();
await this.exchangeService.destroy();
}

protected async watchOrders() {
Expand Down
2 changes: 2 additions & 0 deletions packages/db/src/xprisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const xprismaClient = prismaClient.$extends({
secretKey: true,
password: true,
isDemoAccount: true,
isPaperAccount: true,
},
compute(exchangeAccount) {
return {
Expand All @@ -95,6 +96,7 @@ const xprismaClient = prismaClient.$extends({
secretKey: exchangeAccount.secretKey,
password: exchangeAccount.password,
isDemoAccount: exchangeAccount.isDemoAccount,
isPaperAccount: exchangeAccount.isPaperAccount,
};
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/exchanges/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"types": "src/index.ts",
"exports": {
".": "./src/index.ts",
"./client": "./src/client/cache/providers/memory-cache.provider.ts",
"./client": "./src/client/index.ts",
"./server": "./src/server/cache/providers/prisma-cache.provider.ts",
"./dist": "./src/index.ts"
},
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions packages/exchanges/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./constants.js";
export * from "./cache/providers/memory-cache.provider.js";
2 changes: 1 addition & 1 deletion packages/exchanges/src/exchange.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class ExchangeProvider {
return;
}

void exchange.ccxt.close();
void exchange.destroy();
delete this.privateExchanges[id];

console.log(
Expand Down
7 changes: 6 additions & 1 deletion packages/exchanges/src/exchanges/ccxt/exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ import type { IExchange, IExchangeCredentials } from "../../types/index.js";
import { cache } from "../../cache.js";
import { fetcher } from "../../utils/next/fetcher.js";
import { normalize } from "./normalize.js";
import { exchangeCodeMapCCXT } from "./constants.js";
import { exchangeCodeMapCCXT } from "../../client/constants.js";

export class CCXTExchange implements IExchange {
public isPaper = false;
public exchangeCode: ExchangeCode;
public ccxt: Exchange;

Expand Down Expand Up @@ -81,6 +82,10 @@ export class CCXTExchange implements IExchange {
}
}

async destroy() {
await this.ccxt.close();
}

async loadMarkets(): Promise<Dictionary<Market>> {
const cacheProvider = cache.getCacheProvider();
return cacheProvider.getMarkets(this.exchangeCode, this.ccxt);
Expand Down
8 changes: 6 additions & 2 deletions packages/exchanges/src/exchanges/ccxt/factory.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { ExchangeCode } from "@opentrader/types";
import { CCXTExchange } from "./exchange.js";
import { PaperExchange } from "./paper-exchange.js";
import type { IExchangeCredentials } from "../../types/index.js";

export function createExchange(exchangeCode: ExchangeCode) {
return (credentials?: IExchangeCredentials) =>
new CCXTExchange(exchangeCode, credentials);
return (credentials?: IExchangeCredentials) => {
if (credentials?.isPaperAccount) return new PaperExchange(exchangeCode);

return new CCXTExchange(exchangeCode, credentials);
};
}
Loading

0 comments on commit 0fed0c5

Please sign in to comment.