-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.mjs
373 lines (331 loc) · 13.1 KB
/
index.mjs
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import { loadStdlib } from "@reach-sh/stdlib";
import * as childBackend from "./build/index.Child.mjs";
import * as masterBackend from "./build/index.Master.mjs";
export const fromSome = (x, d) => {
if (x.length !== 2 || x[0] === "None") {
return d;
} else {
return x[1];
}
};
const tokens = [
{
name: "v1",
symbol: "V1",
decimals: 0,
supply: 1000,
},
];
const stdlib = loadStdlib(process.env);
const showAccountInfo = async (name, acc) => {
console.log("");
console.log(`Account ${name}:`);
console.log(` Address: ${stdlib.formatAddress(acc.getAddress())}`);
console.log(` Balance: ${await acc.balanceOf()}`);
console.log("");
};
const Test = async (backend) => {
console.log("Running Test...");
const pc = stdlib.parseCurrency;
const bn = stdlib.bigNumberify;
const startingBalance = pc(100);
const [accAlice, accIssuer] = await stdlib.newTestAccounts(
2,
startingBalance
);
await stdlib.wait(1);
await showAccountInfo("alice", accAlice);
await showAccountInfo("issuer", accIssuer);
console.log("===========================================");
console.log("Minting tokens...");
console.log("===========================================");
const tokArr = await Promise.all(
tokens.map((el) =>
stdlib.launchToken(accIssuer, el.name, el.symbol, {
decimals: el.decimals,
supply: el.supply,
})
)
);
const tokObj = {};
tokens.forEach((el, i) => (tokObj[el.name] = tokArr[i]));
for (let i = 1; i < tokArr.length; i++) {
await accAlice.tokenAccept(tokArr[i].id);
}
for (let i = 1; i < tokArr.length; i++) {
await tokArr[i].mint(accAlice, 100);
}
console.log("Tokens minted!");
console.log("Hello, Alice!");
const ctcAlice = accAlice.contract(backend);
// monitor events
["new", "setup", "grant", "ready", "foo"].forEach((el) => {
ctcAlice.e[el].monitor((ej) => {
console.log("...........................................");
console.log(`${el} event!`);
console.log(ej);
console.log("...........................................");
});
});
console.log("Deploying master contract...");
await stdlib.withDisconnect(() =>
ctcAlice.p.Alice({
ready: () => {
console.log("Ready!");
stdlib.disconnect(null); // causes withDisconnect to immediately return null
},
})
);
await showAccountInfo("alice", accAlice);
const ctcInfoMaster = await ctcAlice.getInfo();
const ctcAddressMaster = stdlib.formatAddress(
await ctcAlice.getContractAddress()
);
const accMaster = await stdlib.connectAccount({ addr: ctcAddressMaster });
await showAccountInfo("ctc(master)", accMaster);
console.log({ ctcInfoMaster });
console.log({ ctcAddressMaster });
const appCount = 1;
const ctcs = [];
const addrs = [];
// New
for (let i = 0; i < appCount; i++) {
console.log(`Deploying contract ${i}...`);
const ctc = await ctcAlice.a.Master.new();
const ctc2 = accAlice.contract(childBackend, ctc);
const addr = stdlib.formatAddress(await ctc2.getContractAddress());
const acc = await stdlib.connectAccount({ addr });
ctcs.push(ctc);
addrs.push(addr);
console.log(ctc);
console.log(stdlib.bigNumberToNumber(ctc));
console.log(addr);
console.log("Contract deployed!");
await showAccountInfo("alice", accAlice);
await showAccountInfo(`ctc(child${i})`, acc);
await showAccountInfo("ctc(master)", accMaster);
}
// Setup
for (let i = 0; i < appCount; i++) {
const ctcInfo = ctcs[i];
const ctc2 = accAlice.contract(childBackend, ctcInfo);
const addr = stdlib.formatAddress(await ctc2.getContractAddress());
const acc = await stdlib.connectAccount({ addr });
console.log(`Setting up contract ${i}...`);
await ctcAlice.a.Master.setup(ctcInfo);
console.log("Contract set up!");
console.log(stdlib.bigNumberToNumber(await accAlice.balanceOf()));
console.log(stdlib.formatCurrency(await accAlice.balanceOf()));
await showAccountInfo("alice", accAlice);
await showAccountInfo(`ctc(child${i})`, acc);
await showAccountInfo("ctc(master)", accMaster);
}
// Ready
for (let i = 0; i < appCount; i++) {
const ctcInfo = ctcs[i];
const ctc = accAlice.contract(childBackend, ctcInfo);
const addr = stdlib.formatAddress(await ctc.getContractAddress());
const acc = await stdlib.connectAccount({ addr });
await stdlib.withDisconnect(() =>
ctc.p.Alice({
getParams: () => ({
ctc: ctcInfoMaster,
token: tokObj.v1.id,
}),
ready: () => {
console.log("Ready!");
stdlib.disconnect(null); // causes withDisconnect to immediately return null
},
})
);
console.log(stdlib.bigNumberToNumber(await accAlice.balanceOf()));
console.log(stdlib.formatCurrency(await accAlice.balanceOf()));
console.log(await ctc.v.state());
await showAccountInfo("alice", accAlice);
await showAccountInfo(`ctc(child${i})`, acc);
await showAccountInfo("ctc(master)", accMaster);
}
// Grant issuer
do {
const i = 0;
const ctcInfo = ctcs[i];
const ctc = accAlice.contract(childBackend, ctcInfo);
const addr = stdlib.formatAddress(await ctc.getContractAddress());
const acc = await stdlib.connectAccount({ addr });
await ctc.a.C.grant(accIssuer);
console.log(stdlib.bigNumberToNumber(await accAlice.balanceOf()));
console.log(stdlib.formatCurrency(await accAlice.balanceOf()));
console.log(await ctc.v.state());
await showAccountInfo("alice", accAlice);
await showAccountInfo("issuer", accIssuer);
await showAccountInfo(`ctc(child${i})`, acc);
await showAccountInfo("ctc(master)", accMaster);
} while (0);
// Call foo directly from child
do {
const i = 0;
const ctcInfo = ctcs[i];
const ctc = accAlice.contract(childBackend, ctcInfo);
const addr = stdlib.formatAddress(await ctc.getContractAddress());
const acc = await stdlib.connectAccount({ addr });
console.log(
`issuer clicks: ${stdlib.bigNumberToNumber(
fromSome(await ctc.v.clicks(accIssuer), stdlib.bigNumberify(0))
)}`
);
console.log(
`alice clicks: ${stdlib.bigNumberToNumber(
fromSome(await ctc.v.clicks(accAlice), stdlib.bigNumberify(0))
)}`
);
console.log("Issuer calling foo...");
await ctc.a.U0.foo(accIssuer);
await showAccountInfo("alice", accAlice);
await showAccountInfo("issuer", accIssuer);
await showAccountInfo(`ctc(child${i})`, acc);
await showAccountInfo("ctc(master)", accMaster);
console.log("Alice calling foo...");
await ctc.a.U0.foo(accAlice);
await showAccountInfo("alice", accAlice);
await showAccountInfo("issuer", accIssuer);
await showAccountInfo(`ctc(child${i})`, acc);
await showAccountInfo("ctc(master)", accMaster);
console.log(
`issuer clicks: ${stdlib.bigNumberToNumber(
fromSome(await ctc.v.clicks(accIssuer), stdlib.bigNumberify(0))
)}`
);
console.log(
`alice clicks: ${stdlib.bigNumberToNumber(
fromSome(await ctc.v.clicks(accAlice), stdlib.bigNumberify(0))
)}`
);
console.log(stdlib.bigNumberToNumber(await accAlice.balanceOf()));
console.log(stdlib.formatCurrency(await accAlice.balanceOf()));
console.log(await ctc.v.state());
} while (0);
// Call foo2 remotely from master accessing 1 box
do {
const i = 0;
const ctcInfo = ctcs[i];
const ctc = accAlice.contract(masterBackend, ctcInfoMaster);
const ctc2 = accAlice.contract(childBackend, ctcInfo);
const addr = stdlib.formatAddress(await ctc2.getContractAddress());
const acc = await stdlib.connectAccount({ addr });
console.log("Alice calling foo2 (remote)...");
await ctc.a.Master.foo2(ctcInfo, accIssuer);
await showAccountInfo("alice", accAlice);
await showAccountInfo("issuer", accIssuer);
await showAccountInfo(`ctc(child${i})`, acc);
await showAccountInfo("ctc(master)", accMaster);
console.log("Alice calling foo2 (remote)...");
await ctc.a.Master.foo2(ctcInfo, accIssuer);
await showAccountInfo("alice", accAlice);
await showAccountInfo("issuer", accIssuer);
await showAccountInfo(`ctc(child${i})`, acc);
await showAccountInfo("ctc(master)", accMaster);
console.log(stdlib.bigNumberToNumber(await accAlice.balanceOf()));
console.log(stdlib.formatCurrency(await accAlice.balanceOf()));
console.log(await ctc2.v.state());
console.log(await ctc2.v.clicks(accAlice));
console.log(await ctc2.v.clicks(accIssuer));
console.log("Alice calling foo3 (remote)...");
await ctc.a.Master.foo3(ctcInfo, accIssuer, accAlice);
await showAccountInfo("alice", accAlice);
await showAccountInfo("issuer", accIssuer);
await showAccountInfo(`ctc(child${i})`, acc);
await showAccountInfo("ctc(master)", accMaster);
console.log(stdlib.bigNumberToNumber(await accAlice.balanceOf()));
console.log(stdlib.formatCurrency(await accAlice.balanceOf()));
console.log(await ctc2.v.state());
console.log(await ctc2.v.clicks(accAlice));
console.log(await ctc2.v.clicks(accIssuer));
console.log("Alice calling foo4...");
await ctc2.a.U0.foo4(accIssuer, accAlice);
await showAccountInfo("alice", accAlice);
await showAccountInfo("issuer", accIssuer);
await showAccountInfo(`ctc(child${i})`, acc);
await showAccountInfo("ctc(master)", accMaster);
console.log(stdlib.bigNumberToNumber(await accAlice.balanceOf()));
console.log(stdlib.formatCurrency(await accAlice.balanceOf()));
console.log(await ctc2.v.state());
console.log(await ctc2.v.clicks(accAlice));
console.log(await ctc2.v.clicks(accIssuer));
console.log("Alice calling foo4 (remote)...");
try {
await ctc.a.Master.foo4(ctcInfo, accIssuer, accAlice); // "Error: Network request error. Received status 400: TransactionPool.Remember: transaction FLPXG4MGKITBQ44O23MG6JYTS4MQUI4QVXNBXXYEHM5NLMMLKA7Q: logic eval error: logic eval error: - would result negative. Details: pc=1500, opcodes=dup\nstore 0\n-\n. Details: pc=1264, opcodes=load 16\nitxn_field Applications\nitxn_submit\n
} catch (e) {
console.log(e);
}
await showAccountInfo("alice", accAlice);
await showAccountInfo("issuer", accIssuer);
await showAccountInfo(`ctc(child${i})`, acc);
await showAccountInfo("ctc(master)", accMaster);
console.log(stdlib.bigNumberToNumber(await accAlice.balanceOf()));
console.log(stdlib.formatCurrency(await accAlice.balanceOf()));
console.log(await ctc2.v.state());
console.log(await ctc2.v.clicks(accAlice));
console.log(await ctc2.v.clicks(accIssuer));
console.log("Alice calling foo4...");
await ctc2.a.U0.foo4(accIssuer, accAlice);
await showAccountInfo("alice", accAlice);
await showAccountInfo("issuer", accIssuer);
await showAccountInfo(`ctc(child${i})`, acc);
await showAccountInfo("ctc(master)", accMaster);
console.log(stdlib.bigNumberToNumber(await accAlice.balanceOf()));
console.log(stdlib.formatCurrency(await accAlice.balanceOf()));
console.log(await ctc2.v.state());
console.log(await ctc2.v.clicks(accAlice));
console.log(await ctc2.v.clicks(accIssuer));
console.log("Alice calling foo6 (remote)...");
try {
await ctc.a.Master.foo6(ctcInfo, accIssuer, accAlice);
} catch (e) {
console.log(e);
}
await showAccountInfo("alice", accAlice);
await showAccountInfo("issuer", accIssuer);
await showAccountInfo(`ctc(child${i})`, acc);
await showAccountInfo("ctc(master)", accMaster);
console.log(stdlib.bigNumberToNumber(await accAlice.balanceOf()));
console.log(stdlib.formatCurrency(await accAlice.balanceOf()));
console.log(await ctc2.v.state());
console.log(await ctc2.v.clicks(accAlice));
console.log(await ctc2.v.clicks(accIssuer));
} while (0);
do {
const i = 0;
const ctcInfo = ctcs[i];
const ctc = accAlice.contract(masterBackend, ctcInfoMaster);
const ctc2 = accAlice.contract(childBackend, ctcInfo);
const addr = stdlib.formatAddress(await ctc2.getContractAddress());
const acc = await stdlib.connectAccount({ addr });
console.log(stdlib.bigNumberToNumber(await accAlice.balanceOf()));
console.log(stdlib.formatCurrency(await accAlice.balanceOf()));
console.log(await ctc2.v.state());
console.log(await ctc2.v.clicks(accAlice));
console.log(await ctc2.v.clicks(accIssuer));
console.log("Deleting boxes...");
try {
await ctc2.a.U0.foo5(accIssuer, accAlice);
await ctc2.a.U0.foo5(accAlice, accIssuer);
} catch (e) {
console.log(e);
}
console.log(stdlib.bigNumberToNumber(await accAlice.balanceOf()));
console.log(stdlib.formatCurrency(await accAlice.balanceOf()));
console.log(await ctc2.v.state());
console.log(await ctc2.v.clicks(accAlice));
console.log(await ctc2.v.clicks(accIssuer));
await showAccountInfo("alice", accAlice);
await showAccountInfo("issuer", accIssuer);
await showAccountInfo(`ctc(child${i})`, acc);
await showAccountInfo("ctc(master)", accMaster);
} while (0);
console.log("Goodbye, Alice!");
};
const main = async () => {
console.log("Running main...");
await Test(masterBackend);
};
main();