diff --git a/charms-data/src/lib.rs b/charms-data/src/lib.rs index c411056..a042693 100644 --- a/charms-data/src/lib.rs +++ b/charms-data/src/lib.rs @@ -497,6 +497,15 @@ pub const TOKEN: char = 't'; /// Special `App.tag` value for non-fungible tokens (NFTs). See [`App`] for more details. pub const NFT: char = 'n'; +/// Check if the transaction is a simple transfer of assets specified by `app`. +pub fn is_simple_transfer(app: &App, tx: &Transaction) -> bool { + match app.tag { + TOKEN => token_amounts_balanced(app, tx) && nft_state_preserved(app, tx), + NFT => nft_state_preserved(app, tx), + _ => false, + } +} + /// Check if the provided app's token amounts are balanced in the transaction. This means that the /// sum of the token amounts in the `tx` inputs is equal to the sum of the token amounts in the `tx` /// outputs. diff --git a/charms-sdk/src/lib.rs b/charms-sdk/src/lib.rs index 5036679..4dd7506 100644 --- a/charms-sdk/src/lib.rs +++ b/charms-sdk/src/lib.rs @@ -5,7 +5,7 @@ pub use sp1_zkvm; macro_rules! main { ($path:path) => { fn main() { - use charms_sdk::data::{util, App, Data, Transaction}; + use charms_sdk::data::{is_simple_transfer, util, App, Data, Transaction}; fn read_input() -> (App, Transaction, Data, Data) { let buf = charms_sdk::sp1_zkvm::io::read_vec(); @@ -20,7 +20,7 @@ macro_rules! main { } let (app, tx, x, w): (App, Transaction, Data, Data) = read_input(); - assert!($path(&app, &tx, &x, &w)); + assert!(is_simple_transfer(&app, &tx) || $path(&app, &tx, &x, &w)); commit(app, tx, x); } diff --git a/examples/toad-token/src/lib.rs b/examples/toad-token/src/lib.rs index ba2cf22..9f9d4f0 100644 --- a/examples/toad-token/src/lib.rs +++ b/examples/toad-token/src/lib.rs @@ -1,7 +1,4 @@ -use charms_sdk::data::{ - check, nft_state_preserved, sum_token_amount, token_amounts_balanced, App, Data, Transaction, - UtxoId, B32, NFT, TOKEN, -}; +use charms_sdk::data::{check, sum_token_amount, App, Data, Transaction, UtxoId, B32, NFT, TOKEN}; use sha2::{Digest, Sha256}; pub fn app_contract(app: &App, tx: &Transaction, x: &Data, w: &Data) -> bool { @@ -25,9 +22,7 @@ fn nft_contract_satisfied(app: &App, tx: &Transaction, w: &Data) -> bool { identity: app.identity.clone(), vk: app.vk.clone(), }; - check!( - nft_state_preserved(app, tx) || can_mint_nft(app, tx, w) || can_mint_token(&token_app, tx) - ); + check!(can_mint_nft(app, tx, w) || can_mint_token(&token_app, tx)); true } @@ -58,7 +53,7 @@ pub(crate) fn hash(data: &str) -> B32 { } fn token_contract_satisfied(token_app: &App, tx: &Transaction) -> bool { - check!(token_amounts_balanced(token_app, tx) || can_mint_token(token_app, tx)); + check!(can_mint_token(token_app, tx)); true }