Skip to content

Commit

Permalink
Make PromiseIndex a nominal typing, require explicit construction
Browse files Browse the repository at this point in the history
  • Loading branch information
ailisp committed Oct 12, 2022
1 parent 0d9023c commit c49abf5
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 31 deletions.
10 changes: 5 additions & 5 deletions lib/api.d.ts

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

4 changes: 2 additions & 2 deletions lib/types/index.d.ts

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

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

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

6 changes: 5 additions & 1 deletion lib/utils.d.ts

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

6 changes: 6 additions & 0 deletions lib/utils.js

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

20 changes: 10 additions & 10 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -388,33 +388,33 @@ export function promiseThen(
args: Bytes,
amount: NearAmount,
gas: NearAmount
): bigint {
): PromiseIndex {
return env.promise_then(
promiseIndex,
accountId,
methodName,
args,
amount,
gas
);
) as PromiseIndex;
}

/**
* Join an arbitrary array of NEAR promises.
*
* @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;
}

/**
* Create a NEAR promise which will have multiple promise actions inside.
*
* @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;
}

/**
Expand All @@ -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;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ class PromiseSingle {

constructRecursively(): PromiseIndex {
if (this.promiseIndex !== null) {
return this.promiseIndex;
return this.promiseIndex as PromiseIndex;
}

const promiseIndex = this.after
Expand All @@ -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(
Expand Down
2 changes: 0 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { BlockHeight, EpochHeight, Balance, StorageUsage } from "./primitives";
import {
PromiseResult,
PromiseError,
PromiseIndex,
ReceiptIndex,
IteratorIndex,
} from "./vm_types";
Expand All @@ -26,7 +25,6 @@ export {
StorageUsage,
PromiseResult,
PromiseError,
PromiseIndex,
ReceiptIndex,
IteratorIndex,
Gas,
Expand Down
4 changes: 0 additions & 4 deletions src/types/vm_types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* The index for NEAR promises.
*/
export type PromiseIndex = bigint;
/**
* The index for NEAR receipts.
*/
Expand Down
5 changes: 4 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit c49abf5

Please sign in to comment.