diff --git a/crates/token/src/tx.rs b/crates/token/src/tx.rs index 979be0ec20c..de4a658b3de 100644 --- a/crates/token/src/tx.rs +++ b/crates/token/src/tx.rs @@ -1,5 +1,6 @@ //! Token transaction +use std::borrow::Cow; use std::collections::{BTreeMap, BTreeSet}; use namada_core::collections::HashSet; @@ -21,6 +22,7 @@ pub fn multi_transfer( env: &mut ENV, transfers: Transfer, tx_data: &BatchedTx, + event_desc: Cow<'static, str>, ) -> Result<()> where ENV: TxEnv + EmitEvents + action::Write, @@ -28,7 +30,7 @@ where // Effect the transparent multi transfer(s) let debited_accounts = if let Some(transparent) = transfers.transparent_part() { - apply_transparent_transfers(env, transparent) + apply_transparent_transfers(env, transparent, event_desc) .wrap_err("Transparent token transfer failed")? } else { HashSet::new() @@ -48,13 +50,14 @@ where } /// Transfer tokens from `sources` to `targets` and submit a transfer event. -/// +/// /// Returns an `Err` if any source has insufficient balance or if the transfer /// to any destination would overflow (This can only happen if the total supply /// doesn't fit in `token::Amount`). Returns a set of debited accounts. pub fn apply_transparent_transfers( env: &mut ENV, transfers: TransparentTransfersRef<'_>, + event_desc: Cow<'static, str>, ) -> Result> where ENV: TxEnv + EmitEvents, @@ -107,7 +110,7 @@ where } env.emit(TokenEvent { - descriptor: "transfer-from-wasm".into(), + descriptor: event_desc, level: EventLevel::Tx, operation: TokenOperation::Transfer { sources: evt_sources, diff --git a/crates/trans_token/src/tx.rs b/crates/trans_token/src/tx.rs index 589fc16da5b..4e8dc923e8e 100644 --- a/crates/trans_token/src/tx.rs +++ b/crates/trans_token/src/tx.rs @@ -1,5 +1,7 @@ //! Token transfers +use std::borrow::Cow; + use namada_core::address::Address; use namada_events::{EmitEvents, EventLevel}; use namada_tx_env::{Result, TxEnv}; @@ -15,6 +17,7 @@ pub fn transfer( dest: &Address, token: &Address, amount: Amount, + event_desc: Cow<'static, str>, ) -> Result<()> where ENV: TxEnv + EmitEvents, @@ -35,7 +38,7 @@ where crate::storage::transfer(env, token, src, dest, amount)?; env.emit(TokenEvent { - descriptor: "transfer-from-wasm".into(), + descriptor: event_desc, level: EventLevel::Tx, operation: TokenOperation::transfer( UserAccount::Internal(src.clone()), @@ -67,6 +70,8 @@ mod test { use super::*; use crate::event::{PostBalances, SourceAccounts, TargetAccounts}; + const EVENT_DESC: Cow<'static, str> = Cow::Borrowed("event-desc"); + proptest! { #[test] fn test_valid_transfer_tx( @@ -97,7 +102,7 @@ mod test { }); assert_eq!(read_balance(ctx(), &token, &src).unwrap(), amount); - transfer(ctx(), &src, &dest, &token, amount).unwrap(); + transfer(ctx(), &src, &dest, &token, amount, EVENT_DESC).unwrap(); // Dest received the amount assert_eq!(read_balance(ctx(), &token, &dest).unwrap(), amount); @@ -159,7 +164,7 @@ mod test { (TargetAccounts::KEY.to_string(), exp_targets), ( "token-event-descriptor".to_string(), - "transfer-from-wasm".to_string(), + EVENT_DESC.to_string(), ), ]), ); @@ -183,7 +188,7 @@ mod test { tx_env.credit_tokens(&dest, &token, src_balance); }); - transfer(ctx(), &src, &dest, &token, amount).unwrap(); + transfer(ctx(), &src, &dest, &token, amount, EVENT_DESC).unwrap(); // Dest balance is still the same assert_eq!(read_balance(ctx(), &token, &dest).unwrap(), dest_balance); @@ -225,7 +230,7 @@ mod test { tx_env.credit_tokens(&dest, &token, src_balance); }); - transfer(ctx(), &src, &dest, &token, amount).unwrap(); + transfer(ctx(), &src, &dest, &token, amount, EVENT_DESC).unwrap(); // Dest balance is still the same assert_eq!(read_balance(ctx(), &token, &dest).unwrap(), dest_balance); diff --git a/crates/tx_prelude/src/token.rs b/crates/tx_prelude/src/token.rs index 89d42e99642..b77ef598dfc 100644 --- a/crates/tx_prelude/src/token.rs +++ b/crates/tx_prelude/src/token.rs @@ -1,11 +1,53 @@ //! Shielded and transparent tokens related functions +use namada_core::collections::HashSet; #[cfg(any(test, feature = "testing"))] pub use namada_token::testing; -pub use namada_token::tx::{ - apply_shielded_transfer, apply_transparent_transfers, multi_transfer, - transfer, -}; +pub use namada_token::tx::apply_shielded_transfer; +use namada_token::TransparentTransfersRef; pub use namada_token::{ storage_key, utils, Amount, DenominatedAmount, Store, Transfer, }; +use namada_tx::BatchedTx; +use namada_tx_env::Address; + +use crate::{Ctx, Result, TxResult}; + +const EVENT_DESC: &str = "transfer-from-wasm"; + +/// Transfer transparent token, insert the verifier expected by the VP and an +/// emit an event. +pub fn transfer( + ctx: &mut Ctx, + src: &Address, + dest: &Address, + token: &Address, + amount: Amount, +) -> TxResult { + namada_token::tx::transfer(ctx, src, dest, token, amount, EVENT_DESC.into()) +} + +/// Transparent and shielded token transfers that can be used in a transaction. +pub fn multi_transfer( + ctx: &mut Ctx, + transfers: Transfer, + tx_data: &BatchedTx, +) -> TxResult { + namada_token::tx::multi_transfer(ctx, transfers, tx_data, EVENT_DESC.into()) +} + +/// Transfer tokens from `sources` to `targets` and submit a transfer event. +/// +/// Returns an `Err` if any source has insufficient balance or if the transfer +/// to any destination would overflow (This can only happen if the total supply +/// doesn't fit in `token::Amount`). Returns a set of debited accounts. +pub fn apply_transparent_transfers( + ctx: &mut Ctx, + transfers: TransparentTransfersRef<'_>, +) -> Result> { + namada_token::tx::apply_transparent_transfers( + ctx, + transfers, + EVENT_DESC.into(), + ) +}