Skip to content

Commit

Permalink
Merge branch 'preview'
Browse files Browse the repository at this point in the history
  • Loading branch information
quanghuynguyen1902 committed Jun 4, 2024
2 parents 49aec88 + ecdc45c commit 5d8700d
Show file tree
Hide file tree
Showing 12 changed files with 400 additions and 211 deletions.
35 changes: 35 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
# [6.52.0-rc.5](https://github.com/consolelabs/mochi-discord/compare/v6.52.0-rc.4...v6.52.0-rc.5) (2024-06-03)


### Features

* trading vault config view ([#1683](https://github.com/consolelabs/mochi-discord/issues/1683)) ([99d3c64](https://github.com/consolelabs/mochi-discord/commit/99d3c640cd0e6d39a310e7263eb4e3034429af6d))

# [6.52.0-rc.4](https://github.com/consolelabs/mochi-discord/compare/v6.52.0-rc.3...v6.52.0-rc.4) (2024-06-03)


### Bug Fixes

* update select vault for profile ([#1682](https://github.com/consolelabs/mochi-discord/issues/1682)) ([5458938](https://github.com/consolelabs/mochi-discord/commit/5458938bcf258af750e296c6afbae27390ce9fba))

# [6.52.0-rc.3](https://github.com/consolelabs/mochi-discord/compare/v6.52.0-rc.2...v6.52.0-rc.3) (2024-06-03)


### Bug Fixes

* vault report - back btn interaction ([#1681](https://github.com/consolelabs/mochi-discord/issues/1681)) ([a9a80a0](https://github.com/consolelabs/mochi-discord/commit/a9a80a0cb20031df8373084182a92f2892d9c39c))

# [6.52.0-rc.2](https://github.com/consolelabs/mochi-discord/compare/v6.52.0-rc.1...v6.52.0-rc.2) (2024-06-03)


### Bug Fixes

* /bal ([e4541bb](https://github.com/consolelabs/mochi-discord/commit/e4541bbfd1a41b5eacc83ce21f08b24e73e16e89))

# [6.52.0-rc.1](https://github.com/consolelabs/mochi-discord/compare/v6.51.0...v6.52.0-rc.1) (2024-06-03)


### Features

* vault commands flow ([#1679](https://github.com/consolelabs/mochi-discord/issues/1679)) ([5e1e371](https://github.com/consolelabs/mochi-discord/commit/5e1e3712c0ad59f83185e630605d3bcf94afab69))

# [6.51.0](https://github.com/consolelabs/mochi-discord/compare/v6.50.2...v6.51.0) (2024-05-31)


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,5 @@
"node": "18.x"
},
"name": "mochi-discord",
"version": "6.51.0"
"version": "6.52.0-rc.5"
}
27 changes: 25 additions & 2 deletions src/adapters/mochi-pay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,13 @@ class MochiPay extends Fetcher {
return data
}

async listEarningVaults(profileId: string): Promise<any> {
async listEarningVaults(
profileId: string,
fetchTradeDetails = false,
): Promise<any> {
const { data: res, ok } = await this.jsonFetch(
`${MOCHI_PAY_API_BASE_URL}/profiles/${profileId}/syndicates/earning-vaults`,
{ query: { fetchTradeDetails } },
)
let data = []
if (ok) {
Expand All @@ -352,9 +356,14 @@ class MochiPay extends Fetcher {
return data
}

async getEarningVault(profileId: string, vaultId: string): Promise<any> {
async getEarningVault(
profileId: string,
vaultId: string,
query?: { roundId?: string },
): Promise<any> {
return await this.jsonFetch(
`${MOCHI_PAY_API_BASE_URL}/profiles/${profileId}/syndicates/earning-vaults/${vaultId}`,
{ query },
)
}

Expand All @@ -363,6 +372,20 @@ class MochiPay extends Fetcher {
`${MOCHI_PAY_API_BASE_URL}/profiles/${profileId}/syndicates/earning-vaults/${vaultId}/trade-rounds`,
)
}

async getEarningVaultConfigs(
profileId: string,
vaultId: string,
): Promise<any> {
const { ok, data: res } = await this.jsonFetch(
`${MOCHI_PAY_API_BASE_URL}/profiles/${profileId}/syndicates/earning-vaults/${vaultId}/configs`,
)
let data = null
if (ok) {
data = res as any
}
return data
}
}

export default new MochiPay()
13 changes: 10 additions & 3 deletions src/commands/balances/index/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,15 @@ export function formatView(
}
})
.sort((a, b) => {
return b.usdVal - a.usdVal
if (b.avgCost && a.avgCost && b.avgCost.startsWith("+")) return 1
if (b.avgCost && a.avgCost && a.avgCost.startsWith("+")) return -1
if (!b.avgCost && a.avgCost) return 1
if (b.avgCost && !a.avgCost) return -1
return -1
})
// .sort((a, b) => {
// return b.usdVal - a.usdVal
// })
.filter((b) => b.text)
const paginated = chunk(formattedBal, PAGE_SIZE)
const { joined: text } = formatDataTable(
Expand All @@ -540,10 +547,10 @@ export function formatView(
price: `$${b.price}`,
})),
{
cols: ["balance", "usd", "price", "avgCost"],
cols: ["balance", "usd", "avgCost"],
rowAfterFormatter: (formatted, i) =>
`${paginated[page][i].emoji}${formatted}${paginated[page][i].tailEmoji}`,
separator: [` ${APPROX} `, VERTICAL_BAR, VERTICAL_BAR],
separator: [VERTICAL_BAR, VERTICAL_BAR],
alignment: ["left", "left", "left"],
},
)
Expand Down
6 changes: 5 additions & 1 deletion src/commands/profile/index/slash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ const machineConfig: (target: Target) => MachineConfig = (target) => ({
addWallet: (i) => handleWalletAddition(i),
},
select: {
vault: async (i) => await runGetVaultDetail(i.values[0].split("_")[1], i),
vault: async (i) =>
await runGetVaultDetail({
interaction: i,
selectedVault: i.values[0].split("_")[1],
}),
},
// indicates this action to result in ephemeral response
ephemeral: {
Expand Down
118 changes: 79 additions & 39 deletions src/commands/vault/info/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import {
getDiscordRenderableByProfileId,
getProfileIdByDiscord,
} from "utils/profile"
import { faker } from "@faker-js/faker"
import mochiPay from "adapters/mochi-pay"
import moment from "moment"
import { utils } from "@consolelabs/mochi-formatter"

function formatDate(d: Date) {
return `${d.getUTCDate()}.${d.getUTCMonth() + 1}.${d.getUTCFullYear()}`
}
const getPnlIcon = (n: number) => (n >= 0 ? ":green_circle:" : ":red_circle:")

const formatDate = (d: Date) =>
`${d.getUTCDate()}.${d.getUTCMonth() + 1}.${d.getUTCFullYear()}`

export async function handleVaultRounds(
vaultId: string,
Expand Down Expand Up @@ -84,6 +84,10 @@ export async function handleVaultRounds(
})

return {
context: {
vaultId,
vaultType: "trading",
},
msgOpts: {
embeds: [embed],
components: [
Expand Down Expand Up @@ -114,39 +118,70 @@ export async function handleVaultRounds(
}
}

export async function vaultReport(interaction: ButtonInteraction) {
export async function vaultReport(
interaction: ButtonInteraction,
report: any,
vaultId: string,
) {
const basicInfo = [
`<:Look:1150701811536248865> \`Positions. \` Open 1 / Close 9`,
`${getEmoji("CASH")} \`Init. \` $11,023.61`,
`:dart: \`PnL. \` -$586.16 (:red_circle: -5.32%)`,
`<:Look:1150701811536248865> \`Positions. \` Open ${report.total_open_trade} / Close ${report.total_closed_trade}`,
`${getEmoji("CASH")} \`Init. \` ${utils.formatUsdPriceDigit({
value: report.first_initial_balance,
shorten: false,
})}`,
`:dart: \`PnL. \` ${utils.formatUsdPriceDigit({
value: report.total_pnl,
shorten: false,
})} (${getPnlIcon(report.total_pnl)} ${utils.formatPercentDigit(
report.total_realized_pl * 100,
)})`,
].join("\n")

const openTrades = [
"**Open trades**",
`\`24.05.08\` ${getEmoji(
"ANIMATED_COIN_1",
)} Init: $10,410 💰 Current: $22 **(:green_circle: 0.22%)**`,
].join("\n")

const closedTrades = [
"**Closed trades**",
`\`24.05.07\` ${getEmoji(
"WAVING_HAND",
)} Init: $10,653 💰 PnL: -$247 (:red_circle: -2.32%)`,
`\`24.05.07\` ${getEmoji(
"WAVING_HAND",
)} Init: $10,785 💰 PnL: -$131 (:red_circle: -1.22%)`,
`\`24.05.07\` ${getEmoji(
"WAVING_HAND",
)} Init: $10,802 💰 PnL: -$17 (:red_circle: -0.16%)`,
].join("\n")
const { open_trades, close_trades } = report
const openTrades = open_trades
? [
`**Open trades (${report.total_open_trade})**`,
`\`${formatDate(new Date(open_trades.opened_time))}\` ${getEmoji(
"ANIMATED_COIN_1",
)} Init: ${utils.formatUsdPriceDigit({
value: open_trades.initial_balance,
shorten: false,
})} 💰 Current: ${utils.formatUsdPriceDigit({
value: open_trades.unrealized_pnl,
shorten: false,
})} (${getPnlIcon(
open_trades.unrealized_pl,
)} ${utils.formatPercentDigit(open_trades.unrealized_pl * 100)})`,
].join("\n") + "\n\n"
: ""

const closeTrades = close_trades?.length
? [
`**Closed trades (${report.total_closed_trade})**`,
...close_trades.slice(0, 5).map(
(t: any) =>
`\`${formatDate(new Date(t.closed_time))}\` ${getEmoji(
"WAVING_HAND",
)} Init: ${utils.formatUsdPriceDigit({
value: t.initial_balance,
shorten: false,
})} 💰 PnL: ${utils.formatUsdPriceDigit({
value: t.realized_pnl ?? 0,
shorten: false,
})} (${getPnlIcon(t.realized_pl)} ${utils.formatPercentDigit(
t.realized_pl * 100,
)})`,
),
].join("\n")
: ""

const embed = composeEmbedMessage2(interaction as any, {
color: msgColors.BLUE,
author: ["Trading vault report", getEmojiURL(emojis.ANIMATED_DIAMOND)],
description: `${basicInfo}\n\n${openTrades}\n\n${closedTrades}`,
description: `${basicInfo}\n\n${openTrades}${closeTrades}`,
})
return {
context: { vaultId, vaultType: "trading" },
msgOpts: {
embeds: [embed],
components: [],
Expand All @@ -164,11 +199,17 @@ function getVaultEquityEmoji(percent: string | number = 0) {
return "🐳"
}

export async function runGetVaultDetail(
selectedVault: string,
interaction: OriginalMessage,
export async function runGetVaultDetail({
interaction,
selectedVault,
vaultType = "spot",
) {
roundId,
}: {
interaction: OriginalMessage
selectedVault: string
vaultType?: string
roundId?: string
}) {
// trading vault
if (vaultType === "trading" && "user" in interaction) {
const profileId = await getProfileIdByDiscord(interaction.user.id)
Expand All @@ -180,7 +221,7 @@ export async function runGetVaultDetail(
originalError,
log,
status = 500,
} = await mochiPay.getEarningVault(profileId, selectedVault)
} = await mochiPay.getEarningVault(profileId, selectedVault, { roundId })
if (!ok) {
if (status === 400 && originalError) {
throw new InternalError({
Expand All @@ -193,7 +234,7 @@ export async function runGetVaultDetail(
}

const creator = await getDiscordRenderableByProfileId(profileId)
const { account, investor_report: report } = data
const { investor_report: report } = data
const { open_trades, close_trades } = report
const basicInfo = [
`${getEmoji("ANIMATED_VAULT", true)}\`Name. \` ${data.name}`,
Expand All @@ -213,9 +254,6 @@ export async function runGetVaultDetail(
)}`,
].join("\n")

const getPnlIcon = (n: number) =>
n >= 0 ? ":green_circle:" : ":red_circle:"

const startRound = moment(new Date(report.opened_trade_round_time))
const roundFields = [
`**Round info**`,
Expand Down Expand Up @@ -268,7 +306,7 @@ export async function runGetVaultDetail(
})} (${getPnlIcon(
open_trades.unrealized_pl,
)} ${utils.formatPercentDigit(open_trades.unrealized_pl * 100)})`,
].join("\n")
].join("\n") + "\n\n"
: ""

const closeTrades = close_trades?.length
Expand Down Expand Up @@ -301,7 +339,7 @@ export async function runGetVaultDetail(
)}\``,
].join("\n")

const description = `${basicInfo}\n\n${vaultEquity}\n\n${address}\n\n${roundFields}\n\n${openTrades}\n\n${closeTrades}`
const description = `${basicInfo}\n\n${vaultEquity}\n\n${address}\n\n${roundFields}\n\n${openTrades}${closeTrades}`
const embed = composeEmbedMessage2(interaction as any, {
color: msgColors.BLUE,
author: ["Trading vault info", getEmojiURL(emojis.ANIMATED_DIAMOND)],
Expand All @@ -314,6 +352,8 @@ export async function runGetVaultDetail(
evm: data.evm_wallet_address,
sol: data.solana_wallet_address,
},
vaultId: selectedVault,
report,
},
msgOpts: {
embeds: [embed],
Expand Down
Loading

0 comments on commit 5d8700d

Please sign in to comment.