Skip to content

Commit

Permalink
feat: view function update for near js sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
gutsyphilip committed Jun 8, 2022
1 parent 46e7973 commit db0fe8a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 18 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.

24 changes: 16 additions & 8 deletions lib/account.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/transaction.d.ts

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

1 change: 1 addition & 0 deletions lib/transaction.js

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

26 changes: 18 additions & 8 deletions src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,11 @@ export class Account {
});
}

/** @hidden */
private encodeJSContractArgs(contractId: string, method: string, args) {
return Buffer.concat([Buffer.from(contractId), Buffer.from([0]), Buffer.from(method), Buffer.from([0]), Buffer.from(args)]);
}

async functionCall(props: FunctionCallOptions): Promise<FinalExecutionOutcome>;
/**
* @deprecated
Expand Down Expand Up @@ -438,10 +443,7 @@ export class Account {
let functionCallArgs;

if(jsContract){
function encodeCall(contractId: string, method: string, args) {
return Buffer.concat([Buffer.from(contractId), Buffer.from([0]), Buffer.from(method), Buffer.from([0]), Buffer.from(args)]);
}
const encodedArgs = encodeCall( contractId, methodName, JSON.stringify(Object.values(args)) );
const encodedArgs = this.encodeJSContractArgs( contractId, methodName, JSON.stringify(Object.values(args)) );
functionCallArgs = ['call_js_contract', encodedArgs, gas, attachedDeposit, null, true ];
} else{
const stringifyArg = stringify === undefined ? stringifyJsonOrBytes : stringify;
Expand Down Expand Up @@ -529,22 +531,30 @@ export class Account {
* @param args Any arguments to the view contract method, wrapped in JSON
* @param options.parse Parse the result of the call. Receives a Buffer (bytes array) and converts it to any object. By default result will be treated as json.
* @param options.stringify Convert input arguments into a bytes array. By default the input is treated as a JSON.
* @param options.jsContract Is contract from JS SDK, automatically encodes args from JS SDK to binary.
* @returns {Promise<any>}
*/
async viewFunction(
contractId: string,
methodName: string,
args: any = {},
{ parse = parseJsonFromRawResponse, stringify = bytesJsonStringify } = {}
{ parse = parseJsonFromRawResponse, /* */
stringify = bytesJsonStringify, jsContract=false } = {}
): Promise<any> {
let encodedArgs;
this.validateArgs(args);
const serializedArgs = stringify(args).toString('base64');

if(jsContract){
encodedArgs = this.encodeJSContractArgs( contractId, methodName, !args ? '' : JSON.stringify(Object.values(args)) );
} else{
encodedArgs = stringify(args).toString('base64');
}

const result = await this.connection.provider.query<CodeResult>({
request_type: 'call_function',
account_id: contractId,
account_id: jsContract ? this.connection.jsvmAccountId : contractId,
method_name: methodName,
args_base64: serializedArgs,
args_base64: encodedArgs,
finality: 'optimistic'
});

Expand Down
3 changes: 2 additions & 1 deletion src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ export function stringifyJsonOrBytes(args: any): Buffer {
* @param gas max amount of gas that method call can use
* @param deposit amount of NEAR (in yoctoNEAR) to send together with the call
* @param stringify Convert input arguments into bytes array.
* @param jsContract Is contract from JS SDK, skips stringification of arguments.
*/
export function functionCall(methodName: string, args: Uint8Array | object, gas: BN, deposit: BN, stringify = stringifyJsonOrBytes, jsContract = false): Action {
if(jsContract){
return new Action({ functionCall: new FunctionCall({ methodName, args, gas, deposit }) });
return new Action({ functionCall: new FunctionCall({ methodName, args, gas, deposit }) });
}
return new Action({ functionCall: new FunctionCall({ methodName, args: stringify(args), gas, deposit }) });
}
Expand Down

0 comments on commit db0fe8a

Please sign in to comment.