-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #183 from Mintbase/burn-and-tokens-by-status
✨ Burn structure + Tokens by status query
- Loading branch information
Showing
31 changed files
with
444 additions
and
191 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
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,4 @@ | ||
/* eslint-disable no-undef */ | ||
module.exports = { | ||
setupMeteorWallet: jest.fn(), | ||
}; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './tokenListingCounts/tokenListingCountsByMetaId'; | ||
export * from './ownedTokens/ownedTokens'; | ||
export * from './tokensByStatus/tokensByStatus'; |
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
51 changes: 51 additions & 0 deletions
51
packages/data/src/api/tokensByStatus/tokensByStatus.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,51 @@ | ||
/* eslint-disable @typescript-eslint/camelcase */ | ||
import { GraphQLClient } from 'graphql-request'; | ||
import { GraphqlFetchingError } from '../../graphql/fetch'; | ||
import { TokensByStatus, tokensByStatus } from './tokensByStatus'; | ||
|
||
jest.mock('graphql-request'); | ||
|
||
describe('getTokensFromMetaId', () => { | ||
beforeEach(() => { | ||
jest.spyOn(console, 'error').mockImplementation(() => { | ||
// console.log('Suppressed console error.'); | ||
}); | ||
}); | ||
|
||
it('should handle errors', async () => { | ||
const errMessage = 'exploded'; | ||
const exploded = new GraphqlFetchingError(errMessage); | ||
(GraphQLClient as jest.Mock).mockImplementationOnce(() => ({ | ||
request: (): Promise<TokensByStatus> => Promise.reject(errMessage), | ||
})); | ||
await expect(tokensByStatus('test.id')).rejects.toThrow(exploded); | ||
}); | ||
|
||
it('should show correct values for each type', async () => { | ||
const unburnedTokensMock = getNodeObjectFromTokenIds(['27', '28', '30']); | ||
const burnedTokensMock = getNodeObjectFromTokenIds(['29']); | ||
const listedTokensMock = getNodeObjectFromTokenIds(['27', '28']); | ||
(GraphQLClient as jest.Mock).mockImplementationOnce(() => ({ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
request: (): Promise<any> => | ||
Promise.resolve({ | ||
listedTokens: listedTokensMock, | ||
burnedTokens: burnedTokensMock, | ||
unburnedTokens: unburnedTokensMock, | ||
}), | ||
})); | ||
|
||
const { listedTokens, burnedTokens, unlistedTokens } = await tokensByStatus('test.id'); | ||
expect(listedTokens).toStrictEqual(['27', '28']); | ||
expect(burnedTokens).toStrictEqual(['29']); | ||
expect(unlistedTokens).toStrictEqual(['30']); | ||
}); | ||
}); | ||
|
||
function getNodeObjectFromTokenIds(tokenIds: string[]): { nodes: { token_id: string }[] } { | ||
const arr: { token_id: string }[] = []; | ||
tokenIds.forEach((token: string) => { | ||
arr.push({ token_id: token }); | ||
}); | ||
return { nodes: arr }; | ||
} |
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,48 @@ | ||
import { NftTokensAggregate } from '../../graphql/codegen/graphql'; | ||
import { fetchGraphQl } from '../../graphql/fetch'; | ||
import { tokensByStatusQuery } from './tokensByStatusQuery'; | ||
|
||
export type TokensByStatus = { | ||
listedTokens: string[]; | ||
burnedTokens: string[]; | ||
unlistedTokens: string[]; | ||
}; | ||
|
||
export type TokenByStatusQueryResults = { | ||
listedTokens: NftTokensAggregate; | ||
burnedTokens: NftTokensAggregate; | ||
unburnedTokens: NftTokensAggregate; | ||
}; | ||
|
||
export const tokensByStatus = async (metadataId: string, ownedBy?: string): Promise<TokensByStatus> => { | ||
const { data, error } = await fetchGraphQl<TokenByStatusQueryResults>({ | ||
query: tokensByStatusQuery, | ||
variables: { | ||
metadataId, | ||
ownedBy, | ||
}, | ||
}); | ||
|
||
if (error) { | ||
console.error('Error fetching token listing by status', error.message); | ||
throw error; | ||
} | ||
const listedTokens = getTokenArrayFromNodes(data.listedTokens.nodes); | ||
const burnedTokens = getTokenArrayFromNodes(data.burnedTokens.nodes); | ||
const unburnedTokens = getTokenArrayFromNodes(data.unburnedTokens.nodes); | ||
const unlistedTokens = unburnedTokens.filter((el: string) => !listedTokens.includes(el)); | ||
|
||
return { | ||
listedTokens, | ||
burnedTokens, | ||
unlistedTokens, | ||
}; | ||
}; | ||
|
||
function getTokenArrayFromNodes(nodes: { token_id: string }[]): string[] { | ||
const arr: string[] = []; | ||
nodes.forEach((token: { token_id: string }) => { | ||
arr.push(token.token_id); | ||
}); | ||
return arr; | ||
} |
48 changes: 48 additions & 0 deletions
48
packages/data/src/api/tokensByStatus/tokensByStatusQuery.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,48 @@ | ||
import { gql } from 'graphql-request'; | ||
import { QUERY_OPS_PREFIX } from '../../constants'; | ||
|
||
export const tokensByStatusQuery = gql` | ||
query ${QUERY_OPS_PREFIX}_tokensByStatus ( | ||
$metadataId: String! | ||
$ownedBy: String | ||
) { | ||
# unburned tokens count | ||
unburnedTokens: nft_tokens_aggregate( | ||
where: { | ||
metadata_id: { _eq: $metadataId } | ||
burned_timestamp: { _is_null: true } | ||
owner: { _eq: $ownedBy} | ||
} | ||
) { | ||
nodes { | ||
token_id | ||
} | ||
} | ||
burnedTokens: nft_tokens_aggregate( | ||
where: { | ||
metadata_id: { _eq: $metadataId } | ||
burned_timestamp: { _is_null: false } | ||
owner: { _eq: $ownedBy} | ||
} | ||
) { | ||
nodes { | ||
token_id | ||
} | ||
} | ||
#listed tokens | ||
listedTokens: mb_views_active_listings_aggregate( | ||
where: { | ||
metadata_id: { _eq: $metadataId } | ||
listed_by: { _eq: $ownedBy} | ||
} | ||
) { | ||
nodes { | ||
token_id | ||
} | ||
} | ||
} | ||
`; |
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
Oops, something went wrong.