Skip to content

Commit

Permalink
feat: Add ExecuteMsg::ZapBaseTokens
Browse files Browse the repository at this point in the history
  • Loading branch information
apollo-sturdy committed Sep 27, 2023
1 parent 2e4f580 commit b644d6d
Show file tree
Hide file tree
Showing 5 changed files with 450 additions and 3 deletions.
19 changes: 19 additions & 0 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::query::{
use crate::state::{LIQUIDITY_HELPER, LOCKUP_IDS, ROUTER, TEMP_LOCK_KEY};
use crate::withdraw::{
callback_after_redeem, callback_after_withdraw_liq, execute_redeem, execute_withdraw_unlocked,
execute_zap_base_tokens,
};

// version info for migration info
Expand Down Expand Up @@ -86,6 +87,24 @@ pub fn execute(
min_out,
)
}
ExecuteMsg::ZapBaseTokens {
base_token,
recipient,
receive_choice,
min_out,
} => {
let base_token = base_token.check(deps.api)?;
let min_out = min_out.check(deps.api)?;
execute_zap_base_tokens(
deps,
env,
info,
base_token,
recipient,
receive_choice,
min_out,
)
}
ExecuteMsg::Unlock { vault_address } => {
execute_unlock(deps, env, info, api.addr_validate(&vault_address)?)
}
Expand Down
14 changes: 13 additions & 1 deletion src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use apollo_cw_asset::{AssetInfo, AssetList, AssetListUnchecked};
use apollo_cw_asset::{AssetInfo, AssetList, AssetListUnchecked, AssetUnchecked};
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{to_binary, Addr, CosmosMsg, Env, StdResult, Uint128, WasmMsg};
use cw_dex::Pool;
Expand Down Expand Up @@ -38,6 +38,18 @@ pub enum ExecuteMsg {
/// received is less than this, the transaction will fail.
min_out: AssetListUnchecked,
},
/// Zap a vault's base token to other assets
ZapBaseTokens {
/// The base token to swap from
base_token: AssetUnchecked,
/// The recipient of the redeemed assets
recipient: Option<String>,
/// The asset to swap to
receive_choice: ReceiveChoice,
/// The minimum amount of assets to receive. If the amount of assets
/// received is less than this, the transaction will fail.
min_out: AssetListUnchecked,
},
/// Call unlock on the specified vault and burn the sent vault tokens to
/// create an unlocking position. The unlocking position can be withdrawn
/// from after the unlock period has passed by calling WithdrawUnlocked.
Expand Down
34 changes: 34 additions & 0 deletions src/withdraw.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use apollo_cw_asset::{Asset, AssetInfo, AssetList};
use apollo_utils::assets::receive_assets;
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{
to_binary, Addr, CosmosMsg, DepsMut, Empty, Env, Event, MessageInfo, Response, Uint128, WasmMsg,
Expand Down Expand Up @@ -140,6 +141,39 @@ pub fn withdraw(
.add_event(event))
}

pub fn execute_zap_base_tokens(
deps: DepsMut,
env: Env,
info: MessageInfo,
base_token: Asset,
recipient: Option<String>,
receive_choice: ReceiveChoice,
min_out: AssetList,
) -> Result<Response, ContractError> {
// Unwrap recipient or use sender
let recipient = recipient.map_or(Ok(info.sender.clone()), |x| deps.api.addr_validate(&x))?;

let receive_assets_res = receive_assets(&info, &env, &vec![base_token.clone()].into())?;

let event = Event::new("apollo/vault-zapper/execute_zap_base_tokens")
.add_attribute("base_token", to_binary(&base_token.info)?.to_string())
.add_attribute("recipient", &recipient)
.add_attribute("receive_choice", to_binary(&receive_choice)?.to_string())
.add_attribute("min_out", to_binary(&min_out)?.to_string());

Ok(receive_assets_res
.add_message(
CallbackMsg::AfterRedeem {
receive_choice,
vault_base_token: base_token.info,
recipient,
min_out,
}
.into_cosmos_msg(&env)?,
)
.add_event(event))
}

pub fn callback_after_redeem(
deps: DepsMut,
env: Env,
Expand Down
72 changes: 70 additions & 2 deletions tests/common/robot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use std::collections::HashMap;
use std::str::FromStr;

use super::DENOM_CREATION_FEE;
use apollo_cw_asset::{AssetInfo, AssetList, AssetListUnchecked};
use apollo_cw_asset::{Asset, AssetInfo, AssetList, AssetListUnchecked};
use apollo_utils::assets::separate_natives_and_cw20s;
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{assert_approx_eq, coin, Addr, Coin, Coins, Decimal, Uint128};
use cosmwasm_std::testing::mock_dependencies;
use cosmwasm_std::{assert_approx_eq, coin, Addr, Api, Coin, Coins, Decimal, Uint128};
use cw_dex::Pool;
use cw_dex_router::helpers::CwDexRouterUnchecked;
use cw_it::astroport::robot::AstroportTestRobot;
Expand Down Expand Up @@ -312,6 +313,73 @@ impl<'a> VaultZapperRobot<'a> {
)
}

/// Zap base tokens via the vault zapper
pub fn zap_base_tokens(
&self,
amount: impl Into<u128>,
recipient: Option<String>,
receive_choice: ReceiveChoice,
min_out: impl Into<AssetListUnchecked>,
unwrap_choice: Unwrap,
signer: &SigningAccount,
) -> &Self {
let min_out = min_out.into();

let base_token = self.deps.vault_robot.base_token();
let deps = mock_dependencies();
let base_token_asset_info = match deps.api.addr_validate(&base_token) {
Ok(addr) => AssetInfo::cw20(addr),
Err(_) => AssetInfo::native(&base_token),
};
let base_token = Asset::new(base_token_asset_info, amount.into());

// Increase allowance for Cw20s
let (funds, cw20s) = separate_natives_and_cw20s(&vec![base_token.clone()].into());
for cw20 in cw20s {
self.increase_cw20_allowance(
&cw20.address,
&self.vault_zapper_addr,
cw20.amount,
signer,
);
}

println!("Zapping base tokens: {:?}", base_token);

unwrap_choice.unwrap(self.wasm().execute(
&self.vault_zapper_addr,
&ExecuteMsg::ZapBaseTokens {
base_token: base_token.into(),
recipient,
receive_choice,
min_out,
},
&funds,
signer,
));
self
}

/// Zap all of the signer's base tokens via the vault zapper
pub fn zap_all_base_tokens(
&self,
recipient: Option<String>,
receive_choice: ReceiveChoice,
min_out: impl Into<AssetListUnchecked>,
unwrap_choice: Unwrap,
signer: &SigningAccount,
) -> &Self {
let balance = self.query_base_token_balance(signer.address());
self.zap_base_tokens(
balance,
recipient,
receive_choice,
min_out,
unwrap_choice,
signer,
)
}

/// Call unlock on the specified vault via the vault zapper
pub fn zapper_unlock_from_vault(
&self,
Expand Down
Loading

0 comments on commit b644d6d

Please sign in to comment.