Skip to content

Commit

Permalink
Don't serialize args in account.ts
Browse files Browse the repository at this point in the history
Convert args in transaction.functionCall
  • Loading branch information
vgrichina committed Aug 3, 2020
1 parent f859641 commit 4c568fb
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
6 changes: 5 additions & 1 deletion lib/account.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/transaction.d.ts

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

5 changes: 4 additions & 1 deletion lib/transaction.js

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

7 changes: 6 additions & 1 deletion src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export class Account {
async functionCall(contractId: string, methodName: string, args: any, gas?: BN, amount?: BN): Promise<FinalExecutionOutcome> {
args = args || {};
this.validateArgs(args);
return this.signAndSendTransaction(contractId, [functionCall(methodName, Buffer.from(JSON.stringify(args)), gas || DEFAULT_FUNC_CALL_GAS, amount)]);
return this.signAndSendTransaction(contractId, [functionCall(methodName, args, gas || DEFAULT_FUNC_CALL_GAS, amount)]);
}

/**
Expand Down Expand Up @@ -316,6 +316,11 @@ export class Account {
}

private validateArgs(args: any) {
const isUint8Array = args.byteLength !== undefined && args.byteLength === args.length;
if (isUint8Array) {
return;
}

if (Array.isArray(args) || typeof args !== 'object') {
throw new PositionalArgsError();
}
Expand Down
7 changes: 5 additions & 2 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ export function deployContract(code: Uint8Array): Action {
return new Action({ deployContract: new DeployContract({code}) });
}

export function functionCall(methodName: string, args: Uint8Array, gas: BN, deposit: BN): Action {
return new Action({functionCall: new FunctionCall({methodName, args, gas, deposit }) });
export function functionCall(methodName: string, args: Uint8Array | object, gas: BN, deposit: BN): Action {
const anyArgs = args as any;
const isUint8Array = anyArgs.byteLength !== undefined && anyArgs.byteLength === anyArgs.length;
const serializedArgs = isUint8Array ? args : Buffer.from(JSON.stringify(args));
return new Action({functionCall: new FunctionCall({methodName, args: serializedArgs, gas, deposit }) });
}

export function transfer(deposit: BN): Action {
Expand Down

0 comments on commit 4c568fb

Please sign in to comment.