Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/commands/KeysCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class KeysCommand {
);
const keychainFiles = files.filter((f) => f.endsWith(".keychain.json"));

const keypairData = await Promise.all(
const keypairResults = await Promise.allSettled(
keypairFiles.map(async (file) => {
const name = file.replace(".json", "");
const signer = await Signer.load(name);
Expand All @@ -139,6 +139,17 @@ export class KeysCommand {
})
);

const keypairData = keypairResults
.map((result, index) => {
if (result.status === "fulfilled") {
return result.value;
}
const name = keypairFiles[index]?.replace(".json", "") ?? "unknown";
console.warn(`Warning: Failed to load keypair "${name}": ${result.reason}`);
return null;
})
.filter((item): item is NonNullable<typeof item> => item !== null);

const keychainData = keychainFiles.map((file) => {
const name = file.replace(".keychain.json", "");
const config = KeychainConfig.load(name);
Expand Down
16 changes: 8 additions & 8 deletions src/commands/PerpsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Config } from "../lib/Config.ts";
import { NumberConverter } from "../lib/NumberConverter.ts";
import { Output } from "../lib/Output.ts";
import { Signer } from "../lib/Signer.ts";
import { DateConverter } from "../lib/DateConverter.ts";

export class PerpsCommand {
public static register(program: Command): void {
Expand Down Expand Up @@ -311,6 +312,12 @@ export class PerpsCommand {
if (opts.leverage && opts.size) {
throw new Error("Only one of --leverage or --size can be provided.");
}
if (opts.leverage) {
const lev = Number(opts.leverage);
if (isNaN(lev) || lev < 1 || lev > 100) {
throw new Error("Leverage must be a number between 1 and 100.");
}
}
if (opts.limit && (opts.tp || opts.sl)) {
throw new Error(
"--limit cannot be combined with --tp or --sl. Use 'perps set' after the limit order fills."
Expand Down Expand Up @@ -904,14 +911,7 @@ export class PerpsCommand {
}

private static parseTimestamp(value: string): string {
if (/^\d+$/.test(value)) {
return value;
}
const ms = new Date(value).getTime();
if (isNaN(ms)) {
throw new Error(`Invalid date: ${value}`);
}
return String(Math.floor(ms / 1000));
return DateConverter.toUnixSeconds(value);
}

private static async history(opts: {
Expand Down
24 changes: 16 additions & 8 deletions src/commands/SpotCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Config } from "../lib/Config.ts";
import { NumberConverter } from "../lib/NumberConverter.ts";
import { Output } from "../lib/Output.ts";
import { Signer } from "../lib/Signer.ts";
import { DateConverter } from "../lib/DateConverter.ts";
import { Swap } from "../lib/Swap.ts";

export class SpotCommand {
Expand Down Expand Up @@ -400,6 +401,20 @@ export class SpotCommand {
}
const info = tokenMap.get(mint);
if (!info) {
console.warn(`Warning: Token metadata not found for mint ${mint}. Displaying with limited info.`);
outputTokens.push({
id: mint,
symbol: mint.slice(0, 8) + '...',
decimals: 0,
amount: Number(ata.amount),
rawAmount: ata.amount,
value: 0,
price: 0,
priceChange: 0,
isVerified: false,
scaledUiMultiplier: 1,
reclaimableLamports: ata.reclaimableLamports,
});
continue;
}
const multiplier = Swap.getScaledUiMultiplier(info);
Expand Down Expand Up @@ -928,13 +943,6 @@ export class SpotCommand {
}

private static parseTimestamp(value: string): string {
if (/^\d+$/.test(value)) {
return new Date(Number(value) * 1000).toISOString();
}
const ms = new Date(value).getTime();
if (isNaN(ms)) {
throw new Error(`Invalid date: ${value}`);
}
return new Date(ms).toISOString();
return DateConverter.toISO(value);
}
}
31 changes: 31 additions & 0 deletions src/lib/DateConverter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export class DateConverter {
/**
* Parse a timestamp value and return an ISO string.
* Accepts Unix seconds (numeric string) or an ISO date string.
*/
static toISO(value: string): string {
if (/^\d+$/.test(value)) {
return new Date(Number(value) * 1000).toISOString();
}
const ms = new Date(value).getTime();
if (isNaN(ms)) {
throw new Error(`Invalid date: ${value}`);
}
return new Date(ms).toISOString();
}

/**
* Parse a timestamp value and return Unix seconds as a string.
* Accepts Unix seconds (numeric string) or an ISO date string.
*/
static toUnixSeconds(value: string): string {
if (/^\d+$/.test(value)) {
return value;
}
const ms = new Date(value).getTime();
if (isNaN(ms)) {
throw new Error(`Invalid date: ${value}`);
}
return String(Math.floor(ms / 1000));
}
}