Skip to content

Commit

Permalink
Unique wallet listing works (#1505)
Browse files Browse the repository at this point in the history
  • Loading branch information
smakhtin authored Jul 4, 2024
1 parent 2f737b9 commit a516a88
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 4 deletions.
15 changes: 14 additions & 1 deletion apps/api/src/routes/wallets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import { DeepAsset } from '@sni/types';
import {
getArbitrumWalletAssets,
getCrossmintWalletAssets,
getUniqueWalletAssets,
} from '@sni/clients/wallets-client';

import { env } from 'hono/adapter';
import { zValidator } from '@hono/zod-validator';
import { parseAssetDID } from '@sni/address-utils';
import { reportUnknownNetwork as errorResponse } from '../../shared';
import { errorResponse } from '../../shared';

app.get(
'/:walletAddress',
Expand Down Expand Up @@ -68,6 +69,18 @@ app.get(
} catch (e) {
return errorResponse(e, c);
}
case 'unique':
case 'opal':
try {
assets = await getUniqueWalletAssets(
network,
walletAddress,
Number.parseInt(contractAddress)
);
return c.json(assets);
} catch (e) {
return errorResponse(e, c);
}
default:
return c.json({ error: true, message: 'Invalid network' }, 400);
}
Expand Down
5 changes: 3 additions & 2 deletions apps/api/src/shared.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Context } from 'hono';
import { logger } from './utils/logger';

export function reportUnknownNetwork(e: unknown, c: Context) {
export function errorResponse(e: unknown, c: Context) {
if (e instanceof Error) {
return c.json({ error: true, message: e.message }, 500);
logger.error(e);
}

return c.json({ error: true, message: 'Internal server error' }, 500);
Expand Down
8 changes: 7 additions & 1 deletion packages/clients/assets-client/targets/unique/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DeepAsset } from '@sni/types';
import { UniqueNFTResponseSchema } from './schemas';
import { createAssetDID } from '@sni/address-utils';

export async function getUniqueAsset(
network: string,
Expand Down Expand Up @@ -29,6 +30,11 @@ export async function getUniqueAsset(
id: nftResponse.collectionId.toString(),
name: '', //TODO: Make name optional in the schema
},
address: `did:asset:eip155:11155420.unique2:${collectionId}:${nftResponse.tokenId}`, //TODO: Proper network ID
address: createAssetDID(
network,
'unique2',
nftResponse.collectionId,
nftResponse.tokenId
),
};
}
1 change: 1 addition & 0 deletions packages/clients/wallets-client/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { getArbitrumWalletAssets } from './targets/arbitrum/client';
export { getCrossmintWalletAssets } from './targets/crossmint/client';
export { getUniqueWalletAssets } from './targets/unique/client';
32 changes: 32 additions & 0 deletions packages/clients/wallets-client/targets/unique/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { DeepAsset, ExternalApiError } from '@sni/types';
import { getUniqueAsset } from '../../../assets-client/targets/unique/client'; //TODO: Add path aliases
import { AccountTokensResponseSchema } from './schemas';

//TODO: Cover with tests
export async function getUniqueWalletAssets(
network: 'unique' | 'opal',
walletAddress: string,
collectionId: number
): Promise<DeepAsset[]> {
const response = await fetch(
`https://rest.unique.network/${network}/v1/tokens/account-tokens?address=${walletAddress.toLowerCase()}&collectionId=${collectionId}`,
{
method: 'GET',
headers: { Accept: 'application/json' },
}
);

if (response.status !== 200) {
throw new ExternalApiError(
`Failed to fetch wallet data from ${network} network. ${response.statusText}`
);
}

const nfts = AccountTokensResponseSchema.parse(await response.json()).tokens;

const deepAssets = await Promise.all(
nfts.map((nft) => getUniqueAsset(network, nft.collectionId, nft.tokenId))
);

return deepAssets;
}
10 changes: 10 additions & 0 deletions packages/clients/wallets-client/targets/unique/schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { z } from 'zod';

export const AccountTokensResponseSchema = z.object({
tokens: z.array(
z.object({
collectionId: z.number(),
tokenId: z.number(),
})
),
});
2 changes: 2 additions & 0 deletions packages/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ export type DeepAsset = {
};
address: string; //TODO: Rename to did?
};

export class ExternalApiError extends Error {}

0 comments on commit a516a88

Please sign in to comment.