Skip to content

Commit a282313

Browse files
lrettigpoemm
authored andcommitted
add assemblyscript
1 parent 1d2327c commit a282313

File tree

4 files changed

+358
-0
lines changed

4 files changed

+358
-0
lines changed

AssemblyScript/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Contents:
2+
```
3+
wrc20.ts AssemblyScript source code for wrc20.
4+
wrc20.wasm Compiled WebAssembly for wrc20.
5+
wrc20.wat Text version of wrc20.wasm.
6+
```
7+
8+
For compilation instructions and updates to the source code, see: https://github.com/ewasm/assemblyscript-ewasm-api .
9+

AssemblyScript/wrc20.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const DEBUG = false;
2+
3+
// ewasm main function
4+
export function main(): void {
5+
// assume the memory is already expanded..
6+
// ethereum_return(0, ethereum_callDataSize())
7+
8+
// Make sure we have enough args
9+
if (getCallDataSize() < 4)
10+
revert(0, 0);
11+
12+
var ptrSelector = <i32>allocate_memory(4);
13+
callDataCopy(ptrSelector, 0, 4);
14+
var selector = load<i32>(ptrSelector);
15+
switch(selector) {
16+
case 0x9993021a:
17+
do_balance();
18+
break;
19+
case 0x5d359fbd:
20+
do_transfer();
21+
break;
22+
default:
23+
revert(0, 0);
24+
}
25+
}
26+
27+
function do_balance(): void {
28+
if (getCallDataSize() !== 24)
29+
revert(0, 0);
30+
31+
var ptrAddress = <i32>allocate_memory(20);
32+
callDataCopy(ptrAddress, 4, 20);
33+
var ptrBalance = <i32>allocate_memory(32);
34+
storageLoad(ptrAddress, ptrBalance);
35+
if (DEBUG) {
36+
printMemHex(ptrAddress, 32);
37+
printMemHex(ptrBalance, 32);
38+
}
39+
finish(ptrBalance, 32);
40+
}
41+
42+
function do_transfer(): void {
43+
if (getCallDataSize() !== 32)
44+
revert(0, 0);
45+
46+
var ptrSender = <i32>allocate_memory(32);
47+
getCaller(ptrSender);
48+
var ptrRecipient = <i32>allocate_memory(32);
49+
callDataCopy(ptrRecipient, 4, 20);
50+
var ptrValue = <i32>allocate_memory(32);
51+
callDataCopy(ptrValue, 24, 8);
52+
// debug_printMemHex(ptrValue, 32);
53+
var ptrSenderBalance = <i32>allocate_memory(32);
54+
var ptrRecipientBalance = <i32>allocate_memory(32);
55+
storageLoad(ptrSender, ptrSenderBalance);
56+
storageLoad(ptrRecipient, ptrRecipientBalance);
57+
if (DEBUG) {
58+
printMemHex(ptrSender, 32);
59+
printMemHex(ptrRecipient, 32);
60+
printMemHex(ptrSenderBalance, 32);
61+
printMemHex(ptrRecipientBalance, 32);
62+
}
63+
var senderBalance = load<i32>(ptrSenderBalance);
64+
var recipientBalance = load<i32>(ptrRecipientBalance);
65+
var value = load<i32>(ptrValue);
66+
67+
if (senderBalance < value)
68+
revert(0, 0);
69+
70+
store<i32>(ptrSenderBalance, senderBalance - value);
71+
store<i32>(ptrRecipientBalance, recipientBalance + value);
72+
storageStore(ptrSender, ptrSenderBalance);
73+
storageStore(ptrRecipient, ptrRecipientBalance);
74+
}

AssemblyScript/wrc20.wasm

781 Bytes
Binary file not shown.

AssemblyScript/wrc20.wat

Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
(module
2+
(type (;0;) (func))
3+
(type (;1;) (func (result i32)))
4+
(type (;2;) (func (param i32 i32)))
5+
(type (;3;) (func (param i32) (result i32)))
6+
(type (;4;) (func (param i32 i32 i32)))
7+
(type (;5;) (func (param i32)))
8+
(import "ethereum" "getCallDataSize" (func (;0;) (type 1)))
9+
(import "ethereum" "revert" (func (;1;) (type 2)))
10+
(import "ethereum" "callDataCopy" (func (;2;) (type 4)))
11+
(import "ethereum" "storageLoad" (func (;3;) (type 2)))
12+
(import "ethereum" "return" (func (;4;) (type 2)))
13+
(import "ethereum" "getCaller" (func (;5;) (type 5)))
14+
(import "ethereum" "storageStore" (func (;6;) (type 2)))
15+
(import "debug" "printMemHex" (func (;7;) (type 2)))
16+
(func (;8;) (type 3) (param i32) (result i32)
17+
(local i32 i32 i32 i32 i32 i32)
18+
get_local 0
19+
if ;; label = @1
20+
get_local 0
21+
i32.const 1073741824
22+
i32.gt_u
23+
if ;; label = @2
24+
unreachable
25+
end
26+
get_global 5
27+
set_local 1
28+
get_local 1
29+
get_local 0
30+
i32.add
31+
i32.const 7
32+
i32.add
33+
i32.const 7
34+
i32.const -1
35+
i32.xor
36+
i32.and
37+
set_local 2
38+
memory.size
39+
set_local 3
40+
get_local 2
41+
get_local 3
42+
i32.const 16
43+
i32.shl
44+
i32.gt_u
45+
if ;; label = @2
46+
get_local 2
47+
get_local 1
48+
i32.sub
49+
i32.const 65535
50+
i32.add
51+
i32.const 65535
52+
i32.const -1
53+
i32.xor
54+
i32.and
55+
i32.const 16
56+
i32.shr_u
57+
set_local 4
58+
get_local 3
59+
tee_local 5
60+
get_local 4
61+
tee_local 6
62+
get_local 5
63+
get_local 6
64+
i32.gt_s
65+
select
66+
set_local 5
67+
get_local 5
68+
memory.grow
69+
i32.const 0
70+
i32.lt_s
71+
if ;; label = @3
72+
get_local 4
73+
memory.grow
74+
i32.const 0
75+
i32.lt_s
76+
if ;; label = @4
77+
unreachable
78+
end
79+
end
80+
end
81+
get_local 2
82+
set_global 5
83+
get_local 1
84+
return
85+
end
86+
i32.const 0)
87+
(func (;9;) (type 0)
88+
(local i32 i32)
89+
call 0
90+
i32.const 24
91+
i32.ne
92+
if ;; label = @1
93+
i32.const 0
94+
i32.const 0
95+
call 1
96+
end
97+
i32.const 20
98+
call 8
99+
set_local 0
100+
get_local 0
101+
i32.const 4
102+
i32.const 20
103+
call 2
104+
i32.const 32
105+
call 8
106+
set_local 1
107+
get_local 0
108+
get_local 1
109+
call 3
110+
get_local 0
111+
i32.const 32
112+
call 7
113+
get_local 1
114+
i32.const 32
115+
call 7
116+
get_local 1
117+
i32.const 32
118+
call 4)
119+
(func (;10;) (type 0)
120+
(local i32 i32 i32 i32 i32 i32 i32 i32)
121+
call 0
122+
i32.const 32
123+
i32.ne
124+
if ;; label = @1
125+
i32.const 0
126+
i32.const 0
127+
call 1
128+
end
129+
i32.const 32
130+
call 8
131+
set_local 0
132+
get_local 0
133+
call 5
134+
i32.const 32
135+
call 8
136+
set_local 1
137+
get_local 1
138+
i32.const 4
139+
i32.const 20
140+
call 2
141+
i32.const 32
142+
call 8
143+
set_local 2
144+
get_local 2
145+
i32.const 24
146+
i32.const 8
147+
call 2
148+
get_local 2
149+
i32.const 32
150+
call 7
151+
i32.const 32
152+
call 8
153+
set_local 3
154+
i32.const 32
155+
call 8
156+
set_local 4
157+
get_local 0
158+
get_local 3
159+
call 3
160+
get_local 1
161+
get_local 4
162+
call 3
163+
get_local 0
164+
i32.const 32
165+
call 7
166+
get_local 1
167+
i32.const 32
168+
call 7
169+
get_local 3
170+
i32.const 32
171+
call 7
172+
get_local 4
173+
i32.const 32
174+
call 7
175+
get_local 3
176+
i32.load
177+
set_local 5
178+
get_local 4
179+
i32.load
180+
set_local 6
181+
get_local 2
182+
i32.load
183+
set_local 7
184+
get_local 5
185+
get_local 7
186+
i32.lt_s
187+
if ;; label = @1
188+
i32.const 0
189+
i32.const 0
190+
call 1
191+
end
192+
get_local 3
193+
get_local 5
194+
get_local 7
195+
i32.sub
196+
i32.store
197+
get_local 4
198+
get_local 6
199+
get_local 7
200+
i32.add
201+
i32.store
202+
get_local 0
203+
get_local 3
204+
call 6
205+
get_local 1
206+
get_local 4
207+
call 6)
208+
(func (;11;) (type 0)
209+
(local i32 i32 i32)
210+
call 0
211+
i32.const 4
212+
i32.lt_s
213+
if ;; label = @1
214+
i32.const 0
215+
i32.const 0
216+
call 1
217+
end
218+
i32.const 4
219+
call 8
220+
set_local 0
221+
get_local 0
222+
i32.const 0
223+
i32.const 4
224+
call 2
225+
get_local 0
226+
i32.load
227+
set_local 1
228+
block ;; label = @1
229+
block ;; label = @2
230+
block ;; label = @3
231+
block ;; label = @4
232+
get_local 1
233+
set_local 2
234+
get_local 2
235+
i32.const -1718418918
236+
i32.eq
237+
br_if 0 (;@4;)
238+
get_local 2
239+
i32.const 1563795389
240+
i32.eq
241+
br_if 1 (;@3;)
242+
br 2 (;@2;)
243+
end
244+
call 9
245+
br 2 (;@1;)
246+
end
247+
call 10
248+
br 1 (;@1;)
249+
end
250+
i32.const 0
251+
i32.const 0
252+
call 1
253+
end)
254+
(func (;12;) (type 0)
255+
get_global 6
256+
i32.const 7
257+
i32.add
258+
i32.const 7
259+
i32.const -1
260+
i32.xor
261+
i32.and
262+
set_global 4
263+
get_global 4
264+
set_global 5)
265+
(memory (;0;) 1)
266+
(global (;0;) i32 (i32.const 3))
267+
(global (;1;) i32 (i32.const 8))
268+
(global (;2;) i32 (i32.const 7))
269+
(global (;3;) i32 (i32.const 1073741824))
270+
(global (;4;) (mut i32) (i32.const 0))
271+
(global (;5;) (mut i32) (i32.const 0))
272+
(global (;6;) i32 (i32.const 8))
273+
(export "main" (func 11))
274+
(export "memory" (memory 0))
275+
(start 12))

0 commit comments

Comments
 (0)