-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
126 lines (114 loc) · 3.54 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const nearAPI = require("near-api-js");
const BN = require("bn.js");
const fs = require("fs").promises;
const assert = require("assert").strict;
function getConfig(env) {
switch (env) {
case "sandbox":
case "local":
return {
networkId: "sandbox",
nodeUrl: "http://localhost:3030",
masterAccount: "test.near",
contractAccount: "status-message.test.near",
keyPath: "/tmp/near-sandbox/validator_key.json",
};
}
}
const contractMethods = {
viewMethods: ["get_status"],
changeMethods: ["set_status"],
};
let config;
let masterAccount;
let masterKey;
let pubKey;
let keyStore;
let near;
async function initNear() {
config = getConfig(process.env.NEAR_ENV || "sandbox");
const keyFile = require(config.keyPath);
masterKey = nearAPI.utils.KeyPair.fromString(
keyFile.secret_key || keyFile.private_key
);
pubKey = masterKey.getPublicKey();
keyStore = new nearAPI.keyStores.InMemoryKeyStore();
keyStore.setKey(config.networkId, config.masterAccount, masterKey);
near = await nearAPI.connect({
deps: {
keyStore,
},
networkId: config.networkId,
nodeUrl: config.nodeUrl,
});
masterAccount = new nearAPI.Account(near.connection, config.masterAccount);
console.log("Finish init NEAR");
}
async function createContractUser(
accountPrefix,
contractAccountId,
contractMethods
) {
let accountId = accountPrefix + "." + config.masterAccount;
await masterAccount.createAccount(
accountId,
pubKey,
new BN(10).pow(new BN(25))
);
keyStore.setKey(config.networkId, accountId, masterKey);
const account = new nearAPI.Account(near.connection, accountId);
const accountUseContract = new nearAPI.Contract(
account,
contractAccountId,
contractMethods
);
return accountUseContract;
}
async function initTest() {
const contract = await fs.readFile("./res/status_message.wasm");
const _contractAccount = await masterAccount.createAndDeployContract(
config.contractAccount,
pubKey,
contract,
new BN(10).pow(new BN(25))
);
const aliceUseContract = await createContractUser(
"alice",
config.contractAccount,
contractMethods
);
const bobUseContract = await createContractUser(
"bob",
config.contractAccount,
contractMethods
);
console.log("Finish deploy contracts and create test accounts");
return { aliceUseContract, bobUseContract };
}
async function test() {
// 1. Creates testing accounts and deploys a contract
await initNear();
const { aliceUseContract, bobUseContract } = await initTest();
// 2. Performs a `set_status` transaction signed by Alice and then calls `get_status` to confirm `set_status` worked
await aliceUseContract.set_status({ args: { message: "hello" } });
let alice_message = await aliceUseContract.get_status({
account_id: "alice.test.near",
});
assert.equal(alice_message, "hello");
// 3. Gets Bob's status and which should be `null` as Bob has not yet set status
let bob_message = await bobUseContract.get_status({
account_id: "bob.test.near",
});
assert.equal(bob_message, null);
// 4. Performs a `set_status` transaction signed by Bob and then calls `get_status` to show Bob's changed status and should not affect Alice's status
await bobUseContract.set_status({ args: { message: "world" } });
bob_message = await bobUseContract.get_status({
account_id: "bob.test.near",
});
assert.equal(bob_message, "world");
alice_message = await aliceUseContract.get_status({
account_id: "alice.test.near",
});
assert.equal(alice_message, "hello");
}
test();