From f803b94f67fd016c47d0832510f7af9628a2186c Mon Sep 17 00:00:00 2001 From: Rhys Bartels-Waller Date: Mon, 19 Apr 2021 22:49:13 +1000 Subject: [PATCH] feature: tokenMints and tokenMints_aggregate queries --- packages/api-cardano-db-hasura/schema.graphql | 12 +++++ .../token_mints/tokenMints.graphql | 35 ++++++++++++ .../src/executableSchema.ts | 20 +++++++ .../test/assets.query.test.ts | 2 +- .../test/tokenMints.query.test.ts | 53 +++++++++++++++++++ 5 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 packages/api-cardano-db-hasura/src/example_queries/token_mints/tokenMints.graphql create mode 100644 packages/api-cardano-db-hasura/test/tokenMints.query.test.ts diff --git a/packages/api-cardano-db-hasura/schema.graphql b/packages/api-cardano-db-hasura/schema.graphql index c69346ff..01c0f07f 100644 --- a/packages/api-cardano-db-hasura/schema.graphql +++ b/packages/api-cardano-db-hasura/schema.graphql @@ -139,6 +139,18 @@ type Query { offset: Int where: StakeRegistration_bool_exp ): StakeRegistration_aggregate + tokenMints ( + limit: Int + order_by: [TokenMint_order_by!] + offset: Int + where: TokenMint_bool_exp + ): [TokenMint]! + tokenMints_aggregate ( + limit: Int + order_by: [TokenMint_order_by!] + offset: Int + where: TokenMint_bool_exp + ): TokenMint_aggregate! transactions ( limit: Int order_by: [Transaction_order_by!] diff --git a/packages/api-cardano-db-hasura/src/example_queries/token_mints/tokenMints.graphql b/packages/api-cardano-db-hasura/src/example_queries/token_mints/tokenMints.graphql new file mode 100644 index 00000000..073ced85 --- /dev/null +++ b/packages/api-cardano-db-hasura/src/example_queries/token_mints/tokenMints.graphql @@ -0,0 +1,35 @@ +query tokenMints ( + $limit: Int + $offset: Int + $where: TokenMint_bool_exp +) { + tokenMints ( + limit: $limit + offset: $offset + where: $where + ) { + asset { + assetId + assetName + description + fingerprint + logo + name + policyId + ticker + url + } + quantity + transaction { + hash + } + } + tokenMints_aggregate { + aggregate { + count + sum { + quantity + } + } + } +} diff --git a/packages/api-cardano-db-hasura/src/executableSchema.ts b/packages/api-cardano-db-hasura/src/executableSchema.ts index bc148957..e7a39776 100644 --- a/packages/api-cardano-db-hasura/src/executableSchema.ts +++ b/packages/api-cardano-db-hasura/src/executableSchema.ts @@ -311,6 +311,26 @@ export async function buildSchema ( schema: hasuraClient.schema }) }, + tokenMints: (_root, args, context, info) => { + return delegateToSchema({ + args, + context, + fieldName: 'tokenMints', + info, + operation: 'query', + schema: hasuraClient.schema + }) + }, + tokenMints_aggregate: (_root, args, context, info) => { + return delegateToSchema({ + args, + context, + fieldName: 'tokenMints_aggregate', + info, + operation: 'query', + schema: hasuraClient.schema + }) + }, utxos: (_root, args, context, info) => { return delegateToSchema({ args, diff --git a/packages/api-cardano-db-hasura/test/assets.query.test.ts b/packages/api-cardano-db-hasura/test/assets.query.test.ts index 5f80235e..45fa314e 100644 --- a/packages/api-cardano-db-hasura/test/assets.query.test.ts +++ b/packages/api-cardano-db-hasura/test/assets.query.test.ts @@ -41,7 +41,7 @@ describe('assets', () => { fingerprint: { _eq: 'asset12h3p5l3nd5y26lr22am7y7ga3vxghkhf57zkhd' }, tokenMints: { transaction: { includedAt: { _gt: '2017-09-23T21:44:51Z' } } } }] - }, + } } }) const { assets } = result.data diff --git a/packages/api-cardano-db-hasura/test/tokenMints.query.test.ts b/packages/api-cardano-db-hasura/test/tokenMints.query.test.ts new file mode 100644 index 00000000..edc024a3 --- /dev/null +++ b/packages/api-cardano-db-hasura/test/tokenMints.query.test.ts @@ -0,0 +1,53 @@ +/* eslint-disable camelcase */ +import path from 'path' + +import { DocumentNode } from 'graphql' +import util from '@cardano-graphql/util' +import { TestClient } from '@cardano-graphql/util-dev' +import { testClient } from './util' + +function loadQueryNode (name: string): Promise { + return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'token_mints'), name) +} + +describe('tokenMints', () => { + let client: TestClient + beforeAll(async () => { + client = await testClient.mainnet() + }) + + it('can return information on token minting and burning', async () => { + const result = await client.query({ + query: await loadQueryNode('tokenMints'), + variables: { + limit: 2 + } + }) + const { tokenMints_aggregate, tokenMints } = result.data + const { aggregate } = tokenMints_aggregate + expect(aggregate.count).toBeDefined() + expect(tokenMints.length).toBeGreaterThan(0) + expect(parseInt(tokenMints_aggregate.aggregate.count)).toBeGreaterThan(0) + expect(tokenMints[0].asset.fingerprint.slice(0, 5)).toBe('asset') + }) + + it('can return information on assets by fingerprint', async () => { + const result = await client.query({ + query: await loadQueryNode('tokenMints'), + variables: { + where: { + asset: { fingerprint: { _eq: 'asset12h3p5l3nd5y26lr22am7y7ga3vxghkhf57zkhd' } } + }, + orderBy: { + transaction: { includedAt: 'desc' } + } + } + }) + const { tokenMints } = result.data + expect(tokenMints[0].quantity).toBeDefined() + expect(tokenMints[0].transaction.hash).toBeDefined() + expect(tokenMints[0].asset.assetId).toBeDefined() + expect(tokenMints[0].asset.fingerprint).toBeDefined() + expect(tokenMints[0].asset.policyId).toBeDefined() + }) +})