Skip to content

Commit

Permalink
feat(cli): add "exchange add" and "exchange update" commands
Browse files Browse the repository at this point in the history
  • Loading branch information
bludnic committed Jul 12, 2024
1 parent cd27c0a commit 571f87f
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 1 deletion.
43 changes: 43 additions & 0 deletions apps/cli/src/api/exchanges/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { ExchangeCode } from "@opentrader/types";
import type { CommandResult, ConfigName } from "../../types.js";
import { createClient } from "../../daemon.js";

type Options = {
config: ConfigName;
/**
* Exchange name.
*/
name: string | null;
/**
* Exchange label.
*/
label: string;
code: ExchangeCode;
key: string;
secret: string;
password: string | null;
/**
* Is demo account?
*/
demo: boolean;
};

const daemon = createClient();

export async function addExchangeAccount(
options: Options,
): Promise<CommandResult> {
await daemon.exchangeAccount.create.mutate({
name: options.name || options.label,
label: options.label,
exchangeCode: options.code,
apiKey: options.key,
secretKey: options.secret,
password: options.password,
isDemoAccount: options.demo,
});

return {
result: "Exchange account added successfully.",
};
}
60 changes: 60 additions & 0 deletions apps/cli/src/api/exchanges/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { ExchangeCode } from "@opentrader/types";
import { logger } from "@opentrader/logger";
import type { CommandResult, ConfigName } from "../../types.js";
import { createClient } from "../../daemon.js";

type Options = {
config: ConfigName;
/**
* Exchange name.
*/
name: string | null;
/**
* Exchange label.
*/
label: string;
code: ExchangeCode;
key: string;
secret: string;
password: string | null;
/**
* Is demo account?
*/
demo: boolean;
};

const daemon = createClient();

export async function updateExchangeAccount(
options: Options,
): Promise<CommandResult> {
const exchangeAccounts = await daemon.exchangeAccount.list.query();
const exchangeAccount = exchangeAccounts.find(
(account) => account.label === options.label,
);

if (!exchangeAccount) {
logger.error(
`Exchange account with label "${options.label}" not found in DB. Create it first.`,
);
return {
result: undefined,
};
}

await daemon.exchangeAccount.update.mutate({
id: exchangeAccount.id,
body: {
name: options.name || exchangeAccount.name,
exchangeCode: options.code,
apiKey: options.key,
secretKey: options.secret,
password: options.password,
isDemoAccount: options.demo,
},
});

return {
result: `Exchange account with label "${exchangeAccount.label}" updated successfully.`,
};
}
38 changes: 38 additions & 0 deletions apps/cli/src/commands/exchange/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { Command } from "commander";
import { Option } from "commander";
import { DEFAULT_CONFIG_NAME } from "../../config.js";
import { validateExchange } from "../../utils/validate.js";
import { handle } from "../../utils/command.js";
import { addExchangeAccount } from "../../api/exchanges/add.js";

export function addExchangeAccountCommand(program: Command) {
program
.command("exchange-add")
.description("Add an exchange account")
.addOption(
new Option("-e, --code <code>", "Exchange code")
.argParser(validateExchange)
.makeOptionMandatory(true),
)
.addOption(
new Option("-k, --key <key>", "API Key").makeOptionMandatory(true),
)
.addOption(new Option("-s, --secret <secret>", "Secret Key"))
.addOption(
new Option(
"-p, --password <password>",
"Password. Required for some exchanges",
).default(null),
)
.addOption(new Option("-d, --demo", "Is demo account?").default(false))
.addOption(
new Option("-l, --label <label>", "Exchange label").default("DEFAULT"),
)
.addOption(new Option("-n, --name <name>", "Exchange name").default(null))
.addOption(
new Option("-c, --config <config>", "Config file").default(
DEFAULT_CONFIG_NAME,
),
)
.action(handle(addExchangeAccount));
}
42 changes: 42 additions & 0 deletions apps/cli/src/commands/exchange/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { Command } from "commander";
import { Option } from "commander";
import { DEFAULT_CONFIG_NAME } from "../../config.js";
import { validateExchange } from "../../utils/validate.js";
import { handle } from "../../utils/command.js";
import { updateExchangeAccount } from "../../api/exchanges/update.js";

export function updateExchangeAccountCommand(program: Command) {
program
.command("exchange-update")
.description("Update an exchange account")
.addOption(
new Option("-e, --code <code>", "Exchange code")
.argParser(validateExchange)
.makeOptionMandatory(true),
)
.addOption(
new Option("-k, --key <key>", "API Key").makeOptionMandatory(true),
)
.addOption(
new Option("-s, --secret <secret>", "Secret Key").makeOptionMandatory(
true,
),
)
.addOption(
new Option(
"-p, --password <password>",
"Password. Required for some exchanges",
).default(null),
)
.addOption(new Option("-d, --demo", "Is demo account?").default(false))
.addOption(
new Option("-l, --label <label>", "Exchange label").default("DEFAULT"),
)
.addOption(new Option("-n, --name <name>", "Exchange name").default(null))
.addOption(
new Option("-c, --config <config>", "Config file").default(
DEFAULT_CONFIG_NAME,
),
)
.action(handle(updateExchangeAccount));
}
4 changes: 4 additions & 0 deletions apps/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { logPath } from "./utils/app-path.js";
process.env.LOG_FILE = logPath;

import { Command } from "commander";
import { addExchangeAccountCommand } from "./commands/exchange/add.js";
import { updateExchangeAccountCommand } from "./commands/exchange/update.js";
import { addStopCommand } from "./commands/stop.js";
import { addBacktestCommand } from "./commands/backtest.js";
import { addGridLinesCommand } from "./commands/grid-lines.js";
Expand All @@ -36,6 +38,8 @@ program
.description("CLI for OpenTrader")
.version("0.0.1");

addExchangeAccountCommand(program);
updateExchangeAccountCommand(program);
addBacktestCommand(program);
addGridLinesCommand(program);
addTradeCommand(program);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { z } from "zod";
export const ZCreateExchangeAccountInputSchema = zt.ExchangeAccountSchema.pick({
exchangeCode: true,
name: true,
label: true,
apiKey: true,
secretKey: true,
password: true,
Expand Down
2 changes: 1 addition & 1 deletion pro
Submodule pro updated from 85e64e to d4fb85

0 comments on commit 571f87f

Please sign in to comment.