From b769d5f810fa079190a59483ae453e214fb4828d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petar=20Vujovi=C4=87?= Date: Mon, 19 Sep 2022 22:39:54 +0200 Subject: [PATCH] feat: Refactor high level promises Rename variables and parameters to JavaScript standards Refactor code --- lib/promise.d.ts | 102 ++++++++++---------- lib/promise.js | 196 +++++++++++++++++++------------------- src/promise.ts | 241 ++++++++++++++++++++++++----------------------- 3 files changed, 270 insertions(+), 269 deletions(-) diff --git a/lib/promise.d.ts b/lib/promise.d.ts index 936ac453..8b169b81 100644 --- a/lib/promise.d.ts +++ b/lib/promise.d.ts @@ -1,108 +1,108 @@ -import { Bytes } from "./utils"; +import { Bytes, PromiseIndex } from "./utils"; import { Balance, PublicKey, AccountId, Gas, GasWeight } from "./types"; import { Nonce } from "./types/primitives"; export declare abstract class PromiseAction { - abstract add(promise_index: number | bigint): void; + abstract add(promiseIndex: PromiseIndex): void; } export declare class CreateAccount extends PromiseAction { - add(promise_index: number | bigint): void; + add(promiseIndex: PromiseIndex): void; } export declare class DeployContract extends PromiseAction { code: Bytes; constructor(code: Bytes); - add(promise_index: number | bigint): void; + add(promiseIndex: PromiseIndex): void; } export declare class FunctionCall extends PromiseAction { - function_name: string; + functionName: string; args: Bytes; amount: Balance; gas: Gas; - constructor(function_name: string, args: Bytes, amount: Balance, gas: Gas); - add(promise_index: number | bigint): void; + constructor(functionName: string, args: Bytes, amount: Balance, gas: Gas); + add(promiseIndex: PromiseIndex): void; } export declare class FunctionCallWeight extends PromiseAction { - function_name: string; + functionName: string; args: Bytes; amount: Balance; gas: Gas; weight: GasWeight; - constructor(function_name: string, args: Bytes, amount: Balance, gas: Gas, weight: GasWeight); - add(promise_index: number | bigint): void; + constructor(functionName: string, args: Bytes, amount: Balance, gas: Gas, weight: GasWeight); + add(promiseIndex: PromiseIndex): void; } export declare class Transfer extends PromiseAction { amount: Balance; constructor(amount: Balance); - add(promise_index: number | bigint): void; + add(promiseIndex: PromiseIndex): void; } export declare class Stake extends PromiseAction { amount: Balance; - public_key: PublicKey; - constructor(amount: Balance, public_key: PublicKey); - add(promise_index: number | bigint): void; + publicKey: PublicKey; + constructor(amount: Balance, publicKey: PublicKey); + add(promiseIndex: PromiseIndex): void; } export declare class AddFullAccessKey extends PromiseAction { - public_key: PublicKey; + publicKey: PublicKey; nonce: Nonce; - constructor(public_key: PublicKey, nonce: Nonce); - add(promise_index: number | bigint): void; + constructor(publicKey: PublicKey, nonce: Nonce); + add(promiseIndex: PromiseIndex): void; } export declare class AddAccessKey extends PromiseAction { - public_key: PublicKey; + publicKey: PublicKey; allowance: Balance; - receiver_id: AccountId; - function_names: string; + receiverId: AccountId; + functionNames: string; nonce: Nonce; - constructor(public_key: PublicKey, allowance: Balance, receiver_id: AccountId, function_names: string, nonce: Nonce); - add(promise_index: number | bigint): void; + constructor(publicKey: PublicKey, allowance: Balance, receiverId: AccountId, functionNames: string, nonce: Nonce); + add(promiseIndex: PromiseIndex): void; } export declare class DeleteKey extends PromiseAction { - public_key: PublicKey; - constructor(public_key: PublicKey); - add(promise_index: number | bigint): void; + publicKey: PublicKey; + constructor(publicKey: PublicKey); + add(promiseIndex: PromiseIndex): void; } export declare class DeleteAccount extends PromiseAction { - beneficiary_id: AccountId; - constructor(beneficiary_id: AccountId); - add(promise_index: number | bigint): void; + beneficiaryId: AccountId; + constructor(beneficiaryId: AccountId); + add(promiseIndex: PromiseIndex): void; } declare class PromiseSingle { - account_id: AccountId; + accountId: AccountId; actions: PromiseAction[]; after: NearPromise | null; - promise_index: number | bigint | null; - constructor(account_id: AccountId, actions: PromiseAction[], after: NearPromise | null, promise_index: number | bigint | null); - constructRecursively(): number | bigint; + promiseIndex: PromiseIndex | null; + constructor(accountId: AccountId, actions: PromiseAction[], after: NearPromise | null, promiseIndex: PromiseIndex | null); + constructRecursively(): PromiseIndex; } export declare class PromiseJoint { - promise_a: NearPromise; - promise_b: NearPromise; - promise_index: number | bigint | null; - constructor(promise_a: NearPromise, promise_b: NearPromise, promise_index: number | bigint | null); - constructRecursively(): number | bigint; + promiseA: NearPromise; + promiseB: NearPromise; + promiseIndex: PromiseIndex | null; + constructor(promiseA: NearPromise, promiseB: NearPromise, promiseIndex: PromiseIndex | null); + constructRecursively(): PromiseIndex; } declare type PromiseSubtype = PromiseSingle | PromiseJoint; export declare class NearPromise { private subtype; - private should_return; - constructor(subtype: PromiseSubtype, should_return: boolean); - static new(account_id: AccountId): NearPromise; - private add_action; + private shouldReturn; + constructor(subtype: PromiseSubtype, shouldReturn: boolean); + static new(accountId: AccountId): NearPromise; + private addAction; createAccount(): NearPromise; deployContract(code: Bytes): NearPromise; - functionCall(function_name: string, args: Bytes, amount: Balance, gas: Gas): NearPromise; - functionCallWeight(function_name: string, args: Bytes, amount: Balance, gas: Gas, weight: GasWeight): NearPromise; + functionCall(functionName: string, args: Bytes, amount: Balance, gas: Gas): NearPromise; + functionCallWeight(functionName: string, args: Bytes, amount: Balance, gas: Gas, weight: GasWeight): NearPromise; transfer(amount: Balance): NearPromise; - stake(amount: Balance, public_key: PublicKey): NearPromise; - addFullAccessKey(public_key: PublicKey): NearPromise; - addFullAccessKeyWithNonce(public_key: PublicKey, nonce: Nonce): NearPromise; - addAccessKey(public_key: PublicKey, allowance: Balance, receiver_id: AccountId, method_names: string): NearPromise; - addAccessKeyWithNonce(public_key: PublicKey, allowance: Balance, receiver_id: AccountId, method_names: string, nonce: Nonce): NearPromise; - deleteKey(public_key: PublicKey): NearPromise; - deleteAccount(beneficiary_id: AccountId): NearPromise; + stake(amount: Balance, publicKey: PublicKey): NearPromise; + addFullAccessKey(publicKey: PublicKey): NearPromise; + addFullAccessKeyWithNonce(publicKey: PublicKey, nonce: Nonce): NearPromise; + addAccessKey(publicKey: PublicKey, allowance: Balance, receiverId: AccountId, methodNames: string): NearPromise; + addAccessKeyWithNonce(publicKey: PublicKey, allowance: Balance, receiverId: AccountId, methodNames: string, nonce: Nonce): NearPromise; + deleteKey(publicKey: PublicKey): NearPromise; + deleteAccount(beneficiaryId: AccountId): NearPromise; and(other: NearPromise): NearPromise; then(other: NearPromise): NearPromise; asReturn(): NearPromise; - constructRecursively(): number | bigint; + constructRecursively(): PromiseIndex; onReturn(): void; } export declare type PromiseOrValue = NearPromise | T; diff --git a/lib/promise.js b/lib/promise.js index 96a43e53..bdb289a4 100644 --- a/lib/promise.js +++ b/lib/promise.js @@ -2,8 +2,8 @@ import * as near from "./api"; export class PromiseAction { } export class CreateAccount extends PromiseAction { - add(promise_index) { - near.promiseBatchActionCreateAccount(promise_index); + add(promiseIndex) { + near.promiseBatchActionCreateAccount(promiseIndex); } } export class DeployContract extends PromiseAction { @@ -11,33 +11,33 @@ export class DeployContract extends PromiseAction { super(); this.code = code; } - add(promise_index) { - near.promiseBatchActionDeployContract(promise_index, this.code); + add(promiseIndex) { + near.promiseBatchActionDeployContract(promiseIndex, this.code); } } export class FunctionCall extends PromiseAction { - constructor(function_name, args, amount, gas) { + constructor(functionName, args, amount, gas) { super(); - this.function_name = function_name; + this.functionName = functionName; this.args = args; this.amount = amount; this.gas = gas; } - add(promise_index) { - near.promiseBatchActionFunctionCall(promise_index, this.function_name, this.args, this.amount, this.gas); + add(promiseIndex) { + near.promiseBatchActionFunctionCall(promiseIndex, this.functionName, this.args, this.amount, this.gas); } } export class FunctionCallWeight extends PromiseAction { - constructor(function_name, args, amount, gas, weight) { + constructor(functionName, args, amount, gas, weight) { super(); - this.function_name = function_name; + this.functionName = functionName; this.args = args; this.amount = amount; this.gas = gas; this.weight = weight; } - add(promise_index) { - near.promiseBatchActionFunctionCallWeight(promise_index, this.function_name, this.args, this.amount, this.gas, this.weight); + add(promiseIndex) { + near.promiseBatchActionFunctionCallWeight(promiseIndex, this.functionName, this.args, this.amount, this.gas, this.weight); } } export class Transfer extends PromiseAction { @@ -45,183 +45,177 @@ export class Transfer extends PromiseAction { super(); this.amount = amount; } - add(promise_index) { - near.promiseBatchActionTransfer(promise_index, this.amount); + add(promiseIndex) { + near.promiseBatchActionTransfer(promiseIndex, this.amount); } } export class Stake extends PromiseAction { - constructor(amount, public_key) { + constructor(amount, publicKey) { super(); this.amount = amount; - this.public_key = public_key; + this.publicKey = publicKey; } - add(promise_index) { - near.promiseBatchActionStake(promise_index, this.amount, this.public_key.data); + add(promiseIndex) { + near.promiseBatchActionStake(promiseIndex, this.amount, this.publicKey.data); } } export class AddFullAccessKey extends PromiseAction { - constructor(public_key, nonce) { + constructor(publicKey, nonce) { super(); - this.public_key = public_key; + this.publicKey = publicKey; this.nonce = nonce; } - add(promise_index) { - near.promiseBatchActionAddKeyWithFullAccess(promise_index, this.public_key.data, this.nonce); + add(promiseIndex) { + near.promiseBatchActionAddKeyWithFullAccess(promiseIndex, this.publicKey.data, this.nonce); } } export class AddAccessKey extends PromiseAction { - constructor(public_key, allowance, receiver_id, function_names, nonce) { + constructor(publicKey, allowance, receiverId, functionNames, nonce) { super(); - this.public_key = public_key; + this.publicKey = publicKey; this.allowance = allowance; - this.receiver_id = receiver_id; - this.function_names = function_names; + this.receiverId = receiverId; + this.functionNames = functionNames; this.nonce = nonce; } - add(promise_index) { - near.promiseBatchActionAddKeyWithFunctionCall(promise_index, this.public_key.data, this.nonce, this.allowance, this.receiver_id, this.function_names); + add(promiseIndex) { + near.promiseBatchActionAddKeyWithFunctionCall(promiseIndex, this.publicKey.data, this.nonce, this.allowance, this.receiverId, this.functionNames); } } export class DeleteKey extends PromiseAction { - constructor(public_key) { + constructor(publicKey) { super(); - this.public_key = public_key; + this.publicKey = publicKey; } - add(promise_index) { - near.promiseBatchActionDeleteKey(promise_index, this.public_key.data); + add(promiseIndex) { + near.promiseBatchActionDeleteKey(promiseIndex, this.publicKey.data); } } export class DeleteAccount extends PromiseAction { - constructor(beneficiary_id) { + constructor(beneficiaryId) { super(); - this.beneficiary_id = beneficiary_id; + this.beneficiaryId = beneficiaryId; } - add(promise_index) { - near.promiseBatchActionDeleteAccount(promise_index, this.beneficiary_id); + add(promiseIndex) { + near.promiseBatchActionDeleteAccount(promiseIndex, this.beneficiaryId); } } class PromiseSingle { - constructor(account_id, actions, after, promise_index) { - this.account_id = account_id; + constructor(accountId, actions, after, promiseIndex) { + this.accountId = accountId; this.actions = actions; this.after = after; - this.promise_index = promise_index; + this.promiseIndex = promiseIndex; } constructRecursively() { - if (this.promise_index !== null) { - return this.promise_index; + if (this.promiseIndex !== null) { + return this.promiseIndex; } - let promise_index; + let promiseIndex; if (this.after) { - promise_index = near.promiseBatchThen(this.after.constructRecursively(), this.account_id); + promiseIndex = near.promiseBatchThen(this.after.constructRecursively(), this.accountId); } else { - promise_index = near.promiseBatchCreate(this.account_id); + promiseIndex = near.promiseBatchCreate(this.accountId); } for (const action of this.actions) { - action.add(promise_index); + action.add(promiseIndex); } - this.promise_index = promise_index; - return promise_index; + this.promiseIndex = promiseIndex; + return promiseIndex; } } export class PromiseJoint { - constructor(promise_a, promise_b, promise_index) { - this.promise_a = promise_a; - this.promise_b = promise_b; - this.promise_index = promise_index; + constructor(promiseA, promiseB, promiseIndex) { + this.promiseA = promiseA; + this.promiseB = promiseB; + this.promiseIndex = promiseIndex; } constructRecursively() { - if (this.promise_index !== null) { - return this.promise_index; + if (this.promiseIndex !== null) { + return this.promiseIndex; } - const res = near.promiseAnd(BigInt(this.promise_a.constructRecursively()), BigInt(this.promise_b.constructRecursively())); - this.promise_index = res; - return res; + const result = near.promiseAnd(BigInt(this.promiseA.constructRecursively()), BigInt(this.promiseB.constructRecursively())); + this.promiseIndex = result; + return result; } } export class NearPromise { - constructor(subtype, should_return) { + constructor(subtype, shouldReturn) { this.subtype = subtype; - this.should_return = should_return; + this.shouldReturn = shouldReturn; } - static new(account_id) { - const subtype = new PromiseSingle(account_id, [], null, null); - const ret = new NearPromise(subtype, false); - return ret; + static new(accountId) { + const subtype = new PromiseSingle(accountId, [], null, null); + return new NearPromise(subtype, false); } - add_action(action) { + addAction(action) { if (this.subtype instanceof PromiseJoint) { throw new Error("Cannot add action to a joint promise."); } - else { - this.subtype.actions.push(action); - } + this.subtype.actions.push(action); return this; } createAccount() { - return this.add_action(new CreateAccount()); + return this.addAction(new CreateAccount()); } deployContract(code) { - return this.add_action(new DeployContract(code)); + return this.addAction(new DeployContract(code)); } - functionCall(function_name, args, amount, gas) { - return this.add_action(new FunctionCall(function_name, args, amount, gas)); + functionCall(functionName, args, amount, gas) { + return this.addAction(new FunctionCall(functionName, args, amount, gas)); } - functionCallWeight(function_name, args, amount, gas, weight) { - return this.add_action(new FunctionCallWeight(function_name, args, amount, gas, weight)); + functionCallWeight(functionName, args, amount, gas, weight) { + return this.addAction(new FunctionCallWeight(functionName, args, amount, gas, weight)); } transfer(amount) { - return this.add_action(new Transfer(amount)); + return this.addAction(new Transfer(amount)); } - stake(amount, public_key) { - return this.add_action(new Stake(amount, public_key)); + stake(amount, publicKey) { + return this.addAction(new Stake(amount, publicKey)); } - addFullAccessKey(public_key) { - return this.addFullAccessKeyWithNonce(public_key, 0n); + addFullAccessKey(publicKey) { + return this.addFullAccessKeyWithNonce(publicKey, 0n); } - addFullAccessKeyWithNonce(public_key, nonce) { - return this.add_action(new AddFullAccessKey(public_key, nonce)); + addFullAccessKeyWithNonce(publicKey, nonce) { + return this.addAction(new AddFullAccessKey(publicKey, nonce)); } - addAccessKey(public_key, allowance, receiver_id, method_names) { - return this.addAccessKeyWithNonce(public_key, allowance, receiver_id, method_names, 0n); + addAccessKey(publicKey, allowance, receiverId, methodNames) { + return this.addAccessKeyWithNonce(publicKey, allowance, receiverId, methodNames, 0n); } - addAccessKeyWithNonce(public_key, allowance, receiver_id, method_names, nonce) { - return this.add_action(new AddAccessKey(public_key, allowance, receiver_id, method_names, nonce)); + addAccessKeyWithNonce(publicKey, allowance, receiverId, methodNames, nonce) { + return this.addAction(new AddAccessKey(publicKey, allowance, receiverId, methodNames, nonce)); } - deleteKey(public_key) { - return this.add_action(new DeleteKey(public_key)); + deleteKey(publicKey) { + return this.addAction(new DeleteKey(publicKey)); } - deleteAccount(beneficiary_id) { - return this.add_action(new DeleteAccount(beneficiary_id)); + deleteAccount(beneficiaryId) { + return this.addAction(new DeleteAccount(beneficiaryId)); } and(other) { const subtype = new PromiseJoint(this, other, null); - const ret = new NearPromise(subtype, false); - return ret; + return new NearPromise(subtype, false); } then(other) { - if (other.subtype instanceof PromiseSingle) { - if (other.subtype.after !== null) { - throw new Error("Cannot callback promise which is already scheduled after another"); - } - other.subtype.after = this; - } - else { + if (!(other.subtype instanceof PromiseSingle)) { throw new Error("Cannot callback joint promise."); } + if (other.subtype.after !== null) { + throw new Error("Cannot callback promise which is already scheduled after another"); + } + other.subtype.after = this; return other; } asReturn() { - this.should_return = true; + this.shouldReturn = true; return this; } constructRecursively() { - const res = this.subtype.constructRecursively(); - if (this.should_return) { - near.promiseReturn(res); + const result = this.subtype.constructRecursively(); + if (this.shouldReturn) { + near.promiseReturn(result); } - return res; + return result; } // Called by NearBindgen, when return object is a NearPromise instance. onReturn() { diff --git a/src/promise.ts b/src/promise.ts index 897bdeeb..a9c1ae86 100644 --- a/src/promise.ts +++ b/src/promise.ts @@ -1,15 +1,15 @@ -import { Bytes } from "./utils"; +import { Bytes, PromiseIndex } from "./utils"; import * as near from "./api"; import { Balance, PublicKey, AccountId, Gas, GasWeight } from "./types"; import { Nonce } from "./types/primitives"; export abstract class PromiseAction { - abstract add(promise_index: number | bigint): void; + abstract add(promiseIndex: PromiseIndex): void; } export class CreateAccount extends PromiseAction { - add(promise_index: number | bigint) { - near.promiseBatchActionCreateAccount(promise_index); + add(promiseIndex: PromiseIndex) { + near.promiseBatchActionCreateAccount(promiseIndex); } } @@ -18,14 +18,14 @@ export class DeployContract extends PromiseAction { super(); } - add(promise_index: number | bigint) { - near.promiseBatchActionDeployContract(promise_index, this.code); + add(promiseIndex: PromiseIndex) { + near.promiseBatchActionDeployContract(promiseIndex, this.code); } } export class FunctionCall extends PromiseAction { constructor( - public function_name: string, + public functionName: string, public args: Bytes, public amount: Balance, public gas: Gas @@ -33,10 +33,10 @@ export class FunctionCall extends PromiseAction { super(); } - add(promise_index: number | bigint) { + add(promiseIndex: PromiseIndex) { near.promiseBatchActionFunctionCall( - promise_index, - this.function_name, + promiseIndex, + this.functionName, this.args, this.amount, this.gas @@ -46,7 +46,7 @@ export class FunctionCall extends PromiseAction { export class FunctionCallWeight extends PromiseAction { constructor( - public function_name: string, + public functionName: string, public args: Bytes, public amount: Balance, public gas: Gas, @@ -55,10 +55,10 @@ export class FunctionCallWeight extends PromiseAction { super(); } - add(promise_index: number | bigint) { + add(promiseIndex: PromiseIndex) { near.promiseBatchActionFunctionCallWeight( - promise_index, - this.function_name, + promiseIndex, + this.functionName, this.args, this.amount, this.gas, @@ -72,34 +72,34 @@ export class Transfer extends PromiseAction { super(); } - add(promise_index: number | bigint) { - near.promiseBatchActionTransfer(promise_index, this.amount); + add(promiseIndex: PromiseIndex) { + near.promiseBatchActionTransfer(promiseIndex, this.amount); } } export class Stake extends PromiseAction { - constructor(public amount: Balance, public public_key: PublicKey) { + constructor(public amount: Balance, public publicKey: PublicKey) { super(); } - add(promise_index: number | bigint) { + add(promiseIndex: PromiseIndex) { near.promiseBatchActionStake( - promise_index, + promiseIndex, this.amount, - this.public_key.data + this.publicKey.data ); } } export class AddFullAccessKey extends PromiseAction { - constructor(public public_key: PublicKey, public nonce: Nonce) { + constructor(public publicKey: PublicKey, public nonce: Nonce) { super(); } - add(promise_index: number | bigint) { + add(promiseIndex: PromiseIndex) { near.promiseBatchActionAddKeyWithFullAccess( - promise_index, - this.public_key.data, + promiseIndex, + this.publicKey.data, this.nonce ); } @@ -107,230 +107,237 @@ export class AddFullAccessKey extends PromiseAction { export class AddAccessKey extends PromiseAction { constructor( - public public_key: PublicKey, + public publicKey: PublicKey, public allowance: Balance, - public receiver_id: AccountId, - public function_names: string, + public receiverId: AccountId, + public functionNames: string, public nonce: Nonce ) { super(); } - add(promise_index: number | bigint) { + add(promiseIndex: PromiseIndex) { near.promiseBatchActionAddKeyWithFunctionCall( - promise_index, - this.public_key.data, + promiseIndex, + this.publicKey.data, this.nonce, this.allowance, - this.receiver_id, - this.function_names + this.receiverId, + this.functionNames ); } } export class DeleteKey extends PromiseAction { - constructor(public public_key: PublicKey) { + constructor(public publicKey: PublicKey) { super(); } - add(promise_index: number | bigint) { - near.promiseBatchActionDeleteKey(promise_index, this.public_key.data); + add(promiseIndex: PromiseIndex) { + near.promiseBatchActionDeleteKey(promiseIndex, this.publicKey.data); } } export class DeleteAccount extends PromiseAction { - constructor(public beneficiary_id: AccountId) { + constructor(public beneficiaryId: AccountId) { super(); } - add(promise_index: number | bigint) { - near.promiseBatchActionDeleteAccount(promise_index, this.beneficiary_id); + add(promiseIndex: PromiseIndex) { + near.promiseBatchActionDeleteAccount(promiseIndex, this.beneficiaryId); } } class PromiseSingle { constructor( - public account_id: AccountId, + public accountId: AccountId, public actions: PromiseAction[], public after: NearPromise | null, - public promise_index: number | bigint | null + public promiseIndex: PromiseIndex | null ) {} - constructRecursively(): number | bigint { - if (this.promise_index !== null) { - return this.promise_index; + constructRecursively(): PromiseIndex { + if (this.promiseIndex !== null) { + return this.promiseIndex; } - let promise_index; + + let promiseIndex: bigint; + if (this.after) { - promise_index = near.promiseBatchThen( + promiseIndex = near.promiseBatchThen( this.after.constructRecursively(), - this.account_id + this.accountId ); } else { - promise_index = near.promiseBatchCreate(this.account_id); + promiseIndex = near.promiseBatchCreate(this.accountId); } + for (const action of this.actions) { - action.add(promise_index); + action.add(promiseIndex); } - this.promise_index = promise_index; - return promise_index; + + this.promiseIndex = promiseIndex; + + return promiseIndex; } } export class PromiseJoint { constructor( - public promise_a: NearPromise, - public promise_b: NearPromise, - public promise_index: number | bigint | null + public promiseA: NearPromise, + public promiseB: NearPromise, + public promiseIndex: PromiseIndex | null ) {} - constructRecursively(): number | bigint { - if (this.promise_index !== null) { - return this.promise_index; + constructRecursively(): PromiseIndex { + if (this.promiseIndex !== null) { + return this.promiseIndex; } - const res = near.promiseAnd( - BigInt(this.promise_a.constructRecursively()), - BigInt(this.promise_b.constructRecursively()) + + const result = near.promiseAnd( + BigInt(this.promiseA.constructRecursively()), + BigInt(this.promiseB.constructRecursively()) ); - this.promise_index = res; - return res; + this.promiseIndex = result; + + return result; } } type PromiseSubtype = PromiseSingle | PromiseJoint; export class NearPromise { - constructor( - private subtype: PromiseSubtype, - private should_return: boolean - ) {} + constructor(private subtype: PromiseSubtype, private shouldReturn: boolean) {} - static new(account_id: AccountId): NearPromise { - const subtype = new PromiseSingle(account_id, [], null, null); - const ret = new NearPromise(subtype, false); - return ret; + static new(accountId: AccountId): NearPromise { + const subtype = new PromiseSingle(accountId, [], null, null); + return new NearPromise(subtype, false); } - private add_action(action: PromiseAction): NearPromise { + private addAction(action: PromiseAction): NearPromise { if (this.subtype instanceof PromiseJoint) { throw new Error("Cannot add action to a joint promise."); - } else { - this.subtype.actions.push(action); } + + this.subtype.actions.push(action); + return this; } createAccount(): NearPromise { - return this.add_action(new CreateAccount()); + return this.addAction(new CreateAccount()); } deployContract(code: Bytes): NearPromise { - return this.add_action(new DeployContract(code)); + return this.addAction(new DeployContract(code)); } functionCall( - function_name: string, + functionName: string, args: Bytes, amount: Balance, gas: Gas ): NearPromise { - return this.add_action(new FunctionCall(function_name, args, amount, gas)); + return this.addAction(new FunctionCall(functionName, args, amount, gas)); } functionCallWeight( - function_name: string, + functionName: string, args: Bytes, amount: Balance, gas: Gas, weight: GasWeight ): NearPromise { - return this.add_action( - new FunctionCallWeight(function_name, args, amount, gas, weight) + return this.addAction( + new FunctionCallWeight(functionName, args, amount, gas, weight) ); } transfer(amount: Balance): NearPromise { - return this.add_action(new Transfer(amount)); + return this.addAction(new Transfer(amount)); } - stake(amount: Balance, public_key: PublicKey): NearPromise { - return this.add_action(new Stake(amount, public_key)); + stake(amount: Balance, publicKey: PublicKey): NearPromise { + return this.addAction(new Stake(amount, publicKey)); } - addFullAccessKey(public_key: PublicKey): NearPromise { - return this.addFullAccessKeyWithNonce(public_key, 0n); + addFullAccessKey(publicKey: PublicKey): NearPromise { + return this.addFullAccessKeyWithNonce(publicKey, 0n); } - addFullAccessKeyWithNonce(public_key: PublicKey, nonce: Nonce): NearPromise { - return this.add_action(new AddFullAccessKey(public_key, nonce)); + addFullAccessKeyWithNonce(publicKey: PublicKey, nonce: Nonce): NearPromise { + return this.addAction(new AddFullAccessKey(publicKey, nonce)); } addAccessKey( - public_key: PublicKey, + publicKey: PublicKey, allowance: Balance, - receiver_id: AccountId, - method_names: string + receiverId: AccountId, + methodNames: string ): NearPromise { return this.addAccessKeyWithNonce( - public_key, + publicKey, allowance, - receiver_id, - method_names, + receiverId, + methodNames, 0n ); } addAccessKeyWithNonce( - public_key: PublicKey, + publicKey: PublicKey, allowance: Balance, - receiver_id: AccountId, - method_names: string, + receiverId: AccountId, + methodNames: string, nonce: Nonce ): NearPromise { - return this.add_action( - new AddAccessKey(public_key, allowance, receiver_id, method_names, nonce) + return this.addAction( + new AddAccessKey(publicKey, allowance, receiverId, methodNames, nonce) ); } - deleteKey(public_key: PublicKey): NearPromise { - return this.add_action(new DeleteKey(public_key)); + deleteKey(publicKey: PublicKey): NearPromise { + return this.addAction(new DeleteKey(publicKey)); } - deleteAccount(beneficiary_id: AccountId): NearPromise { - return this.add_action(new DeleteAccount(beneficiary_id)); + deleteAccount(beneficiaryId: AccountId): NearPromise { + return this.addAction(new DeleteAccount(beneficiaryId)); } and(other: NearPromise): NearPromise { const subtype = new PromiseJoint(this, other, null); - const ret = new NearPromise(subtype, false); - return ret; + return new NearPromise(subtype, false); } then(other: NearPromise): NearPromise { - if (other.subtype instanceof PromiseSingle) { - if (other.subtype.after !== null) { - throw new Error( - "Cannot callback promise which is already scheduled after another" - ); - } - other.subtype.after = this; - } else { + if (!(other.subtype instanceof PromiseSingle)) { throw new Error("Cannot callback joint promise."); } + + if (other.subtype.after !== null) { + throw new Error( + "Cannot callback promise which is already scheduled after another" + ); + } + + other.subtype.after = this; + return other; } asReturn(): NearPromise { - this.should_return = true; + this.shouldReturn = true; return this; } - constructRecursively(): number | bigint { - const res = this.subtype.constructRecursively(); - if (this.should_return) { - near.promiseReturn(res); + constructRecursively(): PromiseIndex { + const result = this.subtype.constructRecursively(); + + if (this.shouldReturn) { + near.promiseReturn(result); } - return res; + + return result; } // Called by NearBindgen, when return object is a NearPromise instance.