Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cw orch implementation #69

Merged
merged 11 commits into from
Oct 30, 2024
2,614 changes: 2,266 additions & 348 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions contracts/hub/router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ serde = { workspace = true, default-features = false, features = ["derive"] }
thiserror = { workspace = true }
euclid = { workspace = true }
euclid-ibc = { workspace = true }
cw-orch = "=0.23.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
cw-multi-test = { workspace = true }
Expand Down
1 change: 0 additions & 1 deletion contracts/hub/router/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ pub fn execute_register_factory(
data: to_json_binary(&msg)?,
timeout: IbcTimeout::with_timestamp(env.block.time.plus_seconds(timeout)),
};

Ok(response
.add_attribute("channel", ibc_info.channel)
.add_attribute("timeout", timeout.to_string())
Expand Down
2 changes: 2 additions & 0 deletions contracts/hub/router/src/ibc/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ pub fn ibc_receive_internal_call(
// Get the chain data from current channel received
let channel = msg.packet.dest.channel_id;
let chain_uid = CHANNEL_TO_CHAIN_UID.load(deps.storage, channel)?;

let chain = CHAIN_UID_TO_CHAIN.load(deps.storage, chain_uid.clone())?;

// Ensure source port is the registered factory
ensure!(
msg.packet.src.port_id == format!("wasm.{address}", address = chain.factory),
Expand Down
25 changes: 25 additions & 0 deletions contracts/hub/router/src/interface.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::contract::{execute, instantiate, query};
use cw_orch::{interface, prelude::*};
use euclid::msgs::router::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
pub const CONTRACT_ID: &str = "router_contract";

#[interface(InstantiateMsg, ExecuteMsg, QueryMsg, MigrateMsg, id = CONTRACT_ID)]
pub struct RouterContract<Chain: CwEnv>;

// Implement the Uploadable trait so it can be uploaded to the mock.
impl<Chain> Uploadable for RouterContract<Chain> {
fn wrapper() -> Box<dyn MockContract<Empty>> {
Box::new(
ContractWrapper::new_with_empty(execute, instantiate, query)
.with_reply(crate::contract::reply)
.with_ibc(
crate::ibc::channel::ibc_channel_open,
crate::ibc::channel::ibc_channel_connect,
crate::ibc::channel::ibc_channel_close,
crate::ibc::receive::ibc_packet_receive,
crate::ibc::ack_and_timeout::ibc_packet_ack,
crate::ibc::ack_and_timeout::ibc_packet_timeout,
),
)
}
}
5 changes: 5 additions & 0 deletions contracts/hub/router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ mod tests;

#[cfg(not(target_arch = "wasm32"))]
pub mod mock;

#[cfg(not(target_arch = "wasm32"))]
mod interface;
#[cfg(not(target_arch = "wasm32"))]
pub use crate::interface::RouterContract;
1 change: 1 addition & 0 deletions contracts/hub/virtual_balance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ schemars = { workspace = true }
serde = { workspace = true, default-features = false, features = ["derive"] }
thiserror = { workspace = true }
euclid = { workspace = true }
cw-orch = "=0.23.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
cw-multi-test = { workspace = true }
Expand Down
14 changes: 14 additions & 0 deletions contracts/hub/virtual_balance/src/interface.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::contract::{execute, instantiate, query};
use cw_orch::{interface, prelude::*};
use euclid::msgs::virtual_balance::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
pub const CONTRACT_ID: &str = "virtual_balance_contract";

#[interface(InstantiateMsg, ExecuteMsg, QueryMsg, MigrateMsg, id = CONTRACT_ID)]
pub struct VirtualBalanceContract<Chain: CwEnv>;

// Implement the Uploadable trait so it can be uploaded to the mock.
impl<Chain> Uploadable for VirtualBalanceContract<Chain> {
fn wrapper() -> Box<dyn MockContract<Empty>> {
Box::new(ContractWrapper::new_with_empty(execute, instantiate, query))
}
}
5 changes: 5 additions & 0 deletions contracts/hub/virtual_balance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ pub mod state;
mod tests;

pub mod mock;

#[cfg(not(target_arch = "wasm32"))]
mod interface;
#[cfg(not(target_arch = "wasm32"))]
pub use crate::interface::VirtualBalanceContract;
2 changes: 2 additions & 0 deletions contracts/liquidity/escrow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ schemars = { workspace = true }
serde = { workspace = true, default-features = false, features = ["derive"] }
thiserror = { workspace = true }
euclid = { workspace = true }
cw-orch = "=0.23.0"


[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
cw-multi-test = { workspace = true }
Expand Down
14 changes: 14 additions & 0 deletions contracts/liquidity/escrow/src/interface.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::contract::{execute, instantiate, query};
use cw_orch::{interface, prelude::*};
use euclid::msgs::escrow::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
pub const CONTRACT_ID: &str = "escrow_contract";

#[interface(InstantiateMsg, ExecuteMsg, QueryMsg, MigrateMsg, id = CONTRACT_ID)]
pub struct EscrowContract<Chain: CwEnv>;

// Implement the Uploadable trait so it can be uploaded to the mock.
impl<Chain> Uploadable for EscrowContract<Chain> {
fn wrapper() -> Box<dyn MockContract<Empty>> {
Box::new(ContractWrapper::new_with_empty(execute, instantiate, query))
}
}
5 changes: 5 additions & 0 deletions contracts/liquidity/escrow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ mod tests;

#[cfg(not(target_arch = "wasm32"))]
pub mod mock;

#[cfg(not(target_arch = "wasm32"))]
mod interface;
#[cfg(not(target_arch = "wasm32"))]
pub use crate::interface::EscrowContract;
1 change: 0 additions & 1 deletion contracts/liquidity/escrow/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use crate::contract::{execute, instantiate, query, reply};
use cosmwasm_std::{to_json_binary, Addr, Coin, CosmosMsg, Empty, WasmMsg};
use cw_multi_test::{AppResponse, Contract, ContractWrapper, Executor};

use euclid::{
msgs::escrow::{ExecuteMsg, InstantiateMsg, QueryMsg, TokenIdResponse},
token::{Token, TokenType},
Expand Down
1 change: 1 addition & 0 deletions contracts/liquidity/factory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ serde = { workspace = true, default-features = false, features = ["derive"] }
thiserror = { workspace = true }
euclid = { workspace = true }
euclid-ibc = { workspace = true }
cw-orch = "=0.23.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
cw-multi-test = { workspace = true }
Expand Down
9 changes: 3 additions & 6 deletions contracts/liquidity/factory/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ pub fn add_liquidity_request(
);
ensure!(
PAIR_TO_VLP.has(deps.storage, pair.get_tupple()),
ContractError::PoolDoesNotExists {}
ContractError::PoolDoesNotExist {}
);

let channel = HUB_CHANNEL.load(deps.storage)?;
Expand Down Expand Up @@ -410,7 +410,7 @@ pub fn remove_liquidity_request(

ensure!(
PAIR_TO_VLP.has(deps.storage, pair.get_tupple()),
ContractError::PoolDoesNotExists {}
ContractError::PoolDoesNotExist {}
);
// TODO: Do we want to add check for lp shares for early fail?

Expand Down Expand Up @@ -653,15 +653,14 @@ pub fn execute_deposit_token(
asset_in.validate(deps.as_ref())?;

let tx_id = generate_tx(deps.branch(), &env, &sender)?;

let channel = HUB_CHANNEL.load(deps.storage)?;

let timeout = get_timeout(timeout)?;

ensure!(
!PENDING_TOKEN_DEPOSIT.has(deps.storage, (sender_addr.clone(), tx_id.clone())),
ContractError::TxAlreadyExist {}
);

// Verify that this asset is allowed
let escrow = TOKEN_TO_ESCROW.load(deps.storage, asset_in.token.clone())?;

Expand All @@ -671,12 +670,10 @@ pub fn execute_deposit_token(
denom: asset_in.token_type.clone(),
},
)?;

ensure!(
token_allowed.allowed,
ContractError::UnsupportedDenomination {}
);

let mut fund_manager = FundManager::new(&info.funds);

match &asset_in.token_type {
Expand Down
2 changes: 0 additions & 2 deletions contracts/liquidity/factory/src/ibc/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ pub fn ibc_packet_receive(
let tx_id = msg
.map(|m| m.get_tx_id())
.unwrap_or("tx_id_not_found".to_string());

Ok(IbcReceiveResponse::new()
.add_attribute("method", "ibc_packet_receive")
.add_attribute("tx_id", tx_id)
Expand All @@ -58,7 +57,6 @@ pub fn ibc_receive_internal_call(
msg: IbcPacketReceiveMsg,
) -> Result<Response, ContractError> {
let router = msg.packet.src.port_id.replace("wasm.", "");

let state = STATE.load(deps.storage)?;
ensure!(
state.router_contract == router,
Expand Down
25 changes: 25 additions & 0 deletions contracts/liquidity/factory/src/interface.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::contract::{execute, instantiate, query};
use cw_orch::{interface, prelude::*};
use euclid::msgs::factory::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
pub const CONTRACT_ID: &str = "factory_contract";

#[interface(InstantiateMsg, ExecuteMsg, QueryMsg, MigrateMsg, id = CONTRACT_ID)]
pub struct FactoryContract<Chain: CwEnv>;

// Implement the Uploadable trait so it can be uploaded to the mock.
impl<Chain> Uploadable for FactoryContract<Chain> {
fn wrapper() -> Box<dyn MockContract<Empty>> {
Box::new(
ContractWrapper::new_with_empty(execute, instantiate, query)
.with_reply(crate::contract::reply)
.with_ibc(
crate::ibc::channel::ibc_channel_open,
crate::ibc::channel::ibc_channel_connect,
crate::ibc::channel::ibc_channel_close,
crate::ibc::receive::ibc_packet_receive,
crate::ibc::ack_and_timeout::ibc_packet_ack,
crate::ibc::ack_and_timeout::ibc_packet_timeout,
),
)
}
}
5 changes: 5 additions & 0 deletions contracts/liquidity/factory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ pub mod tests;

#[cfg(not(target_arch = "wasm32"))]
pub mod mock;

#[cfg(not(target_arch = "wasm32"))]
mod interface;
#[cfg(not(target_arch = "wasm32"))]
pub use crate::interface::FactoryContract;
1 change: 1 addition & 0 deletions packages/euclid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ cw20 = { workspace = true }
serde = { workspace = true }
schemars = { workspace = true }
cw20-base = { workspace = true, features = ["library"] }
cw-orch = "=0.23.0"

syn = "1.0"
quote = "1.0"
Expand Down
4 changes: 2 additions & 2 deletions packages/euclid/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ pub enum ContractError {
#[error("Pool already created for this chain")]
PoolAlreadyExists {},

#[error("Pool doesn't for this chain")]
PoolDoesNotExists {},
#[error("Pool doesn't exist for this chain")]
PoolDoesNotExist {},

#[error("only unordered channels are supported")]
OrderedChannel {},
Expand Down
3 changes: 2 additions & 1 deletion packages/euclid/src/msgs/escrow/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct InstantiateMsg {
}

#[cw_serde]
#[derive(cw_orch::ExecuteFns)]
pub enum ExecuteMsg {
// Updates allowed denoms
AddAllowedDenom { denom: TokenType },
Expand All @@ -26,7 +27,7 @@ pub enum ExecuteMsg {
}

#[cw_serde]
#[derive(QueryResponses)]
#[derive(cw_orch::QueryFns, QueryResponses)]
pub enum QueryMsg {
#[returns(StateResponse)]
State {},
Expand Down
1 change: 1 addition & 0 deletions packages/euclid/src/msgs/factory/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct InstantiateMsg {
}

#[cw_serde]
#[derive(cw_orch::ExecuteFns)]
pub enum ExecuteMsg {
AddLiquidityRequest {
pair_info: PairWithDenomAndAmount,
Expand Down
2 changes: 1 addition & 1 deletion packages/euclid/src/msgs/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub enum ExecuteMsg {
}

#[cw_serde]
#[derive(QueryResponses)]
#[derive(cw_orch::QueryFns, QueryResponses)]
pub enum QueryMsg {
#[returns(StateResponse)]
GetState {},
Expand Down
8 changes: 4 additions & 4 deletions packages/euclid/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ forward_ref_partial_eq!(Pair, Pair);

impl Pair {
pub fn new(token_1: Token, token_2: Token) -> Result<Self, ContractError> {
let pair = if token_1.le(&token_2) {
let pair = if token_1.le(&token_2.to_string()) {
Self { token_1, token_2 }
} else {
Self {
Expand All @@ -155,7 +155,7 @@ impl Pair {
self.token_2.validate()?;

ensure!(
self.token_1.le(&self.token_2),
self.token_1.le(&self.token_2.to_string()),
ContractError::new("Token order is wrong")
);
Ok(())
Expand All @@ -169,7 +169,7 @@ impl Pair {
}

pub fn get_tupple(&self) -> (Token, Token) {
if self.token_1.le(&self.token_2) {
if self.token_1.le(&self.token_2.to_string()) {
(self.token_1.clone(), self.token_2.clone())
} else {
(self.token_2.clone(), self.token_1.clone())
Expand Down Expand Up @@ -437,7 +437,7 @@ pub struct PairWithAmount {

impl PairWithAmount {
pub fn new(token_1: TokenWithAmount, token_2: TokenWithAmount) -> Result<Self, ContractError> {
let pair_with_amount = if token_1.token.le(&token_2.token) {
let pair_with_amount = if token_1.token.le(&token_2.token.to_string()) {
Self { token_1, token_2 }
} else {
Self {
Expand Down
3 changes: 2 additions & 1 deletion tests-integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ cw-storage-plus = { workspace = true }
itertools = { workspace = true }
cosmwasm-schema = { workspace = true }
cw-utils = { workspace = true }

cw-orch = "=0.23.0"
cw-orch-interchain = "=0.1.0"

anyhow = "1.0.79"

Expand Down
Loading
Loading