Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query account by public key #408

Merged
merged 5 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,511 changes: 1,488 additions & 23 deletions package-lock.json

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions packages/data/src/api/accountsByPublicKey/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[//]: # `{ "title": "accountsByPublicKey", "order": "1.0.18" }`

# accountsByPublicKey

{% 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 accounts for which `publicKey` is a full access key.

### accountsByPublicKey(publicKey: string): Promise<ParsedDataReturn<string[]>>

Example:



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

```typescript
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: I believe you can also go with just ts here:

Suggested change
```typescript
```ts

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, but this is consistent with the rest of documentation (I just copied the template from another README)


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

const { data, error } = await accountsByPublicKey('ed25519:12345...');

if (error) {console.log('error', error)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ubernit:

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above, we'll keep it consistent with other docs


console.log(data) // => 'foo.testnet'

```

{% endcode %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const accountsByPublicKeyMock = {
accounts: [{ id: 'foo.testnet' }],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { gql } from 'graphql-request';
import { QUERY_OPS_PREFIX } from '../../constants';

export const accountsByPublicKeyQuery = gql`
query ${QUERY_OPS_PREFIX}_accountsByPublicKey(
$publicKey: String!
) {
accounts: access_keys(
where: {
public_key: { _eq: $publicKey }
removed_at: { _is_null: false }
}
) {
id: account_id
}
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { GraphQLClient } from 'graphql-request';
import { accountsByPublicKeyMock } from './accountsByPublicKey.mock';
import { accountsByPublicKey } from './accountsByPublicKey';

jest.mock('graphql-request');

describe('accountsByPublicKey', () => {
jest.spyOn(console, 'error').mockImplementation(() => null);
afterAll(() => {
jest.resetAllMocks();
jest.restoreAllMocks();
});

afterEach(() => {
jest.clearAllMocks();
});

it('should return attribute rarity', async () => {
(GraphQLClient as jest.Mock).mockImplementationOnce(() => ({
request: (): Promise<{ accounts: Array<{ id: string }> }> => Promise.resolve(accountsByPublicKeyMock),
}));

const result = await accountsByPublicKey('ed25519:yadda');

expect(result?.data?.length).toBe(1);
expect(result?.data?.[0]).toBe('foo.testnet');
});

it('should handle errors', async () => {
const errMessage = 'exploded';
(GraphQLClient as jest.Mock).mockImplementationOnce(() => ({
request: (): Promise<{ accounts: Array<{ id: string }> }> => Promise.reject(new Error(errMessage)),
}));

const result = await accountsByPublicKey('ed25519:yadda');

expect(result).toStrictEqual({ error: errMessage });
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Network } from '@mintbase-js/sdk';
import { fetchGraphQl } from '../../graphql/fetch';
import { ParsedDataReturn } from '../../types';
import { parseData } from '../../utils';
import { accountsByPublicKeyQuery } from './accountsByPublicKey.query';
import { AccountsByPublicKeyResults } from './accountsByPublicKey.type';

export const accountsByPublicKey = async (
publicKey: string,
network?: Network,
): Promise<ParsedDataReturn<string[]>> => {


const { data, error } = await fetchGraphQl<AccountsByPublicKeyResults>({
query: accountsByPublicKeyQuery,
variables: { publicKey },
...(network && { network:network }),
});

const errorMsg = error ? `Error fetching accounts by public key, ${error}` : '';
const accountIds = error ? [] : data.accounts.map(a => a.id);

return parseData<string[]>(accountIds, error, errorMsg);

};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface AccountsByPublicKeyResults {
accounts: Array<{ id: string }>;
}
2 changes: 1 addition & 1 deletion packages/data/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './accountsByPublicKey/accountsByPublicKey';
export * from './attributesByMetaId/attributesByMetaId';
export * from './metadataByMetadataId/metadataByMetadataId';
export * from './nearPrice/nearPrice';
Expand All @@ -18,4 +19,3 @@ export * from './attributesByContract/attributesByContract';
export * from './queries';
export * from './userOwnedTokens/userOwnedTokens';
export * from './userMintedTokens/userMintedTokens';

Loading