diff --git a/src/adapters/mochi-pay.ts b/src/adapters/mochi-pay.ts index 60794b352..343812673 100644 --- a/src/adapters/mochi-pay.ts +++ b/src/adapters/mochi-pay.ts @@ -357,6 +357,12 @@ class MochiPay extends Fetcher { `${MOCHI_PAY_API_BASE_URL}/profiles/${profileId}/syndicates/earning-vaults/${vaultId}`, ) } + + async getTradeRounds(profileId: string, vaultId: string): Promise { + return await this.jsonFetch( + `${MOCHI_PAY_API_BASE_URL}/profiles/${profileId}/syndicates/earning-vaults/${vaultId}/trade-rounds`, + ) + } } export default new MochiPay() diff --git a/src/commands/vault/info/processor.ts b/src/commands/vault/info/processor.ts index ffd5db780..aabe2db5b 100644 --- a/src/commands/vault/info/processor.ts +++ b/src/commands/vault/info/processor.ts @@ -36,50 +36,48 @@ function formatDate(d: Date) { return `${d.getUTCDate()}.${d.getUTCMonth() + 1}.${d.getUTCFullYear()}` } -const rounds = [ - { - id: faker.string.uuid(), - start_date: faker.date.anytime(), - end_date: faker.date.anytime(), - initial: faker.finance.amount({ autoFormat: true, symbol: "", dec: 0 }), - realized_pl: faker.number.float({ min: -100, fractionDigits: 2 }), - trade_count: faker.number.int({ min: 1, max: 20 }), - claimed: faker.finance.amount({ min: 0, dec: 0 }), - }, - { - id: faker.string.uuid(), - start_date: faker.date.anytime(), - end_date: faker.date.anytime(), - initial: faker.finance.amount({ autoFormat: true, symbol: "", dec: 0 }), - realized_pl: faker.number.float({ min: -100, fractionDigits: 2 }), - trade_count: faker.number.int({ min: 1, max: 20 }), - claimed: faker.finance.amount({ min: 0, dec: 0 }), - }, - { - id: faker.string.uuid(), - start_date: faker.date.anytime(), - end_date: faker.date.anytime(), - initial: faker.finance.amount({ autoFormat: true, symbol: "", dec: 0 }), - realized_pl: faker.number.float({ min: -100, fractionDigits: 2 }), - trade_count: faker.number.int({ min: 1, max: 20 }), - claimed: faker.finance.amount({ min: 0, dec: 0 }), - }, -] - -export async function vaultRounds(interaction: ButtonInteraction) { +export async function handleVaultRounds( + vaultId: string, + interaction: ButtonInteraction, +) { + const profileId = await getProfileIdByDiscord(interaction.user.id) + const { + data, + ok, + curl, + error, + originalError, + log, + status = 500, + } = await mochiPay.getTradeRounds(profileId, vaultId) + if (!ok) { + if (status === 400 && originalError) { + throw new InternalError({ + msgOrInteraction: interaction, + title: "Command error", + description: originalError, + }) + } + throw new APIError({ curl, error, description: log, status }) + } + const embed = composeEmbedMessage2(interaction as any, { color: msgColors.BLUE, author: ["All rounds", getEmojiURL(emojis.CALENDAR)], - description: rounds - .map((r, i) => + description: data + ?.map((r: any, i: number) => [ `${getEmoji(`NUM_${i + 1}` as any)} **${formatDate( - r.start_date, - )} - ${formatDate(r.end_date)}**`, - `${getEmoji("ANIMATED_COIN_1")} Init: $${r.initial}, 💰 Realized: $${ - r.realized_pl - }, total of ${r.trade_count} trade(s)`, - `You claimed **$${r.claimed}** this round`, + new Date(r?.start_date), + )} - ${formatDate(new Date(r?.end_date))}**`, + `${getEmoji("ANIMATED_COIN_1")} Init: $${utils.formatUsdPriceDigit({ + value: r?.initial_balance ?? 0, + shorten: false, + })}, 💰 Realized: $${utils.formatUsdPriceDigit({ + value: r?.realized_pnl ?? 0, + shorten: false, + })}, total of ${r?.trade_count ?? 0} trade(s)`, + `You claimed **$${r?.claimed ?? 0}** this round`, ].join("\n"), ) .join("\n\n"), @@ -92,11 +90,11 @@ export async function vaultRounds(interaction: ButtonInteraction) { new MessageActionRow().addComponents( new MessageSelectMenu() .addOptions( - rounds.map((r, i) => ({ - label: `${formatDate(r.start_date)} - ${formatDate( - r.end_date, + data?.map((r: any, i: number) => ({ + label: `${formatDate(new Date(r?.start_date))} - ${formatDate( + new Date(r?.end_date), )}`, - value: r.id, + value: r?.id, emoji: getEmoji(`NUM_${i + 1}` as any), })), ) @@ -108,7 +106,8 @@ export async function vaultRounds(interaction: ButtonInteraction) { .setLabel("Claim all") .setStyle("SECONDARY") .setCustomId("claim") - .setEmoji("<:FeelsGood:1177549805048836126>"), + .setEmoji("<:FeelsGood:1177549805048836126>") + .setDisabled(true), ), ], }, diff --git a/src/commands/vault/info/slash.ts b/src/commands/vault/info/slash.ts index 937431c6a..11d308182 100644 --- a/src/commands/vault/info/slash.ts +++ b/src/commands/vault/info/slash.ts @@ -13,7 +13,7 @@ import { MachineConfig, route, RouterSpecialAction } from "utils/router" import { SlashCommandSubcommandBuilder } from "@discordjs/builders" -import { runGetVaultDetail, vaultReport, vaultRounds } from "./processor" +import { handleVaultRounds, runGetVaultDetail, vaultReport } from "./processor" const machineConfig: (n: string) => MachineConfig = (vaultName) => ({ id: "vault-info", @@ -22,7 +22,7 @@ const machineConfig: (n: string) => MachineConfig = (vaultName) => ({ button: { vaultReport, vaultInfo: (i) => runGetVaultDetail(vaultName, i), - vaultRounds, + vaultRounds: async (i) => await handleVaultRounds(vaultName, i), }, select: { vaultInfo: (i) => runGetVaultDetail(vaultName, i), diff --git a/src/commands/vault/list/processor.ts b/src/commands/vault/list/processor.ts index dffa9b818..279e4e04f 100644 --- a/src/commands/vault/list/processor.ts +++ b/src/commands/vault/list/processor.ts @@ -17,7 +17,7 @@ import { } from "utils/common" import { getSlashCommand } from "utils/commands" import { wrapError } from "utils/wrap-error" -import { runGetVaultDetail } from "../info/processor" +import { handleVaultRounds, runGetVaultDetail } from "../info/processor" import { composeEmbedMessage, formatDataTable } from "ui/discord/embed" import profile from "adapters/profile" import { ModelVault } from "types/api" @@ -194,6 +194,7 @@ function collectSelection( const { msgOpts } = await runGetVaultDetail(selectedVault, i, vaultType) msgOpts.components = [ + ...msgOpts.components, new MessageActionRow().addComponents( new MessageButton() .setLabel("Back") @@ -214,7 +215,15 @@ function collectSelection( if (!i.deferred) { await i.deferUpdate().catch(() => null) } - i.editReply({ embeds: reply.embeds, components }) + if (i.customId === "rounds") { + const { msgOpts } = await handleVaultRounds(selectedVault, i) + i.editReply({ + embeds: msgOpts.embeds, + components: msgOpts.components, + }) + } else { + i.editReply({ embeds: reply.embeds, components }) + } }) }) })