Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vault): render all rounds ui #1677

Merged
merged 1 commit into from
May 30, 2024
Merged
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
6 changes: 6 additions & 0 deletions src/adapters/mochi-pay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {
return await this.jsonFetch(
`${MOCHI_PAY_API_BASE_URL}/profiles/${profileId}/syndicates/earning-vaults/${vaultId}/trade-rounds`,
)
}
}

export default new MochiPay()
87 changes: 43 additions & 44 deletions src/commands/vault/info/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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),
})),
)
Expand All @@ -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),
),
],
},
Expand Down
4 changes: 2 additions & 2 deletions src/commands/vault/info/slash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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),
Expand Down
13 changes: 11 additions & 2 deletions src/commands/vault/list/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -194,6 +194,7 @@ function collectSelection(
const { msgOpts } = await runGetVaultDetail(selectedVault, i, vaultType)

msgOpts.components = [
...msgOpts.components,
new MessageActionRow().addComponents(
new MessageButton()
.setLabel("Back")
Expand All @@ -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 })
}
})
})
})
Expand Down
Loading