diff --git a/src/lib.rs b/src/lib.rs index 8a97753..9e8dda4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,26 +46,36 @@ fn unsafe_alloc_buffer(len: usize) -> Vec { #[derive(Copy, Clone)] pub struct Uint160 { - bytes: [u8; 20], + pub bytes: [u8; 20], } #[derive(Copy, Clone)] pub struct Uint256 { - bytes: [u8; 32], + pub bytes: [u8; 32], } #[derive(Copy, Clone)] pub struct Uint128 { - bytes: [u8; 16], + pub bytes: [u8; 16], } +type Address = Uint160; +type Hash = Uint256; +type Value = Uint128; + trait EwasmType { fn new() -> Self; } -type Address = Uint160; -type Hash = Uint256; -type Value = Uint128; +trait RawPtr { + fn as_raw_ptr(&self) -> *const Self; +} + +impl RawPtr for T { + fn as_raw_ptr(&self) -> *const T { + self as *const T + } +} impl EwasmType for Address { fn new() -> Self { @@ -115,20 +125,20 @@ pub fn gas_left() -> u64 { } } -pub fn current_address() -> [u8;20] { - let mut ret = [0u8;20]; +pub fn current_address() -> Address { + let mut ret = Address::new(); unsafe { - ethereum_getAddress(ret.as_mut_ptr() as *const u32); + ethereum_getAddress(ret.as_raw_ptr() as *const u32); } ret } -pub fn external_balance(address: &[u8;20]) -> [u8;16] { - let mut ret = [0u8;16]; +pub fn external_balance(address: Address) -> Value { + let mut ret = Value::new(); unsafe { - ethereum_getBalance(address.as_ptr() as *const u32, ret.as_mut_ptr() as *const u32); + ethereum_getBalance(address.as_raw_ptr() as *const u32, ret.as_raw_ptr() as *const u32); } ret }