Skip to content

Commit

Permalink
Merge pull request #13 from fpco/gas-txhash-info-in-actions
Browse files Browse the repository at this point in the history
Include gas, fee, and txhash in some actions
  • Loading branch information
snoyberg authored May 15, 2024
2 parents 02c6d0f + 65f3346 commit e444684
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 37 deletions.
53 changes: 33 additions & 20 deletions packages/cosmos/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,34 +656,34 @@ impl Cosmos {
res.account
.ok_or_else(|| crate::Error::InvalidChainResponse {
message: "no eth account found".to_owned(),
action: action.clone(),
action: action.clone().into(),
})?
.value
.as_ref(),
)
.map_err(|source| crate::Error::InvalidChainResponse {
message: format!("Unable to parse eth_account: {source}"),
action: action.clone(),
action: action.clone().into(),
})?;
eth_account
.base_account
.ok_or_else(|| crate::Error::InvalidChainResponse {
message: "no base account found".to_owned(),
action: action.clone(),
action: action.clone().into(),
})?
} else {
prost::Message::decode(
res.account
.ok_or_else(|| crate::Error::InvalidChainResponse {
message: "no account found".to_owned(),
action: action.clone(),
action: action.clone().into(),
})?
.value
.as_ref(),
)
.map_err(|source| crate::Error::InvalidChainResponse {
message: format!("Unable to parse account: {source}"),
action,
action: action.into(),
})?
};
Ok(base_account)
Expand Down Expand Up @@ -740,18 +740,18 @@ impl Cosmos {
.tx
.ok_or_else(|| crate::Error::InvalidChainResponse {
message: "Missing tx field".to_owned(),
action: action.clone(),
action: action.clone().into(),
})?
.body
.ok_or_else(|| crate::Error::InvalidChainResponse {
message: "Missing tx.body field".to_owned(),
action: action.clone(),
action: action.clone().into(),
})?;
let txres = txres
.tx_response
.ok_or_else(|| crate::Error::InvalidChainResponse {
message: "Missing tx_response field".to_owned(),
action: action.clone(),
action: action.clone().into(),
})?;
Ok((txbody, txres))
}
Expand Down Expand Up @@ -879,7 +879,10 @@ impl Cosmos {
}
Err(match action {
None => crate::Error::WaitForTransactionTimedOut { txhash },
Some(action) => crate::Error::WaitForTransactionTimedOutWhile { txhash, action },
Some(action) => crate::Error::WaitForTransactionTimedOutWhile {
txhash,
action: action.into(),
},
})
}

Expand Down Expand Up @@ -1160,7 +1163,10 @@ impl BlockInfo {
chain_id,
})
})()
.map_err(|message| crate::Error::InvalidChainResponse { message, action })
.map_err(|message| crate::Error::InvalidChainResponse {
message,
action: action.into(),
})
}
}

Expand Down Expand Up @@ -1438,7 +1444,7 @@ impl TxBuilder {
.as_ref()
.ok_or_else(|| crate::Error::InvalidChainResponse {
message: "Missing gas_info in SimulateResponse".to_owned(),
action,
action: action.into(),
})?
.gas_used;

Expand Down Expand Up @@ -1489,13 +1495,14 @@ impl TxBuilder {
// }
let body_ref = &body;
let retry_with_price = |amount| async move {
let amount = Coin {
denom: cosmos.pool.builder.gas_coin().to_owned(),
amount,
};
let auth_info = AuthInfo {
signer_infos: vec![self.make_signer_info(sequence, Some(wallet))],
fee: Some(Fee {
amount: vec![Coin {
denom: cosmos.pool.builder.gas_coin().to_owned(),
amount,
}],
amount: vec![amount.clone()],
gas_limit: gas_to_request,
payer: "".to_owned(),
granter: "".to_owned(),
Expand All @@ -1518,20 +1525,26 @@ impl TxBuilder {
signatures: vec![signature.serialize_compact().to_vec()],
};

let mk_action = move || Action::Broadcast {
txbuilder: self.clone(),
gas_wanted: gas_to_request,
fee: amount.clone(),
};

let PerformQueryWrapper { grpc_url, tonic } = cosmos
.perform_query(
BroadcastTxRequest {
tx_bytes: tx.encode_to_vec(),
mode: BroadcastMode::Sync as i32,
},
Action::Broadcast(self.clone()),
mk_action(),
true,
)
.await?;
let res = tonic.into_inner().tx_response.ok_or_else(|| {
crate::Error::InvalidChainResponse {
message: "Missing inner tx_response".to_owned(),
action: Action::Broadcast(self.clone()),
action: mk_action().into(),
}
})?;

Expand All @@ -1540,7 +1553,7 @@ impl TxBuilder {
code: res.code.into(),
txhash: res.txhash.clone(),
raw_log: res.raw_log,
action: Action::Broadcast(self.clone()).into(),
action: mk_action().into(),
grpc_url,
stage: crate::error::TransactionStage::Broadcast,
});
Expand All @@ -1549,14 +1562,14 @@ impl TxBuilder {
tracing::debug!("Initial BroadcastTxResponse: {res:?}");

let (_, res) = cosmos
.wait_for_transaction_with_action(res.txhash, Some(Action::Broadcast(self.clone())))
.wait_for_transaction_with_action(res.txhash, Some(mk_action()))
.await?;
if !self.skip_code_check && res.code != 0 {
return Err(crate::Error::TransactionFailed {
code: res.code.into(),
txhash: res.txhash.clone(),
raw_log: res.raw_log,
action: Action::Broadcast(self.clone()).into(),
action: mk_action().into(),
grpc_url,
stage: crate::error::TransactionStage::Wait,
});
Expand Down
12 changes: 10 additions & 2 deletions packages/cosmos/src/codeid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ impl Cosmos {
self.make_code_id(res.parse_first_stored_code_id().map_err(|source| {
crate::Error::ChainParse {
source: source.into(),
action: Action::Broadcast(txbuilder),
action: Action::StoreCode {
txbuilder,
txhash: res.txhash,
}
.into(),
}
})?),
)
Expand Down Expand Up @@ -120,7 +124,11 @@ impl Cosmos {
let code_id = self.make_code_id(res.parse_first_stored_code_id().map_err(|source| {
crate::Error::ChainParse {
source: source.into(),
action: Action::Broadcast(txbuilder),
action: Action::StoreCode {
txbuilder,
txhash: res.txhash.clone(),
}
.into(),
}
})?);
Ok((res, code_id))
Expand Down
20 changes: 15 additions & 5 deletions packages/cosmos/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ impl CodeId {
res.parse_first_instantiated_contract()
.map_err(|source| crate::Error::ChainParse {
source: source.into(),
action: Action::Broadcast(txbuilder.clone()),
action: Action::InstantiateContract {
txbuilder: txbuilder.clone(),
txhash: res.txhash.clone(),
}
.into(),
})?;

if addr.get_address_hrp() == self.get_address_hrp() {
Expand All @@ -122,7 +126,11 @@ impl CodeId {
addr,
addr.get_address_hrp()
),
action: Action::Broadcast(txbuilder),
action: Action::InstantiateContract {
txbuilder,
txhash: res.txhash,
}
.into(),
})
}
}
Expand Down Expand Up @@ -290,8 +298,10 @@ impl Contract {
)
.await?
.into_inner();
serde_json::from_slice(&res.data)
.map_err(|source| crate::Error::JsonDeserialize { source, action })
serde_json::from_slice(&res.data).map_err(|source| crate::Error::JsonDeserialize {
source,
action: action.into(),
})
}

/// Perform a contract migration with the given message
Expand Down Expand Up @@ -338,7 +348,7 @@ impl Contract {
.contract_info
.ok_or_else(|| crate::Error::InvalidChainResponse {
message: "Missing contract_info field".to_string(),
action,
action: action.into(),
})
}

Expand Down
49 changes: 43 additions & 6 deletions packages/cosmos/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,24 @@ pub enum Error {
)]
JsonDeserialize {
source: serde_json::Error,
action: Action,
action: Box<Action>,
},
#[error(transparent)]
Query(#[from] QueryError),
#[error("Error parsing data returned from chain: {source}. While performing: {action}")]
ChainParse {
source: Box<crate::error::ChainParseError>,
action: Action,
action: Box<Action>,
},
#[error("Invalid response from chain: {message}. While performing: {action}")]
InvalidChainResponse { message: String, action: Action },
InvalidChainResponse {
message: String,
action: Box<Action>,
},
#[error("Timed out waiting for transaction {txhash}")]
WaitForTransactionTimedOut { txhash: String },
#[error("Timed out waiting for transaction {txhash} during {action}")]
WaitForTransactionTimedOutWhile { txhash: String, action: Action },
WaitForTransactionTimedOutWhile { txhash: String, action: Box<Action> },
#[error("Unable to load WASM code from {}: {source}", path.display())]
LoadingWasmFromFile {
path: PathBuf,
Expand Down Expand Up @@ -248,7 +251,11 @@ pub enum Action {
GetBlock(i64),
GetLatestBlock,
Simulate(TxBuilder),
Broadcast(TxBuilder),
Broadcast {
txbuilder: TxBuilder,
gas_wanted: u64,
fee: cosmos_sdk_proto::cosmos::base::v1beta1::Coin,
},
RawQuery {
contract: Address,
key: StringOrBytes,
Expand All @@ -264,6 +271,18 @@ pub enum Action {
SanityCheck,
OsmosisEpochsInfo,
OsmosisTxFeesInfo,
StoreCode {
txbuilder: TxBuilder,
txhash: String,
},
InstantiateContract {
txbuilder: TxBuilder,
txhash: String,
},
TokenFactory {
txbuilder: TxBuilder,
txhash: String,
},
}

impl Display for Action {
Expand All @@ -278,7 +297,15 @@ impl Display for Action {
Action::GetBlock(height) => write!(f, "get block {height}"),
Action::GetLatestBlock => f.write_str("get latest block"),
Action::Simulate(txbuilder) => write!(f, "simulating transaction: {txbuilder}"),
Action::Broadcast(txbuilder) => write!(f, "broadcasting transaction: {txbuilder}"),
Action::Broadcast {
txbuilder,
gas_wanted,
fee,
} => write!(
f,
"broadcasting transaction with {gas_wanted} gas and {}{} fee: {txbuilder}",
fee.amount, fee.denom
),
Action::RawQuery { contract, key } => {
write!(f, "raw query contract {contract} with key: {key}")
}
Expand All @@ -292,6 +319,16 @@ impl Display for Action {
Action::SanityCheck => f.write_str("sanity check"),
Action::OsmosisEpochsInfo => f.write_str("get Osmosis epochs info"),
Action::OsmosisTxFeesInfo => f.write_str("get Osmosis txfees info"),
Action::StoreCode { txbuilder, txhash } => {
write!(f, "store code in {txhash}: {txbuilder}")
}
Action::InstantiateContract { txbuilder, txhash } => {
write!(f, "instantiate contract in {txhash}: {txbuilder}")
}
Action::TokenFactory { txbuilder, txhash } => write!(
f,
"perform token factory operation in {txhash}: {txbuilder}"
),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/cosmos/src/osmosis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Cosmos {
source: Box::new(ChainParseError::TxFees {
err: err.to_string(),
}),
action: Action::OsmosisTxFeesInfo,
action: Action::OsmosisTxFeesInfo.into(),
})?,
false => {
let eip_base_fee =
Expand All @@ -69,14 +69,14 @@ impl Cosmos {
source: Box::new(ChainParseError::TxFees {
err: err.to_string(),
}),
action: Action::OsmosisTxFeesInfo,
action: Action::OsmosisTxFeesInfo.into(),
})?;

Decimal::from_atomics(eip_base_fee, 18).map_err(|err| Error::ChainParse {
source: Box::new(ChainParseError::TxFees {
err: err.to_string(),
}),
action: Action::OsmosisTxFeesInfo,
action: Action::OsmosisTxFeesInfo.into(),
})?
}
};
Expand Down
6 changes: 5 additions & 1 deletion packages/cosmos/src/tokenfactory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ impl TokenFactory {
})
.ok_or_else(|| crate::Error::InvalidChainResponse {
message: "Failed to get denom from tx events".to_owned(),
action: Action::Broadcast(txbuilder),
action: Action::TokenFactory {
txbuilder,
txhash: res.txhash.clone(),
}
.into(),
})?;

Ok((res, denom))
Expand Down

0 comments on commit e444684

Please sign in to comment.