Skip to content

Commit

Permalink
fix(balance): user now should see the whitelisted tokens in balance
Browse files Browse the repository at this point in the history
  • Loading branch information
phucledien authored Aug 30, 2023
1 parent c8d88ec commit ebe68e1
Show file tree
Hide file tree
Showing 8 changed files with 459 additions and 50 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
},
"dependencies": {
"@bonfida/spl-name-service": "^0.1.64",
"@consolelabs/mochi-formatter": "^0.0.1-rc.56",
"@consolelabs/mochi-rest": "^1.4.0",
"@consolelabs/mochi-ui": "^5.0.0",
"@discordjs/builders": "^0.12.0",
"@discordjs/rest": "^0.5.0",
"@haileybot/captcha-generator": "^1.7.0",
Expand Down
6 changes: 6 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import API from "@consolelabs/mochi-rest"

export default new API({
preview: true,
log: false,
})
20 changes: 14 additions & 6 deletions src/commands/balances/index/processor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Platform } from "@consolelabs/mochi-formatter"
import defi from "adapters/defi"
import mochiPay from "adapters/mochi-pay"
import profile from "adapters/profile"
Expand Down Expand Up @@ -52,9 +51,10 @@ import {
formatTokenDigit,
formatUsdDigit,
} from "utils/defi"
import { fmt } from "utils/formatter"
import { getProfileIdByDiscord } from "utils/profile"
import { paginationButtons } from "utils/router"
import api from "api"
import UI, { Platform } from "@consolelabs/mochi-ui"

export enum BalanceType {
Offchain = 1,
Expand Down Expand Up @@ -412,6 +412,7 @@ export function formatView(
token: {
name: string
symbol: string
address: string
decimal: number
price: number
chain: { short_name?: string; name?: string; symbol?: string }
Expand All @@ -428,14 +429,17 @@ export function formatView(
const formattedBal = balances
.map((balance) => {
const { token, amount } = balance
const { symbol, chain: _chain, decimal, price, native } = token
const { symbol, chain: _chain, decimal, price, native, address } = token
const tokenVal = convertString(amount, decimal)
const usdVal = price * tokenVal
const value = formatTokenDigit(tokenVal.toString())
const usdWorth = formatUsdDigit(usdVal.toString())
let chain = _chain?.symbol || _chain?.short_name || _chain?.name || ""
chain = chain.toLowerCase()
if (tokenVal === 0 || (mode === "filter-dust" && usdVal <= MIN_DUST))
if (
(tokenVal === 0 || (mode === "filter-dust" && usdVal <= MIN_DUST)) &&
!api.isTokenWhitelisted(symbol, address)
)
return {
emoji: "",
text: "",
Expand Down Expand Up @@ -486,6 +490,7 @@ export function formatView(
price,
chain: _chain,
native,
address,
} = token
const tokenVal = convertString(amount, decimal)
const usdVal = price * tokenVal
Expand All @@ -495,7 +500,10 @@ export function formatView(
chain = chain.toLowerCase()

totalWorth += usdVal
if (tokenVal === 0 || (mode === "filter-dust" && usdVal <= MIN_DUST)) {
if (
(tokenVal === 0 || (mode === "filter-dust" && usdVal <= MIN_DUST)) &&
!api.isTokenWhitelisted(symbol, address)
) {
return {
name: "",
value: "",
Expand Down Expand Up @@ -716,7 +724,7 @@ async function switchView(
},
])

const { text: txnRow } = await fmt.components.txns({
const { text: txnRow } = await UI.components.txns({
txns,
on: Platform.Discord,
top: 5,
Expand Down
5 changes: 2 additions & 3 deletions src/commands/transaction/index/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import { composeEmbedMessage } from "ui/discord/embed"
import { ButtonInteraction, CommandInteraction } from "discord.js"
import profile from "adapters/profile"
import mochiPay from "adapters/mochi-pay"
import { fmt } from "utils/formatter"
import { PageSize, Platform } from "@consolelabs/mochi-formatter"
import { paginationButtons } from "utils/router"
import UI, { Platform, PageSize } from "@consolelabs/mochi-ui"

export async function render(
i: CommandInteraction | ButtonInteraction,
Expand Down Expand Up @@ -73,7 +72,7 @@ export async function render(
const total = pagination?.total
? Math.ceil(pagination?.total / PageSize.Medium)
: 1
const { text: description } = await fmt.components.txns({
const { text: description } = await UI.components.txns({
txns: txns as any,
on: Platform.Discord,
groupDate: true,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/watchlist/view/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getSlashCommand } from "utils/commands"
import { ResponseGetWatchlistResponse } from "types/api"
import { groupBy } from "lodash"
import { getProfileIdByDiscord } from "../../../utils/profile"
import { utils } from "@consolelabs/mochi-formatter"
import { utils } from "@consolelabs/mochi-ui"
import { VERTICAL_BAR } from "utils/constants"
import { renderChart } from "./chart"
import { paginationButtons } from "utils/router"
Expand Down
18 changes: 17 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import Discord from "discord.js"
import { APPLICATION_ID, DISCORD_TOKEN, PORT } from "./env"
import {
APPLICATION_ID,
DISCORD_TOKEN,
PORT,
REDIS_DB,
REDIS_HOST,
} from "./env"
import { REST } from "@discordjs/rest"
import { Routes } from "discord-api-types/v9"
import { logger } from "logger"
Expand All @@ -8,8 +14,11 @@ import { createServer, Server, IncomingMessage, ServerResponse } from "http"
import { assignKafka } from "queue/kafka/queue"
import { run } from "queue/kafka/producer"
import { IS_READY } from "listeners/discord/ready"
import UI from "@consolelabs/mochi-ui"
import Redis from "ioredis"
import events from "listeners/discord"
import { getTipsAndFacts } from "cache/tip-fact-cache"
import api from "api"

let server: Server | null = null

Expand Down Expand Up @@ -59,6 +68,13 @@ const rest = new REST({ version: "9" }).setToken(DISCORD_TOKEN)
await getTipsAndFacts()
logger.info("Success getting tips and facts.")

logger.info("Init Mochi API")
const redis = new Redis(`redis://${REDIS_HOST}/${REDIS_DB}`)
await api.init()
UI.api = api
UI.redis = redis
logger.info("Success init Mochi API")

runHttpServer()
} catch (error) {
logger.error("Failed to refresh application (/) commands.")
Expand Down
16 changes: 0 additions & 16 deletions src/utils/formatter.ts

This file was deleted.

Loading

0 comments on commit ebe68e1

Please sign in to comment.