|
| 1 | +import { getOwnedNfts } from '@paima/utils-backend'; |
| 2 | +import { requirePool } from '@tower-defense/db'; |
| 3 | +import { Controller, Get, Query, Route } from 'tsoa'; |
| 4 | +import { cdeName, getContractAddress, getNftMetadata } from '@tower-defense/utils'; |
| 5 | +import { getMainAddress, getRelatedWallets } from '@paima/db'; |
| 6 | + |
| 7 | +interface AccountNftsResult { |
| 8 | + metadata: { |
| 9 | + name: string; |
| 10 | + image: string; |
| 11 | + }; |
| 12 | + contract: string; |
| 13 | + tokenId: number; |
| 14 | +} |
| 15 | + |
| 16 | +interface AccountNftsData { |
| 17 | + pages: number; |
| 18 | + totalItems: number; |
| 19 | + result: AccountNftsResult[]; |
| 20 | +} |
| 21 | + |
| 22 | +@Route('account-nfts') |
| 23 | +export class AccountNftsController extends Controller { |
| 24 | + @Get() |
| 25 | + public async get( |
| 26 | + @Query() account: string, |
| 27 | + @Query() page: number, |
| 28 | + @Query() size: number |
| 29 | + ): Promise<{ response: AccountNftsData }> { |
| 30 | + console.log('account-nfts', account, page, size); |
| 31 | + |
| 32 | + const pool = requirePool(); |
| 33 | + account = (await getMainAddress(account, pool)).address; |
| 34 | + |
| 35 | + const related = await getRelatedWallets(account, pool); |
| 36 | + const allAddresses = [ |
| 37 | + ...related.from.map(x => x.to_address), |
| 38 | + account, |
| 39 | + ...related.to.map(x => x.from_address), |
| 40 | + ]; |
| 41 | + |
| 42 | + let tokenIds = (await Promise.all(allAddresses.map(x => getOwnedNfts(pool, cdeName, x)))) |
| 43 | + .flat() |
| 44 | + .sort(); |
| 45 | + |
| 46 | + const totalItems = tokenIds.length, |
| 47 | + pages = Math.ceil(totalItems / size); |
| 48 | + tokenIds = tokenIds.slice(page * size, (page + 1) * size); |
| 49 | + |
| 50 | + return { |
| 51 | + response: { |
| 52 | + pages, |
| 53 | + totalItems, |
| 54 | + result: tokenIds.map(id => ({ |
| 55 | + metadata: getNftMetadata(id), |
| 56 | + contract: getContractAddress(), |
| 57 | + tokenId: Number(id), |
| 58 | + })), |
| 59 | + }, |
| 60 | + }; |
| 61 | + } |
| 62 | +} |
0 commit comments