-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(zink-codegen): introduce derive macro for events #281
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4977bd5
Event
malik672 b23c12a
event
malik672 6a572d5
changes
malik672 599a09a
test
malik672 a62d258
test
malik672 813d393
changes
malik672 e7a7c39
changes
malik672 98bc773
changes
malik672 171b85b
changes
malik672 7dcb6bb
changes
malik672 8e7b358
changes
malik672 d0292a7
changes
malik672 5ff68b9
Merge branch 'main' into Event
malik672 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,114 @@ | ||
//! 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, Asm, Event}; | ||
|
||
/// A `Ping` event. | ||
#[derive(Event)] | ||
struct Ping; | ||
|
||
#[zink::external] | ||
pub fn log0() { | ||
Ping.log0(); | ||
} | ||
|
||
#[zink::external] | ||
pub fn log1() { | ||
Ping.log1(b"pong"); | ||
pub enum MyEvent { | ||
/// Event with no topics | ||
Topic0, | ||
/// 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), | ||
} | ||
|
||
#[zink::external] | ||
pub fn log2() { | ||
Ping.log2(b"pong", b"ping"); | ||
pub mod event_tests { | ||
use super::*; | ||
|
||
/// Test log0 | ||
#[zink::external] | ||
pub fn test_log0() { | ||
unsafe { zink::ffi::evm::log0(b"MyEvent") } | ||
} | ||
|
||
/// Test log1 | ||
#[zink::external] | ||
pub fn test_log1(value: U256) { | ||
unsafe { | ||
let topic = value.to_bytes32(); | ||
zink::ffi::evm::log1(b"MyEvent", topic) | ||
} | ||
} | ||
|
||
/// Test log2 | ||
#[zink::external] | ||
pub fn test_log2(value1: U256, value2: U256) { | ||
unsafe { | ||
let topic1 = value1.to_bytes32(); | ||
let topic2 = value2.to_bytes32(); | ||
zink::ffi::evm::log2(b"MyEvent", topic1, topic2) | ||
} | ||
} | ||
|
||
/// Test log3 | ||
#[zink::external] | ||
pub fn test_log3(value1: U256, value2: U256, value3: U256) { | ||
unsafe { | ||
let topic1 = value1.to_bytes32(); | ||
let topic2 = value2.to_bytes32(); | ||
let topic3 = value3.to_bytes32(); | ||
zink::ffi::evm::log3(b"MyEvent", topic1, topic2, topic3) | ||
} | ||
} | ||
|
||
/// Test log4 | ||
#[zink::external] | ||
pub fn test_log4(value1: U256, value2: U256, value3: U256, value4: U256) { | ||
unsafe { | ||
let topic1 = value1.to_bytes32(); | ||
let topic2 = value2.to_bytes32(); | ||
let topic3 = value3.to_bytes32(); | ||
let topic4 = value4.to_bytes32(); | ||
zink::ffi::evm::log4(b"MyEvent", topic1, topic2, topic3, topic4) | ||
} | ||
} | ||
|
||
/// Test multiple event logs in one transaction | ||
#[zink::external] | ||
pub fn test_multiple_logs( | ||
value1: U256, | ||
value2: U256, | ||
value3: U256, | ||
value4: U256, | ||
) -> Result<(), ()> { | ||
test_log0(); | ||
test_log1(value1); | ||
test_log2(value1, value2); | ||
test_log3(value1, value2, value3); | ||
test_log4(value1, value2, value3, value4); | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[zink::external] | ||
pub fn log3() { | ||
Ping.log3(b"pong", b"ping", b"pong"); | ||
} | ||
|
||
#[zink::external] | ||
pub fn log4() { | ||
Ping.log4(b"pong", b"ping", b"pong", b"pong"); | ||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_events() { | ||
let value1 = U256::from(U256::empty()); | ||
let value2 = U256::from(U256::empty()); | ||
let value3 = U256::from(U256::empty()); | ||
let value4 = U256::from(U256::empty()); | ||
|
||
// Test each log function | ||
event_tests::test_log0(); | ||
event_tests::test_log1(value1); | ||
event_tests::test_log2(value1, value2); | ||
event_tests::test_log3(value1, value2, value3); | ||
event_tests::test_log4(value1, value2, value3, value4); | ||
|
||
// Test multiple logs | ||
event_tests::test_multiple_logs(value1, value2, value3, value4).unwrap(); | ||
} | ||
} | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
fn main() {} | ||
|
||
#[test] | ||
fn test() -> anyhow::Result<()> { | ||
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(()) | ||
} |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should compile the contract and then call these interfaces to complete the tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@clearloop how do we assert the output, logs don't return yet
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for example here:
https://github.com/zink-lang/zink/blob/main/examples/log.rs#L44-L50
info
, there is a log field, see https://docs.zink-lang.org/rustdocs/zint/struct.Info.html#structfield.logs