/
teal-counter.test.js
259 lines (207 loc) · 7.72 KB
/
teal-counter.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
const algosdk = require("algosdk");
const testingUtils = require("./lib/utils");
const tealCounter = require("../../scripts/lib/utils");
const algoUtils = require("../../scripts/lib/algo-utils");
const environment = require("../../scripts/environment");
const config = require("../../scripts/config");
describe("teal-counter / actual", () => {
const faucetMnemonic = process.env.FAUCET_MNEMONIC;
if (!faucetMnemonic) {
throw new Error(`Please set FAUCET_MNEMONIC to the mnemonic to an Algorand account that is used to create/fund testing accounts.`);
}
const faucetAccount = algosdk.mnemonicToSecretKey(faucetMnemonic);
const algodClient = new algosdk.Algodv2(environment.token, environment.host, environment.port);
//
// Set to the account that deploys the contract for each test.
//
let creatorAccount;
//
// Set to the "application id" for the deployed contract for each test.
//
let appId;
//
// These tests exceed the default 5s timeout.
//
jest.setTimeout(100000);
test("creator can deploy teal counter", async () => {
const initialValue = 15;
const { appId, txnId, confirmedRound, creatorAccount } = await creatorDeploysCounter(initialValue);
await expectGlobalState({
counterValue: {
uint: initialValue,
},
});
await expectTransaction(confirmedRound, txnId, {
txn: {
apaa: [ algosdk.encodeUint64(initialValue) ],
snd: creatorAccount.addr,
type: "appl",
},
});
});
test("creator can increment teal counter", async () => {
const initialValue = 15;
const { appId, creatorAccount } = await creatorDeploysCounter(initialValue);
const { txnId, confirmedRound } = await creatorIncrementsCounter();
await expectGlobalState({
counterValue: {
uint: initialValue + 1,
},
});
await expectTransaction(confirmedRound, txnId, {
txn: {
apaa: [ "increment" ],
apid: appId,
snd: creatorAccount.addr,
type: "appl",
},
});
});
test("creator can decrement teal counter", async () => {
const initialValue = 8;
const { appId, creatorAccount } = await creatorDeploysCounter(initialValue);
const { txnId, confirmedRound } = await creatorDecrementsCounter();
await expectGlobalState({
counterValue: {
uint: initialValue - 1,
},
});
await expectTransaction(confirmedRound, txnId, {
txn: {
apaa: [ "decrement" ],
apid: appId,
snd: creatorAccount.addr,
type: "appl",
},
});
});
test("can increment then decrement", async () => {
const initialValue = 8;
await creatorDeploysCounter(initialValue);
await creatorIncrementsCounter();
await creatorDecrementsCounter();
await expectGlobalState({
counterValue: {
uint: initialValue, // Back to original value.
},
});
});
test("can decrement then increment", async () => {
const initialValue = 8;
await creatorDeploysCounter(initialValue);
await creatorDecrementsCounter();
await creatorIncrementsCounter();
await expectGlobalState({
counterValue: {
uint: initialValue, // Back to original value.
},
});
});
test("can increment x2", async () => {
const initialValue = 8;
await creatorDeploysCounter(initialValue);
await creatorIncrementsCounter();
await creatorIncrementsCounter();
await expectGlobalState({
counterValue: {
uint: initialValue + 2,
},
});
});
test("can decrement x2", async () => {
const initialValue = 8;
await creatorDeploysCounter(initialValue);
await creatorDecrementsCounter();
await creatorDecrementsCounter();
await expectGlobalState({
counterValue: {
uint: initialValue - 2,
},
});
});
test("only the creator can increment", async () => {
await creatorDeploysCounter(0);
// Create a temporary testing account.
const userAccount = await createFundedAccount();
// Some other user attempts to increment the counter...
await expect(() => userIncrementsCounter(userAccount))
.rejects
.toThrow(); // ... and it throws an error.
});
test("only the creator can decrement", async () => {
await creatorDeploysCounter(0);
await creatorIncrementsCounter();
// Create a temporary testing account.
const userAccount = await createFundedAccount();
// Some other user attempts to decrement the counter...
await expect(() => userDecrementsCounter(userAccount))
.rejects
.toThrow(); // ... and it throws an error.
});
//
// Deploys the Teal-counter to the sandbox.
//
async function creatorDeploysCounter(initialValue) {
creatorAccount = await createFundedAccount();
const result = await tealCounter.deployTealCounter(algodClient, creatorAccount, initialValue);
appId = result.appId;
return {
...result,
creatorAccount,
};
}
//
// The creator invokes the "increment" method of the smart contract.
//
async function creatorIncrementsCounter() {
if (!creatorAccount || !appId) {
throw new Error(`Contract has not been deployed, please call "creatorDeploysCounter".`);
}
return await userIncrementsCounter(creatorAccount, appId);
}
//
// The creator invokes the "decrement" method of the smart contract.
//
async function creatorDecrementsCounter() {
if (!creatorAccount || !appId) {
throw new Error(`Contract has not been deployed, please call "creatorDeploysCounter".`);
}
return await userDecrementsCounter(creatorAccount, appId);
}
//
// Some user invokes the "increment" method of the smart contract.
//
async function userIncrementsCounter(userAccount) {
if (!appId) {
throw new Error(`Contract has not been deployed, please call "creatorDeploysCounter".`);
}
return await tealCounter.invokeIncrement(algodClient, userAccount, appId);
}
//
// Some user invokes the "decrement" method of the smart contract.
//
async function userDecrementsCounter(userAccount) {
if (!appId) {
throw new Error(`Contract has not been deployed, please call "creatorDeploysCounter".`);
}
return await tealCounter.invokeDecrement(algodClient, userAccount, appId);
}
//
// Creates a funded account we cause for testing.
//
async function createFundedAccount() {
return await algoUtils.createFundedAccount(algodClient, faucetAccount, 1_000_000, 1_000);
}
//
// Checks that the global state of the contract matches the expected fields.
//
async function expectGlobalState(expectedFields) {
await testingUtils.expectGlobalState(algodClient, creatorAccount.addr, appId, expectedFields);
}
//
// Checks that a transaction commitetd to the blockchain matches the expected fields.
//
async function expectTransaction(confirmedRound, txnId, expectedFields) {
await testingUtils.expectTransaction(algodClient, confirmedRound, txnId, expectedFields);
}
});