-
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-codegen): introduce derive macro for events (#298)
* feat(event): reorder event params for dynamic inputs * feat(zink): derive event methods --------- Co-authored-by: clearloop <[email protected]>
- Loading branch information
Showing
10 changed files
with
250 additions
and
147 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,99 +1,133 @@ | ||
//! Addition example. | ||
#![cfg_attr(target_arch = "wasm32", no_std)] | ||
#![cfg_attr(target_arch = "wasm32", no_main)] | ||
|
||
extern crate zink; | ||
|
||
use zink::Event; | ||
use zink::{primitives::U256, Event}; | ||
|
||
/// A `Ping` event. | ||
#[derive(Event)] | ||
struct Ping; | ||
pub enum MyEvent { | ||
/// Event with one topic | ||
Topic1(U256), | ||
/// Event with two topics | ||
Topic2(U256, U256), | ||
/// Event with three topics | ||
Topic3(U256, U256, U256), | ||
/// Event with four topics | ||
Topic4(U256, U256, U256, U256), | ||
} | ||
|
||
/// Test log0 | ||
#[zink::external] | ||
pub fn log0() { | ||
Ping.log0(); | ||
pub fn test_log0() { | ||
MyEvent::emit_name(); | ||
} | ||
|
||
/// Test log1 | ||
#[zink::external] | ||
pub fn log1() { | ||
Ping.log1(b"pong"); | ||
pub fn test_log1(value: U256) { | ||
MyEvent::Topic1(value).emit(); | ||
} | ||
|
||
/// Test log2 | ||
#[zink::external] | ||
pub fn log2() { | ||
Ping.log2(b"pong", b"ping"); | ||
pub fn test_log2(value1: U256, value2: U256) { | ||
MyEvent::Topic2(value1, value2).emit(); | ||
} | ||
|
||
/// Test log3 | ||
#[zink::external] | ||
pub fn log3() { | ||
Ping.log3(b"pong", b"ping", b"pong"); | ||
pub fn test_log3(value1: U256, value2: U256, value3: U256) { | ||
MyEvent::Topic3(value1, value2, value3).emit(); | ||
} | ||
|
||
/// Test log4 | ||
#[zink::external] | ||
pub fn log4() { | ||
Ping.log4(b"pong", b"ping", b"pong", b"pong"); | ||
pub fn test_log4(value1: U256, value2: U256, value3: U256, value4: U256) { | ||
MyEvent::Topic4(value1, value2, value3, value4).emit(); | ||
} | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
fn main() {} | ||
#[cfg(test)] | ||
mod tests { | ||
|
||
#[test] | ||
fn test() -> anyhow::Result<()> { | ||
use zink::Asm; | ||
use zint::{Bytes32, Contract}; | ||
let mut contract = Contract::search("log")?.compile()?; | ||
|
||
let info = contract.execute(["log0()"])?; | ||
assert_eq!( | ||
info.logs[0].data.data.to_vec(), | ||
b"Ping".to_vec().to_bytes32() | ||
); | ||
|
||
let info = contract.execute(["log1()"])?; | ||
assert_eq!( | ||
info.logs[0].data.data.to_vec(), | ||
b"Ping".to_vec().to_bytes32() | ||
); | ||
assert_eq!(info.logs[0].topics(), vec![b"pong".to_vec().to_bytes32()]); | ||
|
||
let info = contract.execute(["log2()"])?; | ||
assert_eq!( | ||
info.logs[0].data.data.to_vec(), | ||
b"Ping".to_vec().to_bytes32() | ||
); | ||
assert_eq!( | ||
info.logs[0].topics(), | ||
vec![b"pong".to_vec().to_bytes32(), b"ping".to_vec().to_bytes32()] | ||
); | ||
|
||
let info = contract.execute(["log3()"])?; | ||
assert_eq!( | ||
info.logs[0].data.data.to_vec(), | ||
b"Ping".to_vec().to_bytes32() | ||
); | ||
assert_eq!( | ||
info.logs[0].topics(), | ||
vec![ | ||
b"pong".to_vec().to_bytes32(), | ||
b"ping".to_vec().to_bytes32(), | ||
b"pong".to_vec().to_bytes32() | ||
] | ||
); | ||
|
||
let info = contract.execute(["log4()"])?; | ||
assert_eq!( | ||
info.logs[0].data.data.to_vec(), | ||
b"Ping".to_vec().to_bytes32() | ||
); | ||
assert_eq!( | ||
info.logs[0].topics(), | ||
vec![ | ||
b"pong".to_vec().to_bytes32(), | ||
b"ping".to_vec().to_bytes32(), | ||
b"pong".to_vec().to_bytes32(), | ||
b"pong".to_vec().to_bytes32() | ||
] | ||
); | ||
|
||
Ok(()) | ||
|
||
#[test] | ||
fn test_events() { | ||
let mut contract = Contract::search("log") | ||
.unwrap() | ||
.compile() | ||
.expect("failed to compile"); | ||
|
||
let name = b"MyEvent"; | ||
let value1: i32 = 1; | ||
let value2: i32 = 2; | ||
let value3: i32 = 3; | ||
let value4: i32 = 4; | ||
|
||
{ | ||
// Test log0 | ||
let info = contract.execute(&[b"test_log0()".to_vec()]).unwrap(); | ||
assert!(!info.logs.is_empty()); | ||
assert_eq!( | ||
info.logs[0].data.data.to_vec(), | ||
name.to_vec().to_bytes32().to_vec() | ||
); | ||
|
||
// Test log1 | ||
let info = contract | ||
.execute(&[b"test_log1(uint256)".to_vec(), value1.bytes32().to_vec()]) | ||
.expect("failed to execute test_log1"); | ||
assert!(!info.logs.is_empty()); | ||
assert_eq!( | ||
info.logs[0].data.data.to_vec(), | ||
name.to_vec().to_bytes32().to_vec() | ||
); | ||
assert_eq!(info.logs[0].topics()[0].to_vec(), value1.bytes32().to_vec()); | ||
|
||
// Test log2 | ||
let info = contract | ||
.execute(&[ | ||
b"test_log2(uint256,uint256)".to_vec(), | ||
value1.bytes32().to_vec(), | ||
value2.bytes32().to_vec(), | ||
]) | ||
.unwrap(); | ||
assert!(!info.logs.is_empty()); | ||
assert_eq!(info.logs[0].topics()[1].to_vec(), value1.bytes32().to_vec()); | ||
assert_eq!(info.logs[0].topics()[0].to_vec(), value2.bytes32().to_vec()); | ||
|
||
let info = contract | ||
.execute(&[ | ||
b"test_log3(uint256,uint256,uint256)".to_vec(), | ||
value1.bytes32().to_vec(), | ||
value2.bytes32().to_vec(), | ||
value3.bytes32().to_vec(), | ||
]) | ||
.unwrap(); | ||
assert!(!info.logs.is_empty()); | ||
assert_eq!(info.logs[0].topics()[2].to_vec(), value1.bytes32().to_vec()); | ||
assert_eq!(info.logs[0].topics()[1].to_vec(), value2.bytes32().to_vec()); | ||
assert_eq!(info.logs[0].topics()[0].to_vec(), value3.bytes32().to_vec()); | ||
|
||
let info = contract | ||
.execute(&[ | ||
b"test_log4(uint256,uint256,uint256,uint256)".to_vec(), | ||
value1.bytes32().to_vec(), | ||
value2.bytes32().to_vec(), | ||
value3.bytes32().to_vec(), | ||
value4.bytes32().to_vec(), | ||
]) | ||
.unwrap(); | ||
assert!(!info.logs.is_empty()); | ||
assert_eq!(info.logs[0].topics()[3].to_vec(), value1.bytes32().to_vec()); | ||
assert_eq!(info.logs[0].topics()[2].to_vec(), value2.bytes32().to_vec()); | ||
assert_eq!(info.logs[0].topics()[1].to_vec(), value3.bytes32().to_vec()); | ||
assert_eq!(info.logs[0].topics()[0].to_vec(), value4.bytes32().to_vec()); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
fn main() {} |
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
Oops, something went wrong.