-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.js
99 lines (79 loc) · 3.22 KB
/
calculator.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
#!/usr/bin/env node
const { TvmRunnerAsynchronous, SmartContract } = require("ton-contract-executor");
const { compileFunc } = require("@ton-community/func-js");
const { Cell, InternalMessage, CommonMessageInfo, CellMessage, beginCell, Address } = require("ton");
const fs = require('fs').promises;
async function main(args) {
const code = await fs.readFile('3.fc', "binary");
const contractAddress = Address.parse('Ef8RERERERERERERERERERERERERERERERERERERERERERlb')
const compileResult = await compileFunc({
sources: {
'contract.fc': ' #include "stdlib.fc";' + code,
'stdlib.fc': await fs.readFile('stdlib.fc', "binary")
},
entryPoints: ['contract.fc'],
})
if (compileResult.status === 'error') throw new Error('compilation failed' + JSON.stringify(compileResult))
const contract = await SmartContract.fromCell(
Cell.fromBoc(Buffer.from(compileResult.codeBoc, 'base64'))[0],
new Cell(), {debug: true}
)
let totalGas = 0;
async function checkCalculator(expression, solution) {
if (solution === undefined) {
solution = eval(expression);
}
messageBody = beginCell()
.storeUint(0, 32)
.storeBuffer(Buffer.from(expression))
.endCell();
res = await contract.sendInternalMessage(new InternalMessage({
to: contractAddress,
from: contractAddress,
value: 1, // 1 nanoton
bounce: false,
body: new CommonMessageInfo({
body: new CellMessage(messageBody)
})
}));
if (res.type != 'success') {
throw new Error("Failed step for " + expression + ": " + JSON.stringify(res));
}
totalGas += res.gas_consumed;
slice = res.actionList[0].message.body.beginParse();
slice.readUint(32);
result = slice.readRemainingBytes().toString();
if (result == solution) {
console.log("[+] Check passed " + expression + " = " + result);
} else {
throw new Error("[!] Expected for " + expression + ": " + solution + ", but got " + result);
}
}
await checkCalculator("(2+2)");
await checkCalculator("2");
await checkCalculator("2+2");
await checkCalculator("22+2");
await checkCalculator("2+2+3+4");
await checkCalculator("2+2");
await checkCalculator("2+3-4+5");
await checkCalculator("2+2-3-4");
await checkCalculator("2+2+3+4+5+6");
await checkCalculator("2*3+4");
await checkCalculator("2+3*4");
await checkCalculator("2+3*4+5-6");
await checkCalculator("4/2");
await checkCalculator("5+6/5-33", -27);
await checkCalculator("5+6/5-33*9999+1", 5+1-33*9999+1);
await checkCalculator("-1");
await checkCalculator("-1-2");
await checkCalculator("1/1*1");
await checkCalculator("(2+3)*4");
await checkCalculator("(2-3)/2", -1);
// Doesn't work, but it is not covered by the test system)
// await checkCalculator("7*(-5)");
// await checkCalculator("7*-5");
await checkCalculator("((((125*33/11+2)*34)/493)/2)");
console.log("Total gas consumed: ", totalGas);
await TvmRunnerAsynchronous.getShared().cleanup()
}
main();