Skip to content

Commit

Permalink
Make simple transfers always satisfy app contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
imikushin committed Feb 6, 2025
1 parent 70966d2 commit dc268de
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
9 changes: 9 additions & 0 deletions charms-data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions charms-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}

Expand Down
11 changes: 3 additions & 8 deletions examples/toad-token/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit dc268de

Please sign in to comment.