Skip to content

Commit

Permalink
Merge pull request #870 from near/WAL-338-abstractions-if-using-js-sdk
Browse files Browse the repository at this point in the history
Wal 338 abstractions if using js sdk
  • Loading branch information
gutsyphilip authored Jun 8, 2022
2 parents 6f83d39 + 0260347 commit c158218
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 14 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.

20 changes: 16 additions & 4 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/connection.d.ts

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

4 changes: 4 additions & 0 deletions lib/near.d.ts

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

3 changes: 2 additions & 1 deletion lib/near.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.

26 changes: 22 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 from JS SDK to binary.
*/
jsContract?: boolean;
}

interface ReceiptLogWithFailure {
Expand Down Expand Up @@ -428,12 +433,25 @@ 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(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)) );
functionCallArgs = ['call_js_contract', encodedArgs, gas, attachedDeposit, 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 ? this.connection.jsvmAccountId: contractId,
// eslint-disable-next-line prefer-spread
actions: [functionCall.apply(void 0, functionCallArgs)],
walletMeta,
walletCallbackUrl
});
Expand Down
1 change: 1 addition & 0 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class Connection {
readonly networkId: string;
readonly provider: Provider;
readonly signer: Signer;
readonly jsvmAccountId: string;

constructor(networkId: string, provider: Provider, signer: Signer) {
this.networkId = networkId;
Expand Down
9 changes: 8 additions & 1 deletion src/near.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ export interface NearConfig {
* @see {@link https://docs.near.org/docs/tools/near-wallet}
*/
walletUrl?: string;

/**
* JVSM account ID for NEAR JS SDK
*/
jsvmAccountId?: string;
}

/**
Expand All @@ -85,8 +90,10 @@ export class Near {
this.connection = Connection.fromConfig({
networkId: config.networkId,
provider: { type: 'JsonRpcProvider', args: { url: config.nodeUrl, headers: config.headers } },
signer: config.signer || { type: 'InMemorySigner', keyStore: config.keyStore || (config.deps && config.deps.keyStore) }
signer: config.signer || { type: 'InMemorySigner', keyStore: config.keyStore || (config.deps && config.deps.keyStore) },
jsvmAccountId: config.jsvmAccountId || `jsvm.${config.networkId}`
});

if (config.masterAccount) {
// TODO: figure out better way of specifiying initial balance.
// Hardcoded number below must be enough to pay the gas cost to dev-deploy with near-shell for multiple times
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({ methodName, args, gas, deposit }) });
}
return new Action({ functionCall: new FunctionCall({ methodName, args: stringify(args), gas, deposit }) });
}

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

0 comments on commit c158218

Please sign in to comment.