Skip to content

Commit

Permalink
Merge pull request #429 from near/allow-borsh-deserialization
Browse files Browse the repository at this point in the history
feat: enable view calls to deserialize with borsh
  • Loading branch information
chadoh authored Oct 21, 2020
2 parents ca5bdb4 + 7c2accb commit 79a3369
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 98 deletions.
6 changes: 5 additions & 1 deletion lib/account.d.ts

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

11 changes: 7 additions & 4 deletions lib/account.js

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

4 changes: 2 additions & 2 deletions lib/browser-index.js

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

2 changes: 1 addition & 1 deletion lib/common-index.js

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

6 changes: 3 additions & 3 deletions lib/contract.js

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

4 changes: 2 additions & 2 deletions lib/index.js

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

2 changes: 1 addition & 1 deletion lib/utils/index.js

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

4 changes: 2 additions & 2 deletions lib/utils/rpc_errors.js

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

145 changes: 74 additions & 71 deletions lib/utils/serialize.js

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

21 changes: 15 additions & 6 deletions src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ interface ReceiptLogWithFailure {
failure: ServerError;
}

function parseJsonFromRawResponse (response: Uint8Array): any {
return JSON.parse(Buffer.from(response).toString());
}

/**
* More information on [the Account spec](https://nomicon.io/DataStructures/Account.html)
*/
Expand Down Expand Up @@ -112,7 +116,7 @@ export class Account {
}
}

private printLogs(contractId: string, logs: string[], prefix: string = '') {
private printLogs(contractId: string, logs: string[], prefix = '') {
for (const log of logs) {
console.log(`${prefix}Log [${contractId}]: ${log}`);
}
Expand Down Expand Up @@ -182,7 +186,7 @@ export class Account {
}
});
if (!result) {
throw new TypedError(`nonce retries exceeded for transaction. This usually means there are too many parallel requests with the same access key.`, 'RetriesExceeded');
throw new TypedError('nonce retries exceeded for transaction. This usually means there are too many parallel requests with the same access key.', 'RetriesExceeded');
}

const flatLogs = [result.transaction_outcome, ...result.receipts_outcome].reduce((acc, it) => {
Expand Down Expand Up @@ -211,9 +215,9 @@ export class Account {
return result;
}

accessKeyByPublicKeyCache: { [key: string] : AccessKey } = {}
accessKeyByPublicKeyCache: { [key: string]: AccessKey } = {}

private async findAccessKey(receiverId: string, actions: Action[]): Promise<{publicKey: PublicKey, accessKey: AccessKey}> {
private async findAccessKey(receiverId: string, actions: Action[]): Promise<{publicKey: PublicKey; accessKey: AccessKey}> {
// TODO: Find matching access key based on transaction
const publicKey = await this.connection.signer.getPublicKey(this.accountId, this.connection.networkId);
if (!publicKey) {
Expand Down Expand Up @@ -354,14 +358,19 @@ export class Account {
* @param args Any arguments to the view contract method, wrapped in JSON
* @returns {Promise<any>}
*/
async viewFunction(contractId: string, methodName: string, args: any): Promise<any> {
async viewFunction(
contractId: string,
methodName: string,
args: any,
{ parse = parseJsonFromRawResponse } = {}
): Promise<any> {
args = args || {};
this.validateArgs(args);
const result = await this.connection.provider.query(`call/${contractId}/${methodName}`, base_encode(JSON.stringify(args)));
if (result.logs) {
this.printLogs(contractId, result.logs);
}
return result.result && result.result.length > 0 && JSON.parse(Buffer.from(result.result).toString());
return result.result && result.result.length > 0 && parse(Buffer.from(result.result));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export class Contract {
Object.defineProperty(this, methodName, {
writable: false,
enumerable: true,
value: nameFunction(methodName, async (args: object = {}, ...ignored) => {
if (ignored.length || !(isObject(args) || isUint8Array(args))) {
value: nameFunction(methodName, async (args: object = {}, options = {}, ...ignored) => {
if (ignored.length || !(isObject(args) || isUint8Array(args)) || !isObject(options)) {
throw new PositionalArgsError();
}
return this.account.viewFunction(this.contractId, methodName, args);
return this.account.viewFunction(this.contractId, methodName, args, options);
})
});
});
Expand Down
Loading

0 comments on commit 79a3369

Please sign in to comment.