diff --git a/lib/api.d.ts b/lib/api.d.ts index 72dfa5c74..f8eff3c68 100644 --- a/lib/api.d.ts +++ b/lib/api.d.ts @@ -122,7 +122,7 @@ export declare function randomSeed(): Bytes; * @param amount - The amount of NEAR attached to the call. * @param gas - The amount of Gas attached to the call. */ -export declare function promiseCreate(accountId: Bytes, methodName: Bytes, args: Bytes, amount: NearAmount, gas: NearAmount): bigint; +export declare function promiseCreate(accountId: Bytes, methodName: Bytes, args: Bytes, amount: NearAmount, gas: NearAmount): PromiseIndex; /** * Attach a callback NEAR promise to be executed after a provided promise. * @@ -133,26 +133,26 @@ export declare function promiseCreate(accountId: Bytes, methodName: Bytes, args: * @param amount - The amount of NEAR to attach to the call. * @param gas - The amount of Gas to attach to the call. */ -export declare function promiseThen(promiseIndex: PromiseIndex, accountId: Bytes, methodName: Bytes, args: Bytes, amount: NearAmount, gas: NearAmount): bigint; +export declare function promiseThen(promiseIndex: PromiseIndex, accountId: Bytes, methodName: Bytes, args: Bytes, amount: NearAmount, gas: NearAmount): PromiseIndex; /** * Join an arbitrary array of NEAR promises. * * @param promiseIndexes - An arbitrary array of NEAR promise indexes to join. */ -export declare function promiseAnd(...promiseIndexes: PromiseIndex[]): bigint; +export declare function promiseAnd(...promiseIndexes: PromiseIndex[]): PromiseIndex; /** * Create a NEAR promise which will have multiple promise actions inside. * * @param accountId - The account ID of the target contract. */ -export declare function promiseBatchCreate(accountId: Bytes): bigint; +export declare function promiseBatchCreate(accountId: Bytes): PromiseIndex; /** * Attach a callback NEAR promise to a batch of NEAR promise actions. * * @param promiseIndex - The NEAR promise index of the batch. * @param accountId - The account ID of the target contract. */ -export declare function promiseBatchThen(promiseIndex: PromiseIndex, accountId: Bytes): bigint; +export declare function promiseBatchThen(promiseIndex: PromiseIndex, accountId: Bytes): PromiseIndex; /** * Attach a create account promise action to the NEAR promise index with the provided promise index. * diff --git a/lib/types/index.d.ts b/lib/types/index.d.ts index 91fc74895..db3de5647 100644 --- a/lib/types/index.d.ts +++ b/lib/types/index.d.ts @@ -1,9 +1,9 @@ import { AccountId } from "./account_id"; import { BlockHeight, EpochHeight, Balance, StorageUsage } from "./primitives"; -import { PromiseResult, PromiseError, PromiseIndex, ReceiptIndex, IteratorIndex } from "./vm_types"; +import { PromiseResult, PromiseError, ReceiptIndex, IteratorIndex } from "./vm_types"; import { Gas, ONE_TERA_GAS } from "./gas"; import { PublicKey, CurveType, curveTypeFromStr, ParsePublicKeyError, InvalidLengthError, Base58Error, UnknownCurve } from "./public_key"; -export { AccountId, BlockHeight, EpochHeight, Balance, StorageUsage, PromiseResult, PromiseError, PromiseIndex, ReceiptIndex, IteratorIndex, Gas, ONE_TERA_GAS, PublicKey, CurveType, curveTypeFromStr, ParsePublicKeyError, InvalidLengthError, Base58Error, UnknownCurve, }; +export { AccountId, BlockHeight, EpochHeight, Balance, StorageUsage, PromiseResult, PromiseError, ReceiptIndex, IteratorIndex, Gas, ONE_TERA_GAS, PublicKey, CurveType, curveTypeFromStr, ParsePublicKeyError, InvalidLengthError, Base58Error, UnknownCurve, }; /** * The amount of Gas Weight in integers - whole numbers. */ diff --git a/lib/types/vm_types.d.ts b/lib/types/vm_types.d.ts index a8bd7295e..25c50ac71 100644 --- a/lib/types/vm_types.d.ts +++ b/lib/types/vm_types.d.ts @@ -1,7 +1,3 @@ -/** - * The index for NEAR promises. - */ -export declare type PromiseIndex = bigint; /** * The index for NEAR receipts. */ diff --git a/lib/utils.d.ts b/lib/utils.d.ts index 3da90ed3f..21e2dd87d 100644 --- a/lib/utils.d.ts +++ b/lib/utils.d.ts @@ -3,10 +3,13 @@ import { GetOptions } from "./types/collections"; * A string containing byte characters. Can be safely used in NEAR calls. */ export declare type Bytes = string; +declare enum PromiseIndexBrand { + _ = -1 +} /** * A PromiseIndex which represents the ID of a NEAR Promise. */ -export declare type PromiseIndex = number | bigint; +export declare type PromiseIndex = (number | bigint) & PromiseIndexBrand; /** * A number that specifies the amount of NEAR in yoctoNEAR. */ @@ -47,3 +50,4 @@ export declare function deserialize(valueToDeserialize: string): unknown; * @param accountId - The Account ID string you want to validate. */ export declare function validateAccountId(accountId: string): boolean; +export {}; diff --git a/lib/utils.js b/lib/utils.js index ff946f8fe..c20240d1c 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,3 +1,8 @@ +// make PromiseIndex a nominal typing +var PromiseIndexBrand; +(function (PromiseIndexBrand) { + PromiseIndexBrand[PromiseIndexBrand["_"] = -1] = "_"; +})(PromiseIndexBrand || (PromiseIndexBrand = {})); const TYPE_KEY = "typeInfo"; var TypeBrand; (function (TypeBrand) { diff --git a/src/api.ts b/src/api.ts index ba06abc39..131b43a7e 100644 --- a/src/api.ts +++ b/src/api.ts @@ -74,60 +74,51 @@ interface Env { gas: NearAmount ): bigint; promise_then( - promiseIndex: PromiseIndex, + promiseIndex: bigint, accountId: Bytes, methodName: Bytes, args: Bytes, amount: NearAmount, gas: NearAmount ): bigint; - promise_and(...promiseIndexes: PromiseIndex[]): bigint; + promise_and(...promiseIndexes: bigint[]): bigint; promise_batch_create(accountId: Bytes): bigint; - promise_batch_then(promiseIndex: PromiseIndex, accountId: Bytes): bigint; - promise_batch_action_create_account(promiseIndex: PromiseIndex): void; - promise_batch_action_deploy_contract( - promiseIndex: PromiseIndex, - code: Bytes - ): void; + promise_batch_then(promiseIndex: bigint, accountId: Bytes): bigint; + promise_batch_action_create_account(promiseIndex: bigint): void; + promise_batch_action_deploy_contract(promiseIndex: bigint, code: Bytes): void; promise_batch_action_function_call( - promiseIndex: PromiseIndex, + promiseIndex: bigint, methodName: Bytes, args: Bytes, amount: NearAmount, gas: NearAmount ): void; - promise_batch_action_transfer( - promiseIndex: PromiseIndex, - amount: NearAmount - ): void; + promise_batch_action_transfer(promiseIndex: bigint, amount: NearAmount): void; promise_batch_action_stake( - promiseIndex: PromiseIndex, + promiseIndex: bigint, amount: NearAmount, publicKey: Bytes ): void; promise_batch_action_add_key_with_full_access( - promiseIndex: PromiseIndex, + promiseIndex: bigint, publicKey: Bytes, nonce: number | bigint ): void; promise_batch_action_add_key_with_function_call( - promiseIndex: PromiseIndex, + promiseIndex: bigint, publicKey: Bytes, nonce: number | bigint, allowance: NearAmount, receiverId: Bytes, methodNames: Bytes ): void; - promise_batch_action_delete_key( - promiseIndex: PromiseIndex, - publicKey: Bytes - ): void; + promise_batch_action_delete_key(promiseIndex: bigint, publicKey: Bytes): void; promise_batch_action_delete_account( - promiseIndex: PromiseIndex, + promiseIndex: bigint, beneficiaryId: Bytes ): void; promise_batch_action_function_call_weight( - promiseIndex: PromiseIndex, + promiseIndex: bigint, methodName: Bytes, args: Bytes, amount: NearAmount, @@ -135,8 +126,8 @@ interface Env { weight: GasWeight ): void; promise_results_count(): bigint; - promise_result(promiseIndex: PromiseIndex, register: Register): PromiseResult; - promise_return(promiseIndex: PromiseIndex): void; + promise_result(promiseIndex: bigint, register: Register): PromiseResult; + promise_return(promiseIndex: bigint): void; } declare const env: Env; @@ -367,8 +358,14 @@ export function promiseCreate( args: Bytes, amount: NearAmount, gas: NearAmount -): bigint { - return env.promise_create(accountId, methodName, args, amount, gas); +): PromiseIndex { + return env.promise_create( + accountId, + methodName, + args, + amount, + gas + ) as unknown as PromiseIndex; } /** @@ -388,15 +385,15 @@ export function promiseThen( args: Bytes, amount: NearAmount, gas: NearAmount -): bigint { +): PromiseIndex { return env.promise_then( - promiseIndex, + promiseIndex as unknown as bigint, accountId, methodName, args, amount, gas - ); + ) as unknown as PromiseIndex; } /** @@ -404,8 +401,10 @@ export function promiseThen( * * @param promiseIndexes - An arbitrary array of NEAR promise indexes to join. */ -export function promiseAnd(...promiseIndexes: PromiseIndex[]): bigint { - return env.promise_and(...promiseIndexes); +export function promiseAnd(...promiseIndexes: PromiseIndex[]): PromiseIndex { + return env.promise_and( + ...(promiseIndexes as unknown as bigint[]) + ) as unknown as PromiseIndex; } /** @@ -413,8 +412,8 @@ export function promiseAnd(...promiseIndexes: PromiseIndex[]): bigint { * * @param accountId - The account ID of the target contract. */ -export function promiseBatchCreate(accountId: Bytes): bigint { - return env.promise_batch_create(accountId); +export function promiseBatchCreate(accountId: Bytes): PromiseIndex { + return env.promise_batch_create(accountId) as unknown as PromiseIndex; } /** @@ -426,8 +425,11 @@ export function promiseBatchCreate(accountId: Bytes): bigint { export function promiseBatchThen( promiseIndex: PromiseIndex, accountId: Bytes -): bigint { - return env.promise_batch_then(promiseIndex, accountId); +): PromiseIndex { + return env.promise_batch_then( + promiseIndex as unknown as bigint, + accountId + ) as unknown as PromiseIndex; } /** @@ -438,7 +440,7 @@ export function promiseBatchThen( export function promiseBatchActionCreateAccount( promiseIndex: PromiseIndex ): void { - env.promise_batch_action_create_account(promiseIndex); + env.promise_batch_action_create_account(promiseIndex as unknown as bigint); } /** @@ -451,7 +453,10 @@ export function promiseBatchActionDeployContract( promiseIndex: PromiseIndex, code: Bytes ): void { - env.promise_batch_action_deploy_contract(promiseIndex, code); + env.promise_batch_action_deploy_contract( + promiseIndex as unknown as bigint, + code + ); } /** @@ -471,7 +476,7 @@ export function promiseBatchActionFunctionCall( gas: NearAmount ): void { env.promise_batch_action_function_call( - promiseIndex, + promiseIndex as unknown as bigint, methodName, args, amount, @@ -489,7 +494,7 @@ export function promiseBatchActionTransfer( promiseIndex: PromiseIndex, amount: NearAmount ): void { - env.promise_batch_action_transfer(promiseIndex, amount); + env.promise_batch_action_transfer(promiseIndex as unknown as bigint, amount); } /** @@ -504,7 +509,11 @@ export function promiseBatchActionStake( amount: NearAmount, publicKey: Bytes ): void { - env.promise_batch_action_stake(promiseIndex, amount, publicKey); + env.promise_batch_action_stake( + promiseIndex as unknown as bigint, + amount, + publicKey + ); } /** @@ -520,7 +529,7 @@ export function promiseBatchActionAddKeyWithFullAccess( nonce: number | bigint ): void { env.promise_batch_action_add_key_with_full_access( - promiseIndex, + promiseIndex as unknown as bigint, publicKey, nonce ); @@ -545,7 +554,7 @@ export function promiseBatchActionAddKeyWithFunctionCall( methodNames: Bytes ): void { env.promise_batch_action_add_key_with_function_call( - promiseIndex, + promiseIndex as unknown as bigint, publicKey, nonce, allowance, @@ -564,7 +573,10 @@ export function promiseBatchActionDeleteKey( promiseIndex: PromiseIndex, publicKey: Bytes ): void { - env.promise_batch_action_delete_key(promiseIndex, publicKey); + env.promise_batch_action_delete_key( + promiseIndex as unknown as bigint, + publicKey + ); } /** @@ -577,7 +589,10 @@ export function promiseBatchActionDeleteAccount( promiseIndex: PromiseIndex, beneficiaryId: Bytes ): void { - env.promise_batch_action_delete_account(promiseIndex, beneficiaryId); + env.promise_batch_action_delete_account( + promiseIndex as unknown as bigint, + beneficiaryId + ); } /** @@ -599,7 +614,7 @@ export function promiseBatchActionFunctionCallWeight( weight: GasWeight ): void { env.promise_batch_action_function_call_weight( - promiseIndex, + promiseIndex as unknown as bigint, methodName, args, amount, @@ -621,7 +636,7 @@ export function promiseResultsCount(): bigint { * @param promiseIndex - The index of the promise to return the result for. */ export function promiseResult(promiseIndex: PromiseIndex): Bytes { - const status = env.promise_result(promiseIndex, 0); + const status = env.promise_result(promiseIndex as unknown as bigint, 0); assert( Number(status) === PromiseResult.Successful, @@ -643,7 +658,7 @@ export function promiseResult(promiseIndex: PromiseIndex): Bytes { * @param promiseIndex - The index of the promise to execute. */ export function promiseReturn(promiseIndex: PromiseIndex): void { - env.promise_return(promiseIndex); + env.promise_return(promiseIndex as unknown as bigint); } export function sha256(value: Bytes): Bytes { diff --git a/src/types/index.ts b/src/types/index.ts index 7532cf062..907ee47c6 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -3,7 +3,6 @@ import { BlockHeight, EpochHeight, Balance, StorageUsage } from "./primitives"; import { PromiseResult, PromiseError, - PromiseIndex, ReceiptIndex, IteratorIndex, } from "./vm_types"; @@ -26,7 +25,6 @@ export { StorageUsage, PromiseResult, PromiseError, - PromiseIndex, ReceiptIndex, IteratorIndex, Gas, diff --git a/src/types/vm_types.ts b/src/types/vm_types.ts index dc4a2b51f..85d054c63 100644 --- a/src/types/vm_types.ts +++ b/src/types/vm_types.ts @@ -1,7 +1,3 @@ -/** - * The index for NEAR promises. - */ -export type PromiseIndex = bigint; /** * The index for NEAR receipts. */ diff --git a/src/utils.ts b/src/utils.ts index 616f31863..49256caad 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -4,10 +4,15 @@ import { GetOptions } from "./types/collections"; * A string containing byte characters. Can be safely used in NEAR calls. */ export type Bytes = string; + +// make PromiseIndex a nominal typing +enum PromiseIndexBrand { + _ = -1, +} /** * A PromiseIndex which represents the ID of a NEAR Promise. */ -export type PromiseIndex = number | bigint; +export type PromiseIndex = (number | bigint) & PromiseIndexBrand; /** * A number that specifies the amount of NEAR in yoctoNEAR. */