Skip to content

Commit

Permalink
Merge pull request #260 from near/promise-index-type
Browse files Browse the repository at this point in the history
Make PromiseIndex a nominal typing, require explicit construction
  • Loading branch information
volovyks authored Oct 12, 2022
2 parents 0d9023c + 63660ec commit dc6f07b
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 66 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.

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

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

109 changes: 62 additions & 47 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,69 +74,60 @@ 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,
gas: NearAmount,
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;
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -388,33 +385,35 @@ 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;
}

/**
* 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 unknown as bigint[])
) as unknown 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 unknown as PromiseIndex;
}

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

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

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

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

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

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

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

/**
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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 {
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
Loading

0 comments on commit dc6f07b

Please sign in to comment.