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

feat: Custom Timeout for Messages #21

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions contracts/liquidity/factory/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,18 @@ pub fn execute(
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::RequestPoolCreation { pair_info, channel } => {
execute::execute_request_pool_creation(deps, env, info, pair_info, channel)
}
ExecuteMsg::RequestPoolCreation {
pair_info,
channel,
timeout,
} => execute::execute_request_pool_creation(deps, env, info, pair_info, channel, timeout),
ExecuteMsg::ExecuteSwap {
asset,
asset_amount,
min_amount_out,
channel,
swap_id,
timeout,
} => execute::execute_swap(
deps,
env,
Expand All @@ -65,13 +68,15 @@ pub fn execute(
min_amount_out,
channel,
swap_id,
timeout,
),
ExecuteMsg::AddLiquidity {
token_1_liquidity,
token_2_liquidity,
slippage_tolerance,
channel,
liquidity_id,
timeout,
} => execute::execute_add_liquidity(
deps,
env,
Expand All @@ -81,6 +86,7 @@ pub fn execute(
slippage_tolerance,
channel,
liquidity_id,
timeout,
),
}
}
Expand Down
16 changes: 13 additions & 3 deletions contracts/liquidity/factory/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use cosmwasm_std::{
};
use euclid::{
error::ContractError,
timeout::get_timeout,
token::{PairInfo, Token},
};
use euclid_ibc::msg::IbcExecuteMsg;
Expand All @@ -16,6 +17,7 @@ pub fn execute_request_pool_creation(
info: MessageInfo,
pair_info: PairInfo,
channel: String,
timeout: Option<u64>,
) -> Result<Response, ContractError> {
// Load the state
let state = STATE.load(deps.storage)?;
Expand All @@ -25,6 +27,8 @@ pub fn execute_request_pool_creation(
// Create a Request in state
let pool_request = generate_pool_req(deps, &info.sender, env.block.chain_id, channel.clone())?;

let timeout = get_timeout(timeout)?;

// Create IBC packet to send to Router
let ibc_packet = IbcMsg::SendPacket {
channel_id: channel.clone(),
Expand All @@ -34,7 +38,7 @@ pub fn execute_request_pool_creation(
pair_info,
})?,

timeout: IbcTimeout::with_timestamp(env.block.time.plus_seconds(60)),
timeout: IbcTimeout::with_timestamp(env.block.time.plus_seconds(timeout)),
};

msgs.push(ibc_packet.into());
Expand All @@ -54,12 +58,15 @@ pub fn execute_swap(
min_amount_out: Uint128,
channel: String,
swap_id: String,
timeout: Option<u64>,
) -> Result<Response, ContractError> {
// Load the state
let state = STATE.load(deps.storage)?;

let pool_address = info.sender;

let timeout = get_timeout(timeout)?;

// Create IBC packet to send to Router
let ibc_packet = IbcMsg::SendPacket {
channel_id: channel.clone(),
Expand All @@ -72,7 +79,7 @@ pub fn execute_swap(
swap_id,
pool_address,
})?,
timeout: IbcTimeout::with_timestamp(env.block.time.plus_seconds(60)),
timeout: IbcTimeout::with_timestamp(env.block.time.plus_seconds(timeout)),
};

let msg = CosmosMsg::Ibc(ibc_packet);
Expand All @@ -92,12 +99,15 @@ pub fn execute_add_liquidity(
slippage_tolerance: u64,
channel: String,
liquidity_id: String,
timeout: Option<u64>,
) -> Result<Response, ContractError> {
// Load the state
let state = STATE.load(deps.storage)?;

let pool_address = info.sender.clone();

let timeout = get_timeout(timeout)?;

// Create IBC packet to send to Router
let ibc_packet = IbcMsg::SendPacket {
channel_id: channel.clone(),
Expand All @@ -109,7 +119,7 @@ pub fn execute_add_liquidity(
liquidity_id,
pool_address: pool_address.clone().to_string(),
})?,
timeout: IbcTimeout::with_timestamp(env.block.time.plus_seconds(60)),
timeout: IbcTimeout::with_timestamp(env.block.time.plus_seconds(timeout)),
};

let msg = CosmosMsg::Ibc(ibc_packet);
Expand Down
2 changes: 1 addition & 1 deletion contracts/liquidity/factory/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use cosmwasm_std::{
};
use euclid::msgs::pool::ExecuteMsg as PoolExecuteMsg;
use euclid::{
error::{ContractError, Never},
error::ContractError,
msgs::pool::CallbackExecuteMsg,
pool::{LiquidityResponse, Pool, PoolCreationResponse},
swap::SwapResponse,
Expand Down
4 changes: 4 additions & 0 deletions contracts/liquidity/pool/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub fn execute(
asset_amount,
min_amount_out,
channel,
timeout,
} => execute::execute_swap_request(
deps,
info,
Expand All @@ -66,12 +67,14 @@ pub fn execute(
min_amount_out,
channel,
None,
timeout,
),
ExecuteMsg::AddLiquidity {
token_1_liquidity,
token_2_liquidity,
slippage_tolerance,
channel,
timeout,
} => execute::add_liquidity_request(
deps,
info,
Expand All @@ -81,6 +84,7 @@ pub fn execute(
slippage_tolerance,
channel,
None,
timeout,
),
ExecuteMsg::Receive(msg) => execute::receive_cw20(deps, env, info, msg),
ExecuteMsg::Callback(callback_msg) => {
Expand Down
15 changes: 11 additions & 4 deletions contracts/liquidity/pool/src/execute.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_std::{
ensure, from_json, to_json_binary, CosmosMsg, DepsMut, Env, IbcMsg, IbcTimeout, MessageInfo,
Response, Uint128, WasmMsg,
ensure, from_json, to_json_binary, CosmosMsg, DepsMut, Env, IbcTimeout, MessageInfo, Response,
Uint128, WasmMsg,
};
use cw20::Cw20ReceiveMsg;
use euclid::{
Expand All @@ -9,9 +9,9 @@ use euclid::{
msgs::{factory, pool::Cw20HookMsg},
pool::LiquidityResponse,
swap::{self, SwapResponse},
timeout::get_timeout,
token::TokenInfo,
};
use euclid_ibc::msg::IbcExecuteMsg;

use crate::state::{
generate_liquidity_req, generate_swap_req, PENDING_LIQUIDITY, PENDING_SWAPS, STATE,
Expand All @@ -28,6 +28,7 @@ pub fn execute_swap_request(
min_amount_out: Uint128,
channel: String,
msg_sender: Option<String>,
timeout: Option<u64>,
) -> Result<Response, ContractError> {
let state = STATE.load(deps.storage)?;

Expand Down Expand Up @@ -82,7 +83,8 @@ pub fn execute_swap_request(
// Get alternative token
let asset_out: TokenInfo = state.pair_info.get_other_token(asset.clone());

let timeout = IbcTimeout::with_timestamp(env.block.time.plus_seconds(60));
let timeout_duration = get_timeout(timeout)?;
let timeout = IbcTimeout::with_timestamp(env.block.time.plus_seconds(timeout_duration));
let swap_info = generate_swap_req(deps, sender, asset, asset_out, asset_amount, timeout)?;

let msg = FactoryExecuteMsg::ExecuteSwap {
Expand All @@ -91,6 +93,7 @@ pub fn execute_swap_request(
min_amount_out,
channel,
swap_id: swap_info.swap_id,
timeout: Some(timeout_duration),
};

let msg = WasmMsg::Execute {
Expand Down Expand Up @@ -204,6 +207,7 @@ pub fn receive_cw20(
asset,
min_amount_out,
channel,
timeout,
} => {
let contract_adr = info.sender.clone();

Expand All @@ -224,6 +228,7 @@ pub fn receive_cw20(
min_amount_out,
channel,
Some(cw20_msg.sender),
timeout,
)
}
}
Expand All @@ -239,6 +244,7 @@ pub fn add_liquidity_request(
slippage_tolerance: u64,
channel: String,
msg_sender: Option<String>,
timeout: Option<u64>,
) -> Result<Response, ContractError> {
let state = STATE.load(deps.storage)?;

Expand Down Expand Up @@ -319,6 +325,7 @@ pub fn add_liquidity_request(
slippage_tolerance,
channel,
liquidity_id: liquidity_info.liquidity_id,
timeout,
};

let msg = WasmMsg::Execute {
Expand Down
5 changes: 4 additions & 1 deletion packages/euclid/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use thiserror::Error;
use crate::{liquidity::LiquidityTxInfo, pool::PoolRequest, swap::SwapInfo};
#[derive(Error, Debug)]
pub enum Never {}
#[derive(Error, Debug)]
#[derive(Error, Debug, PartialEq)]
pub enum ContractError {
#[error("{0}")]
Std(#[from] StdError),
Expand Down Expand Up @@ -57,6 +57,9 @@ pub enum ContractError {
#[error("Invalid Liquidity Ratio")]
InvalidLiquidityRatio {},

#[error("Invalid Timeout")]
InvalidTimeout {},

#[error("Slippage Tolerance must be between 0 and 100")]
InvalidSlippageTolerance {},

Expand Down
1 change: 1 addition & 0 deletions packages/euclid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ pub mod liquidity;
pub mod msgs;
pub mod pool;
pub mod swap;
pub mod timeout;
pub mod token;
3 changes: 3 additions & 0 deletions packages/euclid/src/msgs/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ pub enum ExecuteMsg {
RequestPoolCreation {
pair_info: PairInfo,
channel: String,
timeout: Option<u64>,
},
ExecuteSwap {
asset: Token,
asset_amount: Uint128,
min_amount_out: Uint128,
channel: String,
swap_id: String,
timeout: Option<u64>,
},
// Add Liquidity Request to the VLP
AddLiquidity {
Expand All @@ -32,6 +34,7 @@ pub enum ExecuteMsg {
slippage_tolerance: u64,
channel: String,
liquidity_id: String,
timeout: Option<u64>,
},
}

Expand Down
3 changes: 3 additions & 0 deletions packages/euclid/src/msgs/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub enum ExecuteMsg {
asset_amount: Uint128,
min_amount_out: Uint128,
channel: String,
timeout: Option<u64>,
},

// Add Liquidity Request to the VLP
Expand All @@ -32,6 +33,7 @@ pub enum ExecuteMsg {
token_2_liquidity: Uint128,
slippage_tolerance: u64,
channel: String,
timeout: Option<u64>,
},

// Recieve CW20 TOKENS structure
Expand Down Expand Up @@ -94,6 +96,7 @@ pub enum Cw20HookMsg {
asset: TokenInfo,
min_amount_out: Uint128,
channel: String,
timeout: Option<u64>,
},
}

Expand Down
Loading
Loading