Created
September 26, 2019 15:39
-
-
Save jacqueswww/cc01530da16677902e262174789b68d9 to your computer and use it in GitHub Desktop.
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
Final Contract Code: | |
proc copy_into_ba(to_ba: var auto; offset: int; from_ba: auto) = | |
for i, x in from_ba: | |
if offset + i > sizeof(to_ba) - 1: | |
break | |
to_ba[offset + i] = x | |
func to_bytes32(a: uint128): bytes32 = | |
var tmp: bytes32 | |
copy_into_ba(tmp, 0, a.toBytesLE) | |
tmp | |
func to_uint128(a: bytes32): uint128 = | |
var | |
tmp_ba: array[32, byte] | |
tmp: uint128 | |
copy_into_ba(tmp_ba, 0, a) | |
tmp = fromBytes(Uint128, tmp_ba) | |
tmp | |
proc assertNotPayable() = | |
var b {.noinit.}: array[16, byte] | |
getCallValue(addr b) | |
if Uint128.fromBytesBE(b) > 0.stuint(128): | |
revert(nil, 0) | |
proc exitMessage(s: string) = | |
revert(cstring(s), s.len.int32) | |
proc concat[I1, I2: static[int]; T](a: array[I1, T]; b: array[I2, T]): array[I1 + I2, T] = | |
result[0 .. a.high] = a | |
result[a.len .. result.high] = b | |
proc getHashedKey[N](table_id: int32; key: array[N, byte]): bytes32 = | |
type | |
CombinedKey {.packed.} = object | |
table_id: int32 | |
key: array[N, byte] | |
var | |
hashed_key {.noinit.}: bytes32 | |
combined_key: CombinedKey | |
sha256_address: array[20, byte] | |
sha256_address[19] = 2'u8 | |
combined_key.table_id = table_id | |
combined_key.key = key | |
var res = call(getGasLeft(), addr sha256_address, nil, addr combined_key, | |
sizeof(combined_key).int32) | |
if res == 1: | |
exitMessage("Could not call sha256 in setTableValue") | |
if getReturnDataSize() != 32.int32: | |
exitMessage("Could not call sha256, Incorrect return size") | |
returnDataCopy(addr hashed_key, 0.int32, 32.int32) | |
hashed_key | |
proc setTableValue[N](table_id: int32; key: array[N, byte]; value: var bytes32) = | |
var hashed_key = getHashedKey(table_id, key) | |
storageStore(hashed_key, addr value) | |
proc getTableValue[N](table_id: int32; key: array[N, byte]): bytes32 = | |
var | |
value: bytes32 | |
hashed_key: bytes32 = getHashedKey(table_id, key) | |
storageLoad(hashed_key, addr value) | |
value | |
proc becomeKing*(name: bytes32) = | |
var msg_sender_tmp_variable_alloc {.noinit.}: address | |
getCaller(addr msg_sender_tmp_variable_alloc) | |
proc msg_value_func(): uint128 = | |
var ba {.noinit.}: array[16, byte] | |
getCallValue(addr ba) | |
var val {.noinit.}: Stuint[128] | |
{.pragma: restrict, codegenDecl: "$# __restrict $#".} | |
let r_ptr {.restrict.} = cast[ptr array[128, byte]](addr val) | |
for i, b in ba: | |
r_ptr[i] = b | |
return val | |
proc get_king_value_from_storage(): wei_value = | |
var | |
tmp {.noinit.}: array[32, byte] | |
tmp_ba {.noinit.}: array[16, byte] | |
pos = 2.stuint(32).toByteArrayBE | |
storageLoad(pos, addr tmp) | |
for i in 0 .. 15: | |
tmp_ba[i] = tmp[i] | |
return Uint128.fromBytesBE(tmp_ba) | |
proc set_king_name_in_storage(value: bytes32) = | |
var pos = 0.stuint(32).toByteArrayBE | |
storageStore(pos, unsafeAddr value) | |
proc set_king_addr_in_storage(value: address) = | |
var | |
tmp {.noinit.}: array[32, byte] | |
pos = 1.stuint(32).toByteArrayBE | |
for i, b in value: | |
tmp[12 + i] = b | |
storageStore(pos, addr tmp) | |
proc set_king_value_in_storage(value: wei_value) = | |
var | |
tmp {.noinit.}: array[32, byte] | |
pos = 2.stuint(32).toByteArrayBE | |
tmp_ba = value.toByteArrayBE | |
for i in 0 .. 15: | |
tmp[i] = tmp_ba[i] | |
storageStore(pos, addr tmp) | |
proc log_BecameKing(name: bytes32; value: uint128) = | |
var | |
data_output_buffer {.noinit.}: array[64, byte] | |
indexed_output_buffer {.noinit.}: array[32, byte] | |
## Set event signature | |
indexed_output_buffer[0 .. 31] = [27'u8, 249'u8, 120'u8, 202'u8, 175'u8, 68'u8, 0'u8, | |
229'u8, 19'u8, 3'u8, 253'u8, 54'u8, 9'u8, 9'u8, | |
69'u8, 25'u8, 209'u8, 163'u8, 226'u8, 149'u8, 53'u8, | |
63'u8, 107'u8, 160'u8, 64'u8, 1'u8, 236'u8, 40'u8, | |
188'u8, 103'u8, 13'u8, 2'u8] | |
copy_into_ba(data_output_buffer, 0, name) | |
copy_into_ba(data_output_buffer, 48, value.toByteArrayBE) | |
log(addr data_output_buffer[0], 64.int32, 1.int32, | |
addr indexed_output_buffer[0], nil, nil, nil) | |
if msg_value_func() > get_king_value_from_storage(): | |
set_king_name_in_storage(name) | |
set_king_addr_in_storage(msg_sender_tmp_variable_alloc) | |
set_king_value_in_storage(msg_value_func()) | |
log_BecameKing(name, msg_value_func()) | |
proc king_name*(): bytes32 = | |
## generated getter | |
proc get_king_name_from_storage(): bytes32 = | |
var | |
tmp {.noinit.}: bytes32 | |
pos = 0.stuint(32).toByteArrayBE | |
storageLoad(pos, addr tmp) | |
return tmp | |
get_king_name_from_storage() | |
proc king_addr*(): address = | |
## generated getter | |
proc get_king_addr_from_storage(): address = | |
var | |
tmp {.noinit.}: array[32, byte] | |
pos = 1.stuint(32).toByteArrayBE | |
storageLoad(pos, addr tmp) | |
var out_var: address | |
for i, b in tmp: | |
if i >= 12: | |
out_var[i - 12] = b | |
return out_var | |
get_king_addr_from_storage() | |
proc king_value*(): wei_value = | |
## generated getter | |
proc get_king_value_from_storage(): wei_value = | |
var | |
tmp {.noinit.}: array[32, byte] | |
tmp_ba {.noinit.}: array[16, byte] | |
pos = 2.stuint(32).toByteArrayBE | |
storageLoad(pos, addr tmp) | |
for i in 0 .. 15: | |
tmp_ba[i] = tmp[i] | |
return Uint128.fromBytesBE(tmp_ba) | |
get_king_value_from_storage() | |
proc main() {.exportwasm.} = | |
var selector: uint32 | |
callDataCopy(selector, 0) | |
bigEndian32(addr selector, addr selector) | |
case selector | |
of 0xD5C8751A'u32: | |
var becomeKing_param_0 {.noinit.}: array[32, byte] | |
callDataCopy(addr becomeKing_param_0, 4, 32) | |
becomeKing(becomeKing_param_0) | |
finish(nil, 0) | |
of 0x15582979'u32: | |
assertNotPayable() | |
var king_name_result_0 {.noinit.}: bytes32 | |
king_name_result_0 = king_name() | |
finish(addr king_name_result_0, 32) | |
of 0x255EB9C7'u32: | |
assertNotPayable() | |
var king_addr_result_0 {.noinit.}: address | |
king_addr_result_0 = king_addr() | |
var king_addr_result_0_arr: array[32, byte] | |
king_addr_result_0_arr[12 .. 31] = king_addr_result_0 | |
finish(addr king_addr_result_0_arr, 32) | |
of 0x5D74C461'u32: | |
assertNotPayable() | |
var king_value_result_0 {.noinit.}: wei_value | |
king_value_result_0 = king_value() | |
var king_value_result_0_arr {.noinit.}: array[32, byte] | |
king_value_result_0_arr[16 .. 31] = toByteArrayBE(king_value_result_0) | |
finish(addr king_value_result_0_arr, 32) | |
else: | |
discard | |
revert(nil, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment