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

⚡️ [API-2293] Swap Directly On Astroport Pools, Deprecate Router Use #84

Merged
merged 6 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
125 changes: 61 additions & 64 deletions contracts/adapters/swap/astroport/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
use crate::{
error::{ContractError, ContractResult},
state::{ENTRY_POINT_CONTRACT_ADDRESS, ROUTER_CONTRACT_ADDRESS},
state::ENTRY_POINT_CONTRACT_ADDRESS,
};
use astroport::{
pair::{
QueryMsg as PairQueryMsg, ReverseSimulationResponse, SimulationResponse,
MAX_ALLOWED_SLIPPAGE,
},
router::ExecuteMsg as RouterExecuteMsg,
use astroport::pair::{
ExecuteMsg as PairExecuteMsg, QueryMsg as PairQueryMsg, ReverseSimulationResponse,
SimulationResponse, MAX_ALLOWED_SLIPPAGE,
};
use cosmwasm_std::{
entry_point, from_json, to_json_binary, Addr, Api, Binary, Decimal, Deps, DepsMut, Env,
MessageInfo, Response, Uint128, WasmMsg,
entry_point, from_json, to_json_binary, Binary, Decimal, Deps, DepsMut, Env, MessageInfo,
Response, Uint128, WasmMsg,
};
use cw2::set_contract_version;
use cw20::{Cw20Coin, Cw20ReceiveMsg};
use cw_utils::one_coin;
use skip::{
asset::Asset,
asset::{get_current_asset_available, Asset},
swap::{
execute_transfer_funds_back, AstroportInstantiateMsg as InstantiateMsg, Cw20HookMsg,
ExecuteMsg, MigrateMsg, QueryMsg, SimulateSwapExactAssetInResponse,
Expand Down Expand Up @@ -59,21 +56,11 @@ pub fn instantiate(
// Store the entry point contract address
ENTRY_POINT_CONTRACT_ADDRESS.save(deps.storage, &checked_entry_point_contract_address)?;

// Validate router contract address
let checked_router_contract_address = deps.api.addr_validate(&msg.router_contract_address)?;

// Store the router contract address
ROUTER_CONTRACT_ADDRESS.save(deps.storage, &checked_router_contract_address)?;

Ok(Response::new()
.add_attribute("action", "instantiate")
.add_attribute(
"entry_point_contract_address",
checked_entry_point_contract_address.to_string(),
)
.add_attribute(
"router_contract_address",
checked_router_contract_address.to_string(),
))
}

Expand All @@ -100,7 +87,7 @@ pub fn receive_cw20(
info.sender = deps.api.addr_validate(&cw20_msg.sender)?;

match from_json(&cw20_msg.msg)? {
Cw20HookMsg::Swap { operations } => execute_swap(deps, env, info, sent_asset, operations),
Cw20HookMsg::Swap { operations } => execute_swap(deps, env, info, operations),
}
}

Expand All @@ -118,8 +105,8 @@ pub fn execute(
match msg {
ExecuteMsg::Receive(cw20_msg) => receive_cw20(deps, env, info, cw20_msg),
ExecuteMsg::Swap { operations } => {
let sent_asset: Asset = one_coin(&info)?.into();
execute_swap(deps, env, info, sent_asset, operations)
one_coin(&info)?;
execute_swap(deps, env, info, operations)
}
ExecuteMsg::TransferFundsBack {
swapper,
Expand All @@ -131,14 +118,16 @@ pub fn execute(
swapper,
return_denom,
)?),
ExecuteMsg::AstroportPoolSwap { operation } => {
execute_astroport_pool_swap(deps, env, info, operation)
}
}
}

fn execute_swap(
deps: DepsMut,
env: Env,
info: MessageInfo,
sent_asset: Asset,
operations: Vec<SwapOperation>,
) -> ContractResult<Response> {
// Get entry point contract address from storage
Expand All @@ -149,13 +138,20 @@ fn execute_swap(
return Err(ContractError::Unauthorized);
}

// Create the astroport swap message
let swap_msg = create_astroport_swap_msg(
deps.api,
ROUTER_CONTRACT_ADDRESS.load(deps.storage)?,
sent_asset,
&operations,
)?;
// Create a response object to return
let mut response: Response = Response::new().add_attribute("action", "execute_swap");

// Add an astroport pool swap message to the response for each swap operation
for operation in &operations {
let swap_msg = WasmMsg::Execute {
contract_addr: env.contract.address.to_string(),
msg: to_json_binary(&ExecuteMsg::AstroportPoolSwap {
operation: operation.clone(),
})?,
funds: vec![],
};
response = response.add_message(swap_msg);
}

let return_denom = match operations.last() {
Some(last_op) => last_op.denom_out.clone(),
Expand All @@ -172,44 +168,48 @@ fn execute_swap(
funds: vec![],
};

Ok(Response::new()
.add_message(swap_msg)
Ok(response
.add_message(transfer_funds_back_msg)
.add_attribute("action", "dispatch_swap_and_transfer_back"))
.add_attribute("action", "dispatch_swaps_and_transfer_back"))
}

////////////////////////
/// HELPER FUNCTIONS ///
////////////////////////
fn execute_astroport_pool_swap(
deps: DepsMut,
env: Env,
info: MessageInfo,
operation: SwapOperation,
) -> ContractResult<Response> {
// Ensure the caller is the contract itself
if info.sender != env.contract.address {
return Err(ContractError::Unauthorized);
}

// Get the current asset available on contract to swap in
let offer_asset = get_current_asset_available(&deps, &env, &operation.denom_in)?;

// Converts the swap operations to astroport AstroSwap operations
fn create_astroport_swap_msg(
api: &dyn Api,
router_contract_address: Addr,
asset_in: Asset,
swap_operations: &[SwapOperation],
) -> ContractResult<WasmMsg> {
// Convert the swap operations to astroport swap operations
let astroport_swap_operations = swap_operations
.iter()
.map(|swap_operation| swap_operation.into_astroport_swap_operation(api))
.collect();
// Error if the offer asset amount is zero
if offer_asset.amount().is_zero() {
return Err(ContractError::NoOfferAssetAmount);
}

// Create the astroport router execute message arguments
let astroport_router_msg_args = RouterExecuteMsg::ExecuteSwapOperations {
operations: astroport_swap_operations,
minimum_receive: None,
to: None,
// Create the astroport pool swap message args
let astroport_pool_swap_msg_args = PairExecuteMsg::Swap {
offer_asset: offer_asset.into_astroport_asset(deps.api)?,
ask_asset_info: None,
belief_price: None,
max_spread: Some(MAX_ALLOWED_SLIPPAGE.parse::<Decimal>()?),
to: None,
};

// Create the astroport router swap message
let swap_msg = asset_in.into_wasm_msg(
router_contract_address.to_string(),
to_json_binary(&astroport_router_msg_args)?,
// Create the wasm astroport pool swap message
let swap_msg = offer_asset.into_wasm_msg(
operation.pool,
to_json_binary(&astroport_pool_swap_msg_args)?,
)?;

Ok(swap_msg)
Ok(Response::new()
.add_message(swap_msg)
.add_attribute("action", "dispatch_astroport_pool_swap"))
}

/////////////
Expand All @@ -219,9 +219,6 @@ fn create_astroport_swap_msg(
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> ContractResult<Binary> {
match msg {
QueryMsg::RouterContractAddress {} => {
to_json_binary(&ROUTER_CONTRACT_ADDRESS.load(deps.storage)?)
}
QueryMsg::SimulateSwapExactAssetIn {
asset_in,
swap_operations,
Expand Down Expand Up @@ -262,7 +259,7 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> ContractResult<Binary> {
.map_err(From::from)
}

// Queries the astroport router contract to simulate a swap exact amount in
// Queries the astroport pool contracts to simulate a swap exact amount in
fn query_simulate_swap_exact_asset_in(
deps: Deps,
asset_in: Asset,
Expand Down Expand Up @@ -306,7 +303,7 @@ fn query_simulate_swap_exact_asset_out(
Ok(asset_in)
}

// Queries the astroport router contract to simulate a swap exact amount in with metadata
// Queries the astroport pool contracts to simulate a swap exact amount in with metadata
fn query_simulate_swap_exact_asset_in_with_metadata(
deps: Deps,
asset_in: Asset,
Expand Down
3 changes: 3 additions & 0 deletions contracts/adapters/swap/astroport/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@ pub enum ContractError {

#[error("Operation exceeds max spread limit")]
MaxSpreadAssertion,

#[error("Contract has no balance of offer asset")]
NoOfferAssetAmount,
}
1 change: 0 additions & 1 deletion contracts/adapters/swap/astroport/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ use cosmwasm_std::Addr;
use cw_storage_plus::Item;

pub const ENTRY_POINT_CONTRACT_ADDRESS: Item<Addr> = Item::new("entry_point_contract_address");
pub const ROUTER_CONTRACT_ADDRESS: Item<Addr> = Item::new("router_contract_address");
Loading