forked from aragon/radspec
-
Notifications
You must be signed in to change notification settings - Fork 1
/
examples.js
432 lines (412 loc) · 16.4 KB
/
examples.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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import test from 'ava'
import BN from 'bn.js'
import { evaluateRaw } from '../../src/lib'
import { defaultHelpers } from '../../src/helpers'
import { tenPow } from '../../src/helpers/lib/formatBN'
import { ETH } from '../../src/helpers/lib/token'
import knownFunctions from '../../src/data/knownFunctions'
import { keccak256 } from 'web3-utils'
const int = (value) => ({
type: 'int256',
value
})
const address = (value = '0x0000000000000000000000000000000000000001') => ({
type: 'address',
value
})
const bool = (value) => ({
type: 'bool',
value
})
const string = (value) => ({
type: 'string',
value
})
const bytes32 = (value) => ({
type: 'bytes32',
value
})
const bytes = (value) => ({
type: 'bytes',
value
})
const comparisonCases = [
[{
source: '`a > 2`',
bindings: { a: int(3) }
}, 'true'],
[{
source: '`a > b`',
bindings: { a: int(2), b: int(3) }
}, 'false'],
[{
source: '`a >= b`',
bindings: { a: int(3), b: int(2) }
}, 'true'],
[{
source: '`a >= b`',
bindings: { a: int(1), b: int(2) }
}, 'false'],
[{
source: '`a >= b`',
bindings: { a: int(2), b: int(2) }
}, 'true'],
[{
source: '`a < b`',
bindings: { a: int(3), b: int(2) }
}, 'false'],
[{
source: '`a < b`',
bindings: { a: int(2), b: int(3) }
}, 'true'],
[{
source: '`a <= b`',
bindings: { a: int(3), b: int(2) }
}, 'false'],
[{
source: '`a <= b`',
bindings: { a: int(1), b: int(2) }
}, 'true'],
[{
source: '`a <= b`',
bindings: { a: int(3), b: int(3) }
}, 'true'],
[{
source: '`a == b`',
bindings: { a: int(3), b: int(3) }
}, 'true'],
[{
source: '`a != b`',
bindings: { a: int(3), b: int(3) }
}, 'false'],
[{
source: '`a > 0x01`',
bindings: { a: address('0x0000000000000000000000000000000000000002') }
}, 'true'],
[{
source: '`a != 0x01`',
bindings: { a: address('0x0000000000000000000000000000000000000002') }
}, 'true'],
[{
source: '`a != 0x01`',
bindings: { a: address('0x0000000000000000000000000000000000000002') }
}, 'true'],
[{
source: '`a > 0x01`',
bindings: { a: bytes32('0x0000000000000000000000000000000000000000000000000000000000000002') }
}, 'true']
]
const helperCases = [
[{
source: "helper `@echo(@echo('hi '), 1 + 100000 ^ 0)`",
bindings: {}
}, 'helper hi hi '],
[{
source: 'Balance: `@tokenAmount(token, balance, false, 5)` ANT',
bindings: { token: address('0x960b236A07cf122663c4303350609A66A7B288C0'), balance: int('647413054590000000000000') }
}, 'Balance: 647413.05459 ANT'],
[{
source: 'Balance: `@tokenAmount(token, balance, false, 5)` ANT (non-checksummed)',
bindings: { token: address('0x960b236a07cf122663c4303350609a66a7b288c0'), balance: int('647413054590000000000000') }
}, 'Balance: 647413.05459 ANT (non-checksummed)'],
[{
source: 'Balance: `@tokenAmount(token, balance, false, 7)` ANT (trailing zeros)',
bindings: { token: address('0x960b236A07cf122663c4303350609A66A7B288C0'), balance: int('647413054590000000000000') }
}, 'Balance: 647413.0545900 ANT (trailing zeros)'],
[{
source: 'Balance: `@tokenAmount(token, balance, false, 5)` ANT (non-precise)',
bindings: { token: address('0x960b236A07cf122663c4303350609A66A7B288C0'), balance: int('647413054595780000000000') }
}, 'Balance: ~647413.05459 ANT (non-precise)'],
[{
source: 'Balance: `@tokenAmount(token, balance)`',
bindings: { token: address(ETH), balance: int('647413054595780000000000') }
}, 'Balance: 647413.05459578 ETH'],
[{
source: 'Balance: `@tokenAmount(token, balance)`',
bindings: { token: address('0x89205A3A3b2A69De6Dbf7f01ED13B2108B2c43e7'), balance: int('10') }
}, 'Balance: 10 🦄'],
[{
source: 'Balance: `@tokenAmount(token, balance)`',
bindings: { token: address('0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359'), balance: int('10000000000000000000') }
}, 'Balance: 10 DAI'],
[{
source: 'Balance: `@tokenAmount(token, balance)`',
bindings: { token: address('0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359'), balance: int('1000000000000000') }
}, 'Balance: 0.001 DAI'],
[{
source: 'Balance: `@tokenAmount(token, balance)`',
bindings: { token: address('0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359'), balance: int('1') }
}, 'Balance: 0.000000000000000001 DAI'],
[{
source: 'Balance: `@tokenAmount(token, balance, true, 3)`',
bindings: { token: address('0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359'), balance: int('1') }
}, 'Balance: ~0.000 DAI'],
[{
source: 'Balance: `@tokenAmount(token, balance, true, 3)`',
bindings: { token: address('0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359'), balance: int('1000000000000000001') }
}, 'Balance: ~1.000 DAI'],
[{
source: 'Balance: `@tokenAmount(token, balance)`',
bindings: { token: address('0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359'), balance: int('1000000000000000001') }
}, 'Balance: 1.000000000000000001 DAI'],
[{
source: 'Balance: `@tokenAmount(self.token(): address, balance)`',
bindings: { balance: int('10000000000000000000') },
options: { to: '0xD39902f046B5885D70e9E66594b65f84D4d1c952' }
}, 'Balance: 10 ANT'],
[{
source: 'Ethereum launched `@formatDate(date)`',
bindings: { date: int('1438269793') }
}, 'Ethereum launched Jul. 30th 2015'],
[{
source: "Ethereum launched on a `@formatDate(date, 'EEEE')` in `@formatDate(date, 'MMMM yyyy')`",
bindings: { date: int('1438269793') }
}, 'Ethereum launched on a Thursday in July 2015'],
[{
source: "Period duration is `@transformTime(time, 'day')`",
bindings: { time: int(3600 * 24 * 2 + 50) }
}, 'Period duration is 2 days'],
[{
source: "Period duration is `@transformTime(time, 'best')`",
bindings: { time: int(3600 * 24 * 30) }
}, 'Period duration is 1 month'],
[{
source: '3600 seconds is `@transformTime(3600)`',
bindings: {}
}, '3600 seconds is 1 hour'],
[{
source: "10k minutes is `@transformTime(10 ^ 4, 'second', 'minute')`",
bindings: {}
}, '10k minutes is 600000 seconds'],
[{
source: 'Hello `@fromHex(firstName)` `@fromHex(lastName, "utf8")`, `@fromHex(n, "number")` `@fromHex("0x69", "ascii")`s the definitive response.',
bindings: { firstName: bytes32('0x446f75676c6173'), lastName: bytes32('0x4164616d73'), n: bytes('0x2a') }
}, 'Hello Douglas Adams, 42 is the definitive response.'],
[{
source: 'Change required support to `@formatPct(support)`%',
bindings: { support: int((new BN(50)).mul(tenPow(16))) } // 50 * 10^16
}, 'Change required support to 50%'],
[{
source: 'Change required support to `@formatPct(support, 10 ^ 18, 1)`%',
bindings: { support: int((new BN(40)).mul(tenPow(16)).add((new BN(43)).mul(tenPow(14)))) } // 40 * 10^16 + 43 * 10^14
}, 'Change required support to ~40.4%'],
[{
source: 'The genesis block is #`@getBlock(n)`',
bindings: { n: int(0) },
options: { userHelpers: { getBlock: (eth) => async (n) => ({ type: 'string', value: (await eth.getBlock(n)).number }) } }
}, 'The genesis block is #0'],
[{
source: 'Bar `@bar(shift)` foo `@foo(n)`',
bindings: { shift: bool(true), n: int(7) },
options: { userHelpers: { bar: () => shift => ({ type: 'string', value: shift ? 'BAR' : 'bar' }), foo: () => n => ({ type: 'number', value: n * 7 }) } }
}, 'Bar BAR foo 49']
]
const dataDecodeCases = [
[{
source: 'Perform action: `@radspec(addr, data)`',
bindings: {
addr: address(),
data: bytes('0x13af40350000000000000000000000000000000000000000000000000000000000000002') // setOwner(address), on knownFunctions
}
}, 'Perform action: Set 0x0000000000000000000000000000000000000002 as the new owner'],
[{
source: 'Payroll: `@radspec(addr, data)`!',
bindings: {
addr: address(),
data: bytes('0x6881385b') // payday(), on knownFunctions
}
}, 'Payroll: Get owed Payroll allowance!'],
[{
source: 'Transfer: `@radspec(addr, data)`',
bindings: {
addr: address('0x960b236a07cf122663c4303350609a66a7b288c0'),
data: bytes('0xa9059cbb00000000000000000000000031ab1f92344e3277ce9404e4e097dab7514e6d2700000000000000000000000000000000000000000000000821ab0d4414980000') // transfer(), on knownFunctions requiring helpers
}
}, 'Transfer: Transfer 150 ANT to 0x31AB1f92344e3277ce9404E4e097dab7514E6D27'],
[{
source: 'Cast a `@radspec(addr, data)`',
bindings: {
addr: address(),
data: bytes('0xdf133bca') // vote(uint256,bool,bool), from on-chain registry
}
}, 'Cast a Vote'],
[{
source: 'Perform action: `@radspec(addr, data)`',
bindings: {
addr: address(),
data: bytes('0x12345678') // random signature
}
}, 'Perform action: Unknown function (0x12345678)'],
[{
source: 'Perform action: `@radspec(addr, data)`',
bindings: {
addr: address(),
data: bytes('0x12') // bad signature
}
}, 'Perform action: Unknown function (0x12)']
]
const cases = [
// Bindings
[{
source: 'a is `a`, b is `b` and "c d" is `c d`',
bindings: { a: int(1), b: int(2), c: int(3), d: int(4) }
}, 'a is 1, b is 2 and "c d" is 3 4'],
[{
source: "An empty string`''`",
bindings: {}
}, 'An empty string'],
// Maths
[{
source: 'Will multiply `a` by 7 and return `a * 7`',
bindings: { a: int(122) }
}, 'Will multiply 122 by 7 and return 854'],
[{
source: 'First case is `2 * 2 + 6`, second case is `2 * (2 + 6)`'
}, 'First case is 10, second case is 16'],
[{
source: 'First case is `2^5`, second case is `2^2 + 1`'
}, 'First case is 32, second case is 5'],
[{
source: 'First case is `(11 - 1) * 2^5`, second case is `3 * 2 ^ (4 - 1) + 1`'
}, 'First case is 320, second case is 25'],
[{
source: 'First case is `(11 - 1) / 2`, second case is `3 * 2 ^ (4 - 1) / 3`'
}, 'First case is 5, second case is 8'],
[{
source: 'First case is `(11 - 1) % 3`, second case is `3 * 2 % 5`'
}, 'First case is 1, second case is 1'],
[{
source: "Basic arithmetic: `a` + `b` is `a + b`, - `c` that's `a + b - c`, quick mafs",
bindings: { a: int(2), b: int(2), c: int(1) }
}, "Basic arithmetic: 2 + 2 is 4, - 1 that's 3, quick mafs"],
[{
source: 'This will default to `b`: `a || b`',
bindings: { a: int(0), b: int(1) }
}, 'This will default to 1: 1'],
[{
source: 'This will default to `a`: `a || b`',
bindings: { a: int(1), b: int(0) }
}, 'This will default to 1: 1'],
[{
source: 'This will default to `b`: `a || b`',
bindings: {
a: bytes32('0x0000000000000000000000000000000000000000000000000000000000000000'),
b: int(1)
}
}, 'This will default to 1: 1'],
// Conditionals
[{
source: 'True is not `false ? true : false`',
bindings: {}
}, 'True is not false'],
[{
source: "`a == 0x0 ? 'concat ' + a : 'else'`",
bindings: { a: address('0x0000000000000000000000000000000000000000') }
}, 'concat 0x0000000000000000000000000000000000000000'],
[{
source: "`a == 0x0 ? 'concat ' + a : 'else'`",
bindings: { a: address('0x0000000000000000000000000000000000000001') }
}, 'else'],
// External calls
[{
source: 'Allocate `amount token.symbol(): string`.',
bindings: { amount: int(100), token: address('0x960b236A07cf122663c4303350609A66A7B288C0') }
}, 'Allocate 100 ANT.'],
[{
source: 'Allocate `amount token.symbol(): string` (non-checksummed).',
bindings: { amount: int(100), token: address('0x960b236a07cf122663c4303350609a66a7b288c0') }
}, 'Allocate 100 ANT (non-checksummed).'],
[{
source: 'Burns the `token.symbol(): string` balance of `person` (balance is `token.balanceOf(person): uint256 / 1000000000000000000`)',
bindings: { token: address('0x960b236A07cf122663c4303350609A66A7B288C0'), person: address('0x0000000000000000000000000000000000000001') }
}, 'Burns the ANT balance of 0x0000000000000000000000000000000000000001 (balance is 0)'],
[{
source: 'Burns the `self.symbol(): string` balance of `person` (balance is `self.balanceOf(person): uint256 / 1000000000000000000`)',
bindings: { person: address('0x0000000000000000000000000000000000000001') },
options: { to: '0x960b236A07cf122663c4303350609A66A7B288C0' }
}, 'Burns the ANT balance of 0x0000000000000000000000000000000000000001 (balance is 0)'],
[{
source: 'Send ETH to the sale at block `((self.controller(): address).sale(): address).initialBlock(): uint` from `person`',
bindings: { person: address('0x0000000000000000000000000000000000000001') },
options: { to: '0x960b236A07cf122663c4303350609A66A7B288C0' }
}, 'Send ETH to the sale at block 3723000 from 0x0000000000000000000000000000000000000001'],
[{
source: "Initialize Finance app for Vault at `_vault` with period length of `(_periodDuration - _periodDuration % 86400) / 86400` day`_periodDuration >= 172800 ? 's' : ' '`",
bindings: { _periodDuration: int(86400 * 2), _vault: address('0x960b236A07cf122663c4303350609A66A7B288C0') }
}, 'Initialize Finance app for Vault at 0x960b236A07cf122663c4303350609A66A7B288C0 with period length of 2 days'],
[{
source: "Vote `_supports ? 'yay' : 'nay'`",
bindings: { _supports: bool(false) }
}, 'Vote nay'],
[{
source: 'Token `_amount / 10^18`',
bindings: { _amount: int(new BN(10).mul(new BN(10).pow(new BN(18)))) }
}, 'Token 10'],
[{
source: "`_bool ? 'h' + _var + 'o' : 'bye'`",
bindings: { _bool: bool(true), _var: string('ell') }
}, 'hello'],
// External calls with multiple return values
[{
source: "Explicit: Transaction with ID `txId` was sent to `self.getTransaction(txId): (uint64, uint256, uint256, uint64, address, <address>, bool, uint64)`",
bindings: { txId: { type: 'uint256', value: 1 } },
options: { to: '0xf562B25Db6e707694ceC3A4908dC58fF6bDABa40' },
}, 'Explicit: Transaction with ID 1 was sent to 0x52EC80600642CeddE9De1F570335481C348BE74C'],
[{
source: "Implicit: `token.symbol(): (string)`",
bindings: { token: address('0x960b236a07cf122663c4303350609a66a7b288c0') }
}, 'Implicit: ANT'],
[{
source: "Explicit (last type): `self.getTransaction(txId): (uint64, uint256, uint256, uint64, address, address, bool, <uint64>)`",
bindings: { txId: { type: 'uint256', value: 1 } },
options: { to: '0xf562B25Db6e707694ceC3A4908dC58fF6bDABa40' },
}, 'Explicit (last type): 1568811601'],
[{
source: "Explicit (first type): `self.getTransaction(txId): (<uint64>, uint256, uint256, uint64, address, address, bool, uint64)`",
bindings: { txId: { type: 'uint256', value: 1 } },
options: { to: '0xf562B25Db6e707694ceC3A4908dC58fF6bDABa40' },
}, 'Explicit (first type): 0'],
// msg.(sender | value | data) options
[{
source: "No value: Send `@tokenAmount(token, msg.value)` from `msg.sender` to `receiver`",
bindings: { token: address(ETH), receiver: address('0x8401Eb5ff34cc943f096A32EF3d5113FEbE8D4Eb') },
options: { from: '0xb4124cEB3451635DAcedd11767f004d8a28c6eE7'},
}, 'No value: Send 0 ETH from 0xb4124cEB3451635DAcedd11767f004d8a28c6eE7 to 0x8401Eb5ff34cc943f096A32EF3d5113FEbE8D4Eb'],
[{
source: "With value: Send `@tokenAmount(token, msg.value)` from `msg.sender` to `receiver`",
bindings: { token: address(ETH), receiver: address('0x8401Eb5ff34cc943f096A32EF3d5113FEbE8D4Eb') },
options: { from: '0xb4124cEB3451635DAcedd11767f004d8a28c6eE7', value: '1000000000000000000' },
}, 'With value: Send 1 ETH from 0xb4124cEB3451635DAcedd11767f004d8a28c6eE7 to 0x8401Eb5ff34cc943f096A32EF3d5113FEbE8D4Eb'],
[{
source: "Sending tx with data `msg.data` to contract at `contract`",
bindings: { contract: address('0x960b236A07cf122663c4303350609A66A7B288C0') },
options: { data: '0xabcdef'}
}, "Sending tx with data 0xabcdef to contract at 0x960b236A07cf122663c4303350609A66A7B288C0"],
// using msg.data on a helper
[{
source: "Performs a call to `@radspec(contract, msg.data)`",
bindings: { contract: address('0x960b236A07cf122663c4303350609A66A7B288C0') },
options: { data: keccak256(Object.keys(knownFunctions)[3]).slice(0,10) }
}, `Performs a call to ${Object.values(knownFunctions)[3]}`],
...comparisonCases,
...helperCases,
...dataDecodeCases
]
cases.forEach(([input, expected], index) => {
test(`${index} - ${input.source}`, async (t) => {
const { userHelpers } = input.options || {}
const actual = await evaluateRaw(
input.source,
input.bindings,
{
...input.options,
availableHelpers: { ...defaultHelpers, ...userHelpers }
}
)
t.is(
actual,
expected,
`Expected "${input.source}" to evaluate to "${expected}", but evaluated to "${actual}"`
)
})
})