Skip to content

Commit

Permalink
Simplify ibc_token_transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
soareschen committed May 9, 2022
1 parent 99cab13 commit 6fe286d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
8 changes: 7 additions & 1 deletion tools/test-framework/src/chain/driver/tagged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde_json as json;
use crate::error::Error;
use crate::ibc::denom::Denom;
use crate::prelude::TaggedConnectionIdRef;
use crate::types::id::{TaggedChannelIdRef, TaggedPortIdRef};
use crate::types::id::{TaggedChainIdRef, TaggedChannelIdRef, TaggedPortIdRef};
use crate::types::tagged::*;
use crate::types::wallet::{Wallet, WalletAddress};

Expand All @@ -31,6 +31,8 @@ use super::ChainDriver;
*/
#[async_trait]
pub trait TaggedChainDriverExt<Chain> {
fn chain_id(&self) -> TaggedChainIdRef<Chain>;

async fn send_tx(
&self,
wallet: &MonoTagged<Chain, &Wallet>,
Expand Down Expand Up @@ -133,6 +135,10 @@ pub trait TaggedChainDriverExt<Chain> {

#[async_trait]
impl<'a, Chain: Send> TaggedChainDriverExt<Chain> for MonoTagged<Chain, &'a ChainDriver> {
fn chain_id(&self) -> TaggedChainIdRef<Chain> {
MonoTagged::new(&self.value().chain_id)
}

async fn send_tx(
&self,
wallet: &MonoTagged<Chain, &Wallet>,
Expand Down
42 changes: 33 additions & 9 deletions tools/test-framework/src/relayer/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ use ibc_relayer::transfer::{
};

use crate::chain::driver::tagged::TaggedChainDriverExt;
use crate::chain::driver::ChainDriver;
use crate::error::Error;
use crate::ibc::denom::Denom;
use crate::types::binary::chains::ConnectedChains;
use crate::types::binary::channel::ConnectedChannel;
use crate::types::single::node::TaggedFullNodeExt;
use crate::types::id::{TaggedChannelIdRef, TaggedPortIdRef};
use crate::types::tagged::*;
use crate::types::wallet::{Wallet, WalletAddress};

Expand Down Expand Up @@ -73,20 +73,44 @@ pub fn tx_raw_ft_transfer<SrcChain: ChainHandle, DstChain: ChainHandle>(
Ok(events)
}

pub async fn ibc_token_transfer<SrcChain: ChainHandle, DstChain: ChainHandle>(
chains: &ConnectedChains<SrcChain, DstChain>,
channel: &ConnectedChannel<SrcChain, DstChain>,
pub fn ibc_token_transfer<SrcChain: ChainHandle, DstChain: ChainHandle>(
chain_driver: &MonoTagged<SrcChain, &ChainDriver>,
channel_id: &TaggedChannelIdRef<'_, SrcChain, DstChain>,
port_id: &TaggedPortIdRef<'_, SrcChain, DstChain>,
sender: &MonoTagged<SrcChain, &Wallet>,
recipient: &MonoTagged<DstChain, &WalletAddress>,
denom: &MonoTagged<SrcChain, &Denom>,
amount: u64,
) -> Result<(), Error> {
chain_driver
.value()
.runtime
.block_on(async_ibc_token_transfer(
chain_driver,
channel_id,
port_id,
sender,
recipient,
denom,
amount,
))
}

pub async fn async_ibc_token_transfer<SrcChain: ChainHandle, DstChain: ChainHandle>(
chain_driver: &MonoTagged<SrcChain, &ChainDriver>,
channel_id: &TaggedChannelIdRef<'_, SrcChain, DstChain>,
port_id: &TaggedPortIdRef<'_, SrcChain, DstChain>,
sender: &MonoTagged<SrcChain, &Wallet>,
recipient: &MonoTagged<DstChain, &WalletAddress>,
denom: &MonoTagged<SrcChain, &Denom>,
amount: u64,
) -> Result<(), Error> {
let chain_driver = chains.node_a.chain_driver();
let chain_id = chain_driver.chain_id();

let tx_config = &chain_driver.value().tx_config;

let status = query_status(
chains.chain_id_a().value(),
chain_id.value(),
&tx_config.rpc_client,
&tx_config.rpc_address,
)
Expand All @@ -95,8 +119,8 @@ pub async fn ibc_token_transfer<SrcChain: ChainHandle, DstChain: ChainHandle>(
let timeout = TransferTimeout::new(500, Duration::from_secs(60), &status)?;

let message = build_transfer_message(
channel.port_a.value().clone(),
*channel.channel_id_a.value(),
(*port_id.value()).clone(),
**channel_id.value(),
amount.into(),
denom.value().to_string(),
Signer::new(sender.value().address.0.clone()),
Expand Down
4 changes: 4 additions & 0 deletions tools/test-framework/src/types/single/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ use crate::types::process::ChildProcess;
use crate::types::tagged::*;
use crate::types::wallet::TestWallets;

pub type TaggedFullNode<Chain> = MonoTagged<Chain, FullNode>;

pub type TaggedFullNodeRef<'a, Chain> = MonoTagged<Chain, &'a FullNode>;

/**
Represents a full node running as a child process managed by the test.
*/
Expand Down

0 comments on commit 6fe286d

Please sign in to comment.