-
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.
- Loading branch information
Showing
4 changed files
with
130 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,77 @@ | ||
//! Example of revert | ||
#![cfg_attr(target_arch = "wasm32", no_std)] | ||
#![cfg_attr(target_arch = "wasm32", no_main)] | ||
|
||
use zink::{ | ||
primitives::{Address, U256}, | ||
Error, | ||
}; | ||
|
||
extern crate zink; | ||
|
||
// Define custom errors | ||
#[derive(Error)] | ||
pub enum ContractError { | ||
Unauthorized, | ||
InsufficientBalance(U256, U256), | ||
InvalidAddress(Address), | ||
} | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
fn main() {} | ||
|
||
/// check if the passing address is owner | ||
#[zink::external] | ||
pub fn revert() { | ||
zink::revert!("revert works") | ||
pub fn simple_revert() { | ||
zink::revert!("simple revert message"); | ||
} | ||
|
||
#[zink::external] | ||
pub fn assert_example() { | ||
zink::assert!(false, "assertion failed"); | ||
} | ||
|
||
#[zink::external] | ||
pub fn custom_error() { | ||
ContractError::Unauthorized; | ||
} | ||
|
||
#[zink::external] | ||
pub fn insufficient_balance(amount: U256) { | ||
let balance = U256::empty(); | ||
ContractError::InsufficientBalance(balance, amount); | ||
} | ||
|
||
/// check if the passing address is owner | ||
#[zink::external] | ||
pub fn assert() { | ||
zink::assert!(false, "assert works"); | ||
pub fn check_address(addr: Address) { | ||
ContractError::InvalidAddress(addr); | ||
} | ||
|
||
#[test] | ||
fn test_revert() -> anyhow::Result<()> { | ||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use zint::Contract; | ||
let mut contract = Contract::search("revert")?.compile()?; | ||
|
||
let info = contract.execute(["revert()".as_bytes()])?; | ||
assert_eq!(info.revert, Some("revert works".into())); | ||
// #[test] | ||
// fn test_errors() -> anyhow::Result<()> { | ||
// let mut contract = Contract::search("errors")?.compile()?; | ||
|
||
// // Test string revert | ||
// let info = contract.execute(["simple_revert()".as_bytes()])?; | ||
// assert_eq!(info.revert, Some("simple revert message".into())); | ||
|
||
// // Test assertion | ||
// let info = contract.execute(["assert_example()".as_bytes()])?; | ||
// assert_eq!(info.revert, Some("assertion failed".into())); | ||
|
||
// // Test custom error | ||
// let info = contract.execute(["custom_error()".as_bytes()])?; | ||
// assert!(info.revert.is_some()); | ||
// // In practice, you'd decode the custom error from the revert data | ||
|
||
// // Test custom error with data | ||
// let info = contract.execute(["insufficient_balance(uint256)".as_bytes(), &[100u8; 32]])?; | ||
// assert!(info.revert.is_some()); | ||
// // You'd decode the balance values from the revert data | ||
|
||
let info = contract.execute(["assert()".as_bytes()])?; | ||
assert_eq!(info.revert, Some("assert works".into())); | ||
Ok(()) | ||
// 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