Skip to content

Commit

Permalink
pass transfer event desc through from tx_prelude
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Sep 14, 2024
1 parent 342057b commit d138330
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 12 deletions.
9 changes: 6 additions & 3 deletions crates/token/src/tx.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Token transaction
use std::borrow::Cow;
use std::collections::{BTreeMap, BTreeSet};

use namada_core::collections::HashSet;
Expand All @@ -21,14 +22,15 @@ pub fn multi_transfer<ENV>(
env: &mut ENV,
transfers: Transfer,
tx_data: &BatchedTx,
event_desc: Cow<'static, str>,
) -> Result<()>
where
ENV: TxEnv + EmitEvents + action::Write<Err = Error>,
{
// 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()
Expand All @@ -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>(
env: &mut ENV,
transfers: TransparentTransfersRef<'_>,
event_desc: Cow<'static, str>,
) -> Result<HashSet<Address>>
where
ENV: TxEnv + EmitEvents,
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 10 additions & 5 deletions crates/trans_token/src/tx.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -15,6 +17,7 @@ pub fn transfer<ENV>(
dest: &Address,
token: &Address,
amount: Amount,
event_desc: Cow<'static, str>,
) -> Result<()>
where
ENV: TxEnv + EmitEvents,
Expand All @@ -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()),
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(),
),
]),
);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
50 changes: 46 additions & 4 deletions crates/tx_prelude/src/token.rs
Original file line number Diff line number Diff line change
@@ -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<HashSet<Address>> {
namada_token::tx::apply_transparent_transfers(
ctx,
transfers,
EVENT_DESC.into(),
)
}

0 comments on commit d138330

Please sign in to comment.