Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
yito88 committed Jan 25, 2024
1 parent 89c0bb4 commit 7a395d4
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 170 deletions.
14 changes: 7 additions & 7 deletions crates/ibc/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use namada_core::types::hash::Hash;
use namada_core::types::ibc::{IbcEvent, MsgTransfer};
use namada_core::types::storage::Epochs;
use namada_core::types::time::DateTimeUtc;
use namada_core::types::token::DenominatedAmount;
use namada_core::types::token::Amount;
use namada_governance::storage::proposal::PGFIbcTarget;
use namada_parameters::read_epoch_duration_parameter;
use namada_state::wl_storage::{PrefixIter, WriteLogAndStorage};
Expand Down Expand Up @@ -99,9 +99,9 @@ where
src: &Address,
dest: &Address,
token: &Address,
amount: DenominatedAmount,
amount: Amount,
) -> Result<(), StorageError> {
token::transfer(self, token, src, dest, amount.amount())
token::transfer(self, token, src, dest, amount)
}

/// Handle masp tx
Expand All @@ -118,9 +118,9 @@ where
&mut self,
target: &Address,
token: &Address,
amount: DenominatedAmount,
amount: Amount,
) -> Result<(), StorageError> {
token::credit_tokens(self.wl_storage, token, target, amount.amount())?;
token::credit_tokens(self.wl_storage, token, target, amount)?;
let minter_key = token::storage_key::minter_key(token);
self.wl_storage
.write(&minter_key, Address::Internal(InternalAddress::Ibc))
Expand All @@ -131,9 +131,9 @@ where
&mut self,
target: &Address,
token: &Address,
amount: DenominatedAmount,
amount: Amount,
) -> Result<(), StorageError> {
token::burn(self.wl_storage, token, target, amount.amount())
token::burn(self.wl_storage, token, target, amount)
}

fn log_string(&self, message: String) {
Expand Down
18 changes: 5 additions & 13 deletions crates/ibc/src/context/nft_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use namada_core::ibc::core::handler::types::error::ContextError;
use namada_core::ibc::core::host::types::identifiers::{ChannelId, PortId};
use namada_core::types::address::Address;
use namada_core::types::ibc::{NftClass, NftMetadata, IBC_ESCROW_ADDRESS};
use namada_core::types::token::DenominatedAmount;
use namada_core::types::token::Amount;

use super::common::IbcCommonContext;
use crate::storage;
Expand Down Expand Up @@ -249,7 +249,7 @@ where
from_account,
&IBC_ESCROW_ADDRESS,
&ibc_token,
DenominatedAmount::new(1.into(), 0.into()),
Amount::from_u64(1),
)
.map_err(|e| ContextError::from(e).into())
}
Expand All @@ -271,7 +271,7 @@ where
&IBC_ESCROW_ADDRESS,
to_account,
&ibc_token,
DenominatedAmount::new(1.into(), 0.into()),
Amount::from_u64(1),
)
.map_err(|e| ContextError::from(e).into())
}
Expand All @@ -297,11 +297,7 @@ where

self.inner
.borrow_mut()
.mint_token(
account,
&ibc_token,
DenominatedAmount::new(1.into(), 0.into()),
)
.mint_token(account, &ibc_token, Amount::from_u64(1))
.map_err(|e| ContextError::from(e).into())
}

Expand All @@ -316,11 +312,7 @@ where

self.inner
.borrow_mut()
.burn_token(
account,
&ibc_token,
DenominatedAmount::new(1.into(), 0.into()),
)
.burn_token(account, &ibc_token, Amount::from_u64(1))
.map_err(|e| ContextError::from(e).into())
}
}
8 changes: 4 additions & 4 deletions crates/ibc/src/context/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pub use ics23::ProofSpec;
use namada_core::types::address::Address;
use namada_core::types::ibc::IbcEvent;
use namada_core::types::token::DenominatedAmount;
use namada_core::types::token::Amount;
use namada_storage::{Error, StorageRead, StorageWrite};

/// IBC context trait to be implemented in integration that can read and write
Expand All @@ -23,7 +23,7 @@ pub trait IbcStorageContext: StorageRead + StorageWrite {
src: &Address,
dest: &Address,
token: &Address,
amount: DenominatedAmount,
amount: Amount,
) -> Result<(), Error>;

/// Handle masp tx
Expand All @@ -38,15 +38,15 @@ pub trait IbcStorageContext: StorageRead + StorageWrite {
&mut self,
target: &Address,
token: &Address,
amount: DenominatedAmount,
amount: Amount,
) -> Result<(), Error>;

/// Burn token
fn burn_token(
&mut self,
target: &Address,
token: &Address,
amount: DenominatedAmount,
amount: Amount,
) -> Result<(), Error>;

/// Logging
Expand Down
5 changes: 2 additions & 3 deletions crates/ibc/src/context/token_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use namada_core::ibc::core::host::types::identifiers::{ChannelId, PortId};
use namada_core::types::address::Address;
use namada_core::types::ibc::IBC_ESCROW_ADDRESS;
use namada_core::types::uint::Uint;
use namada_trans_token::{read_denom, Amount, DenominatedAmount, Denomination};
use namada_trans_token::{read_denom, Amount, Denomination};

use super::common::IbcCommonContext;
use crate::storage;
Expand Down Expand Up @@ -44,7 +44,7 @@ where
fn get_token_amount(
&self,
coin: &PrefixedCoin,
) -> Result<(Address, DenominatedAmount), TokenTransferError> {
) -> Result<(Address, Amount), TokenTransferError> {
let token = match Address::decode(coin.denom.base_denom.as_str()) {
Ok(token_addr) if coin.denom.trace_path.is_empty() => token_addr,
_ => storage::ibc_token(coin.denom.to_string()),
Expand All @@ -65,7 +65,6 @@ where
.into(),
)
})?;
let amount = DenominatedAmount::new(amount, denom);

Ok((token, amount))
}
Expand Down
55 changes: 10 additions & 45 deletions crates/namada/src/ledger/native_vp/ibc/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::ledger::ibc::storage::is_ibc_key;
use crate::ledger::native_vp::CtxPreStorageRead;
use crate::state::write_log::StorageModification;
use crate::state::{self as ledger_storage, StorageHasher};
use crate::token::{self as token, Amount, DenominatedAmount};
use crate::token::{self as token, burn, credit_tokens, transfer, Amount};
use crate::types::address::{Address, InternalAddress};
use crate::types::ibc::IbcEvent;
use crate::types::storage::{
Expand Down Expand Up @@ -201,20 +201,9 @@ where
src: &Address,
dest: &Address,
token: &Address,
amount: DenominatedAmount,
amount: Amount,
) -> Result<()> {
let amount = crate::token::denom_to_amount(amount, token, self)?;
let src_key = token::storage_key::balance_key(token, src);
let dest_key = token::storage_key::balance_key(token, dest);
let src_bal: Option<Amount> = self.ctx.read(&src_key)?;
let mut src_bal = src_bal.expect("The source has no balance");
src_bal.spend(&amount);
let mut dest_bal: Amount =
self.ctx.read(&dest_key)?.unwrap_or_default();
dest_bal.receive(&amount);

self.write(&src_key, src_bal.serialize_to_vec())?;
self.write(&dest_key, dest_bal.serialize_to_vec())
transfer(self, token, src, dest, amount)
}

fn handle_masp_tx(
Expand All @@ -230,21 +219,9 @@ where
&mut self,
target: &Address,
token: &Address,
amount: DenominatedAmount,
amount: Amount,
) -> Result<()> {
let amount = crate::token::denom_to_amount(amount, token, self)?;
let target_key = token::storage_key::balance_key(token, target);
let mut target_bal: Amount =
self.ctx.read(&target_key)?.unwrap_or_default();
target_bal.receive(&amount);

let minted_key = token::storage_key::minted_balance_key(token);
let mut minted_bal: Amount =
self.ctx.read(&minted_key)?.unwrap_or_default();
minted_bal.receive(&amount);

self.write(&target_key, target_bal.serialize_to_vec())?;
self.write(&minted_key, minted_bal.serialize_to_vec())?;
credit_tokens(self, token, target, amount)?;

let minter_key = token::storage_key::minter_key(token);
self.write(
Expand All @@ -257,21 +234,9 @@ where
&mut self,
target: &Address,
token: &Address,
amount: DenominatedAmount,
amount: Amount,
) -> Result<()> {
let amount = crate::token::denom_to_amount(amount, token, self)?;
let target_key = token::storage_key::balance_key(token, target);
let mut target_bal: Amount =
self.ctx.read(&target_key)?.unwrap_or_default();
target_bal.spend(&amount);

let minted_key = token::storage_key::minted_balance_key(token);
let mut minted_bal: Amount =
self.ctx.read(&minted_key)?.unwrap_or_default();
minted_bal.spend(&amount);

self.write(&target_key, target_bal.serialize_to_vec())?;
self.write(&minted_key, minted_bal.serialize_to_vec())
burn(self, token, target, amount)
}

fn log_string(&self, message: String) {
Expand Down Expand Up @@ -419,7 +384,7 @@ where
_src: &Address,
_dest: &Address,
_token: &Address,
_amount: DenominatedAmount,
_amount: Amount,
) -> Result<()> {
unimplemented!("Validation doesn't transfer")
}
Expand All @@ -436,7 +401,7 @@ where
&mut self,
_target: &Address,
_token: &Address,
_amount: DenominatedAmount,
_amount: Amount,
) -> Result<()> {
unimplemented!("Validation doesn't mint")
}
Expand All @@ -445,7 +410,7 @@ where
&mut self,
_target: &Address,
_token: &Address,
_amount: DenominatedAmount,
_amount: Amount,
) -> Result<()> {
unimplemented!("Validation doesn't burn")
}
Expand Down
9 changes: 3 additions & 6 deletions crates/namada/src/vm/host_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2596,11 +2596,10 @@ where
src: &Address,
dest: &Address,
token: &Address,
amount: crate::token::DenominatedAmount,
amount: crate::token::Amount,
) -> Result<(), namada_state::StorageError> {
use crate::token;

let amount = token::denom_to_amount(amount, token, self)?;
if amount != token::Amount::default() && src != dest {
let src_key = balance_key(token, src);
let dest_key = balance_key(token, dest);
Expand Down Expand Up @@ -2632,11 +2631,10 @@ where
&mut self,
target: &Address,
token: &Address,
amount: crate::token::DenominatedAmount,
amount: crate::token::Amount,
) -> Result<(), namada_state::StorageError> {
use crate::token;

let amount = token::denom_to_amount(amount, token, self)?;
let target_key = balance_key(token, target);
let mut target_bal =
self.read::<token::Amount>(&target_key)?.unwrap_or_default();
Expand All @@ -2661,11 +2659,10 @@ where
&mut self,
target: &Address,
token: &Address,
amount: crate::token::DenominatedAmount,
amount: crate::token::Amount,
) -> Result<(), namada_state::StorageError> {
use crate::token;

let amount = token::denom_to_amount(amount, token, self)?;
let target_key = balance_key(token, target);
let mut target_bal =
self.read::<token::Amount>(&target_key)?.unwrap_or_default();
Expand Down
13 changes: 6 additions & 7 deletions crates/tx_prelude/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ use std::rc::Rc;

use namada_core::types::address::{Address, InternalAddress};
pub use namada_core::types::ibc::{IbcEvent, IbcShieldedTransfer};
use namada_core::types::token::DenominatedAmount;
use namada_core::types::token::Amount;
pub use namada_ibc::storage::is_ibc_key;
pub use namada_ibc::{
IbcActions, IbcCommonContext, IbcStorageContext, NftTransferModule,
ProofSpec, TransferModule,
};
use namada_token::denom_to_amount;
use namada_tx_env::TxEnv;

use crate::token::{burn, mint, transfer};
Expand Down Expand Up @@ -48,7 +47,7 @@ impl IbcStorageContext for Ctx {
src: &Address,
dest: &Address,
token: &Address,
amount: DenominatedAmount,
amount: Amount,
) -> std::result::Result<(), Error> {
transfer(self, src, dest, token, amount)
}
Expand All @@ -66,24 +65,24 @@ impl IbcStorageContext for Ctx {
&mut self,
target: &Address,
token: &Address,
amount: DenominatedAmount,
amount: Amount,
) -> Result<(), Error> {
mint(
self,
&Address::Internal(InternalAddress::Ibc),
target,
token,
denom_to_amount(amount, token, self)?,
amount,
)
}

fn burn_token(
&mut self,
target: &Address,
token: &Address,
amount: DenominatedAmount,
amount: Amount,
) -> Result<(), Error> {
burn(self, target, token, denom_to_amount(amount, token, self)?)
burn(self, target, token, amount)
}

fn log_string(&self, message: String) {
Expand Down
3 changes: 1 addition & 2 deletions crates/tx_prelude/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ pub fn transfer(
src: &Address,
dest: &Address,
token: &Address,
amount: DenominatedAmount,
amount: Amount,
) -> TxResult {
let amount = denom_to_amount(amount, token, ctx)?;
if amount != Amount::default() && src != dest {
let src_key = balance_key(token, src);
let dest_key = balance_key(token, dest);
Expand Down
2 changes: 1 addition & 1 deletion wasm/wasm_source/src/tx_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn apply_tx(ctx: &mut Ctx, tx_data: Tx) -> TxResult {
&transfer.source,
&transfer.target,
&transfer.token,
transfer.amount,
transfer.amount.amount(),
)?;

let shielded = transfer
Expand Down
Loading

0 comments on commit 7a395d4

Please sign in to comment.