Skip to content

Commit 046e678

Browse files
author
Willem Wyndham
authored
Add types, documentation, and jest tests (#67)
1 parent 62db89c commit 046e678

20 files changed

+4204
-127
lines changed

.npmignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ bindgen/src
77
bindgen/out
88
bindgen/asconfig.js
99
bindgen/test.js
10-
runtime/dist/tsconfig.tsbuildinfo
10+
runtime/dist/tsconfig.tsbuildinfo
11+
**/*/__tests__

install-near-vm.js

-1
This file was deleted.

jest.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
testMatch: ["**/__tests__/**/*.spec.ts"],
5+
testPathIgnorePatterns: ["/assembly/", "/node_modules/"],
6+
};

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,21 @@
3333
"test": "yarn test:bindgen && yarn asp:ci && yarn test:nearcore && yarn test:runtime",
3434
"test:nearcore": "(cd nearcore-tests; yarn test)",
3535
"test:bindgen": "(cd bindgen; yarn test)",
36-
"test:runtime": "(cd runtime; node asconfig && ./test.ts)",
36+
"test:runtime": "(cd runtime; node asconfig) && jest",
3737
"asp:ci": "asp",
3838
"asp": "yarn asp:ci --verbose",
3939
"doc": "rm -rf docs; typedoc assembly --tsconfig assembly/tsconfig.json && touch docs/.nojekyll"
4040
},
4141
"devDependencies": {
4242
"@types/bn.js": "^4.11.6",
4343
"@types/bs58": "^4.0.1",
44+
"@types/jest": "^25.2.1",
4445
"@types/js-base64": "^2.3.1",
45-
"@types/node": "^13.13.2",
46+
"@types/node": "^13.13.4",
4647
"assemblyscript": "^0.9.4",
48+
"jest": "^25.5.4",
4749
"near-hello": "nearprotocol/near-hello#d4712392ab5ec56b6b0f0aa40dd4a2c744114a10",
50+
"ts-jest": "^25.4.0",
4851
"ts-node": "^8.6.2",
4952
"typedoc": "^0.17.3",
5053
"typescript": "^3.6.4"

runtime/__tests__/cross.spec.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Runtime, Account } from "..";
2+
3+
let runtime: Runtime;
4+
let sentences: Account, words: Account, alice: Account;
5+
describe("cross contract calls", () => {
6+
beforeEach(() => {
7+
runtime = new Runtime();
8+
alice = runtime.newAccount("alice");
9+
sentences = runtime.newAccount("sentences", __dirname + "/../out/sentences.wasm");
10+
words = runtime.newAccount("words.examples", __dirname + "/../out/words.wasm");
11+
});
12+
13+
test("single promise", () => {
14+
let res = alice.call_other("sentences", "reverseWordOne");
15+
expect(res.return_data.text).toBe("elpmas")
16+
});
17+
18+
test("promise + then with no arguments", () => {
19+
let res = alice.call_other("sentences", "reverseWordTwo");
20+
expect(res.return_data).toBe(true)
21+
});
22+
test("promise + then with arguments", () => {
23+
let res = alice.call_other("sentences", "reverseWordThree");
24+
expect(res.return_data).toBe(true)
25+
});
26+
})

runtime/dist/runtime.d.ts

+54-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { AccountContext } from "./context";
2-
declare type State = {
3-
[key: string]: string;
4-
};
2+
import { State, StandaloneOutput, ResultsObject } from './types';
3+
/**
4+
* Account object of client and contracts.
5+
*/
56
export declare class Account {
67
account_id: string;
78
wasmFile: string | null;
@@ -10,28 +11,65 @@ export declare class Account {
1011
balance: number;
1112
lockedBalance: number;
1213
signerAccountPk: string;
14+
/**
15+
* Sholud only be constructed by a runtime instance.
16+
* @param account_id
17+
* @param wasmFile
18+
* @param runtime
19+
*/
1320
constructor(account_id: string, wasmFile: string | null, runtime: Runtime);
14-
createAccountContext(input?: any, prepaid_gas?: number): Partial<AccountContext>;
15-
call_step_other(account_id: string, method_name: string, input?: any, prepaid_gas?: number): any;
16-
call_step(method_name: string, input?: any, prepaid_gas?: number): any;
21+
private createAccountContext;
22+
/**
23+
* Single execution of contract method.
24+
* @param account_id contractId to call
25+
* @param method_name method to call
26+
* @param input object of arguments of method
27+
* @param prepaid_gas How much gas to use.
28+
*/
29+
call_step_other(account_id: string, method_name: string, input?: any, prepaid_gas?: number): StandaloneOutput;
30+
/**
31+
* Single execution of contract method to the same contract.
32+
* @param method_name method to call
33+
* @param input object of arguments of method
34+
* @param prepaid_gas How much gas to use.
35+
*/
36+
call_step(method_name: string, input?: any, prepaid_gas?: number): StandaloneOutput;
37+
/**
38+
* Execute contract and any promises generated until no more promises are generated or gas runs out.
39+
* @param account_id Initial Contract to call.
40+
* @param method_name Method to call.
41+
* @param input object of input to method.
42+
* @param prepaid_gas How much gas to use.
43+
*/
1744
call_other(account_id: string, method_name: string, input?: any, prepaid_gas?: number): {
1845
return_data: any;
1946
err: any;
20-
result: any;
47+
result: StandaloneOutput;
2148
calls: any;
22-
results: any;
49+
results: ResultsObject;
2350
};
51+
/**
52+
* Execute this contract and any promises generated until no more promises are generated or gas runs out.
53+
* @param method_name Method to call.
54+
* @param input object of input to method.
55+
* @param prepaid_gas How much gas to use.
56+
*/
2457
call(method_name: string, input?: any, prepaid_gas?: number): {
2558
return_data: any;
2659
err: any;
27-
result: any;
60+
result: StandaloneOutput;
2861
calls: any;
29-
results: any;
62+
results: ResultsObject;
3063
};
64+
/**
65+
* View contract call to this contract.
66+
* @param method_name view method.
67+
* @param input object of input to method.
68+
*/
3169
view(method_name: string, input?: any): {
32-
return_data: any;
70+
return_value: string;
3371
err: any;
34-
result: any;
72+
result: StandaloneOutput;
3573
};
3674
/**
3775
* Current state of contract.
@@ -41,18 +79,17 @@ export declare class Account {
4179
export declare class Runtime {
4280
accounts: Map<string, Account>;
4381
constructor();
44-
log(input: any): void;
82+
private log;
4583
newAccount(accoundId: string, wasmFile?: string | null): Account;
4684
getOrCreateAccount(account_id: string): Account;
4785
getAccount(account_id: string): Account;
48-
call_step(account_id: string, method_name: string, input?: string, accountContext?: Partial<AccountContext>): any;
86+
call_step(account_id: string, method_name: string, input?: string, accountContext?: Partial<AccountContext>): StandaloneOutput;
4987
call(account_id: string, method_name: string, input: string | undefined, accountContext: Partial<AccountContext>): {
5088
return_data: any;
5189
err: any;
52-
result: any;
90+
result: StandaloneOutput;
5391
calls: any;
54-
results: any;
92+
results: ResultsObject;
5593
};
5694
private spawn;
5795
}
58-
export {};

runtime/dist/runtime.js

+62-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)