Skip to content

Commit

Permalink
Fix osmosis tx receipt guarding.
Browse files Browse the repository at this point in the history
  • Loading branch information
dowlandaiello committed Jul 1, 2024
1 parent 85bf346 commit 1b47899
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 5 deletions.
73 changes: 72 additions & 1 deletion examples/configs/logs.json
Original file line number Diff line number Diff line change
@@ -1 +1,72 @@
{}
{
"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"
}
}
]
}
12 changes: 10 additions & 2 deletions examples/osmosis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() -> Result<(), Box<dyn Error>> {
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()?;
Expand All @@ -37,7 +37,15 @@ fn main() -> Result<(), Box<dyn Error>> {
.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(())
}
9 changes: 7 additions & 2 deletions src/utils/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Value>(raw_log).map_err(|_| Error::TxFailed {
hash: hash.to_owned(),
error: raw_log.to_owned(),
Expand Down Expand Up @@ -246,7 +250,7 @@ impl TestContext {
&self,
denom_a: impl AsRef<str>,
denom_b: impl AsRef<str>,
) -> Result<String, Error> {
) -> Result<u64, Error> {
let osmosis = self.get_chain(OSMOSIS_CHAIN_NAME);
let denom_a_str = denom_a.as_ref();

Expand Down Expand Up @@ -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")))
}
}
84 changes: 84 additions & 0 deletions src/utils/setup/osmosis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,52 @@ impl<'a> CreateOsmoPoolTxBuilder<'a> {
}
}

pub struct FundOsmoPoolTxBuilder<'a> {
key: &'a str,
pool_id: Option<u64>,
max_amounts_in: Vec<(u64, &'a str)>,
share_amount_out: Option<u64>,
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 {
Expand Down Expand Up @@ -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<Item = (u64, &'a str)>,
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::<Vec<_>>().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(())
}
}

0 comments on commit 1b47899

Please sign in to comment.