-
Notifications
You must be signed in to change notification settings - Fork 24
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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 | ||||||
|
||||||
import { accountsByPublicKey } from '@mintbase-js/data' | ||||||
|
||||||
const { data, error } = await accountsByPublicKey('ed25519:12345...'); | ||||||
|
||||||
if (error) {console.log('error', error)} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ubernit:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 }>; | ||
} |
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'); | ||
}); | ||
}); |
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; | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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)