From c49abf5b2cac322b3f5d817b1f04e6344a08aac8 Mon Sep 17 00:00:00 2001 From: Bo Yao Date: Wed, 12 Oct 2022 12:03:47 +0800 Subject: [PATCH] Make PromiseIndex a nominal typing, require explicit construction --- lib/api.d.ts | 10 +++++----- lib/types/index.d.ts | 4 ++-- lib/types/vm_types.d.ts | 4 ---- lib/utils.d.ts | 6 +++++- lib/utils.js | 6 ++++++ src/api.ts | 20 ++++++++++---------- src/promise.ts | 4 ++-- src/types/index.ts | 2 -- src/types/vm_types.ts | 4 ---- src/utils.ts | 5 ++++- 10 files changed, 34 insertions(+), 31 deletions(-) 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..e7bbb937c 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 { + _ = "" +} /** * 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..58414f429 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,3 +1,9 @@ +// make PromiseIndex a nominal typing +var PromiseIndexBrand; +(function (PromiseIndexBrand) { + PromiseIndexBrand["_"] = ""; +})(PromiseIndexBrand || (PromiseIndexBrand = {})); +; const TYPE_KEY = "typeInfo"; var TypeBrand; (function (TypeBrand) { diff --git a/src/api.ts b/src/api.ts index ba06abc39..efb5b8e7a 100644 --- a/src/api.ts +++ b/src/api.ts @@ -367,8 +367,8 @@ 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 PromiseIndex; } /** @@ -388,7 +388,7 @@ export function promiseThen( args: Bytes, amount: NearAmount, gas: NearAmount -): bigint { +): PromiseIndex { return env.promise_then( promiseIndex, accountId, @@ -396,7 +396,7 @@ export function promiseThen( args, amount, gas - ); + ) as PromiseIndex; } /** @@ -404,8 +404,8 @@ 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 PromiseIndex; } /** @@ -413,8 +413,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 PromiseIndex; } /** @@ -426,8 +426,8 @@ 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, accountId) as PromiseIndex; } /** diff --git a/src/promise.ts b/src/promise.ts index 2cf333273..793b23835 100644 --- a/src/promise.ts +++ b/src/promise.ts @@ -255,7 +255,7 @@ class PromiseSingle { constructRecursively(): PromiseIndex { if (this.promiseIndex !== null) { - return this.promiseIndex; + return this.promiseIndex as PromiseIndex; } const promiseIndex = this.after @@ -279,7 +279,7 @@ export class PromiseJoint { constructRecursively(): PromiseIndex { if (this.promiseIndex !== null) { - return this.promiseIndex; + return this.promiseIndex as PromiseIndex; } const result = near.promiseAnd( 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..73d608eb4 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -4,10 +4,13 @@ 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 { _ = "" }; /** * 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. */