Skip to content

Commit

Permalink
feat: initial pass at supporting JS contract
Browse files Browse the repository at this point in the history
  • Loading branch information
gutsyphilip committed Jun 3, 2022
1 parent 6f83d39 commit 58396df
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 12 deletions.
4 changes: 4 additions & 0 deletions lib/account.d.ts

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

19 changes: 15 additions & 4 deletions 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.

25 changes: 21 additions & 4 deletions src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ export interface FunctionCallOptions {
* Convert input arguments into bytes array.
*/
stringify?: (input: any) => Buffer;

/**
* Is contract from JS SDK, automatically encodes args
*/
jsContract?: boolean;
}

interface ReceiptLogWithFailure {
Expand Down Expand Up @@ -428,12 +433,24 @@ export class Account {
});
}

private functionCallV2({ contractId, methodName, args = {}, gas = DEFAULT_FUNCTION_CALL_GAS, attachedDeposit, walletMeta, walletCallbackUrl, stringify }: FunctionCallOptions): Promise<FinalExecutionOutcome> {
private functionCallV2({ contractId, methodName, args = {}, gas = DEFAULT_FUNCTION_CALL_GAS, attachedDeposit, walletMeta, walletCallbackUrl, stringify, jsContract }: FunctionCallOptions): Promise<FinalExecutionOutcome> {
this.validateArgs(args);
const stringifyArg = stringify === undefined ? stringifyJsonOrBytes : stringify;
let functionCallArgs;

if(jsContract){
function encodeCall(contract:string, method:string, args) {
return Buffer.concat([Buffer.from(contract), Buffer.from([0]), Buffer.from(method), Buffer.from([0]), Buffer.from(args)])
}
let encodedArgs = encodeCall( contractId, methodName, JSON.stringify(Object.values(args)) );
functionCallArgs= ['call_js_contract', encodedArgs, gas, null, null, true ]
} else{
const stringifyArg = stringify === undefined ? stringifyJsonOrBytes : stringify;
functionCallArgs = [methodName, args, gas, attachedDeposit, stringifyArg, false]
}

return this.signAndSendTransaction({
receiverId: contractId,
actions: [functionCall(methodName, args, gas, attachedDeposit, stringifyArg)],
receiverId: jsContract ? 'call_js_contract' : contractId,
actions: [functionCall.apply(void 0, functionCallArgs)],
walletMeta,
walletCallbackUrl
});
Expand Down
7 changes: 5 additions & 2 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ export function stringifyJsonOrBytes(args: any): Buffer {
* @param deposit amount of NEAR (in yoctoNEAR) to send together with the call
* @param stringify Convert input arguments into bytes array.
*/
export function functionCall(methodName: string, args: Uint8Array | object, gas: BN, deposit: BN, stringify = stringifyJsonOrBytes): Action {
return new Action({functionCall: new FunctionCall({methodName, args: stringify(args), gas, deposit }) });
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({ contractId: "jsvm.testnet", methodName, args, gas }) });
}
return new Action({ functionCall: new FunctionCall({ methodName, args: stringify(args), gas, deposit }) });
}

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

0 comments on commit 58396df

Please sign in to comment.