diff --git a/README.md b/README.md index de9af9c..b0d0cd2 100644 --- a/README.md +++ b/README.md @@ -7,25 +7,8 @@ The AssemblyScript project in this repository is a proof of concept barebones "E 1. Use [WebassemblyStudio](https://webassembly.studio/) or another IDE to bootstrap the project and edit `main.ts`. 2. If using WebassemblyStudio, download the project code. 3. Navigate to the top directory of the project and run `npm install`. -4. Run `npm build` to compile the AssemblyScript source to WASM bytecode. Find the compiled `wasm.out` file in the `out/` directory. At this point you have a valid WASM binary but it's not ready to be deployed onto Ethereum just yet. You need to fix the import names and namespaces. We're working on a tool to automate this process but for now this must be done by hand. -5. If you haven't already, download and compile the [WebAssembly binary toolkit (WABT)](https://github.com/WebAssembly/wabt): -``` -> git clone --recursive https://github.com/WebAssembly/wabt ~/wabt -> cd ~/wabt -> make -``` -6. Use WABT to convert the WASM bytecode to WAST (WebAssembly text format). This will let us edit the raw WAST. -``` -> ~/wabt/bin/wasm2wat main.wasm > main.wast -``` -7. Open `main.wast` in your favorite text editor. For each `import` at the top, change the `"env"` namespace to `"ethereum"` and remove the `ethereum_` from the name of the function to import. For example, this: -``` -(import "env" "ethereum_getCallDataSize" (func $main/ethereum_getCallDataSize (type $t1))) -``` -Should become this: -``` -(import "ethereum" "getCallDataSize" (func $main/ethereum_getCallDataSize (type $t1))) -``` -You now have valid ewasm-compatible WAST which is ready to be deployed to the ewasm testnet using a tool such as [ewasm-studio](https://github.com/ewasm/ewasm-studio). If you need to compile this back to WASM bytecode, you can do so using the `wabt/bin/wat2wasm` compiler. +4. Run `npm run build` to compile the AssemblyScript source to WASM bytecode. Find the compiled `main.wasm` file in the `build/` directory. At this point you have a valid WASM binary. + +You now have a valid ewasm-compatible WAST which is ready to be deployed to the ewasm testnet using a tool such as [ewasm-studio](https://github.com/ewasm/ewasm-studio). Another option is to test the code using `testeth`. See [this example](https://github.com/lrettig/tests/blob/wrc20-challenge/src/GeneralStateTestsFiller/stEWASMTests/wrc20ChallengeFiller.yml) of a test filler that fully tests this code. diff --git a/assembly/lib/README.md b/assembly/lib/README.md new file mode 100644 index 0000000..2798e83 --- /dev/null +++ b/assembly/lib/README.md @@ -0,0 +1 @@ +Custom Ethereum standard library. diff --git a/assembly/lib/ethereum.d.ts b/assembly/lib/ethereum.d.ts new file mode 100644 index 0000000..7df77b6 --- /dev/null +++ b/assembly/lib/ethereum.d.ts @@ -0,0 +1,14 @@ +// This file provides syntax highlighting for a TypeScript IDE + +declare function finish(dataOffset: i32, length: i32): void; +declare function revert(dataOffset: i32, length: i32): void; +declare function callDataCopy(resultOffset: i32, dataOffset: i32, length: i32): void; +declare function getCallDataSize(): i32; +declare function getCaller(dataOffset: i32): void; +declare function storageStore(pathOffset: i32, valueOffset: i32): void; +declare function storageLoad(pathOffset: i32, resultOffset: i32): void; +declare function printMemHex(dataOffset: i32, length: i32): void; + +declare function allocate_memory(size: usize): usize; +declare function free_memory(ptr: usize): void; +declare function reset_memory(): void; diff --git a/assembly/lib/ethereum.json b/assembly/lib/ethereum.json new file mode 100644 index 0000000..ac20b6e --- /dev/null +++ b/assembly/lib/ethereum.json @@ -0,0 +1,10 @@ +{ + "extends": "../../node_modules/assemblyscript/std/assembly.json", + "files": [ + "../../node_modules/assemblyscript/std/assembly.d.ts", + "./ethereum.d.ts" + ], + "include": [ + "./**/*.ts" + ] +} diff --git a/assembly/lib/ethereum.ts b/assembly/lib/ethereum.ts new file mode 100644 index 0000000..2f50448 --- /dev/null +++ b/assembly/lib/ethereum.ts @@ -0,0 +1,23 @@ +// This file sets up functions provided by the VM for use with AssemblyScript + +import "allocator/arena"; + +@external("return") +export declare function finish(dataOffset: i32, length: i32): void; + +export declare function revert(dataOffset: i32, length: i32): void; + +export declare function callDataCopy(resultOffset: i32, dataOffset: i32, length: i32): void; + +export declare function getCallDataSize(): i32; + +export declare function getCaller(dataOffset: i32): void; + +export declare function storageStore(pathOffset: i32, valueOffset: i32): void; + +export declare function storageLoad(pathOffset: i32, resultOffset: i32): void; + +@external("debug", "printMemHex") +export declare function printMemHex(dataOffset: i32, length: i32): void; + +// TODO: need to implement a nice wrapper over the native functions which use native types and handles the memory. diff --git a/assembly/lib/tsconfig.json b/assembly/lib/tsconfig.json new file mode 100644 index 0000000..22dcba5 --- /dev/null +++ b/assembly/lib/tsconfig.json @@ -0,0 +1,6 @@ +{ + "extends": "../../node_modules/assemblyscript/std/assembly.json", + "include": [ + "./**/*.ts" + ] +} diff --git a/assembly/main.ts b/assembly/main.ts deleted file mode 100644 index 0d9d448..0000000 --- a/assembly/main.ts +++ /dev/null @@ -1,84 +0,0 @@ -import 'allocator/arena'; - -// ewasm imports -declare function ethereum_return(dataOffset: i32, length: i32): void; -declare function ethereum_revert(dataOffset: i32, length: i32): void; -declare function ethereum_callDataCopy(resultOffset: i32, dataOffset: i32, length: i32): void; -declare function ethereum_getCallDataSize(): i32; -declare function ethereum_getCaller(dataOffset: i32): void; -declare function ethereum_storageStore(pathOffset: i32, valueOffset: i32): void; -declare function ethereum_storageLoad(pathOffset: i32, resultOffset: i32): void; -// declare function debug_printMemHex(dataOffset: i32, length: i32): void; - -// TODO: need to implement a nice wrapper over the native functions which use native types and handles the memory. - -// ewasm main function -export function main(): void { - // assume the memory is already expanded.. - // ethereum_return(0, ethereum_callDataSize()) - - // Make sure we have enough args - if (ethereum_getCallDataSize() < 4) - ethereum_revert(0, 0); - - var ptrSelector : i32 = allocate_memory(4); - ethereum_callDataCopy(ptrSelector, 0, 4); - var selector : i32 = load(ptrSelector); - switch(selector) { - case 0x9993021a: - do_balance(); - break; - case 0x5d359fbd: - do_transfer(); - break; - default: - ethereum_revert(0, 0); - } -} - -function do_balance(): void { - if (ethereum_getCallDataSize() !== 24) - ethereum_revert(0, 0); - - var ptrAddress : i32 = allocate_memory(20); - ethereum_callDataCopy(ptrAddress, 4, 20); - var ptrBalance : i32 = allocate_memory(32); - ethereum_storageLoad(ptrAddress, ptrBalance); - // debug_printMemHex(ptrAddress, 32); - // debug_printMemHex(ptrBalance, 32); - ethereum_return(ptrBalance, 32); -} - -function do_transfer(): void { - if (ethereum_getCallDataSize() !== 32) - ethereum_revert(0, 0); - - var ptrSender : i32 = allocate_memory(32); - ethereum_getCaller(ptrSender); - var ptrRecipient : i32 = allocate_memory(32); - ethereum_callDataCopy(ptrRecipient, 4, 20); - var ptrValue : i32 = allocate_memory(32); - ethereum_callDataCopy(ptrValue, 24, 8); - // debug_printMemHex(ptrValue, 32); - var ptrSenderBalance = allocate_memory(32); - var ptrRecipientBalance = allocate_memory(32); - ethereum_storageLoad(ptrSender, ptrSenderBalance); - ethereum_storageLoad(ptrRecipient, ptrRecipientBalance); - // debug_printMemHex(ptrSender, 32); - // debug_printMemHex(ptrRecipient, 32); - // debug_printMemHex(ptrSenderBalance, 32); - // debug_printMemHex(ptrRecipientBalance, 32); - - var senderBalance = load(ptrSenderBalance); - var recipientBalance = load(ptrRecipientBalance); - var value = load(ptrValue); - - if (senderBalance < value) - ethereum_revert(0, 0); - - store(ptrSenderBalance, senderBalance - value); - store(ptrRecipientBalance, recipientBalance + value); - ethereum_storageStore(ptrSender, ptrSenderBalance); - ethereum_storageStore(ptrRecipient, ptrRecipientBalance); -} - diff --git a/assembly/src/main.ts b/assembly/src/main.ts new file mode 100644 index 0000000..3394346 --- /dev/null +++ b/assembly/src/main.ts @@ -0,0 +1,74 @@ +const DEBUG = false; + +// ewasm main function +export function main(): void { + // assume the memory is already expanded.. + // ethereum_return(0, ethereum_callDataSize()) + + // Make sure we have enough args + if (getCallDataSize() < 4) + revert(0, 0); + + var ptrSelector = allocate_memory(4); + callDataCopy(ptrSelector, 0, 4); + var selector = load(ptrSelector); + switch(selector) { + case 0x9993021a: + do_balance(); + break; + case 0x5d359fbd: + do_transfer(); + break; + default: + revert(0, 0); + } +} + +function do_balance(): void { + if (getCallDataSize() !== 24) + revert(0, 0); + + var ptrAddress = allocate_memory(20); + callDataCopy(ptrAddress, 4, 20); + var ptrBalance = allocate_memory(32); + storageLoad(ptrAddress, ptrBalance); + if (DEBUG) { + printMemHex(ptrAddress, 32); + printMemHex(ptrBalance, 32); + } + finish(ptrBalance, 32); +} + +function do_transfer(): void { + if (getCallDataSize() !== 32) + revert(0, 0); + + var ptrSender = allocate_memory(32); + getCaller(ptrSender); + var ptrRecipient = allocate_memory(32); + callDataCopy(ptrRecipient, 4, 20); + var ptrValue = allocate_memory(32); + callDataCopy(ptrValue, 24, 8); + // debug_printMemHex(ptrValue, 32); + var ptrSenderBalance = allocate_memory(32); + var ptrRecipientBalance = allocate_memory(32); + storageLoad(ptrSender, ptrSenderBalance); + storageLoad(ptrRecipient, ptrRecipientBalance); + if (DEBUG) { + printMemHex(ptrSender, 32); + printMemHex(ptrRecipient, 32); + printMemHex(ptrSenderBalance, 32); + printMemHex(ptrRecipientBalance, 32); + } + var senderBalance = load(ptrSenderBalance); + var recipientBalance = load(ptrRecipientBalance); + var value = load(ptrValue); + + if (senderBalance < value) + revert(0, 0); + + store(ptrSenderBalance, senderBalance - value); + store(ptrRecipientBalance, recipientBalance + value); + storageStore(ptrSender, ptrSenderBalance); + storageStore(ptrRecipient, ptrRecipientBalance); +} diff --git a/assembly/src/tsconfig.json b/assembly/src/tsconfig.json new file mode 100644 index 0000000..b150518 --- /dev/null +++ b/assembly/src/tsconfig.json @@ -0,0 +1,6 @@ +{ + "extends": "../lib/ethereum.json", + "include": [ + "./**/*.ts" + ] +} diff --git a/assembly/tsconfig.json b/assembly/tsconfig.json deleted file mode 100644 index c76b0d8..0000000 --- a/assembly/tsconfig.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "extends": "../node_modules/assemblyscript/std/assembly.json", - "include": [ - "./**/*.ts" - ] -} diff --git a/build/.gitignore b/build/.gitignore new file mode 100644 index 0000000..2a77455 --- /dev/null +++ b/build/.gitignore @@ -0,0 +1,2 @@ +*.wasm +*.map diff --git a/build/main.wat b/build/main.wat new file mode 100644 index 0000000..a37a3ac --- /dev/null +++ b/build/main.wat @@ -0,0 +1,479 @@ +(module + (type $v (func)) + (type $i (func (result i32))) + (type $iiv (func (param i32 i32))) + (type $ii (func (param i32) (result i32))) + (type $iiiv (func (param i32 i32 i32))) + (type $iv (func (param i32))) + (import "ethereum" "getCallDataSize" (func $~lib/ethereum/getCallDataSize (result i32))) + (import "ethereum" "revert" (func $~lib/ethereum/revert (param i32 i32))) + (import "ethereum" "callDataCopy" (func $~lib/ethereum/callDataCopy (param i32 i32 i32))) + (import "ethereum" "storageLoad" (func $~lib/ethereum/storageLoad (param i32 i32))) + (import "ethereum" "return" (func $~lib/ethereum/finish (param i32 i32))) + (import "ethereum" "getCaller" (func $~lib/ethereum/getCaller (param i32))) + (import "ethereum" "storageStore" (func $~lib/ethereum/storageStore (param i32 i32))) + (global $~lib/allocator/arena/startOffset (mut i32) (i32.const 0)) + (global $~lib/allocator/arena/offset (mut i32) (i32.const 0)) + (global $~started (mut i32) (i32.const 0)) + (global $HEAP_BASE i32 (i32.const 8)) + (memory $0 0) + (export "main" (func $main/main)) + (export "memory" (memory $0)) + (func $~lib/allocator/arena/allocate_memory (; 7 ;) (type $ii) (param $0 i32) (result i32) + (local $1 i32) + (local $2 i32) + (local $3 i32) + (local $4 i32) + ;;@ ~lib/allocator/arena.ts:17:2 + (if + ;;@ ~lib/allocator/arena.ts:17:6 + (get_local $0) + ;;@ ~lib/allocator/arena.ts:17:12 + (block + ;;@ ~lib/allocator/arena.ts:18:4 + (if + ;;@ ~lib/allocator/arena.ts:18:8 + (i32.gt_u + (get_local $0) + ;;@ ~lib/allocator/arena.ts:18:15 + (i32.const 1073741824) + ) + ;;@ ~lib/allocator/arena.ts:18:28 + (unreachable) + ) + ;;@ ~lib/allocator/arena.ts:22:4 + (if + ;;@ ~lib/allocator/arena.ts:22:8 + (i32.gt_u + ;;@ ~lib/allocator/arena.ts:20:4 + (tee_local $0 + ;;@ ~lib/allocator/arena.ts:20:17 + (i32.and + (i32.add + ;;@ ~lib/allocator/arena.ts:20:18 + (i32.add + ;;@ ~lib/allocator/arena.ts:19:4 + (tee_local $1 + ;;@ ~lib/allocator/arena.ts:19:14 + (get_global $~lib/allocator/arena/offset) + ) + ;;@ ~lib/allocator/arena.ts:20:24 + (get_local $0) + ) + ;;@ ~lib/allocator/arena.ts:20:31 + (i32.const 7) + ) + (i32.const -8) + ) + ) + ;;@ ~lib/allocator/arena.ts:22:17 + (i32.shl + ;;@ ~lib/allocator/arena.ts:21:4 + (tee_local $2 + ;;@ ~lib/allocator/arena.ts:21:22 + (current_memory) + ) + ;;@ ~lib/allocator/arena.ts:22:39 + (i32.const 16) + ) + ) + ;;@ ~lib/allocator/arena.ts:25:6 + (if + ;;@ ~lib/allocator/arena.ts:25:10 + (i32.lt_s + (grow_memory + ;;@ ~lib/allocator/arena.ts:24:24 + (select + ;;@ ~lib/allocator/arena.ts:24:28 + (get_local $2) + (tee_local $4 + ;;@ ~lib/allocator/arena.ts:23:6 + (tee_local $3 + ;;@ ~lib/allocator/arena.ts:23:24 + (i32.shr_u + (i32.and + ;;@ ~lib/allocator/arena.ts:23:25 + (i32.add + ;;@ ~lib/allocator/arena.ts:23:26 + (i32.sub + (get_local $0) + ;;@ ~lib/allocator/arena.ts:23:35 + (get_local $1) + ) + ;;@ ~lib/allocator/arena.ts:23:41 + (i32.const 65535) + ) + (i32.const -65536) + ) + ;;@ ~lib/allocator/arena.ts:23:64 + (i32.const 16) + ) + ) + ) + (i32.gt_s + (get_local $2) + (get_local $4) + ) + ) + ) + ;;@ ~lib/allocator/arena.ts:25:37 + (i32.const 0) + ) + ;;@ ~lib/allocator/arena.ts:25:40 + (if + ;;@ ~lib/allocator/arena.ts:26:12 + (i32.lt_s + (grow_memory + ;;@ ~lib/allocator/arena.ts:26:24 + (get_local $3) + ) + ;;@ ~lib/allocator/arena.ts:26:39 + (i32.const 0) + ) + ;;@ ~lib/allocator/arena.ts:26:42 + (unreachable) + ) + ) + ) + ;;@ ~lib/allocator/arena.ts:31:4 + (set_global $~lib/allocator/arena/offset + ;;@ ~lib/allocator/arena.ts:31:13 + (get_local $0) + ) + ;;@ ~lib/allocator/arena.ts:32:11 + (return + (get_local $1) + ) + ) + ) + ;;@ ~lib/allocator/arena.ts:34:9 + (i32.const 0) + ) + (func $main/do_balance (; 8 ;) (type $v) + (local $0 i32) + ;;@ main.ts:28:2 + (if + ;;@ main.ts:28:6 + (i32.ne + (call $~lib/ethereum/getCallDataSize) + ;;@ main.ts:28:28 + (i32.const 24) + ) + ;;@ main.ts:29:4 + (call $~lib/ethereum/revert + ;;@ main.ts:29:11 + (i32.const 0) + ;;@ main.ts:29:14 + (i32.const 0) + ) + ) + ;;@ main.ts:32:2 + (call $~lib/ethereum/callDataCopy + ;;@ main.ts:31:2 + (tee_local $0 + ;;@ main.ts:31:19 + (call $~lib/allocator/arena/allocate_memory + ;;@ main.ts:31:40 + (i32.const 20) + ) + ) + ;;@ main.ts:32:27 + (i32.const 4) + ;;@ main.ts:32:30 + (i32.const 20) + ) + ;;@ main.ts:34:2 + (call $~lib/ethereum/storageLoad + ;;@ main.ts:34:14 + (get_local $0) + ;;@ main.ts:33:2 + (tee_local $0 + ;;@ main.ts:33:19 + (call $~lib/allocator/arena/allocate_memory + ;;@ main.ts:33:40 + (i32.const 32) + ) + ) + ) + ;;@ main.ts:39:2 + (call $~lib/ethereum/finish + ;;@ main.ts:39:9 + (get_local $0) + ;;@ main.ts:39:21 + (i32.const 32) + ) + ) + (func $main/do_transfer (; 9 ;) (type $v) + (local $0 i32) + (local $1 i32) + (local $2 i32) + (local $3 i32) + (local $4 i32) + (local $5 i32) + (local $6 i32) + ;;@ main.ts:43:2 + (if + ;;@ main.ts:43:6 + (i32.ne + (call $~lib/ethereum/getCallDataSize) + ;;@ main.ts:43:28 + (i32.const 32) + ) + ;;@ main.ts:44:4 + (call $~lib/ethereum/revert + ;;@ main.ts:44:11 + (i32.const 0) + ;;@ main.ts:44:14 + (i32.const 0) + ) + ) + ;;@ main.ts:47:2 + (call $~lib/ethereum/getCaller + ;;@ main.ts:46:2 + (tee_local $3 + ;;@ main.ts:46:18 + (call $~lib/allocator/arena/allocate_memory + ;;@ main.ts:46:39 + (i32.const 32) + ) + ) + ) + ;;@ main.ts:49:2 + (call $~lib/ethereum/callDataCopy + ;;@ main.ts:48:2 + (tee_local $4 + ;;@ main.ts:48:21 + (call $~lib/allocator/arena/allocate_memory + ;;@ main.ts:48:42 + (i32.const 32) + ) + ) + ;;@ main.ts:49:29 + (i32.const 4) + ;;@ main.ts:49:32 + (i32.const 20) + ) + ;;@ main.ts:51:2 + (call $~lib/ethereum/callDataCopy + ;;@ main.ts:50:2 + (tee_local $0 + ;;@ main.ts:50:17 + (call $~lib/allocator/arena/allocate_memory + ;;@ main.ts:50:38 + (i32.const 32) + ) + ) + ;;@ main.ts:51:25 + (i32.const 24) + ;;@ main.ts:51:29 + (i32.const 8) + ) + ;;@ main.ts:53:2 + (set_local $1 + ;;@ main.ts:53:25 + (call $~lib/allocator/arena/allocate_memory + ;;@ main.ts:53:46 + (i32.const 32) + ) + ) + ;;@ main.ts:54:2 + (set_local $2 + ;;@ main.ts:54:28 + (call $~lib/allocator/arena/allocate_memory + ;;@ main.ts:54:49 + (i32.const 32) + ) + ) + ;;@ main.ts:55:2 + (call $~lib/ethereum/storageLoad + ;;@ main.ts:55:14 + (get_local $3) + ;;@ main.ts:55:25 + (get_local $1) + ) + ;;@ main.ts:56:2 + (call $~lib/ethereum/storageLoad + ;;@ main.ts:56:14 + (get_local $4) + ;;@ main.ts:56:28 + (get_local $2) + ) + ;;@ main.ts:64:2 + (set_local $5 + ;;@ main.ts:64:25 + (i32.load + ;;@ main.ts:64:35 + (get_local $2) + ) + ) + ;;@ main.ts:67:2 + (if + ;;@ main.ts:67:6 + (i32.lt_s + ;;@ main.ts:63:2 + (tee_local $6 + ;;@ main.ts:63:22 + (i32.load + ;;@ main.ts:63:32 + (get_local $1) + ) + ) + ;;@ main.ts:65:2 + (tee_local $0 + ;;@ main.ts:65:14 + (i32.load + ;;@ main.ts:65:24 + (get_local $0) + ) + ) + ) + ;;@ main.ts:68:4 + (call $~lib/ethereum/revert + ;;@ main.ts:68:11 + (i32.const 0) + ;;@ main.ts:68:14 + (i32.const 0) + ) + ) + ;;@ main.ts:70:2 + (i32.store + ;;@ main.ts:70:13 + (get_local $1) + ;;@ main.ts:70:31 + (i32.sub + (get_local $6) + ;;@ main.ts:70:47 + (get_local $0) + ) + ) + ;;@ main.ts:71:2 + (i32.store + ;;@ main.ts:71:13 + (get_local $2) + ;;@ main.ts:71:34 + (i32.add + (get_local $5) + ;;@ main.ts:71:53 + (get_local $0) + ) + ) + ;;@ main.ts:72:2 + (call $~lib/ethereum/storageStore + ;;@ main.ts:72:15 + (get_local $3) + ;;@ main.ts:72:26 + (get_local $1) + ) + ;;@ main.ts:73:2 + (call $~lib/ethereum/storageStore + ;;@ main.ts:73:15 + (get_local $4) + ;;@ main.ts:73:29 + (get_local $2) + ) + ) + (func $main/main (; 10 ;) (type $v) + (local $0 i32) + (if + (i32.eqz + (get_global $~started) + ) + (block + (call $start) + (set_global $~started + (i32.const 1) + ) + ) + ) + ;;@ main.ts:9:2 + (if + ;;@ main.ts:9:6 + (i32.lt_s + (call $~lib/ethereum/getCallDataSize) + ;;@ main.ts:9:26 + (i32.const 4) + ) + ;;@ main.ts:10:4 + (call $~lib/ethereum/revert + ;;@ main.ts:10:11 + (i32.const 0) + ;;@ main.ts:10:14 + (i32.const 0) + ) + ) + ;;@ main.ts:13:2 + (call $~lib/ethereum/callDataCopy + ;;@ main.ts:12:2 + (tee_local $0 + ;;@ main.ts:12:20 + (call $~lib/allocator/arena/allocate_memory + ;;@ main.ts:12:41 + (i32.const 4) + ) + ) + ;;@ main.ts:13:28 + (i32.const 0) + ;;@ main.ts:13:31 + (i32.const 4) + ) + ;;@ main.ts:15:2 + (block $break|0 + (block $case2|0 + (block $case1|0 + (if + (i32.ne + (tee_local $0 + ;;@ main.ts:14:17 + (i32.load + ;;@ main.ts:14:27 + (get_local $0) + ) + ) + ;;@ main.ts:16:9 + (i32.const -1718418918) + ) + (block + (br_if $case1|0 + (i32.eq + (get_local $0) + ;;@ main.ts:19:9 + (i32.const 1563795389) + ) + ) + (br $case2|0) + ) + ) + ;;@ main.ts:17:6 + (call $main/do_balance) + ;;@ main.ts:18:6 + (br $break|0) + ) + ;;@ main.ts:20:6 + (call $main/do_transfer) + ;;@ main.ts:21:6 + (br $break|0) + ) + ;;@ main.ts:23:6 + (call $~lib/ethereum/revert + ;;@ main.ts:23:13 + (i32.const 0) + ;;@ main.ts:23:16 + (i32.const 0) + ) + ) + ) + (func $start (; 11 ;) (type $v) + (set_global $~lib/allocator/arena/startOffset + (i32.and + (i32.add + (get_global $HEAP_BASE) + (i32.const 7) + ) + (i32.const -8) + ) + ) + (set_global $~lib/allocator/arena/offset + (get_global $~lib/allocator/arena/startOffset) + ) + ) +) diff --git a/gulpfile.js b/gulpfile.js index 7a167f9..9bd741a 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -4,8 +4,12 @@ gulp.task("build", callback => { const asc = require("assemblyscript/bin/asc"); asc.main([ "main.ts", - "--baseDir", "assembly", - "--binaryFile", "../out/main.wasm", + "--lib" , "assembly/lib", + "--baseDir", "assembly/src", + "--binaryFile", "../../build/main.wasm", + "--textFile", "../../build/main.wat", + "--validate", + "--optimize", "--sourceMap", "--measure" ], callback); diff --git a/src/main.js b/src/main.js index e5a1fc3..90bbda0 100644 --- a/src/main.js +++ b/src/main.js @@ -1,10 +1,10 @@ -WebAssembly.instantiateStreaming(fetch("../out/main.wasm"), { - env: { - ethereum_callDataSize: function() { +WebAssembly.instantiateStreaming(fetch("../build/main.wasm"), { + ethereum: { + callDataSize: function() { console.log("callDataSize -> 32"); return "9993021aed09375dc6b20050d242d1611af97ee4a6e93cad".length/2; }, - ethereum_return: function(offset, length) { + "return": function(offset, length) { console.log("return(" + offset + "," + length + ")"); } }