Skip to content

Commit

Permalink
Allow another logs format for get_tx_events.
Browse files Browse the repository at this point in the history
  • Loading branch information
dowlandaiello committed Jun 28, 2024
1 parent 5114992 commit 2724ef3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
6 changes: 3 additions & 3 deletions examples/configs/logs.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"start_time": 1719573012,
"start_time": 1719586636,
"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:45505",
"p2p_address": "0.0.0.0:35401",
"ibc_paths": []
},
{
Expand All @@ -16,7 +16,7 @@
"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:45843",
"p2p_address": "0.0.0.0:33347",
"ibc_paths": []
}
],
Expand Down
30 changes: 22 additions & 8 deletions src/utils/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::path::PathBuf;
impl TestContext {
/// Gets the event log of a transaction as a JSON object,
/// or returns an error if it does not exist.
pub fn get_tx_events(&self, chain_id: &str, hash: &str) -> Result<Vec<Value>, Error> {
pub fn get_tx_events(&self, chain_id: &str, hash: &str) -> Result<Vec<Vec<Value>>, Error> {
let chain = self.get_chain(chain_id);
let logs = chain.rb.query_tx_hash(hash);

Expand All @@ -21,14 +21,25 @@ impl TestContext {
.and_then(|raw_log| raw_log.as_str())
.ok_or(Error::TxMissingLogs)?;

if serde_json::from_str::<Value>(raw_log).is_err() {
return Err(Error::TxFailed {
hash: hash.to_owned(),
error: raw_log.to_owned(),
});
let logs = serde_json::from_str::<Value>(raw_log).map_err(|_| Error::TxFailed {
hash: hash.to_owned(),
error: raw_log.to_owned(),
})?;

// raw_log can be an array or just the event itself
if let Some(arr) = logs.as_array() {
return arr
.into_iter()
.map(|msg| {
msg.get("events")
.cloned()
.and_then(|events| events.as_array().cloned())
.ok_or(Error::TxMissingLogs)
})
.collect::<Result<Vec<Vec<Value>>, Error>>();
}

let logs = logs.get("events").ok_or(Error::TxMissingLogs)?;
let events = logs.get("events").ok_or(Error::TxMissingLogs)?;

if let Some(err) = logs.as_str() {
return Err(Error::TxFailed {
Expand All @@ -37,7 +48,10 @@ impl TestContext {
});
}

logs.as_array().cloned().ok_or(Error::TxMissingLogs)
Ok(vec![events
.as_array()
.cloned()
.ok_or(Error::TxMissingLogs)?])
}

/// Get a new CosmWasm instance for a contract identified by a name.
Expand Down
13 changes: 1 addition & 12 deletions src/utils/setup/astroport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,18 +400,7 @@ impl TestContext {
"transaction did not produce a tx hash",
)))?;

let logs = self.get_tx_events(NEUTRON_CHAIN_ID, tx_hash.as_str())?;

let addr = logs
.into_iter()
.find(|event| {
event.get("type").and_then(|maybe_ty| maybe_ty.as_str()) == Some("instantiate")
})
.and_then(|mut event| event.get_mut("attributes").map(|e| e.take()))
.and_then(|attrs| Some(attrs.as_array()?.get(0)?.get("value")?.as_str()?.to_owned()))
.ok_or(Error::ContainerCmd(String::from("query create_pool logs")))?;

log::debug!("created pool: {}", addr);
let _ = self.get_tx_events(NEUTRON_CHAIN_ID, tx_hash.as_str())?;

Ok(())
}
Expand Down

0 comments on commit 2724ef3

Please sign in to comment.