-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d890305
commit 0ec1362
Showing
9 changed files
with
180 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
packages/data/src/api/userMintedTokens/userMintedTokens.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
45
packages/data/src/api/userMintedTokens/userMintedTokens.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
4
packages/data/src/api/userOwnedTokens/userOwnedTokens.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 0 additions & 35 deletions
35
packages/data/src/api/userOwnedTokens/userOwnedTokens.types.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters