Skip to content

Commit

Permalink
User Owned and Minted Tokens api
Browse files Browse the repository at this point in the history
  • Loading branch information
sainthiago committed Sep 8, 2023
1 parent d890305 commit 0ec1362
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 42 deletions.
2 changes: 1 addition & 1 deletion packages/data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const myFetchMethod = ({showOnlyListed, pagination, network}) => {
| [metadataByMetadataId](https://github.com/Mintbase/mintbase-js/tree/beta/packages/data/src/api/metadataByMetadataId/README.md) | `metadataId: string` |get metadata by metadataId|
| [tokenByAttributes](https://github.com/Mintbase/mintbase-js/tree/beta/packages/data/src/api/tokenByAttributes/README.md) | `contractId: string, filters: AttributesFilters` |get tokens of a certain contract and filters|
| [attributesByContract](https://github.com/Mintbase/mintbase-js/tree/beta/packages/data/src/api/attributesByContract/README.md) | `contractId: string` |get attributes of a certain contract|
| [attributeRarity](https://github.com/Mintbase/mintbase-js/tree/beta/packages/data/src/api/attributeRarity/README.md) | `contractId: string, attributeType: string, attributeValue: string` |get attribute rarity of a certain contract, attribute type and attribute value| [userOwnedTokens](https://github.com/Mintbase/mintbase-js/tree/beta/packages/data/src/api/userOwnedTokens/README.md)
| [attributeRarity](https://github.com/Mintbase/mintbase-js/tree/beta/packages/data/src/api/attributeRarity/README.md) | `contractId: string, attributeType: string, attributeValue: string` |get attribute rarity of a certain contract, attribute type and attribute value| [userOwnedTokens](https://github.com/Mintbase/mintbase-js/tree/beta/packages/data/src/api/userOwnedTokens/README.md)| [userMintedTokens](https://github.com/Mintbase/mintbase-js/tree/beta/packages/data/src/api/userMintedTokens/README.md)

While we will continue to provide public and our new mb_views schema objects, we will also begin to introduce helper methods here that can be used to query data **without having to write any graphql**.

Expand Down
2 changes: 1 addition & 1 deletion packages/data/src/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ Currently, these wrappers are methods for the [GraphQL API](https://docs.mintbas
| [metadataByMetadataId](https://github.com/Mintbase/mintbase-js/tree/beta/packages/data/src/api/metadataByMetadataId/README.md) | `metadataId: string` |get metadata by metadataId|
| [tokenByAttributes](https://github.com/Mintbase/mintbase-js/tree/beta/packages/data/src/api/tokenByAttributes/README.md) | `contractId: string, filters: AttributesFilters` |get tokens of a certain contract and filters|
| [attributesByContract](https://github.com/Mintbase/mintbase-js/tree/beta/packages/data/src/api/attributesByContract/README.md) | `contractId: string` |get attributes of a certain contract|
| [attributeRarity](https://github.com/Mintbase/mintbase-js/tree/beta/packages/data/src/api/attributeRarity/README.md) | `contractId: string, attributeType: string, attributeValue: string` |get attribute rarity of a certain contract, attribute type and attribute value | [userOwnedTokens](https://github.com/Mintbase/mintbase-js/tree/beta/packages/data/src/api/userOwnedTokens/README.md)
| [attributeRarity](https://github.com/Mintbase/mintbase-js/tree/beta/packages/data/src/api/attributeRarity/README.md) | `contractId: string, attributeType: string, attributeValue: string` |get attribute rarity of a certain contract, attribute type and attribute value | [userOwnedTokens](https://github.com/Mintbase/mintbase-js/tree/beta/packages/data/src/api/userOwnedTokens/README.md)| [userMintedTokens](https://github.com/Mintbase/mintbase-js/tree/beta/packages/data/src/api/userMintedTokens/README.md)
53 changes: 53 additions & 0 deletions packages/data/src/api/userMintedTokens/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
[//]: # `{ "title": "userMintedTokens", "order": "1.0.18" }`

# userMintedTokens


{% hint style="warning" %}

This is a work in progress, please reach out to us on [Telegram](https://t.me/mintdev) for support.

For the most reliable data, reference our [existing graphql docs](https://docs.mintbase.io/dev/read-data/mintbase-graph).

{% endhint %}




Returns number of minted tokens by `accountId`.



### getUserMintedTokens(accountId: string, filters: UserTokensFilter,): Promise<ParsedDataReturn<UserTokensQueryResult>>



This is an example of a data api method.




Example:



{% code title="userMintedTokens.ts" overflow="wrap" lineNumbers="true" %}

```typescript

import { userMintedTokens } from '@mintbase-js/data'

const { data, error } = await getUserMintedTokens('mintbase1.near', {
orderBy: 'price asc',
limit: 10,
offset: 0,
listedFilter: true,
});

if (error) {console.log('error', error)}

console.log(data) // => ...

```

{% endcode %}
41 changes: 41 additions & 0 deletions packages/data/src/api/userMintedTokens/userMintedTokens.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { META_SERVICE_HOST_TESTNET } from '../../constants';
import { ORDER_BY_VALUE, ParsedDataReturn, UserTokensFilter, UserTokensQueryResult } from '../../types';
import fetchMock from 'fetch-mock';
import { mbjs } from '@mintbase-js/sdk';
import { getUserOwnedTokens } from '../userOwnedTokens/userOwnedTokens';

describe('userMintedTokens', () => {
beforeAll(() => {
mbjs.config({ network: 'testnet' });
});

beforeEach(() => {
jest.spyOn(console, 'error').mockImplementation(() => {
// console.log('Suppressed console error.');
});
});

it('owned user tokens', async () => {
fetchMock.mock(`begin:${META_SERVICE_HOST_TESTNET}`, { body: { results: [{ token_id: '123' }] } });
const filters: UserTokensFilter = {
orderBy: ORDER_BY_VALUE.PRICE_ASC,
limit: 10,
offset: 0,
listedFilter: true,
};
const { data } = await getUserOwnedTokens('human.id', filters) as ParsedDataReturn<UserTokensQueryResult>;
expect(data?.results).toBeDefined();
});

it('returns errors', async () => {
fetchMock.mock(`begin:${META_SERVICE_HOST_TESTNET}`, 504, { overwriteRoutes: true });
const filters = {
orderBy: 'xpto',
limit: 10,
offset: 0,
listedFilter: true,
};
const { error } = await getUserOwnedTokens('contract.id', filters);
expect(error).toBeDefined();
});
});
45 changes: 45 additions & 0 deletions packages/data/src/api/userMintedTokens/userMintedTokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { mbjs } from '@mintbase-js/sdk';
import { META_SERVICE_HOST, META_SERVICE_HOST_TESTNET, MINTBASE_API_KEY_HEADER } from '../../constants';
import { ParsedDataReturn, UserTokensFilter, UserTokensQueryResult } from '../../types';
import { parseData } from '../../utils';


export const getUserMintedTokens = async (
accountId: string,
filters: UserTokensFilter,
): Promise<ParsedDataReturn<UserTokensQueryResult>> => {

let data;
let error: string;

const { limit, offset, orderBy, listedFilter } = filters;

const useHost = mbjs.keys.network === 'testnet'
? META_SERVICE_HOST_TESTNET
: META_SERVICE_HOST;

try {
const res = await fetch(`${useHost}/human/${accountId}/minted?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,
);
};
4 changes: 2 additions & 2 deletions packages/data/src/api/userOwnedTokens/userOwnedTokens.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { META_SERVICE_HOST_TESTNET } from '../../constants';
import { ParsedDataReturn } from '../../types';
import { getUserOwnedTokens, ORDER_BY_VALUE, UserTokensFilter, UserTokensQueryResult } from './userOwnedTokens';
import { ORDER_BY_VALUE, ParsedDataReturn, UserTokensFilter, UserTokensQueryResult } from '../../types';

import fetchMock from 'fetch-mock';
import { mbjs } from '@mintbase-js/sdk';
import { getUserOwnedTokens } from './userOwnedTokens';

describe('userOwnedTokens', () => {
beforeAll(() => {
Expand Down
4 changes: 1 addition & 3 deletions packages/data/src/api/userOwnedTokens/userOwnedTokens.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
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 { ParsedDataReturn, UserTokensFilter, UserTokensQueryResult } from '../../types';
import { parseData } from '../../utils';
import { ORDER_BY_VALUE, UserTokensQueryResult, UserTokensFilter } from './userOwnedTokens.types';

export { UserTokensQueryResult, ORDER_BY_VALUE, UserTokensFilter };

export const getUserOwnedTokens = async (
accountId: string,
Expand Down
35 changes: 0 additions & 35 deletions packages/data/src/api/userOwnedTokens/userOwnedTokens.types.ts

This file was deleted.

36 changes: 36 additions & 0 deletions packages/data/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,39 @@ export type Attribute = {
attribute_value?: string | null;
attribute_type?: string | null;
}

export enum ORDER_BY_VALUE {
PRICE_DESC = 'price desc nulls last',
PRICE_ASC = 'price asc',
LATEST = 'latest',
OLDEST = 'oldest',
}

export interface UserTokensFilter {
limit: number;
offset: number;
listedFilter: boolean;
orderBy: string;
}

export type UserTokensQueryResult = {
results: UserTokensResult[];
total: number;
}

export interface UserTokensResult {
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 0ec1362

Please sign in to comment.