Skip to content

Commit

Permalink
Finish osmosis rest rpc code
Browse files Browse the repository at this point in the history
  • Loading branch information
sug0 committed Jan 9, 2025
1 parent 6ac571c commit 1772fe4
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 126 deletions.
37 changes: 25 additions & 12 deletions crates/apps_lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3597,7 +3597,7 @@ pub mod args {
pub const NO_CONVERSIONS: ArgFlag = flag("no-conversions");
pub const NO_EXPIRATION: ArgFlag = flag("no-expiration");
pub const NUT: ArgFlag = flag("nut");
pub const OSMOSIS_RPC: Arg<String> = arg("osmosis-rpc");
pub const OSMOSIS_REST_RPC: Arg<String> = arg("osmosis-rest-rpc");
pub const OUT_FILE_PATH_OPT: ArgOpt<PathBuf> = arg_opt("out-file-path");
pub const OUTPUT: ArgOpt<PathBuf> = arg_opt("output");
pub const OUTPUT_DENOM: Arg<String> = arg("output-denom");
Expand Down Expand Up @@ -5169,15 +5169,15 @@ pub mod args {
slippage: self.slippage,
local_recovery_addr: self.local_recovery_addr,
route: self.route,
osmosis_rpc: self.osmosis_rpc,
osmosis_rest_rpc: self.osmosis_rest_rpc,
})
}
}

impl Args for TxOsmosisSwap<CliTypes> {
fn parse(matches: &ArgMatches) -> Self {
let transfer = TxIbcTransfer::parse(matches);
let osmosis_rpc = OSMOSIS_RPC.parse(matches);
let osmosis_rest_rpc = OSMOSIS_REST_RPC.parse(matches);
let output_denom = OUTPUT_DENOM.parse(matches);
let maybe_trans_recipient = TARGET_OPT.parse(matches);
let maybe_shielded_recipient =
Expand Down Expand Up @@ -5230,16 +5230,16 @@ pub mod args {
slippage,
local_recovery_addr,
route,
osmosis_rpc,
osmosis_rest_rpc,
}
}

fn def(app: App) -> App {
app.add_args::<TxIbcTransfer<CliTypes>>()
.arg(
OSMOSIS_RPC
OSMOSIS_REST_RPC
.def()
.help(wrap!("A url pointing to an Osmosis rpc.")),
.help(wrap!("A url pointing to an Osmosis REST rpc.")),
)
.arg(OSMOSIS_POOL_HOP.def().help(wrap!(
"Individual hop of the route to take through Osmosis \
Expand Down Expand Up @@ -7183,11 +7183,22 @@ pub mod args {
query,
output_folder: self.output_folder,
target: chain_ctx.get(&self.target),
token: self.token,
amount: self.amount,
expiration: self.expiration,
port_id: self.port_id,
channel_id: self.channel_id,
asset: match self.asset {
IbcShieldingTransferAsset::LookupNamadaAddress {
port_id,
channel_id,
token,
} => IbcShieldingTransferAsset::LookupNamadaAddress {
port_id,
channel_id,
token,
},
IbcShieldingTransferAsset::Address(addr) => {
IbcShieldingTransferAsset::Address(chain_ctx.get(&addr))
}
},
})
}
}
Expand Down Expand Up @@ -7216,11 +7227,13 @@ pub mod args {
query,
output_folder,
target,
token,
amount,
expiration,
port_id,
channel_id,
asset: IbcShieldingTransferAsset::LookupNamadaAddress {
port_id,
channel_id,
token,
},
}
}

Expand Down
7 changes: 4 additions & 3 deletions crates/apps_lib/src/client/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1735,12 +1735,13 @@ pub async fn gen_ibc_shielding_transfer(
context: &impl Namada,
args: args::GenIbcShieldingTransfer,
) -> Result<(), error::Error> {
if let Some(masp_tx) =
tx::gen_ibc_shielding_transfer(context, args.clone()).await?
let output_folder = args.output_folder.clone();

if let Some(masp_tx) = tx::gen_ibc_shielding_transfer(context, args).await?
{
let tx_id = masp_tx.txid().to_string();
let filename = format!("ibc_masp_tx_{}.memo", tx_id);
let output_path = match &args.output_folder {
let output_path = match output_folder {
Some(path) => path.join(filename),
None => filename.into(),
};
Expand Down
68 changes: 45 additions & 23 deletions crates/sdk/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use namada_core::{storage, token};
use namada_governance::cli::onchain::{
DefaultProposal, PgfFundingProposal, PgfStewardProposal,
};
use namada_ibc::apps::transfer::types::TracePath;
use namada_ibc::IbcShieldingData;
use namada_token::masp::utils::RetryStrategy;
use namada_tx::data::GasLimit;
Expand All @@ -36,7 +35,8 @@ use crate::eth_bridge::bridge_pool;
use crate::ibc::core::host::types::identifiers::{ChannelId, PortId};
use crate::ibc::{NamadaMemo, NamadaMemoData};
use crate::rpc::{
calc_osmosis_denom_from_namada_denom, query_osmosis_pool_routes,
get_registry_from_xcs_osmosis_contract, osmosis_denom_from_namada_denom,
query_osmosis_pool_routes,
};
use crate::signing::SigningTxData;
use crate::wallet::{DatedSpendingKey, DatedViewingKey};
Expand Down Expand Up @@ -539,8 +539,8 @@ pub struct TxOsmosisSwap<C: NamadaTypes = SdkTypes> {
pub local_recovery_addr: String,
/// The route to take through Osmosis pools
pub route: Option<Vec<OsmosisPoolHop>>,
/// An rpc endpoint to Osmosis
pub osmosis_rpc: String,
/// A REST rpc endpoint to Osmosis
pub osmosis_rest_rpc: String,
}

impl TxOsmosisSwap<SdkTypes> {
Expand Down Expand Up @@ -595,13 +595,13 @@ impl TxOsmosisSwap<SdkTypes> {

let Self {
mut transfer,
output_denom,
recipient,
slippage,
local_recovery_addr,
route,
overflow,
osmosis_rpc,
osmosis_rest_rpc,
output_denom: namada_output_denom,
} = self;

let recipient = recipient
Expand Down Expand Up @@ -633,6 +633,20 @@ impl TxOsmosisSwap<SdkTypes> {
)));
}

let registry_xcs_addr = get_registry_from_xcs_osmosis_contract(
&osmosis_rest_rpc,
&transfer.receiver,
)
.await?;

let (osmosis_output_denom, namada_output_addr) =
osmosis_denom_from_namada_denom(
&osmosis_rest_rpc,
&registry_xcs_addr,
&namada_output_denom,
)
.await?;

let route = if let Some(route) = route {
route
} else {
Expand All @@ -641,23 +655,19 @@ impl TxOsmosisSwap<SdkTypes> {
&transfer.token,
transfer.amount,
transfer.channel_id.clone(),
&output_denom,
&osmosis_output_denom,
OSMOSIS_SQS_SERVER,
)
.await?
.pop()
.ok_or_else(|| {
Error::Other(format!(
"No route found to swap {:?} of Namada's {} with Osmosis' \
{}",
transfer.amount, transfer.token, output_denom
"No route found to swap {:?} of {} with {}",
transfer.amount, transfer.token, namada_output_addr,
))
})?
};

let osmosis_output_denom =
calc_osmosis_denom_from_namada_denom(&output_denom);

let (receiver, slippage, final_memo) = match recipient {
Either::Left(transparent_recipient) => {
(transparent_recipient.to_string(), slippage, None)
Expand All @@ -684,16 +694,16 @@ impl TxOsmosisSwap<SdkTypes> {
namada_core::masp::TransferTarget::PaymentAddress(
payment_addr,
),
token: output_denom.clone(),
asset: IbcShieldingTransferAsset::Address(
namada_output_addr,
),
amount: InputAmount::Validated(
token::DenominatedAmount::new(
amount_to_shield,
0u8.into(),
),
),
expiration: transfer.tx.expiration.clone(),
port_id: transfer.port_id.clone(),
channel_id: transfer.channel_id.clone(),
},
)
.await?
Expand Down Expand Up @@ -729,7 +739,7 @@ impl TxOsmosisSwap<SdkTypes> {
contract: transfer.receiver.clone(),
msg: Message {
osmosis_swap: OsmosisSwap {
output_denom,
output_denom: osmosis_output_denom,
slippage,
final_memo,
receiver,
Expand Down Expand Up @@ -3218,14 +3228,26 @@ pub struct GenIbcShieldingTransfer<C: NamadaTypes = SdkTypes> {
pub output_folder: Option<PathBuf>,
/// The target address
pub target: C::TransferTarget,
/// The token address which could be a non-namada address
pub token: String,
/// Transferred token amount
pub amount: InputAmount,
/// The optional expiration of the masp shielding transaction
pub expiration: TxExpiration,
/// Port ID via which the token is received
pub port_id: PortId,
/// Channel ID via which the token is received
pub channel_id: ChannelId,
/// Asset to shield over IBC to Namada
pub asset: IbcShieldingTransferAsset<C>,
}

/// IBC shielding transfer asset, to be used by [`GenIbcShieldingTransfer`]
#[derive(Clone, Debug)]
pub enum IbcShieldingTransferAsset<C: NamadaTypes = SdkTypes> {
/// Attempt to look-up the address of the asset to shield on Namada
LookupNamadaAddress {
/// The token address which could be a non-namada address
token: String,
/// Port ID via which the token is received
port_id: PortId,
/// Channel ID via which the token is received
channel_id: ChannelId,
},
/// Namada address of the token that will be received.
Address(C::Address),
}
Loading

0 comments on commit 1772fe4

Please sign in to comment.