Skip to content

Commit

Permalink
Updates after reivew remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
albertandrejev committed Dec 7, 2023
1 parent 56a1890 commit 38f6891
Show file tree
Hide file tree
Showing 19 changed files with 217 additions and 151 deletions.
217 changes: 119 additions & 98 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ members = ["contracts/interchain-interceptor", "contracts/interchain-interceptor

[workspace.dependencies]
cw-ownable = "0.5.1"
thiserror = "1.0.50"

[profile.release]
rpath = false
Expand Down
5 changes: 1 addition & 4 deletions contracts/interchain-interceptor-authz/.cargo/config
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
[alias]
wasm = "build --release --target wasm32-unknown-unknown"
unit-test = "test --lib"
schema = "run --example schema"
integration-test = "test --test integration"
schema = "run --bin lido-interchain-interceptor-authz-schema"
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use cosmwasm_schema::write_api;

use lido_interchain_interceptor_authz::{
msg::{ExecuteMsg, InstantiateMsg, MigrateMsg},
state::QueryMsg,
};

fn main() {
write_api! {
instantiate: InstantiateMsg,
query: QueryMsg,
execute: ExecuteMsg,
migrate: MigrateMsg
}
}
17 changes: 9 additions & 8 deletions contracts/interchain-interceptor-authz/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use cosmos_sdk_proto::cosmos::{
distribution::v1beta1::MsgWithdrawDelegatorReward,
staking::v1beta1::{MsgBeginRedelegate, MsgDelegate, MsgUndelegate},
};
use cosmwasm_std::{entry_point, to_vec, CosmosMsg, Deps, Reply, StdError, SubMsg, Uint128};
use cosmwasm_std::{entry_point, to_json_vec, CosmosMsg, Deps, Reply, StdError, SubMsg, Uint128};
use cosmwasm_std::{Binary, DepsMut, Env, MessageInfo, Response, StdResult};
use cw2::set_contract_version;
use neutron_sdk::{
Expand All @@ -20,6 +20,7 @@ use neutron_sdk::{
};

use lido_interchain_interceptor_base::{
error::ContractResult,
msg::{QueryMsg, SudoPayload},
state::{InterchainIntercaptorBase, State, ICA_ID, SUDO_PAYLOAD_REPLY_ID},
};
Expand Down Expand Up @@ -71,7 +72,7 @@ pub fn execute(
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> NeutronResult<Response<NeutronMsg>> {
) -> ContractResult<Response<NeutronMsg>> {
let interceptor_base = InterchainInterceptor::default();

match msg {
Expand Down Expand Up @@ -112,7 +113,7 @@ pub fn execute(
fn register_delegations_query(
deps: DepsMut<NeutronQuery>,
validators: Vec<String>,
) -> NeutronResult<Response<NeutronMsg>> {
) -> ContractResult<Response<NeutronMsg>> {
let interceptor_base = InterchainInterceptor::default();
let config = interceptor_base.config.load(deps.storage)?;

Expand All @@ -132,7 +133,7 @@ fn execute_delegate(
validator: String,
amount: Uint128,
timeout: Option<u64>,
) -> NeutronResult<Response<NeutronMsg>> {
) -> ContractResult<Response<NeutronMsg>> {
let interceptor_base = InterchainInterceptor::default();
let config: Config = interceptor_base.config.load(deps.storage)?;
let state: State = interceptor_base.state.load(deps.storage)?;
Expand Down Expand Up @@ -184,7 +185,7 @@ fn execute_undelegate(
validator: String,
amount: Uint128,
timeout: Option<u64>,
) -> NeutronResult<Response<NeutronMsg>> {
) -> ContractResult<Response<NeutronMsg>> {
let interceptor_base = InterchainInterceptor::default();
let config: Config = interceptor_base.config.load(deps.storage)?;
let state: State = interceptor_base.state.load(deps.storage)?;
Expand Down Expand Up @@ -238,7 +239,7 @@ fn execute_redelegate(
validator_to: String,
amount: Uint128,
timeout: Option<u64>,
) -> NeutronResult<Response<NeutronMsg>> {
) -> ContractResult<Response<NeutronMsg>> {
let interceptor_base = InterchainInterceptor::default();
let config: Config = interceptor_base.config.load(deps.storage)?;
let state: State = interceptor_base.state.load(deps.storage)?;
Expand Down Expand Up @@ -292,7 +293,7 @@ fn execute_withdraw_reward(
_info: MessageInfo,
validator: String,
timeout: Option<u64>,
) -> NeutronResult<Response<NeutronMsg>> {
) -> ContractResult<Response<NeutronMsg>> {
let interceptor_base = InterchainInterceptor::default();
let config: Config = interceptor_base.config.load(deps.storage)?;
let state: State = interceptor_base.state.load(deps.storage)?;
Expand Down Expand Up @@ -387,7 +388,7 @@ fn msg_with_sudo_callback<C: Into<CosmosMsg<T>>, T>(
let interceptor_base = InterchainInterceptor::default();
interceptor_base
.reply_id_storage
.save(deps.storage, &to_vec(&payload)?)?;
.save(deps.storage, &to_json_vec(&payload)?)?;

Ok(SubMsg::reply_on_success(msg, SUDO_PAYLOAD_REPLY_ID))
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/interchain-interceptor-authz/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl ExecuteMsg {
ack_fee: *ack_fee,
timeout_fee: *timeout_fee,
},
_ => panic!("Not implemented"),
_ => unimplemented!(),
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions contracts/interchain-interceptor/.cargo/config
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
[alias]
wasm = "build --release --target wasm32-unknown-unknown"
unit-test = "test --lib"
schema = "run --example schema"
integration-test = "test --test integration"
schema = "run --bin lido-interchain-interceptor-schema"
19 changes: 10 additions & 9 deletions contracts/interchain-interceptor/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cosmos_sdk_proto::cosmos::{
MsgUndelegateResponse,
},
};
use cosmwasm_std::{entry_point, to_vec, CosmosMsg, Deps, Reply, StdError, SubMsg, Uint128};
use cosmwasm_std::{entry_point, to_json_vec, CosmosMsg, Deps, Reply, StdError, SubMsg, Uint128};
use cosmwasm_std::{Binary, DepsMut, Env, MessageInfo, Response, StdResult};
use cw2::set_contract_version;
use neutron_sdk::{
Expand All @@ -21,6 +21,7 @@ use neutron_sdk::{
};

use lido_interchain_interceptor_base::{
error::ContractResult,
msg::{QueryMsg, SudoPayload},
state::{InterchainIntercaptorBase, State, ICA_ID, SUDO_PAYLOAD_REPLY_ID},
};
Expand Down Expand Up @@ -75,7 +76,7 @@ pub fn execute(
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> NeutronResult<Response<NeutronMsg>> {
) -> ContractResult<Response<NeutronMsg>> {
let interceptor_base = InterchainInterceptor::default();

match msg {
Expand Down Expand Up @@ -124,7 +125,7 @@ pub fn execute(
fn register_delegations_query(
deps: DepsMut<NeutronQuery>,
validators: Vec<String>,
) -> NeutronResult<Response<NeutronMsg>> {
) -> ContractResult<Response<NeutronMsg>> {
let interceptor_base = InterchainInterceptor::default();
let config = interceptor_base.config.load(deps.storage)?;
let state: State = interceptor_base.state.load(deps.storage)?;
Expand All @@ -148,7 +149,7 @@ fn execute_delegate(
validator: String,
amount: Uint128,
timeout: Option<u64>,
) -> NeutronResult<Response<NeutronMsg>> {
) -> ContractResult<Response<NeutronMsg>> {
let interceptor_base = InterchainInterceptor::default();
let config: Config = interceptor_base.config.load(deps.storage)?;
let state: State = interceptor_base.state.load(deps.storage)?;
Expand Down Expand Up @@ -189,7 +190,7 @@ fn execute_undelegate(
validator: String,
amount: Uint128,
timeout: Option<u64>,
) -> NeutronResult<Response<NeutronMsg>> {
) -> ContractResult<Response<NeutronMsg>> {
let interceptor_base = InterchainInterceptor::default();
let config: Config = interceptor_base.config.load(deps.storage)?;
let state: State = interceptor_base.state.load(deps.storage)?;
Expand Down Expand Up @@ -233,7 +234,7 @@ fn execute_redelegate(
validator_to: String,
amount: Uint128,
timeout: Option<u64>,
) -> NeutronResult<Response<NeutronMsg>> {
) -> ContractResult<Response<NeutronMsg>> {
let interceptor_base = InterchainInterceptor::default();
let config: Config = interceptor_base.config.load(deps.storage)?;
let state: State = interceptor_base.state.load(deps.storage)?;
Expand Down Expand Up @@ -277,7 +278,7 @@ fn execute_tokenize_share(
validator: String,
amount: Uint128,
timeout: Option<u64>,
) -> NeutronResult<Response<NeutronMsg>> {
) -> ContractResult<Response<NeutronMsg>> {
let interceptor_base = InterchainInterceptor::default();
let config: Config = interceptor_base.config.load(deps.storage)?;
let state: State = interceptor_base.state.load(deps.storage)?;
Expand Down Expand Up @@ -321,7 +322,7 @@ fn execute_redeem_share(
amount: Uint128,
denom: String,
timeout: Option<u64>,
) -> NeutronResult<Response<NeutronMsg>> {
) -> ContractResult<Response<NeutronMsg>> {
let interceptor_base = InterchainInterceptor::default();
let config: Config = interceptor_base.config.load(deps.storage)?;
let state: State = interceptor_base.state.load(deps.storage)?;
Expand Down Expand Up @@ -410,7 +411,7 @@ fn msg_with_sudo_callback<C: Into<CosmosMsg<T>>, T>(
let interceptor_base = InterchainInterceptor::default();
interceptor_base
.reply_id_storage
.save(deps.storage, &to_vec(&payload)?)?;
.save(deps.storage, &to_json_vec(&payload)?)?;

Ok(SubMsg::reply_on_success(msg, SUDO_PAYLOAD_REPLY_ID))
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/interchain-interceptor/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl ExecuteMsg {
ack_fee: *ack_fee,
timeout_fee: *timeout_fee,
},
_ => panic!("Not implemented"),
_ => unimplemented!(),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/interchain-interceptor-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ prost = "0.12.1"
prost-types = "0.12.1"
protobuf = "3.2.0"
tendermint-proto = "0.34.0"
thiserror = { workspace = true }

cosmwasm-schema = { version = "1.3.1" }
cosmwasm-std = { version = "1.2.1" }
Expand Down
20 changes: 20 additions & 0 deletions packages/interchain-interceptor-base/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use cosmwasm_std::{OverflowError, StdError};
use neutron_sdk::NeutronError;
use thiserror::Error;

#[derive(Error, Debug, PartialEq)]
pub enum ContractError {
#[error("{0}")]
Std(#[from] StdError),

#[error("{0}")]
NeutronError(#[from] NeutronError),

#[error("{0}")]
OverflowError(#[from] OverflowError),

#[error("ICA is not registered")]
IcaNotRegistered {},
}

pub type ContractResult<T> = Result<T, ContractError>;
43 changes: 25 additions & 18 deletions packages/interchain-interceptor-base/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use neutron_sdk::{
},
interchain_queries::v045::new_register_transfers_query_msg,
interchain_txs::helpers::get_port_id,
NeutronError, NeutronResult,
NeutronResult,
};
use serde::{de::DeserializeOwned, Serialize};

use crate::{
error::{ContractError, ContractResult},
msg::ExecuteMsg,
state::{BaseConfig, InterchainIntercaptorBase, State, ICA_ID, LOCAL_DENOM},
};
Expand All @@ -36,7 +37,7 @@ where
deps: DepsMut<NeutronQuery>,
env: Env,
msg: ExecuteMsg,
) -> NeutronResult<Response<NeutronMsg>> {
) -> ContractResult<Response<NeutronMsg>> {
match msg {
ExecuteMsg::RegisterICA {} => self.execute_register_ica(deps, env),
ExecuteMsg::RegisterQuery {} => self.register_transfers_query(deps),
Expand All @@ -52,22 +53,28 @@ where
&self,
deps: DepsMut<NeutronQuery>,
env: Env,
) -> NeutronResult<Response<NeutronMsg>> {
) -> ContractResult<Response<NeutronMsg>> {
let config = self.config.load(deps.storage)?;
let state: State = self.state.load(deps.storage)?;
match state.ica {
None => {
let register = NeutronMsg::register_interchain_account(
config.connection_id(),
ICA_ID.to_string(),
);
let _key = get_port_id(env.contract.address.as_str(), ICA_ID);
if state.under_execution {
Err(ContractError::Std(cosmwasm_std::StdError::GenericErr {
msg: "ICA is already registered or under execution".to_string(),
}))
} else {
let register =
NeutronMsg::register_interchain_account(config.connection_id(), ICA_ID.to_string());
let _key = get_port_id(env.contract.address.as_str(), ICA_ID);

self.state.save(
deps.storage,
&State {
last_processed_height: None,
ica: None,
under_execution: true,
},
)?;

Ok(Response::new().add_message(register))
}
Some(_) => Err(NeutronError::Std(cosmwasm_std::StdError::GenericErr {
msg: "ICA already registered".to_string(),
})),
Ok(Response::new().add_message(register))
}
}

Expand All @@ -77,7 +84,7 @@ where
recv_fee: Uint128,
ack_fee: Uint128,
timeout_fee: Uint128,
) -> NeutronResult<Response<NeutronMsg>> {
) -> ContractResult<Response<NeutronMsg>> {
let fees = IbcFee {
recv_fee: vec![CosmosCoin {
denom: LOCAL_DENOM.to_string(),
Expand All @@ -99,7 +106,7 @@ where
fn register_transfers_query(
&self,
deps: DepsMut<NeutronQuery>,
) -> NeutronResult<Response<NeutronMsg>> {
) -> ContractResult<Response<NeutronMsg>> {
let config = self.config.load(deps.storage)?;
let state: State = self.state.load(deps.storage)?;

Expand All @@ -112,7 +119,7 @@ where
)?;
Ok(Response::new().add_message(msg))
} else {
Err(NeutronError::IntegrationTestsMock {})
Err(ContractError::IcaNotRegistered {})
}
}
}
1 change: 1 addition & 0 deletions packages/interchain-interceptor-base/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod error;
pub mod execute;
pub mod msg;
pub mod query;
Expand Down
12 changes: 6 additions & 6 deletions packages/interchain-interceptor-base/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{to_binary, Binary, Deps, Env, StdResult};
use cosmwasm_std::{to_json_binary, Binary, Deps, Env, StdResult};
use neutron_sdk::bindings::query::NeutronQuery;
use serde::{de::DeserializeOwned, Serialize};

Expand Down Expand Up @@ -28,27 +28,27 @@ where
delegations,
last_updated_height,
};
to_binary(&response)
to_json_binary(&response)
}

fn query_state(&self, deps: Deps<NeutronQuery>, _env: Env) -> StdResult<Binary> {
let state: State = self.state.load(deps.storage)?;
to_binary(&state)
to_json_binary(&state)
}

fn query_done_transactions(&self, deps: Deps<NeutronQuery>, _env: Env) -> StdResult<Binary> {
deps.api.debug("WASMDEBUG: query_done_transactions");
let state: Vec<C> = self.transactions.load(deps.storage)?;
to_binary(&state)
to_json_binary(&state)
}

fn query_config(&self, deps: Deps<NeutronQuery>, _env: Env) -> StdResult<Binary> {
let config: T = self.config.load(deps.storage)?;
to_binary(&config)
to_json_binary(&config)
}

fn query_transactions(&self, deps: Deps<NeutronQuery>, _env: Env) -> StdResult<Binary> {
let transactions: Vec<Transfer> = self.recipient_txs.load(deps.storage)?;
to_binary(&transactions)
to_json_binary(&transactions)
}
}
Loading

0 comments on commit 38f6891

Please sign in to comment.