From 1b4789986bf069d09cc98d52aa0c60a835cb7a6c Mon Sep 17 00:00:00 2001 From: Dowland Aiello Date: Mon, 1 Jul 2024 05:25:06 -0700 Subject: [PATCH] Fix osmosis tx receipt guarding. --- examples/configs/logs.json | 73 ++++++++++++++++++++++++++++++++- examples/osmosis.rs | 12 +++++- src/utils/fixtures.rs | 9 +++- src/utils/setup/osmosis.rs | 84 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 173 insertions(+), 5 deletions(-) diff --git a/examples/configs/logs.json b/examples/configs/logs.json index 9e26dfe..5463486 100644 --- a/examples/configs/logs.json +++ b/examples/configs/logs.json @@ -1 +1,72 @@ -{} \ No newline at end of file +{ + "start_time": 1719836382, + "chains": [ + { + "chain_id": "localcosmos-1", + "chain_name": "localcosmos-1", + "rpc_address": "http://0.0.0.0:26658", + "rest_address": "http://0.0.0.0:1318", + "grpc_address": "0.0.0.0:9091", + "p2p_address": "0.0.0.0:32887", + "ibc_paths": [] + }, + { + "chain_id": "localneutron-1", + "chain_name": "localneutron-1", + "rpc_address": "http://0.0.0.0:26657", + "rest_address": "http://0.0.0.0:1317", + "grpc_address": "0.0.0.0:9090", + "p2p_address": "0.0.0.0:39595", + "ibc_paths": [ + "neutron-osmosis" + ] + }, + { + "chain_id": "localosmosis-1", + "chain_name": "localosmosis-1", + "rpc_address": "http://0.0.0.0:26659", + "rest_address": "http://0.0.0.0:1319", + "grpc_address": "0.0.0.0:9092", + "p2p_address": "0.0.0.0:45489", + "ibc_paths": [ + "neutron-osmosis" + ] + } + ], + "ibc_channels": [ + { + "chain_id": "localneutron-1", + "channel": { + "state": "STATE_OPEN", + "ordering": "ORDER_UNORDERED", + "counterparty": { + "port_id": "transfer", + "channel_id": "channel-0" + }, + "connection_hops": [ + "connection-0" + ], + "version": "ics20-1", + "port_id": "transfer", + "channel_id": "channel-0" + } + }, + { + "chain_id": "localosmosis-1", + "channel": { + "state": "STATE_OPEN", + "ordering": "ORDER_UNORDERED", + "counterparty": { + "port_id": "transfer", + "channel_id": "channel-0" + }, + "connection_hops": [ + "connection-0" + ], + "version": "ics20-1", + "port_id": "transfer", + "channel_id": "channel-0" + } + } + ] +} \ No newline at end of file diff --git a/examples/osmosis.rs b/examples/osmosis.rs index 85bbf04..b84baae 100644 --- a/examples/osmosis.rs +++ b/examples/osmosis.rs @@ -23,7 +23,7 @@ fn main() -> Result<(), Box> { ctx.get_tokenfactory_denom("osmo1kuf2kxwuv2p8k3gnpja7mzf05zvep0cysqyf2a", "bruhtoken"); ctx.build_tx_mint_tokenfactory_token() .with_chain_name(OSMOSIS_CHAIN_NAME) - .with_amount(10000000000) + .with_amount(10000000000000000000) .with_denom(&bruhtoken) .with_recipient_addr("osmo1kuf2kxwuv2p8k3gnpja7mzf05zvep0cysqyf2a") .send()?; @@ -37,7 +37,15 @@ fn main() -> Result<(), Box> { .send()?; // Get its id - let pool_id = ctx.get_osmo_pool("uosmo", bruhtoken)?; + let pool_id = ctx.get_osmo_pool("uosmo", &bruhtoken)?; + + // Fund the pool + ctx.build_tx_fund_osmo_pool() + .with_pool_id(pool_id) + .with_max_amount_in("uosmo", 10000) + .with_max_amount_in(&bruhtoken, 10000) + .with_share_amount_out(1000000000000) + .send()?; Ok(()) } diff --git a/src/utils/fixtures.rs b/src/utils/fixtures.rs index 69bc8d6..c5003a8 100644 --- a/src/utils/fixtures.rs +++ b/src/utils/fixtures.rs @@ -40,6 +40,10 @@ impl TestContext { .and_then(|raw_log| raw_log.as_str()) .ok_or(Error::TxMissingLogs)?; + if &raw_log == &"" { + return Ok(()); + } + let logs = serde_json::from_str::(raw_log).map_err(|_| Error::TxFailed { hash: hash.to_owned(), error: raw_log.to_owned(), @@ -246,7 +250,7 @@ impl TestContext { &self, denom_a: impl AsRef, denom_b: impl AsRef, - ) -> Result { + ) -> Result { let osmosis = self.get_chain(OSMOSIS_CHAIN_NAME); let denom_a_str = denom_a.as_ref(); @@ -290,6 +294,7 @@ impl TestContext { "q poolmanager list-pools-by-denom", )))?; - Ok(pool.to_owned()) + pool.parse() + .map_err(|_| Error::ContainerCmd(String::from("q poolmanager list-pools-by-denom"))) } } diff --git a/src/utils/setup/osmosis.rs b/src/utils/setup/osmosis.rs index 0f32698..79c0f42 100644 --- a/src/utils/setup/osmosis.rs +++ b/src/utils/setup/osmosis.rs @@ -65,6 +65,52 @@ impl<'a> CreateOsmoPoolTxBuilder<'a> { } } +pub struct FundOsmoPoolTxBuilder<'a> { + key: &'a str, + pool_id: Option, + max_amounts_in: Vec<(u64, &'a str)>, + share_amount_out: Option, + test_ctx: &'a mut TestContext, +} + +impl<'a> FundOsmoPoolTxBuilder<'a> { + pub fn with_key(&mut self, key: &'a str) -> &mut Self { + self.key = key; + + self + } + + pub fn with_pool_id(&mut self, pool_id: u64) -> &mut Self { + self.pool_id = Some(pool_id); + + self + } + + pub fn with_max_amount_in(&mut self, denom: &'a str, amount: u64) -> &mut Self { + self.max_amounts_in.push((amount, denom)); + + self + } + + pub fn with_share_amount_out(&mut self, share_amount_out: u64) -> &mut Self { + self.share_amount_out = Some(share_amount_out); + + self + } + + /// Sends the transaction, returning the pool ID if it was created successfully. + pub fn send(&mut self) -> Result<(), Error> { + self.test_ctx.tx_fund_osmo_pool( + self.key, + self.pool_id + .ok_or(Error::MissingBuilderParam(String::from("pool_id")))?, + self.max_amounts_in.iter().cloned(), + self.share_amount_out + .ok_or(Error::MissingBuilderParam(String::from("share_amount_out")))?, + ) + } +} + impl TestContext { pub fn build_tx_create_osmo_pool(&mut self) -> CreateOsmoPoolTxBuilder { CreateOsmoPoolTxBuilder { @@ -127,4 +173,42 @@ impl TestContext { Ok(()) } + + pub fn build_tx_fund_osmo_pool(&mut self) -> FundOsmoPoolTxBuilder { + FundOsmoPoolTxBuilder { + key: DEFAULT_KEY, + pool_id: Default::default(), + max_amounts_in: Default::default(), + share_amount_out: Default::default(), + test_ctx: self, + } + } + + /// Creates an osmosis pool with the given denoms. + fn tx_fund_osmo_pool<'a>( + &mut self, + key: &str, + pool_id: u64, + max_amounts_in: impl Iterator, + share_amount_out: u64, + ) -> Result<(), Error> { + let osmosis = self.get_chain(OSMOSIS_CHAIN_NAME); + + // Enter LP + let receipt = osmosis.rb.tx( + format!("tx gamm join-pool --pool-id {pool_id} --max-amounts-in {} --share-amount-out {share_amount_out} --from {key} --fees 2500uosmo --gas 1000000", max_amounts_in.map(|(weight, denom)| format!("{weight}{denom}")).collect::>().join(",")) + .as_str(), + true, + )?; + + let _ = self.guard_tx_errors( + OSMOSIS_CHAIN_NAME, + receipt + .get("txhash") + .and_then(|receipt| receipt.as_str()) + .ok_or(Error::TxMissingLogs)?, + )?; + + Ok(()) + } }