Skip to content

Commit

Permalink
get owned tokens api
Browse files Browse the repository at this point in the history
  • Loading branch information
sainthiago committed Sep 8, 2023
1 parent e14ca07 commit f3e08f6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
47 changes: 47 additions & 0 deletions packages/data/src/api/userOwnedTokens/userOwnedTokens.ts
Original file line number Diff line number Diff line change
@@ -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<ParsedDataReturn<UserTokensQueryResult>> => {

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<UserTokensQueryResult>(
data,
error,
error,
);
};
23 changes: 23 additions & 0 deletions packages/data/src/api/userOwnedTokens/userOwnedTokens.types.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit f3e08f6

Please sign in to comment.