Skip to content

Commit

Permalink
remove trace-path
Browse files Browse the repository at this point in the history
  • Loading branch information
yito88 committed Oct 2, 2023
1 parent 1b79599 commit b874ffa
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 70 deletions.
10 changes: 0 additions & 10 deletions apps/src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2516,7 +2516,6 @@ pub mod args {
use std::path::PathBuf;
use std::str::FromStr;

use namada::ibc::applications::transfer::TracePath;
use namada::ibc::core::ics24_host::identifier::{ChannelId, PortId};
pub use namada::sdk::args::*;
use namada::types::address::Address;
Expand Down Expand Up @@ -2726,7 +2725,6 @@ pub mod args {
pub const TM_ADDRESS: Arg<String> = arg("tm-address");
pub const TOKEN_OPT: ArgOpt<WalletAddress> = TOKEN.opt();
pub const TOKEN: Arg<WalletAddress> = arg("token");
pub const TRACE_PATH: ArgOpt<TracePath> = arg_opt("trace-path");
pub const TRANSFER_SOURCE: Arg<WalletTransferSource> = arg("source");
pub const TRANSFER_TARGET: Arg<WalletTransferTarget> = arg("target");
pub const TX_HASH: Arg<String> = arg("tx-hash");
Expand Down Expand Up @@ -3506,7 +3504,6 @@ pub mod args {
source: ctx.get_cached(&self.source),
target: ctx.get(&self.target),
token: ctx.get(&self.token),
trace_path: self.trace_path,
amount: self.amount,
native_token: ctx.native_token.clone(),
tx_code_path: self.tx_code_path.to_path_buf(),
Expand All @@ -3520,15 +3517,13 @@ pub mod args {
let source = TRANSFER_SOURCE.parse(matches);
let target = TRANSFER_TARGET.parse(matches);
let token = TOKEN.parse(matches);
let trace_path = TRACE_PATH.parse(matches);
let amount = InputAmount::Unvalidated(AMOUNT.parse(matches));
let tx_code_path = PathBuf::from(TX_TRANSFER_WASM);
Self {
tx,
source,
target,
token,
trace_path,
amount,
native_token: (),
tx_code_path,
Expand All @@ -3546,7 +3541,6 @@ pub mod args {
to produce the signature.",
))
.arg(TOKEN.def().help("The transfer token."))
.arg(TRACE_PATH.def().help("The transfer token's trace path."))
.arg(AMOUNT.def().help("The amount to transfer in decimal."))
}
}
Expand All @@ -3558,7 +3552,6 @@ pub mod args {
source: ctx.get(&self.source),
receiver: self.receiver,
token: ctx.get(&self.token),
trace_path: self.trace_path,
amount: self.amount,
port_id: self.port_id,
channel_id: self.channel_id,
Expand All @@ -3576,7 +3569,6 @@ pub mod args {
let source = SOURCE.parse(matches);
let receiver = RECEIVER.parse(matches);
let token = TOKEN.parse(matches);
let trace_path = TRACE_PATH.parse(matches);
let amount = InputAmount::Unvalidated(AMOUNT.parse(matches));
let port_id = PORT_ID.parse(matches);
let channel_id = CHANNEL_ID.parse(matches);
Expand All @@ -3589,7 +3581,6 @@ pub mod args {
source,
receiver,
token,
trace_path,
amount,
port_id,
channel_id,
Expand All @@ -3610,7 +3601,6 @@ pub mod args {
"The receiver address on the destination chain as string.",
))
.arg(TOKEN.def().help("The transfer token."))
.arg(TRACE_PATH.def().help("The transfer token's trace path."))
.arg(AMOUNT.def().help("The amount to transfer in decimal."))
.arg(PORT_ID.def().help("The port ID."))
.arg(CHANNEL_ID.def().help("The channel ID."))
Expand Down
16 changes: 16 additions & 0 deletions apps/src/lib/cli/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ use std::path::{Path, PathBuf};
use std::str::FromStr;

use color_eyre::eyre::Result;
use namada::ledger::ibc::storage::ibc_token;
use namada::sdk::masp::ShieldedContext;
use namada::sdk::wallet::Wallet;
use namada::types::address::{Address, InternalAddress};
use namada::types::chain::ChainId;
use namada::types::ethereum_events::EthAddress;
use namada::types::ibc::is_ibc_denom;
use namada::types::io::Io;
use namada::types::key::*;
use namada::types::masp::*;
Expand Down Expand Up @@ -367,6 +369,20 @@ impl ArgFromContext for Address {
})
.unwrap_or(Err(Skip))
})
// An IBC token
.or_else(|_| {
is_ibc_denom(raw)
.map(|(trace_path, base_denom)| {
let base_token = ctx
.wallet
.find_address(&base_denom)
.map(|addr| addr.to_string())
.unwrap_or(base_denom);
let ibc_denom = format!("{trace_path}/{base_token}");
ibc_token(ibc_denom)
})
.ok_or(Skip)
})
// Or it can be an alias that may be found in the wallet
.or_else(|_| ctx.wallet.find_address(raw).cloned().ok_or(Skip))
.map_err(|_| format!("Unknown address {raw}"))
Expand Down
7 changes: 3 additions & 4 deletions apps/src/lib/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use namada::sdk::wallet::{AddressVpType, Wallet};
use namada::types::address::{masp, Address, InternalAddress};
use namada::types::control_flow::ProceedOrElse;
use namada::types::hash::Hash;
use namada::types::ibc::split_ibc_denom;
use namada::types::ibc::is_ibc_denom;
use namada::types::io::Io;
use namada::types::key::*;
use namada::types::masp::{BalanceOwner, ExtendedViewingKey, PaymentAddress};
Expand Down Expand Up @@ -385,7 +385,7 @@ pub async fn query_transparent_balance<
display_line!(IO, "{}: {}", token_alias, balance);
}
Err(e) => {
display_line!(IO, "Eror in querying: {e}");
display_line!(IO, "Querying error: {e}");
display_line!(
IO,
"No {} balance found for {}",
Expand Down Expand Up @@ -761,7 +761,7 @@ fn get_ibc_denom_alias(
wallet: &Wallet<CliWalletUtils>,
ibc_denom: impl AsRef<str>,
) -> String {
split_ibc_denom(&ibc_denom)
is_ibc_denom(&ibc_denom)
.map(|(trace_path, base_token)| {
let base_token_alias = match Address::decode(&base_token) {
Ok(base_token) => wallet.lookup_alias(&base_token),
Expand All @@ -776,7 +776,6 @@ fn get_ibc_denom_alias(
.unwrap_or(ibc_denom.as_ref().to_string())
}


/// Query Proposals
pub async fn query_proposal<
C: namada::ledger::queries::Client + Sync,
Expand Down
1 change: 0 additions & 1 deletion benches/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,6 @@ impl BenchShieldedCtx {
source: source.clone(),
target: target.clone(),
token: address::nam(),
trace_path: None,
amount: InputAmount::Validated(DenominatedAmount {
amount,
denom: 0.into(),
Expand Down
4 changes: 2 additions & 2 deletions core/src/ledger/ibc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::ibc_proto::google::protobuf::Any;
use crate::types::address::Address;
use crate::types::chain::ChainId;
use crate::types::ibc::{
split_ibc_denom, EVENT_TYPE_DENOM_TRACE, EVENT_TYPE_PACKET,
is_ibc_denom, EVENT_TYPE_DENOM_TRACE, EVENT_TYPE_PACKET,
};

#[allow(missing_docs)]
Expand Down Expand Up @@ -160,7 +160,7 @@ where
e
))
})?;
if let Some((_, base_token)) = split_ibc_denom(&ibc_denom) {
if let Some((_, base_token)) = is_ibc_denom(&ibc_denom) {
self.ctx
.borrow_mut()
.store_ibc_denom(base_token, trace_hash, &ibc_denom)
Expand Down
7 changes: 4 additions & 3 deletions core/src/types/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ mod ibc_rs_conversion {

/// Returns the trace path and the token string if the denom is an IBC
/// denom.
pub fn split_ibc_denom(
denom: impl AsRef<str>,
) -> Option<(TracePath, String)> {
pub fn is_ibc_denom(denom: impl AsRef<str>) -> Option<(TracePath, String)> {
let prefixed_denom = PrefixedDenom::from_str(denom.as_ref()).ok()?;
if prefixed_denom.trace_path.is_empty() {
return None;
}
// The base token isn't decoded because it could be non Namada token
Some((
prefixed_denom.trace_path,
Expand Down
5 changes: 0 additions & 5 deletions shared/src/sdk/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use namada_core::types::time::DateTimeUtc;
use serde::{Deserialize, Serialize};
use zeroize::Zeroizing;

use crate::ibc::applications::transfer::TracePath;
use crate::ibc::core::ics24_host::identifier::{ChannelId, PortId};
use crate::types::address::Address;
use crate::types::keccak::KeccakHash;
Expand Down Expand Up @@ -136,8 +135,6 @@ pub struct TxTransfer<C: NamadaTypes = SdkTypes> {
pub target: C::TransferTarget,
/// Transferred token address
pub token: C::Address,
/// Transferred token's trace path
pub trace_path: Option<TracePath>,
/// Transferred token amount
pub amount: InputAmount,
/// Native token address
Expand Down Expand Up @@ -168,8 +165,6 @@ pub struct TxIbcTransfer<C: NamadaTypes = SdkTypes> {
pub receiver: String,
/// Transferred token address
pub token: C::Address,
/// Transferred token's trace path
pub trace_path: Option<TracePath>,
/// Transferred token amount
pub amount: InputAmount,
/// Port ID
Expand Down
45 changes: 44 additions & 1 deletion shared/src/sdk/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use namada_core::ledger::governance::storage::proposal::StorageProposal;
use namada_core::ledger::governance::utils::Vote;
use namada_core::ledger::storage::LastBlock;
use namada_core::types::account::Account;
use namada_core::types::address::Address;
use namada_core::types::address::{Address, InternalAddress};
use namada_core::types::storage::Key;
use namada_core::types::token::{
Amount, DenominatedAmount, Denomination, MaspDenom,
Expand All @@ -25,6 +25,9 @@ use namada_proof_of_stake::types::{
use serde::Serialize;

use crate::ledger::events::Event;
use crate::ledger::ibc::storage::{
ibc_denom_key, ibc_denom_key_prefix, is_ibc_denom_key,
};
use crate::ledger::queries::vp::pos::EnrichedBondsAndUnbondsDetails;
use crate::ledger::queries::RPC;
use crate::proto::Tx;
Expand Down Expand Up @@ -1088,3 +1091,43 @@ pub async fn format_denominated_amount<
});
DenominatedAmount { amount, denom }.to_string()
}

/// Look up the IBC denomination from a IbcToken.
pub async fn query_ibc_denom<
C: crate::ledger::queries::Client + Sync,
IO: Io,
>(
client: &C,
token: &Address,
owner: Option<&Address>,
) -> String {
let hash = match token {
Address::Internal(InternalAddress::IbcToken(hash)) => hash,
_ => return token.to_string(),
};

if let Some(owner) = owner {
let ibc_denom_key = ibc_denom_key(owner.to_string(), hash);
if let Ok(ibc_denom) =
query_storage_value::<C, String>(client, &ibc_denom_key).await
{
return ibc_denom;
}
}

// No owner is specified or the owner doesn't have the token
let ibc_denom_prefix = ibc_denom_key_prefix(None);
if let Ok(Some(ibc_denoms)) =
query_storage_prefix::<C, IO, String>(client, &ibc_denom_prefix).await
{
for (key, ibc_denom) in ibc_denoms {
if let Some((_, token_hash)) = is_ibc_denom_key(&key) {
if token_hash == *hash {
return ibc_denom;
}
}
}
}

token.to_string()
}
1 change: 0 additions & 1 deletion shared/src/sdk/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ pub async fn wrap_tx<
fee_payer_address.clone(),
),
token: args.fee_token.clone(),
trace_path: None,
amount: args::InputAmount::Validated(DenominatedAmount {
// NOTE: must unshield the total fee amount, not the
// diff, because the ledger evaluates the transaction in
Expand Down
39 changes: 16 additions & 23 deletions shared/src/sdk/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ use namada_proof_of_stake::types::{CommissionPair, ValidatorState};

use crate::ibc::applications::transfer::msgs::transfer::MsgTransfer;
use crate::ibc::applications::transfer::packet::PacketData;
use crate::ibc::applications::transfer::{PrefixedCoin, PrefixedDenom};
use crate::ibc::applications::transfer::PrefixedCoin;
use crate::ibc::core::ics04_channel::timeout::TimeoutHeight;
use crate::ibc::core::timestamp::Timestamp as IbcTimestamp;
use crate::ibc::core::Msg;
use crate::ibc::Height as IbcHeight;
use crate::ledger::ibc::storage::ibc_token;
use crate::proto::{MaspBuilder, Tx};
use crate::sdk::args::{self, InputAmount};
use crate::sdk::error::{EncodingError, Error, QueryError, Result, TxError};
Expand Down Expand Up @@ -1414,16 +1413,10 @@ pub async fn build_ibc_transfer<
.await
.map_err(|e| Error::from(QueryError::Wasm(e.to_string())))?;

let ibc_denom = PrefixedDenom {
trace_path: args.trace_path.unwrap_or_default(),
base_denom: args
.token
.to_string()
.parse()
.expect("Conversion from the token shouldn't fail"),
};
let ibc_denom =
rpc::query_ibc_denom::<_, IO>(client, &args.token, Some(&source)).await;
let token = PrefixedCoin {
denom: ibc_denom,
denom: ibc_denom.parse().expect("Invalid IBC denom"),
// Set the IBC amount as an integer
amount: validated_amount.into(),
};
Expand Down Expand Up @@ -1666,11 +1659,6 @@ pub async fn build_transfer<
) -> Result<(Tx, Option<Epoch>)> {
let source = args.source.effective_address();
let target = args.target.effective_address();
let token = if let Some(trace_path) = &args.trace_path {
ibc_token(format!("{}/{}", trace_path.clone(), args.token))
} else {
args.token.clone()
};

// Check that the source address exists on chain
source_exists_or_err::<_, IO>(source.clone(), args.tx.force, client)
Expand All @@ -1679,16 +1667,20 @@ pub async fn build_transfer<
target_exists_or_err::<_, IO>(target.clone(), args.tx.force, client)
.await?;
// Check source balance
let balance_key = token::balance_key(&token, &source);
let balance_key = token::balance_key(&args.token, &source);

// validate the amount given
let validated_amount =
validate_amount::<_, IO>(client, args.amount, &token, args.tx.force)
.await?;
let validated_amount = validate_amount::<_, IO>(
client,
args.amount,
&args.token,
args.tx.force,
)
.await?;

args.amount = InputAmount::Validated(validated_amount);
let post_balance = check_balance_too_low_err::<C, IO>(
&token,
&args.token,
&source,
validated_amount.amount,
balance_key,
Expand All @@ -1699,7 +1691,7 @@ pub async fn build_transfer<
let tx_source_balance = Some(TxSourcePostBalance {
post_balance,
source: source.clone(),
token: token.clone(),
token: args.token.clone(),
});

let masp_addr = masp();
Expand All @@ -1712,7 +1704,7 @@ pub async fn build_transfer<
// TODO Refactor me, we shouldn't rely on any specific token here.
(token::Amount::default(), args.native_token.clone())
} else {
(validated_amount.amount, token)
(validated_amount.amount, args.token.clone())
};
// Determine whether to pin this transaction to a storage key
let key = match &args.target {
Expand Down Expand Up @@ -2215,6 +2207,7 @@ fn validate_untrusted_code_err<IO: Io>(
Ok(())
}
}

async fn query_wasm_code_hash_buf<
C: crate::ledger::queries::Client + Sync,
IO: Io,
Expand Down
Loading

0 comments on commit b874ffa

Please sign in to comment.