-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* pausable macros * stellar sdk update for macro fixes
- Loading branch information
1 parent
cefb6e9
commit 481c97a
Showing
10 changed files
with
270 additions
and
90 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "openzeppelin-pausable-macros" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
proc-macro = true | ||
doctest = false | ||
|
||
[dependencies] | ||
proc-macro2 = { workspace = true } | ||
quote = { workspace = true } | ||
syn = { workspace = true } |
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,47 @@ | ||
use syn::{FnArg, ItemFn, PatType, Type}; | ||
|
||
pub fn check_env_arg(input_fn: &ItemFn) -> (syn::Ident, bool) { | ||
// Get the first argument | ||
let first_arg = input_fn.sig.inputs.first().unwrap_or_else(|| { | ||
panic!("function '{}' must have at least one argument", input_fn.sig.ident) | ||
}); | ||
|
||
// Extract the pattern and type from the argument | ||
let (pat, ty) = match first_arg { | ||
FnArg::Typed(PatType { pat, ty, .. }) => (pat, ty), | ||
_ => | ||
panic!("first argument of function '{}' must be a typed parameter", input_fn.sig.ident), | ||
}; | ||
|
||
// Get the identifier from the pattern | ||
let ident = match &**pat { | ||
syn::Pat::Ident(pat_ident) => pat_ident.ident.clone(), | ||
_ => panic!("first argument of function '{}' must be an identifier", input_fn.sig.ident), | ||
}; | ||
|
||
// Check if the type is Env or &Env | ||
let is_ref = match &**ty { | ||
Type::Reference(type_ref) => match &*type_ref.elem { | ||
Type::Path(path) => { | ||
check_is_env(path, &input_fn.sig.ident); | ||
true | ||
} | ||
_ => panic!("first argument of function '{}' must be Env or &Env", input_fn.sig.ident), | ||
}, | ||
Type::Path(path) => { | ||
check_is_env(path, &input_fn.sig.ident); | ||
false | ||
} | ||
_ => panic!("first argument of function '{}' must be Env or &Env", input_fn.sig.ident), | ||
}; | ||
|
||
(ident, is_ref) | ||
} | ||
|
||
fn check_is_env(path: &syn::TypePath, fn_name: &syn::Ident) { | ||
let is_env = path.path.segments.last().map(|seg| seg.ident == "Env").unwrap_or(false); | ||
|
||
if !is_env { | ||
panic!("first argument of function '{}' must be Env or &Env", fn_name); | ||
} | ||
} |
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,99 @@ | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{parse_macro_input, ItemFn}; | ||
|
||
use crate::helper::check_env_arg; | ||
|
||
mod helper; | ||
|
||
/// Adds a pause check at the beginning of the function that ensures the | ||
/// contract is not paused. | ||
/// | ||
/// This macro will inject a `when_not_paused` check at the start of the | ||
/// function body. If the contract is paused, the function will return early | ||
/// with a panic. | ||
/// | ||
/// # Requirement: | ||
/// | ||
/// - The first argument of the decorated function must be of type `Env` or | ||
/// `&Env` | ||
/// | ||
/// # Example: | ||
/// | ||
/// ```ignore | ||
/// #[when_not_paused] | ||
/// pub fn my_function(env: &Env) { | ||
/// // This code will only execute if the contract is not paused | ||
/// } | ||
/// ``` | ||
#[proc_macro_attribute] | ||
pub fn when_not_paused(_attr: TokenStream, item: TokenStream) -> TokenStream { | ||
let input_fn = parse_macro_input!(item as ItemFn); | ||
let (env_ident, is_ref) = check_env_arg(&input_fn); | ||
|
||
let fn_vis = &input_fn.vis; | ||
let fn_sig = &input_fn.sig; | ||
let fn_block = &input_fn.block; | ||
|
||
let env_arg = if is_ref { | ||
quote! { #env_ident } | ||
} else { | ||
quote! { &#env_ident } | ||
}; | ||
|
||
let output = quote! { | ||
#fn_vis #fn_sig { | ||
openzeppelin_pausable::when_not_paused(#env_arg); | ||
|
||
#fn_block | ||
} | ||
}; | ||
|
||
output.into() | ||
} | ||
|
||
/// Adds a pause check at the beginning of the function that ensures the | ||
/// contract is paused. | ||
/// | ||
/// This macro will inject a `when_paused` check at the start of the function | ||
/// body. If the contract is not paused, the function will return early with a | ||
/// panic. | ||
/// | ||
/// # Requirement: | ||
/// | ||
/// - The first argument of the decorated function must be of type `Env` or | ||
/// `&Env` | ||
/// | ||
/// # Example: | ||
/// | ||
/// ```ignore | ||
/// #[when_paused] | ||
/// pub fn my_function(env: &Env) { | ||
/// // This code will only execute if the contract is paused | ||
/// } | ||
/// ``` | ||
#[proc_macro_attribute] | ||
pub fn when_paused(_attr: TokenStream, item: TokenStream) -> TokenStream { | ||
let input_fn = parse_macro_input!(item as ItemFn); | ||
let (env_ident, is_ref) = check_env_arg(&input_fn); | ||
|
||
let fn_vis = &input_fn.vis; | ||
let fn_sig = &input_fn.sig; | ||
let fn_block = &input_fn.block; | ||
|
||
let env_arg = if is_ref { | ||
quote! { #env_ident } | ||
} else { | ||
quote! { &#env_ident } | ||
}; | ||
|
||
let output = quote! { | ||
#fn_vis #fn_sig { | ||
openzeppelin_pausable::when_paused(#env_arg); | ||
|
||
#fn_block | ||
} | ||
}; | ||
|
||
output.into() | ||
} |
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,4 +1,5 @@ | ||
#![cfg(test)] | ||
|
||
extern crate std; | ||
|
||
use soroban_sdk::{ | ||
|
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,4 +1,5 @@ | ||
#![cfg(test)] | ||
|
||
extern crate std; | ||
|
||
use soroban_sdk::{testutils::Address as _, Address, Env}; | ||
|