diff --git a/packages/data/src/api/userOwnedTokens/userOwnedTokens.ts b/packages/data/src/api/userOwnedTokens/userOwnedTokens.ts new file mode 100644 index 000000000..e5be78eb0 --- /dev/null +++ b/packages/data/src/api/userOwnedTokens/userOwnedTokens.ts @@ -0,0 +1,47 @@ +import { mbjs } from '@mintbase-js/sdk'; +import { META_SERVICE_HOST, META_SERVICE_HOST_TESTNET, MINTBASE_API_KEY_HEADER } from '../../constants'; +import { ParsedDataReturn } from '../../types'; +import { parseData } from '../../utils'; +import { ORDER_BY_VALUE, UserTokensQueryResult } from './userOwnedTokens.types'; + + +export const getUserOwnedTokens = async ( + accountId: string, + limit: number, + offset: number, + orderBy: ORDER_BY_VALUE, + listedFilter: boolean, +): Promise> => { + + let data; + let error: string; + + const useHost = mbjs.keys.network === 'testnet' + ? META_SERVICE_HOST_TESTNET + : META_SERVICE_HOST; + + try { + const res = await fetch(`${useHost}/human/${accountId}/owned?offset=${offset}&limit=${limit}&orderBy=${orderBy}&listedFilter=${listedFilter}`, { + method: 'GET', + headers: { 'Content-type': 'application/json', + [MINTBASE_API_KEY_HEADER]: mbjs.keys.apiKey, + }, + }); + + if (!res.ok) { + error = 'Error fetching human owned nfts'; + throw new Error(error); + } + + data = await res.json(); + } catch (err) { + error = `Error fetching human owned nfts, ${err}`; + } + + + return parseData( + data, + error, + error, + ); +}; \ No newline at end of file diff --git a/packages/data/src/api/userOwnedTokens/userOwnedTokens.types.ts b/packages/data/src/api/userOwnedTokens/userOwnedTokens.types.ts new file mode 100644 index 000000000..235327259 --- /dev/null +++ b/packages/data/src/api/userOwnedTokens/userOwnedTokens.types.ts @@ -0,0 +1,23 @@ +export enum ORDER_BY_VALUE { + PRICE_DESC = 'price desc nulls last', + PRICE_ASC = 'price asc', + LATEST = 'latest', + OLDEST = 'oldest', +} + +export interface UserTokensQueryResult { + nft_contract_id: string; + token_id: string; + minter: string; + owner: string; + base_uri: string; + metadata_id: string; + title: string; + media: string; + reference: string; + reference_blob: null; + minted_timestamp: string; + last_transfer_timestamp: string; + price: string; + currency: string; +}