Skip to content

Commit

Permalink
feat(bot): additional exchange accounts support
Browse files Browse the repository at this point in the history
  • Loading branch information
bludnic committed Aug 20, 2024
1 parent 8c7b600 commit 06c1722
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/backtesting/src/backtesting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class Backtesting<T extends IBotConfiguration<T>> {
this.processor = createStrategyRunner({
store: this.store,
exchange: this.exchange,
additionalExchanges: [], // @todo not supported yet
botConfig,
botTemplate,
});
Expand Down
18 changes: 14 additions & 4 deletions packages/bot-processor/src/strategy-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,25 @@ export class StrategyRunner<T extends IBotConfiguration> {
private control: IBotControl,
private botConfig: T,
private exchange: IExchange,
private additionalExchanges: IExchange[],
private botTemplate: BotTemplate<T>,
) {}

async start(state: BotState) {
const context = createContext(this.control, this.botConfig, this.exchange, "start", state);
const context = createContext(
this.control,
this.botConfig,
this.exchange,
this.additionalExchanges,
"start",
state,
);

await this.runTemplate(context);
}

async stop(state: BotState) {
const context = createContext(this.control, this.botConfig, this.exchange, "stop", state);
const context = createContext(this.control, this.botConfig, this.exchange, this.additionalExchanges, "stop", state);

await this.runTemplate(context);
}
Expand All @@ -53,6 +61,7 @@ export class StrategyRunner<T extends IBotConfiguration> {
this.control,
this.botConfig,
this.exchange,
this.additionalExchanges,
"process",
state,
market,
Expand Down Expand Up @@ -89,12 +98,13 @@ export class StrategyRunner<T extends IBotConfiguration> {
export function createStrategyRunner<T extends IBotConfiguration>(options: {
store: IStore;
exchange: IExchange;
additionalExchanges: IExchange[];
botConfig: T;
botTemplate: BotTemplate<T>;
}) {
const { botConfig, store, exchange, botTemplate } = options;
const { botConfig, store, exchange, additionalExchanges, botTemplate } = options;

const botControl = new BotControl(store, botConfig);

return new StrategyRunner(botControl, botConfig, exchange, botTemplate);
return new StrategyRunner(botControl, botConfig, exchange, additionalExchanges, botTemplate);
}
1 change: 1 addition & 0 deletions packages/bot-processor/src/types/bot/bot-context.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type TBotContext<T extends IBotConfiguration, S extends BotState = BotSta
* Default exchange instance.
*/
exchange: IExchange;
additionalExchanges: IExchange[];
/**
* Bot control panel
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/bot-processor/src/utils/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export function createContext<T extends IBotConfiguration>(
control: IBotControl,
config: T,
exchange: IExchange,
additionalExchanges: IExchange[],
command: "start" | "stop" | "process", // @todo add type in file
state: BotState,
market: MarketData = {
Expand All @@ -18,6 +19,7 @@ export function createContext<T extends IBotConfiguration>(
control,
config,
exchange,
additionalExchanges,
command,
onStart: command === "start",
onStop: command === "stop",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- CreateTable
CREATE TABLE "_AdditionalExchangeAccounts" (
"A" INTEGER NOT NULL,
"B" INTEGER NOT NULL,
CONSTRAINT "_AdditionalExchangeAccounts_A_fkey" FOREIGN KEY ("A") REFERENCES "Bot" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "_AdditionalExchangeAccounts_B_fkey" FOREIGN KEY ("B") REFERENCES "ExchangeAccount" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateIndex
CREATE UNIQUE INDEX "_AdditionalExchangeAccounts_AB_unique" ON "_AdditionalExchangeAccounts"("A", "B");

-- CreateIndex
CREATE INDEX "_AdditionalExchangeAccounts_B_index" ON "_AdditionalExchangeAccounts"("B");
7 changes: 6 additions & 1 deletion packages/prisma/src/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ model ExchangeAccount {
smartTrades SmartTrade[]
orders Order[]
bots Bot[]
// Will include the only the bots that uses this account as primary
primaryBots Bot[]
bots Bot[] @relation(name: "AdditionalExchangeAccounts")
expired Boolean @default(false)
}
Expand Down Expand Up @@ -209,6 +212,8 @@ model Bot {
exchangeAccount ExchangeAccount @relation(fields: [exchangeAccountId], references: [id])
exchangeAccountId Int
additionalExchangeAccounts ExchangeAccount[] @relation(name: "AdditionalExchangeAccounts")
owner User @relation(fields: [ownerId], references: [id])
ownerId Int
logs BotLog[]
Expand Down
12 changes: 12 additions & 0 deletions packages/processing/src/bot/bot.processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,18 @@ export class BotProcessing {
},
});

const additionalExchangeAccounts = await xprisma.exchangeAccount.findMany({
where: {
bots: {
some: { id: this.bot.id },
},
},
});

const exchange = exchangeProvider.fromAccount(exchangeAccount);
const additionalExchanges = additionalExchangeAccounts.map((exchangeAccount) =>
exchangeProvider.fromAccount(exchangeAccount),
);

const configuration: IBotConfiguration = {
id: this.bot.id,
Expand All @@ -178,6 +189,7 @@ export class BotProcessing {
const processor = createStrategyRunner({
store: storeAdapter,
exchange,
additionalExchanges,
botConfig: configuration,
botTemplate: strategyFn,
});
Expand Down

0 comments on commit 06c1722

Please sign in to comment.