-
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(cli): add "exchange add" and "exchange update" commands
- Loading branch information
Showing
7 changed files
with
189 additions
and
1 deletion.
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
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.", | ||
}; | ||
} |
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,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.`, | ||
}; | ||
} |
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,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)); | ||
} |
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 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)); | ||
} |
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
Submodule pro
updated
from 85e64e to d4fb85