-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate new asc features and a simple standard library
- Loading branch information
Showing
14 changed files
with
628 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Custom Ethereum standard library. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "../../node_modules/assemblyscript/std/assembly.json", | ||
"files": [ | ||
"../../node_modules/assemblyscript/std/assembly.d.ts", | ||
"./ethereum.d.ts" | ||
], | ||
"include": [ | ||
"./**/*.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "../../node_modules/assemblyscript/std/assembly.json", | ||
"include": [ | ||
"./**/*.ts" | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = <i32>allocate_memory(4); | ||
callDataCopy(ptrSelector, 0, 4); | ||
var selector = load<i32>(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 = <i32>allocate_memory(20); | ||
callDataCopy(ptrAddress, 4, 20); | ||
var ptrBalance = <i32>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 = <i32>allocate_memory(32); | ||
getCaller(ptrSender); | ||
var ptrRecipient = <i32>allocate_memory(32); | ||
callDataCopy(ptrRecipient, 4, 20); | ||
var ptrValue = <i32>allocate_memory(32); | ||
callDataCopy(ptrValue, 24, 8); | ||
// debug_printMemHex(ptrValue, 32); | ||
var ptrSenderBalance = <i32>allocate_memory(32); | ||
var ptrRecipientBalance = <i32>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<i32>(ptrSenderBalance); | ||
var recipientBalance = load<i32>(ptrRecipientBalance); | ||
var value = load<i32>(ptrValue); | ||
|
||
if (senderBalance < value) | ||
revert(0, 0); | ||
|
||
store<i32>(ptrSenderBalance, senderBalance - value); | ||
store<i32>(ptrRecipientBalance, recipientBalance + value); | ||
storageStore(ptrSender, ptrSenderBalance); | ||
storageStore(ptrRecipient, ptrRecipientBalance); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "../lib/ethereum.json", | ||
"include": [ | ||
"./**/*.ts" | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.wasm | ||
*.map |
Oops, something went wrong.