Skip to content

Commit

Permalink
refactor: use axelar-wasm-std ContractError (#105)
Browse files Browse the repository at this point in the history
All contracts now return axelar-wasm-std ContractError at the highest level. Going forward, this allows us to stepwise integrate error-stack into each contract in an isolated manner
  • Loading branch information
cgorenflo authored Sep 19, 2023
1 parent 3ff39b3 commit 8d62f88
Show file tree
Hide file tree
Showing 29 changed files with 431 additions and 148 deletions.
27 changes: 27 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions contracts/aggregate-verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ optimize = """docker run --rm -v "$(pwd)":/code \
"""

[dependencies]
axelar-wasm-std = { workspace = true }
axelar-wasm-std-derive = { workspace = true }
connection-router = { workspace = true, features = ["library"] }
cosmwasm-schema = "1.1.3"
cosmwasm-std = "1.1.3"
cosmwasm-storage = "1.1.3"
cw-storage-plus = "1.0.1"
cw-utils = "1.0.1"
cw2 = "0.15.1"
error-stack = { workspace = true }
report = { workspace = true }
schemars = "0.8.10"
serde = { version = "1.0.145", default-features = false, features = ["derive"] }
serde_json = "1.0.89"
Expand Down
13 changes: 10 additions & 3 deletions contracts/aggregate-verifier/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn instantiate(
_env: Env,
_info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
) -> Result<Response, axelar_wasm_std::ContractError> {
let verifier = deps.api.addr_validate(&msg.verifier_address)?;
CONFIG.save(deps.storage, &Config { verifier })?;

Expand All @@ -36,9 +36,10 @@ pub fn execute(
_env: Env,
_info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
) -> Result<Response, axelar_wasm_std::ContractError> {
match msg {
ExecuteMsg::VerifyMessages { messages } => {
// todo: this conversion can go away once the message types are merged
let msgs = messages
.into_iter()
.map(state::Message::try_from)
Expand All @@ -47,6 +48,7 @@ pub fn execute(
verify_messages(deps, msgs)
}
}
.map_err(axelar_wasm_std::ContractError::from)
}

pub mod execute {
Expand Down Expand Up @@ -80,7 +82,11 @@ pub mod execute {
const VERIFY_REPLY: u64 = 0;

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn reply(_deps: DepsMut, _: Env, reply: Reply) -> Result<Response, ContractError> {
pub fn reply(
_deps: DepsMut,
_: Env,
reply: Reply,
) -> Result<Response, axelar_wasm_std::ContractError> {
match parse_reply_execute_data(reply) {
Ok(MsgExecuteContractResponse { data: Some(data) }) => {
// check format of data
Expand All @@ -97,6 +103,7 @@ pub fn reply(_deps: DepsMut, _: Env, reply: Reply) -> Result<Response, ContractE
e
))),
}
.map_err(axelar_wasm_std::ContractError::from)
}

#[cfg_attr(not(feature = "library"), entry_point)]
Expand Down
3 changes: 2 additions & 1 deletion contracts/aggregate-verifier/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use axelar_wasm_std_derive::IntoContractError;
use cosmwasm_std::StdError;
use thiserror::Error;

#[derive(Error, Debug, PartialEq)]
#[derive(Error, Debug, PartialEq, IntoContractError)]
pub enum ContractError {
#[error(transparent)]
Std(#[from] StdError),
Expand Down
6 changes: 3 additions & 3 deletions contracts/aggregate-verifier/tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use aggregate_verifier::contract::*;
use aggregate_verifier::error::ContractError;
use aggregate_verifier::msg::{ExecuteMsg, InstantiateMsg};
use axelar_wasm_std::ContractError;
use connection_router::msg::Message;
use connection_router::types::ID_SEPARATOR;
use cosmwasm_std::from_binary;
Expand Down Expand Up @@ -67,8 +67,8 @@ fn bad_message_id() {
)
.unwrap_err();
assert_eq!(
ContractError::RouterError(connection_router::error::ContractError::InvalidMessageID),
res.downcast().unwrap()
res.downcast::<ContractError>().unwrap().to_string(),
ContractError::from(connection_router::error::ContractError::InvalidMessageID).to_string()
)
}

Expand Down
4 changes: 4 additions & 0 deletions contracts/batching/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axelar-wasm-std = { workspace = true }
axelar-wasm-std-derive = { workspace = true }
cosmwasm-schema = "1.1.3"
cosmwasm-std = "1.1.3"
cw2 = "0.15.1"
error-stack = { workspace = true }
report = { workspace = true }
thiserror = { workspace = true }
2 changes: 1 addition & 1 deletion contracts/batching/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use axelar_wasm_std::ContractError;
#[cfg(not(feature = "library"))]
use cosmwasm_std::DepsMut;
use cosmwasm_std::{
entry_point, Env, Event, MessageInfo, Reply, Response, StdResult, SubMsg, SubMsgResult,
};

use crate::error::ContractError;
use crate::msg::{BatchMsg, ExecuteMsg, InstantiateMsg};

#[cfg_attr(not(feature = "library"), entry_point)]
Expand Down
3 changes: 3 additions & 0 deletions contracts/connection-router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ optimize = """docker run --rm -v "$(pwd)":/code \

[dependencies]
axelar-wasm-std = { workspace = true }
axelar-wasm-std-derive = { workspace = true }
cosmwasm-schema = "1.1.3"
cosmwasm-std = "1.1.3"
cosmwasm-storage = "1.1.3"
cw-storage-plus = "1.0.1"
cw2 = "0.15.1"
error-stack = { workspace = true }
flagset = { version = "0.4.3", features = ["serde"] }
report = { workspace = true }
schemars = "0.8.10"
serde = { version = "1.0.145", default-features = false, features = ["derive"] }
serde_json = "1.0.89"
Expand Down
8 changes: 5 additions & 3 deletions contracts/connection-router/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn instantiate(
_env: Env,
_info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
) -> Result<Response, axelar_wasm_std::ContractError> {
let admin = deps.api.addr_validate(&msg.admin_address)?;
let governance = deps.api.addr_validate(&msg.governance_address)?;
CONFIG.save(
Expand All @@ -32,7 +32,7 @@ pub fn execute(
_env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
) -> Result<Response, axelar_wasm_std::ContractError> {
match msg {
ExecuteMsg::RegisterChain {
chain,
Expand Down Expand Up @@ -63,9 +63,11 @@ pub fn execute(
info,
msgs.into_iter()
.map(Message::try_from)
.collect::<Result<Vec<Message>, _>>()?,
.collect::<Result<Vec<Message>, _>>()
.map_err(axelar_wasm_std::ContractError::from)?,
),
}
.map_err(axelar_wasm_std::ContractError::from)
}

pub mod execute {
Expand Down
3 changes: 2 additions & 1 deletion contracts/connection-router/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use axelar_wasm_std_derive::IntoContractError;
use cosmwasm_std::StdError;
use thiserror::Error;

use crate::types::ChainName;

#[derive(Error, Debug, PartialEq)]
#[derive(Error, Debug, PartialEq, IntoContractError)]
pub enum ContractError {
#[error(transparent)]
Std(#[from] StdError),
Expand Down
Loading

0 comments on commit 8d62f88

Please sign in to comment.