|
| 1 | +import { Hono } from 'hono'; |
| 2 | +import { TokenBalance } from '../../src/types/daoTreasury'; |
| 3 | +import { fetchMoralis } from '../shared/moralisApi'; |
| 4 | +import { DefiResponse, NFTResponse, TokenResponse } from '../shared/moralisTypes'; |
| 5 | +import type { Env } from '../types'; |
| 6 | +import { withBalanceCache } from './balanceCache'; |
| 7 | +import { getParams } from './middleware'; |
| 8 | +import { |
| 9 | + transformDefiResponse, |
| 10 | + transformNFTResponse, |
| 11 | + transformTokenResponse, |
| 12 | +} from './transformers'; |
| 13 | + |
| 14 | +const endpoints = { |
| 15 | + tokens: { |
| 16 | + moralisPath: (address: string) => `/wallets/${address}/tokens`, |
| 17 | + transform: transformTokenResponse, |
| 18 | + postProcess: (data: TokenBalance[]) => data.filter(token => token.balance !== '0'), |
| 19 | + fetch: async ({ chain, address }: { chain: string; address: string }, c: { env: Env }) => { |
| 20 | + const result = await fetchMoralis<TokenResponse>({ |
| 21 | + endpoint: endpoints.tokens.moralisPath(address), |
| 22 | + chain, |
| 23 | + apiKey: c.env.MORALIS_API_KEY, |
| 24 | + }); |
| 25 | + const transformed = result.map(endpoints.tokens.transform); |
| 26 | + return endpoints.tokens.postProcess(transformed); |
| 27 | + }, |
| 28 | + }, |
| 29 | + nfts: { |
| 30 | + moralisPath: (address: string) => `/${address}/nft`, |
| 31 | + transform: transformNFTResponse, |
| 32 | + params: { |
| 33 | + format: 'decimal', |
| 34 | + media_items: 'true', |
| 35 | + normalizeMetadata: 'true', |
| 36 | + }, |
| 37 | + fetch: async ({ chain, address }: { chain: string; address: string }, c: { env: Env }) => { |
| 38 | + const result = await fetchMoralis<NFTResponse>({ |
| 39 | + endpoint: endpoints.nfts.moralisPath(address), |
| 40 | + chain, |
| 41 | + apiKey: c.env.MORALIS_API_KEY, |
| 42 | + params: endpoints.nfts.params, |
| 43 | + }); |
| 44 | + return result.map(endpoints.nfts.transform); |
| 45 | + }, |
| 46 | + }, |
| 47 | + defi: { |
| 48 | + moralisPath: (address: string) => `/wallets/${address}/defi/positions`, |
| 49 | + transform: transformDefiResponse, |
| 50 | + fetch: async ({ chain, address }: { chain: string; address: string }, c: { env: Env }) => { |
| 51 | + const result = await fetchMoralis<DefiResponse>({ |
| 52 | + endpoint: endpoints.defi.moralisPath(address), |
| 53 | + chain, |
| 54 | + apiKey: c.env.MORALIS_API_KEY, |
| 55 | + }); |
| 56 | + return result.map(endpoints.defi.transform); |
| 57 | + }, |
| 58 | + }, |
| 59 | +} as const; |
| 60 | + |
| 61 | +type BalanceType = keyof typeof endpoints; |
| 62 | +const ALL_BALANCE_TYPES: BalanceType[] = ['tokens', 'nfts', 'defi']; |
| 63 | + |
| 64 | +export const router = new Hono<{ Bindings: Env }>().use('*', getParams).get('/', async c => { |
| 65 | + const { address, network } = c.var; |
| 66 | + const flavors = c.req.queries('flavor') as BalanceType[] | undefined; |
| 67 | + const requestedTypes = flavors?.filter(t => ALL_BALANCE_TYPES.includes(t)) ?? ALL_BALANCE_TYPES; |
| 68 | + |
| 69 | + const results = await Promise.all( |
| 70 | + requestedTypes.map(async type => { |
| 71 | + const result = await withBalanceCache(c, type, () => |
| 72 | + endpoints[type].fetch({ chain: network, address }, c), |
| 73 | + ); |
| 74 | + return [type, result] as const; |
| 75 | + }), |
| 76 | + ); |
| 77 | + |
| 78 | + const response = Object.fromEntries(results); |
| 79 | + if (results.some(([, result]) => 'error' in result)) { |
| 80 | + return c.json(response, 503); |
| 81 | + } |
| 82 | + return c.json(response); |
| 83 | +}); |
0 commit comments