Skip to content

Commit

Permalink
Prototype account.viewState
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrichina committed Nov 11, 2020
1 parent cc30113 commit f7ad042
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 13 deletions.
2 changes: 2 additions & 0 deletions lib/account.d.ts

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

9 changes: 9 additions & 0 deletions lib/account.js

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

4 changes: 1 addition & 3 deletions lib/providers/json-rpc-provider.d.ts

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

15 changes: 10 additions & 5 deletions lib/providers/json-rpc-provider.js

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

1 change: 1 addition & 0 deletions lib/providers/provider.d.ts

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

12 changes: 12 additions & 0 deletions src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*/
Expand Down
14 changes: 9 additions & 5 deletions src/providers/json-rpc-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {
const result = await this.sendJsonRpc('query', [path, data]);
async query(...args: any[]): Promise<any> {
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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -241,6 +242,7 @@ export abstract class Provider {

abstract async sendTransaction(signedTransaction: SignedTransaction): Promise<FinalExecutionOutcome>;
abstract async txStatus(txHash: Uint8Array, accountId: string): Promise<FinalExecutionOutcome>;
abstract async query(params: object): Promise<any>;
abstract async query(path: string, data: string): Promise<any>;
abstract async block(blockId: BlockId): Promise<BlockResult>;
abstract async chunk(chunkId: ChunkId): Promise<ChunkResult>;
Expand Down

0 comments on commit f7ad042

Please sign in to comment.