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 all 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
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"useNx": true,
"version": "0.4.1-beta.5",
"version": "0.4.1-beta.6",
"packages": [
"packages/*"
]
Expand Down
1,513 changes: 1,489 additions & 24 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mintbase-next-test-suite",
"version": "0.4.1-beta.5",
"version": "0.4.1-beta.6",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
4 changes: 2 additions & 2 deletions packages/auth/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mintbase-js/auth",
"version": "0.4.1-beta.4",
"version": "0.4.1-beta.6",
"description": "Wallet and auth functions for Mintbase JS SDK",
"main": "lib/index.js",
"scripts": {
Expand All @@ -19,7 +19,7 @@
"author": "",
"license": "MIT",
"dependencies": {
"@mintbase-js/sdk": "^0.4.1-beta.4",
"@mintbase-js/sdk": "^0.4.1-beta.6",
"@near-wallet-selector/core": "^8.0.3",
"@near-wallet-selector/default-wallets": "^8.0.3",
"@near-wallet-selector/here-wallet": "^8.0.3",
Expand Down
4 changes: 2 additions & 2 deletions packages/data/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mintbase-js/data",
"version": "0.4.1-beta.4",
"version": "0.4.1-beta.6",
"description": "Query wrappers for Mintbase JS SDK",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -28,6 +28,6 @@
"@graphql-codegen/cli": "^2.13.7",
"@graphql-codegen/client-preset": "1.1.1",
"@graphql-codegen/introspection": "2.2.1",
"@mintbase-js/sdk": "^0.4.1-beta.4"
"@mintbase-js/sdk": "^0.4.1-beta.6"
}
}
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';

8 changes: 4 additions & 4 deletions packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mintbase-js/react",
"version": "0.4.1-beta.4",
"version": "0.4.1-beta.6",
"description": "React app tools for Mintbase JS SDK",
"main": "lib/index.js",
"scripts": {
Expand All @@ -23,9 +23,9 @@
"@testing-library/user-event": "^14.4.3"
},
"dependencies": {
"@mintbase-js/auth": "^0.4.1-beta.4",
"@mintbase-js/data": "^0.4.1-beta.4",
"@mintbase-js/sdk": "^0.4.1-beta.4",
"@mintbase-js/auth": "^0.4.1-beta.6",
"@mintbase-js/data": "^0.4.1-beta.6",
"@mintbase-js/sdk": "^0.4.1-beta.6",
"@near-wallet-selector/core": "^8.0.3",
"@near-wallet-selector/modal-ui": "^8.0.3",
"near-api-js": "^2.1.3",
Expand Down
6 changes: 5 additions & 1 deletion packages/rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ For a transaction hash, determine the status of a transaction on the configured

Calls a token contract in order to determine the percentage amounts paid out to royalty accounts.

### `getAccessKeys(accountId: string): Promise<AccessKey>`

Gets all access keys (public key and permissions object) for a given account.

## Configuration

Before calling these methods the near network should be configured using the [config SDK method](https://docs.mintbase.io/dev/mintbase-sdk-ref/sdk/config)

## Future

We will be adding more contract view methods here as needs arise.
We will be adding more contract view methods here as needs arise.
4 changes: 2 additions & 2 deletions packages/rpc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mintbase-js/rpc",
"version": "0.4.1-beta.4",
"version": "0.4.1-beta.6",
"description": "NEAR RPC interactions for Mintbase JS SDK",
"main": "lib/index.js",
"scripts": {
Expand All @@ -25,7 +25,7 @@
"isomorphic-unfetch": "^3.1.0"
},
"devDependencies": {
"@mintbase-js/sdk": "^0.4.1-beta.4",
"@mintbase-js/sdk": "^0.4.1-beta.6",
"@types/bn.js": "^5.1.1"
}
}
1 change: 1 addition & 0 deletions packages/rpc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './methods/balance';
export * from './methods/payouts';
export * from './methods/account';
export * from './methods/social';
export * from './methods/keys';
44 changes: 44 additions & 0 deletions packages/rpc/src/methods/keys.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import fetch from 'isomorphic-unfetch';
import { AccessKey, getAccessKeys } from './keys';

jest.mock('isomorphic-unfetch');

describe('keys', () => {
const getRes = async (): Promise<AccessKey[]> => await getAccessKeys('benipsen.near');

it('should return access keys for accounts', async () => {
(fetch as jest.Mock).mockResolvedValueOnce({
json: jest.fn().mockResolvedValueOnce({
result: {
keys: [
{ public_key: 'pubkey', permission: 'FullAccess' },
],
},
}),
});
const res = await getRes();
expect(res.length).toBe(1);
expect(res[0].public_key).toBe('pubkey');
expect(res[0].permission).toBe('FullAccess');
});

it('should throw on returned error', async () => {
(fetch as jest.Mock).mockResolvedValueOnce({
json: jest.fn().mockResolvedValueOnce({
result: { error: 'some error' },
}),
});

await expect(getRes).rejects.toThrow('some error');
});

it('should throw on malformed response', async () => {
(fetch as jest.Mock).mockResolvedValueOnce({
json: jest.fn().mockResolvedValueOnce({
result: { foo: 'bar' },
}),
});

await expect(getRes).rejects.toThrow('Malformed response');
});
});
36 changes: 36 additions & 0 deletions packages/rpc/src/methods/keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { requestFromNearRpc } from '../util';

export type AccessKey = {
public_key: string;
permission: AccessKeyPermissions;
};

export type AccessKeyPermissions = 'FullAccess' | {
'FunctionCall': {
allowance: string;
receiver_id: string;
method_names: string[];
};
}

export const getAccessKeys = async (accountId: string): Promise<AccessKey[]> => {
const res = await requestFromNearRpc({
jsonrpc: '2.0',
id: 'dontcare',
method: 'query',
params: {
request_type: 'view_access_key_list',
finality: 'final',
account_id: accountId,
},
});

const accessKeys = res?.result?.keys;
if (res?.error) {
throw res.error;
}
if (!accessKeys) {
throw new Error(`Malformed response: ${JSON.stringify(res)}`);
}
return accessKeys;
};
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mintbase-js/sdk",
"version": "0.4.1-beta.4",
"version": "0.4.1-beta.6",
"description": "Core functions for Mintbase JS SDK",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions packages/storage/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/storage/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mintbase-js/storage",
"version": "0.4.1-beta.4",
"version": "0.4.1-beta.6",
"description": "Storage functions for Mintbase JS SDK",
"main": "lib/index.js",
"scripts": {
Expand All @@ -16,7 +16,7 @@
"author": "",
"license": "MIT",
"dependencies": {
"@mintbase-js/sdk": "^0.4.1-beta.4",
"@mintbase-js/sdk": "^0.4.1-beta.6",
"near-api-js": "^2.1.3",
"superagent": "^8.0.3"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/testing/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mintbase-js/testing",
"version": "0.4.1-beta.4",
"version": "0.4.1-beta.6",
"description": "Integration test suite and testing utils",
"main": "lib/index.js",
"scripts": {
Expand All @@ -21,7 +21,7 @@
"dependencies": {
"@google-cloud/firestore": "^6.4.2",
"@google-cloud/secret-manager": "^4.1.4",
"@mintbase-js/auth": "^0.4.1-beta.4",
"@mintbase-js/auth": "^0.4.1-beta.6",
"@mintbase-js/sdk": "^0.3.2-upgrade-packages-3378beb.0",
"graphql-request": "^5.0.0"
},
Expand Down
Loading