diff --git a/lib/account.d.ts b/lib/account.d.ts index 93c08933fe..c0e3f3f481 100644 --- a/lib/account.d.ts +++ b/lib/account.d.ts @@ -3,6 +3,7 @@ import { AccessKey, Action, SignedTransaction } from './transaction'; import { FinalExecutionOutcome } from './providers'; import { Connection } from './connection'; import { PublicKey } from './utils/key_pair'; +import { Finality } from './providers/provider'; export interface AccountState { amount: string; code_hash: string; @@ -118,6 +119,7 @@ export declare class Account { viewFunction(contractId: string, methodName: string, args: any, { parse }?: { parse?: typeof parseJsonFromRawResponse; }): Promise; + viewState(prefix: string | Uint8Array, finality?: Finality): Promise; /** * @returns array of {access_key: AccessKey, public_key: PublicKey} items. */ diff --git a/lib/account.js b/lib/account.js index cf2a2c3075..65bff6026c 100644 --- a/lib/account.js +++ b/lib/account.js @@ -288,6 +288,15 @@ class Account { } return result.result && result.result.length > 0 && parse(Buffer.from(result.result)); } + async viewState(prefix, finality = 'optimistic') { + const { values } = await this.connection.provider.query({ + request_type: 'view_state', + finality, + account_id: this.accountId, + prefix_base64: Buffer.from(prefix).toString('base64') + }); + return values.map(({ key, value }) => ({ key: Buffer.from(key, 'base64'), value: Buffer.from(value, 'base64') })); + } /** * @returns array of {access_key: AccessKey, public_key: PublicKey} items. */ diff --git a/lib/providers/json-rpc-provider.d.ts b/lib/providers/json-rpc-provider.d.ts index 43b4060c9a..ba60265a71 100644 --- a/lib/providers/json-rpc-provider.d.ts +++ b/lib/providers/json-rpc-provider.d.ts @@ -35,10 +35,8 @@ export declare class JsonRpcProvider extends Provider { txStatus(txHash: Uint8Array, accountId: string): Promise; /** * Query the RPC as [shown in the docs](https://docs.nearprotocol.com/docs/interaction/rpc#query) - * @param path Path parameter for the RPC (ex. "contract/my_token") - * @param data Data parameter (ex. "", "AQ4", or whatever is needed) */ - query(path: string, data: string): Promise; + query(...args: any[]): Promise; /** * Query for block info from the RPC * See [docs for more info](https://docs.nearprotocol.com/docs/interaction/rpc#block) diff --git a/lib/providers/json-rpc-provider.js b/lib/providers/json-rpc-provider.js index d15dc3b055..22c4fcb8df 100644 --- a/lib/providers/json-rpc-provider.js +++ b/lib/providers/json-rpc-provider.js @@ -59,13 +59,18 @@ class JsonRpcProvider extends provider_1.Provider { } /** * Query the RPC as [shown in the docs](https://docs.nearprotocol.com/docs/interaction/rpc#query) - * @param path Path parameter for the RPC (ex. "contract/my_token") - * @param data Data parameter (ex. "", "AQ4", or whatever is needed) */ - async query(path, data) { - const result = await this.sendJsonRpc('query', [path, data]); + async query(...args) { + let result; + if (args.length === 1) { + result = await this.sendJsonRpc('query', args[0]); + } + else { + const [path, data] = args; + result = await this.sendJsonRpc('query', [path, data]); + } if (result && result.error) { - throw new Error(`Querying ${path} failed: ${result.error}.\n${JSON.stringify(result, null, 2)}`); + throw new Error(`Querying ${args} failed: ${result.error}.\n${JSON.stringify(result, null, 2)}`); } return result; } diff --git a/lib/providers/provider.d.ts b/lib/providers/provider.d.ts index 05216d3eb9..d14151883b 100644 --- a/lib/providers/provider.d.ts +++ b/lib/providers/provider.d.ts @@ -198,6 +198,7 @@ export declare abstract class Provider { abstract status(): Promise; abstract sendTransaction(signedTransaction: SignedTransaction): Promise; abstract txStatus(txHash: Uint8Array, accountId: string): Promise; + abstract query(params: object): Promise; abstract query(path: string, data: string): Promise; abstract block(blockId: BlockId): Promise; abstract chunk(chunkId: ChunkId): Promise; diff --git a/src/account.ts b/src/account.ts index 315dd677a0..c28779bdfb 100644 --- a/src/account.ts +++ b/src/account.ts @@ -26,6 +26,7 @@ import { parseRpcError } from './utils/rpc_errors'; import { ServerError } from './generated/rpc_error_types'; import exponentialBackoff from './utils/exponential-backoff'; +import { Finality } from './providers/provider'; // Default amount of gas to be sent with the function calls. Used to pay for the fees // incurred while running the contract execution. The unused amount will be refunded back to @@ -373,6 +374,17 @@ export class Account { return result.result && result.result.length > 0 && parse(Buffer.from(result.result)); } + async viewState(prefix: string | Uint8Array, finality: Finality = 'optimistic') { + const { values } = await this.connection.provider.query({ + request_type: 'view_state', + finality, + account_id: this.accountId, + prefix_base64: Buffer.from(prefix,).toString('base64') + }); + + return values.map(({key, value}) => ({ key: Buffer.from(key, 'base64'), value: Buffer.from(value, 'base64')})) + } + /** * @returns array of {access_key: AccessKey, public_key: PublicKey} items. */ diff --git a/src/providers/json-rpc-provider.ts b/src/providers/json-rpc-provider.ts index b4e954af33..82bb83d902 100644 --- a/src/providers/json-rpc-provider.ts +++ b/src/providers/json-rpc-provider.ts @@ -68,13 +68,17 @@ export class JsonRpcProvider extends Provider { /** * Query the RPC as [shown in the docs](https://docs.nearprotocol.com/docs/interaction/rpc#query) - * @param path Path parameter for the RPC (ex. "contract/my_token") - * @param data Data parameter (ex. "", "AQ4", or whatever is needed) */ - async query(path: string, data: string): Promise { - const result = await this.sendJsonRpc('query', [path, data]); + async query(...args: any[]): Promise { + let result + if (args.length === 1) { + result = await this.sendJsonRpc('query', args[0]); + } else { + const [path, data] = args; + result = await this.sendJsonRpc('query', [path, data]); + } if (result && result.error) { - throw new Error(`Querying ${path} failed: ${result.error}.\n${JSON.stringify(result, null, 2)}`); + throw new Error(`Querying ${args} failed: ${result.error}.\n${JSON.stringify(result, null, 2)}`); } return result; } diff --git a/src/providers/provider.ts b/src/providers/provider.ts index 55293a8859..610d68d137 100644 --- a/src/providers/provider.ts +++ b/src/providers/provider.ts @@ -26,6 +26,7 @@ type BlockHash = string; type BlockHeight = number; export type BlockId = BlockHash | BlockHeight; +// TODO: Remove near-final? export type Finality = 'optimistic' | 'near-final' | 'final' export enum ExecutionStatusBasic { @@ -241,6 +242,7 @@ export abstract class Provider { abstract async sendTransaction(signedTransaction: SignedTransaction): Promise; abstract async txStatus(txHash: Uint8Array, accountId: string): Promise; + abstract async query(params: object): Promise; abstract async query(path: string, data: string): Promise; abstract async block(blockId: BlockId): Promise; abstract async chunk(chunkId: ChunkId): Promise;