Skip to content

Commit 4e925c3

Browse files
dcodeIOaxic
authored andcommitted
Integrate new asc features and a simple standard library
1 parent efed05d commit 4e925c3

File tree

14 files changed

+628
-116
lines changed

14 files changed

+628
-116
lines changed

README.md

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,8 @@ The AssemblyScript project in this repository is a proof of concept barebones "E
77
1. Use [WebassemblyStudio](https://webassembly.studio/) or another IDE to bootstrap the project and edit `main.ts`.
88
2. If using WebassemblyStudio, download the project code.
99
3. Navigate to the top directory of the project and run `npm install`.
10-
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.
11-
5. If you haven't already, download and compile the [WebAssembly binary toolkit (WABT)](https://github.com/WebAssembly/wabt):
12-
```
13-
> git clone --recursive https://github.com/WebAssembly/wabt ~/wabt
14-
> cd ~/wabt
15-
> make
16-
```
17-
6. Use WABT to convert the WASM bytecode to WAST (WebAssembly text format). This will let us edit the raw WAST.
18-
```
19-
> ~/wabt/bin/wasm2wat main.wasm > main.wast
20-
```
21-
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:
22-
```
23-
(import "env" "ethereum_getCallDataSize" (func $main/ethereum_getCallDataSize (type $t1)))
24-
```
25-
Should become this:
26-
```
27-
(import "ethereum" "getCallDataSize" (func $main/ethereum_getCallDataSize (type $t1)))
28-
```
29-
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.
10+
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.
11+
12+
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).
3013

3114
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.

assembly/lib/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Custom Ethereum standard library.

assembly/lib/ethereum.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// This file provides syntax highlighting for a TypeScript IDE
2+
3+
declare function finish(dataOffset: i32, length: i32): void;
4+
declare function revert(dataOffset: i32, length: i32): void;
5+
declare function callDataCopy(resultOffset: i32, dataOffset: i32, length: i32): void;
6+
declare function getCallDataSize(): i32;
7+
declare function getCaller(dataOffset: i32): void;
8+
declare function storageStore(pathOffset: i32, valueOffset: i32): void;
9+
declare function storageLoad(pathOffset: i32, resultOffset: i32): void;
10+
declare function printMemHex(dataOffset: i32, length: i32): void;
11+
12+
declare function allocate_memory(size: usize): usize;
13+
declare function free_memory(ptr: usize): void;
14+
declare function reset_memory(): void;

assembly/lib/ethereum.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../../node_modules/assemblyscript/std/assembly.json",
3+
"files": [
4+
"../../node_modules/assemblyscript/std/assembly.d.ts",
5+
"./ethereum.d.ts"
6+
],
7+
"include": [
8+
"./**/*.ts"
9+
]
10+
}

assembly/lib/ethereum.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// This file sets up functions provided by the VM for use with AssemblyScript
2+
3+
import "allocator/arena";
4+
5+
@external("return")
6+
export declare function finish(dataOffset: i32, length: i32): void;
7+
8+
export declare function revert(dataOffset: i32, length: i32): void;
9+
10+
export declare function callDataCopy(resultOffset: i32, dataOffset: i32, length: i32): void;
11+
12+
export declare function getCallDataSize(): i32;
13+
14+
export declare function getCaller(dataOffset: i32): void;
15+
16+
export declare function storageStore(pathOffset: i32, valueOffset: i32): void;
17+
18+
export declare function storageLoad(pathOffset: i32, resultOffset: i32): void;
19+
20+
@external("debug", "printMemHex")
21+
export declare function printMemHex(dataOffset: i32, length: i32): void;
22+
23+
// TODO: need to implement a nice wrapper over the native functions which use native types and handles the memory.

assembly/lib/tsconfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "../../node_modules/assemblyscript/std/assembly.json",
3+
"include": [
4+
"./**/*.ts"
5+
]
6+
}

assembly/main.ts

Lines changed: 0 additions & 84 deletions
This file was deleted.

assembly/src/main.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+
}

assembly/src/tsconfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "../lib/ethereum.json",
3+
"include": [
4+
"./**/*.ts"
5+
]
6+
}

assembly/tsconfig.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)