-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(zink): introduce primitive type Address (#220)
* feat(evm): add dyn types in sol-abi * feat(abi): support all primitive types * chore(example): use u64 for fibonacci * feat(zink): introduce parameter loader * chore(zink): parameter bytes, is this the solution? * chore(zink): support boolean in abi * feat(zink): cast address with bytes24 * chore(zink): empty pointers for addresses * feat(zink): introduce primitive type Address
- Loading branch information
Showing
14 changed files
with
233 additions
and
40 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
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 |
---|---|---|
@@ -1,10 +1,49 @@ | ||
# Solidity ABI | ||
# Solidity ABIA | ||
|
||
An implementation of solidity ABI in rust. | ||
An straightforward solidity ABI implementation for zink. | ||
|
||
This library only contains a part of the solidity ABI implementation, | ||
if you are looking for a complete implementation of solidity ABI | ||
in rust, see [alloy-core][alloy-core]. | ||
|
||
## Static Types | ||
|
||
Only rust primitive types are supported in this static type port, | ||
|
||
| rust | solidity | | ||
|--------|-----------| | ||
| `i8` | `int8` | | ||
| `u8` | `uint8` | | ||
| `i16` | `int16` | | ||
| `u16` | `uint16` | | ||
| `i32` | `int32` | | ||
| `u32` | `uint32` | | ||
| `i64` | `int64` | | ||
| `u64` | `uint64` | | ||
| `i128` | `int128` | | ||
| `u128` | `uint128` | | ||
| `bool` | `bool` | | ||
|
||
|
||
## Dynamic Types | ||
|
||
The implementation of dynamic arguments follows [use-of-dynamic-types][dyn-types], | ||
same as the static types, only ports the rust types: | ||
|
||
| rust | solidity | | ||
|------------|-----------| | ||
| `Vec<u8>` | `bytes` | | ||
| `[u8; 20]` | `address` | | ||
| `String` | `string` | | ||
|
||
More complex types are currently not supported. | ||
|
||
Currently only used by the zink language, so the provided features | ||
is syncing with the development of zink. | ||
|
||
## LICENSE | ||
|
||
GPL-3.0 | ||
|
||
|
||
[alloy-core]: https://github.com/alloy-rs/core | ||
[dyn-types]: https://docs.soliditylang.org/en/latest/abi-spec.html#use-of-dynamic-types | ||
[zink]: https://github.com/zink-lang/zink |
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
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,58 @@ | ||
//! Bytes example. | ||
#![cfg_attr(target_arch = "wasm32", no_std)] | ||
#![cfg_attr(target_arch = "wasm32", no_main)] | ||
|
||
extern crate zink; | ||
|
||
use zink::{primitives::Address, Storage}; | ||
|
||
/// Contract owner storage | ||
#[zink::storage(Address)] | ||
pub struct Owner; | ||
|
||
/// check if the passing address is owner | ||
#[zink::external] | ||
pub fn is_owner(owner: Address) -> bool { | ||
Owner::get().eq(owner) | ||
} | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
fn main() {} | ||
|
||
#[test] | ||
fn test_owner() -> anyhow::Result<()> { | ||
use zint::{Bytes32, Contract}; | ||
let mut contract = Contract::search("owner")?.compile()?; | ||
let not_owner = [8; 20]; | ||
let mut evm = contract | ||
.construct( | ||
[(vec![0].try_into()?, not_owner.to_vec().try_into()?)] | ||
.into_iter() | ||
.collect(), | ||
)? | ||
.deploy()? | ||
.commit(true); | ||
|
||
assert_eq!( | ||
evm.storage(contract.address, [0; 32])?, | ||
not_owner.to_bytes32(), | ||
); | ||
|
||
assert_eq!( | ||
evm.calldata(&contract.encode(&[b"is_owner(address)".to_vec(), [0; 32].to_vec()])?) | ||
.call(contract.address)? | ||
.ret, | ||
false.to_bytes32().to_vec() | ||
); | ||
|
||
assert_eq!( | ||
evm.calldata(&contract.encode(&[ | ||
b"is_owner(address)".to_vec(), | ||
not_owner.to_bytes32().to_vec(), | ||
])?) | ||
.call(contract.address)? | ||
.ret, | ||
true.to_bytes32().to_vec() | ||
); | ||
Ok(()) | ||
} |
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
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 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,28 @@ | ||
use crate::{ffi, storage::StorageValue, Asm}; | ||
|
||
/// Account address | ||
#[repr(C)] | ||
#[derive(Clone, Copy)] | ||
pub struct Address(i32); | ||
|
||
impl Address { | ||
/// if self equal to another | ||
/// | ||
/// NOTE: not using core::cmp because it uses registers in wasm | ||
#[allow(clippy::should_implement_trait)] | ||
pub fn eq(self, other: Self) -> bool { | ||
unsafe { ffi::address_eq(self, other) } | ||
} | ||
} | ||
|
||
impl Asm for Address { | ||
fn push(self) { | ||
unsafe { ffi::asm::push_address(self) } | ||
} | ||
} | ||
|
||
impl StorageValue for Address { | ||
fn sload() -> Self { | ||
unsafe { ffi::asm::sload_address() } | ||
} | ||
} |
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,5 @@ | ||
//! Zink primitive types | ||
mod address; | ||
|
||
pub use address::Address; |
Oops, something went wrong.