Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(data): Add contractAttributes method in data layer (#295)
- Loading branch information
1 parent
064ee8a
commit 2873879
Showing
8 changed files
with
171 additions
and
1 deletion.
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,55 @@ | ||
[//]: # `{ "title": "contractAttributes", "order": "1.0.15" }` | ||
# contractAttributes | ||
|
||
|
||
|
||
{% 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 contract attributes by `contractAddress`. | ||
|
||
|
||
|
||
### contractAttributes( contractAddress: string) | ||
|
||
|
||
|
||
This is an example of a data api method. | ||
|
||
|
||
|
||
|
||
Example: | ||
|
||
|
||
|
||
{% code title="queryContractAttributes.ts" overflow="wrap" lineNumbers="true" %} | ||
|
||
```typescript | ||
|
||
import { contractAttributes } from '@mintbase-js/data' | ||
|
||
|
||
|
||
const { data, error } = await contractAttributes('teammintbase.mintbase1.near'); | ||
|
||
if (error) {console.log('error', error)} | ||
|
||
|
||
console.log(data) // => contract attributes data | ||
|
||
``` | ||
|
||
{% endcode %} |
19 changes: 19 additions & 0 deletions
19
packages/data/src/api/contractAttributes/contractAttributes.mock.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,19 @@ | ||
import { ContractAttributesDataResults } from './contractAttributes.types'; | ||
|
||
export const contractAttributesMock: ContractAttributesDataResults = { | ||
nft_attributes: [ | ||
{ | ||
attribute_type: 'eyes', | ||
}, | ||
{ | ||
attribute_type: 'head', | ||
}, | ||
{ | ||
attribute_type: 'mouth', | ||
}], | ||
nft_attributes_aggregate: { | ||
aggregate: { | ||
count: 6, | ||
}, | ||
}, | ||
}; |
19 changes: 19 additions & 0 deletions
19
packages/data/src/api/contractAttributes/contractAttributes.query.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,19 @@ | ||
import { gql } from 'graphql-request'; | ||
import { QUERY_OPS_PREFIX } from '../../constants'; | ||
|
||
export const contractAttributesQuery = gql` | ||
query ${QUERY_OPS_PREFIX}_getContractAttributes($contractAddress: String!) { | ||
nft_attributes( | ||
where: {nft_contract_id: {_eq: $nft_metadata_id}} | ||
distinct_on: attribute_type | ||
) { | ||
attribute_type | ||
} | ||
nft_attributes_aggregate(where: {nft_contract_id: {_eq: $nft_metadata_id}} | ||
distinct_on: attribute_type) { | ||
aggregate { | ||
count | ||
} | ||
} | ||
} | ||
`; |
40 changes: 40 additions & 0 deletions
40
packages/data/src/api/contractAttributes/contractAttributes.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,40 @@ | ||
import { contractAttributes } from './contractAttributes'; | ||
import { ContractAttributesDataResults } from './contractAttributes.types'; | ||
|
||
import { GraphQLClient } from 'graphql-request'; | ||
import { contractAttributesMock } from './contractAttributes.mock'; | ||
|
||
describe('contractAttributes', () => { | ||
afterAll(() => { | ||
jest.resetAllMocks(); | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return contract attributes', async () => { | ||
(GraphQLClient as jest.Mock).mockImplementationOnce(() => ({ | ||
request: (): Promise<ContractAttributesDataResults> => Promise.resolve(contractAttributesMock), | ||
})); | ||
|
||
const result = await contractAttributes('test.mintbase1.near'); | ||
|
||
expect(result?.data).toBe( | ||
contractAttributesMock, | ||
); | ||
}); | ||
|
||
it('should handle errors', async () => { | ||
const errMessage = 'exploded'; | ||
(GraphQLClient as jest.Mock).mockImplementationOnce(() => ({ | ||
request: (): Promise<ContractAttributesDataResults> => Promise.reject(new Error(errMessage)), | ||
})); | ||
|
||
const call = await contractAttributes('test.mintbase1.near'); | ||
|
||
expect(call).toStrictEqual({ error: errMessage }); | ||
|
||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
packages/data/src/api/contractAttributes/contractAttributes.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,24 @@ | ||
import { Network } from '@mintbase-js/sdk'; | ||
import { fetchGraphQl } from '../../graphql/fetch'; | ||
import { ParsedDataReturn } from '../../types'; | ||
import { parseData } from '../../utils'; | ||
import { contractAttributesQuery } from './contractAttributes.query'; | ||
import { ContractAttributesDataResults } from './contractAttributes.types'; | ||
|
||
|
||
export const contractAttributes = async ( | ||
contractAddress: string, | ||
network?: Network, | ||
): Promise<ParsedDataReturn<ContractAttributesDataResults>> => { | ||
const { data, error } = await fetchGraphQl<ContractAttributesDataResults>({ | ||
query: contractAttributesQuery, | ||
variables: { | ||
contractAddress, | ||
}, | ||
...(network && { network:network }), | ||
}); | ||
|
||
const errorMsg = error ? `Error fetching contract attributes, ${error}` : ''; | ||
|
||
return parseData(data, error, errorMsg); | ||
}; |
11 changes: 11 additions & 0 deletions
11
packages/data/src/api/contractAttributes/contractAttributes.types.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,11 @@ | ||
export interface ContractAttributesDataResults { | ||
nft_attributes: | ||
{ | ||
attribute_type: string; | ||
}[]; | ||
nft_attributes_aggregate: { | ||
aggregate: { | ||
count: number; | ||
}; | ||
}; | ||
} |