Skip to content

Commit

Permalink
WIP:Router ibc
Browse files Browse the repository at this point in the history
  • Loading branch information
SlayerAnsh committed May 31, 2024
1 parent 5c3aa36 commit 7b38330
Show file tree
Hide file tree
Showing 18 changed files with 277 additions and 557 deletions.
4 changes: 3 additions & 1 deletion Cargo.lock

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

68 changes: 12 additions & 56 deletions contracts/hub/router/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ use cw2::set_contract_version;
use euclid::error::ContractError;
// use cw2::set_contract_version;

use crate::query::{query_all_pools, query_state};
use crate::reply::INSTANTIATE_REPLY_ID;
use crate::reply::{self, VLP_INSTANTIATE_REPLY_ID, VLP_POOL_REGISTER_REPLY_ID};
use crate::state::{State, STATE};
use crate::{execute, query, reply};
use euclid::msgs::factory::{ExecuteMsg, InstantiateMsg, QueryMsg};
use crate::{execute, query};
use euclid::msgs::router::{ExecuteMsg, InstantiateMsg, QueryMsg};

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:factory";
Expand All @@ -23,89 +22,46 @@ pub fn instantiate(
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
let state = State {
router_contract: msg.router_contract.clone(),
chain_id: env.block.chain_id,
admin: info.sender.clone().to_string(),
pool_code_id: msg.pool_code_id.clone(),
vlp_code_id: msg.vlp_code_id,
admin: info.sender.to_string(),
};

set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

STATE.save(deps.storage, &state)?;

Ok(Response::new()
.add_attribute("method", "instantiate")
.add_attribute("router_contract", msg.router_contract)
.add_attribute("chain_id", state.chain_id))
.add_attribute("router_contract", env.contract.address))
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut,
env: Env,
_env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::RequestPoolCreation { pair_info, channel } => {
execute::execute_request_pool_creation(deps, env, info, pair_info, channel)
}
ExecuteMsg::ExecuteSwap {
asset,
asset_amount,
min_amount_out,
channel,
swap_id,
} => execute::execute_swap(
deps,
env,
info,
asset,
asset_amount,
min_amount_out,
channel,
swap_id,
),
ExecuteMsg::AddLiquidity {
token_1_liquidity,
token_2_liquidity,
slippage_tolerance,
channel,
liquidity_id,
} => execute::execute_add_liquidity(
deps,
env,
info,
token_1_liquidity,
token_2_liquidity,
slippage_tolerance,
channel,
liquidity_id,
),
ExecuteMsg::UpdatePoolCodeId { new_pool_code_id } => {
execute::execute_update_pool_code_id(deps, info, new_pool_code_id)
ExecuteMsg::UpdateVLPCodeId { new_vlp_code_id } => {
execute::execute_update_vlp_code_id(deps, info, new_vlp_code_id)
}
}
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> Result<Binary, ContractError> {
match msg {
QueryMsg::GetPool { vlp } => query::get_pool(deps, vlp),
QueryMsg::GetState {} => query_state(deps),
QueryMsg::GetAllPools {} => query_all_pools(deps),
QueryMsg::GetState {} => query::query_state(deps),
}
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn reply(deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, ContractError> {
match msg.id {
INSTANTIATE_REPLY_ID => reply::on_pool_instantiate_reply(deps, msg),
VLP_INSTANTIATE_REPLY_ID => reply::on_vlp_instantiate_reply(deps, msg),
VLP_POOL_REGISTER_REPLY_ID => reply::on_pool_register_reply(deps, msg),
id => Err(ContractError::Std(StdError::generic_err(format!(
"Unknown reply id: {}",
id
)))),
}
}

#[cfg(test)]
mod tests {}
165 changes: 55 additions & 110 deletions contracts/hub/router/src/execute.rs
Original file line number Diff line number Diff line change
@@ -1,144 +1,89 @@
use cosmwasm_std::{
to_json_binary, CosmosMsg, DepsMut, Env, IbcMsg, IbcTimeout, MessageInfo, Response, Uint128,
};
use euclid::{
error::ContractError,
token::{PairInfo, Token},
};
use euclid_ibc::msg::IbcExecuteMsg;
use cosmwasm_std::{ensure, to_json_binary, DepsMut, Env, MessageInfo, Response, SubMsg, WasmMsg};
use euclid::{error::ContractError, fee::Fee, msgs, token::PairInfo};

use crate::state::{generate_pool_req, STATE};
use crate::{
reply::{VLP_INSTANTIATE_REPLY_ID, VLP_POOL_REGISTER_REPLY_ID},
state::{STATE, VLPS},
};

// Function to send IBC request to Router in VLS to create a new pool
pub fn execute_request_pool_creation(
deps: DepsMut,
env: Env,
info: MessageInfo,
chain_id: String,
factory: String,
pair_info: PairInfo,
channel: String,
) -> Result<Response, ContractError> {
// Load the state
let state = STATE.load(deps.storage)?;

let mut msgs: Vec<CosmosMsg> = Vec::new();
let pair = (pair_info.token_1.get_token(), pair_info.token_2.get_token());

// Create a Request in state
let pool_request = generate_pool_req(deps, &info.sender, env.block.chain_id, channel.clone())?;
let vlp = VLPS.may_load(deps.storage, pair)?;

// Create IBC packet to send to Router
let ibc_packet = IbcMsg::SendPacket {
channel_id: channel.clone(),
data: to_json_binary(&IbcExecuteMsg::RequestPoolCreation {
pool_rq_id: pool_request.pool_rq_id,
chain: state.chain_id,
factory: env.contract.address.to_string(),
pair_info,
})?,
if vlp.is_none() {
let pair = (pair_info.token_2.get_token(), pair_info.token_1.get_token());
ensure!(
VLPS.load(deps.storage, pair).is_err(),
ContractError::Generic {
err: "pair order is reversed".to_string()
}
);
}

timeout: IbcTimeout::with_timestamp(env.block.time.plus_seconds(60)),
let register_msg = msgs::vlp::ExecuteMsg::RegisterPool {
chain_id,
factory,
pair_info: pair_info.clone(),
};

msgs.push(ibc_packet.into());

Ok(Response::new()
.add_attribute("method", "request_pool_creation")
.add_messages(msgs))
}

// Function to send IBC request to Router in VLS to perform a swap
pub fn execute_swap(
deps: DepsMut,
env: Env,
info: MessageInfo,
asset: Token,
asset_amount: Uint128,
min_amount_out: Uint128,
channel: String,
swap_id: String,
) -> Result<Response, ContractError> {
// Load the state
let state = STATE.load(deps.storage)?;

let pool_address = info.sender;

// Create IBC packet to send to Router
let ibc_packet = IbcMsg::SendPacket {
channel_id: channel.clone(),
data: to_json_binary(&IbcExecuteMsg::Swap {
chain_id: state.chain_id,
asset,
asset_amount,
min_amount_out,
channel,
swap_id,
pool_address,
})?,
timeout: IbcTimeout::with_timestamp(env.block.time.plus_seconds(60)),
};

let msg = CosmosMsg::Ibc(ibc_packet);

Ok(Response::new()
.add_attribute("method", "request_pool_creation")
.add_message(msg))
}

// Function to send IBC request to Router in VLS to add liquidity to a pool
pub fn execute_add_liquidity(
deps: DepsMut,
env: Env,
info: MessageInfo,
token_1_liquidity: Uint128,
token_2_liquidity: Uint128,
slippage_tolerance: u64,
channel: String,
liquidity_id: String,
) -> Result<Response, ContractError> {
// Load the state
let state = STATE.load(deps.storage)?;

let pool_address = info.sender.clone();

// Create IBC packet to send to Router
let ibc_packet = IbcMsg::SendPacket {
channel_id: channel.clone(),
data: to_json_binary(&IbcExecuteMsg::AddLiquidity {
chain_id: state.chain_id,
token_1_liquidity,
token_2_liquidity,
slippage_tolerance,
liquidity_id,
pool_address: pool_address.clone().to_string(),
})?,
timeout: IbcTimeout::with_timestamp(env.block.time.plus_seconds(60)),
};

let msg = CosmosMsg::Ibc(ibc_packet);

Ok(Response::new()
.add_attribute("method", "add_liquidity_request")
.add_message(msg))
if vlp.is_some() {
let msg = WasmMsg::Execute {
contract_addr: vlp.unwrap(),
msg: to_json_binary(&register_msg)?,
funds: vec![],
};
Ok(Response::new().add_submessage(SubMsg::reply_always(msg, VLP_POOL_REGISTER_REPLY_ID)))
} else {
let instantiate_msg = msgs::vlp::InstantiateMsg {
router: env.contract.address.to_string(),
pair: pair_info,
fee: Fee {
lp_fee: 0,
treasury_fee: 0,
staker_fee: 0,
},
execute: Some(register_msg),
};
let msg = WasmMsg::Instantiate {
admin: Some(env.contract.address.to_string()),
code_id: state.vlp_code_id,
msg: to_json_binary(&instantiate_msg)?,
funds: vec![],
label: "VLP".to_string(),
};
Ok(Response::new().add_submessage(SubMsg::reply_always(msg, VLP_INSTANTIATE_REPLY_ID)))
}
}

// Function to update the pool code ID
pub fn execute_update_pool_code_id(
pub fn execute_update_vlp_code_id(
deps: DepsMut,
info: MessageInfo,
new_pool_code_id: u64,
new_vlp_code_id: u64,
) -> Result<Response, ContractError> {
// Load the state
STATE.update(deps.storage, |mut state| -> Result<_, ContractError> {
// Ensure that only the admin can update the pool code ID
if info.sender.to_string() != state.admin {
if info.sender != state.admin {
return Err(ContractError::Unauthorized {});
}

// Update the pool code ID
state.pool_code_id = new_pool_code_id;
state.vlp_code_id = new_vlp_code_id;
Ok(state)
})?;

Ok(Response::new()
.add_attribute("method", "update_pool_code_id")
.add_attribute("new_pool_code_id", new_pool_code_id.to_string()))
.add_attribute("new_vlp_code_id", new_vlp_code_id.to_string()))
}
26 changes: 0 additions & 26 deletions contracts/hub/router/src/helpers.rs

This file was deleted.

Loading

0 comments on commit 7b38330

Please sign in to comment.