Skip to content

Commit

Permalink
feat: onchain asset avg cost (#1678)
Browse files Browse the repository at this point in the history
  • Loading branch information
phucledien authored May 31, 2024
1 parent f5e84b5 commit dd35380
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/adapters/mochi-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ class MochiAPI extends Fetcher {
`${API_BASE_URL}/users/${profileId}/cexs/binance/average-costs`,
)
}

public async getOnchainAverageCost(walletAddr: string) {
return await this.jsonFetch(
`${API_BASE_URL}/onchain/average-cost?walletAddress=${walletAddr}`,
)
}
}

export default new MochiAPI()
15 changes: 11 additions & 4 deletions src/commands/balances/index/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ export function formatView(
}[],
page: number,
earns?: { amount: number; price: number; reward: number; symbol: string }[],
binanceAvgCosts?: Record<string, string>,
avgCosts?: Record<string, string>,
) {
if (earns) {
// Add earnings to balances
Expand Down Expand Up @@ -502,8 +502,8 @@ export function formatView(
let text = `${value} ${name.includes("(earn)") ? name : symbol}`
let avgCost = ""
const currentPrice = usdVal / tokenVal
if (binanceAvgCosts && binanceAvgCosts[symbol.toUpperCase()]) {
const avgPrice = parseFloat(binanceAvgCosts[symbol.toUpperCase()])
if (avgCosts && avgCosts[symbol.toUpperCase()]) {
const avgPrice = parseFloat(avgCosts[symbol.toUpperCase()])
const percentChange = ((currentPrice - avgPrice) / avgPrice) * 100
avgCost =
percentChange > 0
Expand Down Expand Up @@ -632,15 +632,22 @@ async function switchView(
)

// get list token average cost of user on binance
const { data: avg } = await mochiApi.getBinanceAverageCost(profileId)
let averageCosts: Record<string, string> = {}
// just get average cost if rendering cex wallet
if (balanceType === BalanceType.Cex) {
const { data: avg } = await mochiApi.getBinanceAverageCost(profileId)
avg?.forEach((d: { symbol: string; average_cost: string }) => {
averageCosts[d.symbol] = d.average_cost
})
}

if (balanceType === BalanceType.Onchain && props.address != "") {
const { data: avg } = await mochiApi.getOnchainAverageCost(props.address)
avg?.forEach((d: { symbol: string; average_cost: number }) => {
averageCosts[d.symbol] = formatUsdDigit(d.average_cost)
})
}

isOwnWallet = isOwnWallet && !isViewingOther
const embed = composeEmbedMessage(null, {
author: [props.title, props.emoji],
Expand Down

0 comments on commit dd35380

Please sign in to comment.