Skip to content

Commit

Permalink
Add key activity query (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
tifrel authored Oct 19, 2023
1 parent 669a5fe commit 5a4333f
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/data/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './accountsByPublicKey/accountsByPublicKey';
export * from './attributesByMetaId/attributesByMetaId';
export * from './keyActivity/keyActivity';
export * from './metadataByMetadataId/metadataByMetadataId';
export * from './nearPrice/nearPrice';
export * from './ownedTokens/ownedTokens';
Expand Down
36 changes: 36 additions & 0 deletions packages/data/src/api/keyActivity/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[//]: # `{ "title": "keyActivity", "order": "1.0.18" }`

# keyActivity


{% 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 tokens by `metadataId` and statuses: `burned, unburned, listed`.

### getKey(accountId: string)

Fetches all historical additions and deletions of full access keys to an account.

Example:

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

```typescript

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

const { data, error } = await getKeyActivity('foo.near');

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

console.log(data.listedTokens)

```

{% endcode %}
16 changes: 16 additions & 0 deletions packages/data/src/api/keyActivity/keyActivity.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const KEY_ACTIVITY_MOCK = {
keyActivity: [
{
timestamp: '2000-01-01T00:00:00.000000',
receiptId: 'receiptIdAdd',
publicKey: 'pubkey',
kind: 'add',
},
{
timestamp: '2000-01-02T00:00:00.000000',
receiptId: 'receiptIdDel',
publicKey: 'pubkey',
kind: 'del',
},
],
};
16 changes: 16 additions & 0 deletions packages/data/src/api/keyActivity/keyActivity.query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { gql } from 'graphql-request';
import { QUERY_OPS_PREFIX } from '../../constants';

export const keyActivityQuery = gql`
query ${QUERY_OPS_PREFIX}_KeyActivity($accountId: String) {
keyActivity: mb_views_access_key_activity(
order_by: { timestamp: desc }
where: { account_id: { _eq: $accountId} }
) {
timestamp,
receiptId: receipt_id,
publicKey: public_key,
kind
}
}
`;
43 changes: 43 additions & 0 deletions packages/data/src/api/keyActivity/keyActivity.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* eslint-disable @typescript-eslint/camelcase */
import { GraphQLClient } from 'graphql-request';
import { keyActivity } from './keyActivity';
import { KEY_ACTIVITY_MOCK } from './keyActivity.mock';
import { KeyActivityQueryResults } from './keyActivity.types';

jest.mock('graphql-request');

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

it('should show key activity', async () => {

(GraphQLClient as jest.Mock).mockImplementationOnce(() => ({
// eslint-disable-next-line @typescript-eslint/no-explicit-any
request: (): Promise<KeyActivityQueryResults> =>
Promise.resolve(KEY_ACTIVITY_MOCK),
}));

const { data } = await keyActivity('test.near');

expect(JSON.stringify(data)).toBe(JSON.stringify({
keyActivity: [
{
timestamp: '2000-01-01T00:00:00.000000',
receiptId: 'receiptIdAdd',
publicKey: 'pubkey',
kind: 'add',
},
{
timestamp: '2000-01-02T00:00:00.000000',
receiptId: 'receiptIdDel',
publicKey: 'pubkey',
kind: 'del',
},
],
}));
});
});
19 changes: 19 additions & 0 deletions packages/data/src/api/keyActivity/keyActivity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Network } from '@mintbase-js/sdk';
import { fetchGraphQl } from '../../graphql/fetch';
import { ParsedDataReturn } from '../../types';
import { parseData } from '../../utils';
import { KeyActivityQueryResults } from './keyActivity.types';
import { keyActivityQuery } from './keyActivity.query';


export const keyActivity = async (accountId: string, network?: Network): Promise<ParsedDataReturn<KeyActivityQueryResults>> => {
const { data, error } = await fetchGraphQl<KeyActivityQueryResults>({
query: keyActivityQuery,
variables: { accountId },
...(network && { network:network }),
});

const errorMsg = error? `Error fetching token listing by status', ${error}`: '';

return parseData(data, error, errorMsg);
};
8 changes: 8 additions & 0 deletions packages/data/src/api/keyActivity/keyActivity.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export type KeyActivityQueryResults = {
keyActivity: {
timestamp: string;
receiptId: string;
publicKey: string;
kind: string;
}[];
};

0 comments on commit 5a4333f

Please sign in to comment.