Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
init integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberHoward committed Mar 25, 2023
1 parent 3cffa3c commit 8c5a6f7
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 458 deletions.
1 change: 1 addition & 0 deletions contracts/etf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ cw-staking = { git = "https://github.com/Abstract-OS/apis.git", features = ["jun
abstract-boot = { workspace = true, features = ["daemon"] }
abstract-testing = { workspace = true }
boot-cw-plus = { workspace = true }
semver = {workspace = true}
anyhow = {workspace = true}
speculoos = {workspace = true}
4 changes: 2 additions & 2 deletions contracts/etf/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use abstract_etf::contract::EtfApp;
use abstract_etf::msg::StateResponse;
use cosmwasm_schema::{export_schema, remove_schemas, schema_for};
use etf::contract::EtfApp;
use etf::msg::StateResponse;
use std::env::current_dir;
use std::fs::create_dir_all;

Expand Down
12 changes: 6 additions & 6 deletions contracts/etf/src/handlers/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn execute_handler(
msg: EtfExecuteMsg,
) -> EtfResult {
match msg {
EtfExecuteMsg::ProvideLiquidity { asset } => {
EtfExecuteMsg::Deposit { asset } => {
// Check asset
let asset = asset.check(deps.api, None)?;
try_provide_liquidity(deps, info, vault, asset, None)
Expand Down Expand Up @@ -92,7 +92,7 @@ pub fn try_provide_liquidity(
let account_value = vault.query_total_value()?;
let total_value = account_value.total_value.amount;
// Get total supply of LP tokens and calculate share
let total_share = query_supply(&deps.querier, state.liquidity_token_addr.clone())?;
let total_share = query_supply(&deps.querier, state.share_token_address.clone())?;

let share = if total_share == Uint128::zero() || total_value.is_zero() {
// Initial share = deposit amount
Expand All @@ -108,7 +108,7 @@ pub fn try_provide_liquidity(

// mint LP token to liq_manager
let mint_lp = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: state.liquidity_token_addr.to_string(),
contract_addr: state.share_token_address.to_string(),
msg: to_binary(&Cw20ExecuteMsg::Mint {
recipient: liq_manager.to_string(),
amount: share,
Expand Down Expand Up @@ -147,7 +147,7 @@ pub fn try_withdraw_liquidity(
let mut attrs = vec![("liquidity_tokens", amount.to_string())];

// Calculate share of pool and requested pool value
let total_share: Uint128 = query_supply(&deps.querier, state.liquidity_token_addr.clone())?;
let total_share: Uint128 = query_supply(&deps.querier, state.share_token_address.clone())?;

// Get manager fee in LP tokens
let manager_fee = fee.compute(amount);
Expand All @@ -159,7 +159,7 @@ pub fn try_withdraw_liquidity(
if !manager_fee.is_zero() {
// LP token fee
let lp_token_manager_fee = Asset {
info: AssetInfo::Cw20(state.liquidity_token_addr.clone()),
info: AssetInfo::Cw20(state.share_token_address.clone()),
amount: manager_fee,
};
// Construct manager fee msg
Expand All @@ -186,7 +186,7 @@ pub fn try_withdraw_liquidity(

// LP burn msg
let burn_msg: CosmosMsg = wasm_execute(
state.liquidity_token_addr,
state.share_token_address,
// Burn exludes fee
&Cw20ExecuteMsg::Burn {
amount: (amount - manager_fee),
Expand Down
2 changes: 1 addition & 1 deletion contracts/etf/src/handlers/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn instantiate_handler(
msg: EtfInstantiateMsg,
) -> EtfResult {
let state: State = State {
liquidity_token_addr: Addr::unchecked(""),
share_token_address: Addr::unchecked(""),
manager_addr: deps.api.addr_validate(msg.manager_addr.as_str())?,
};

Expand Down
2 changes: 1 addition & 1 deletion contracts/etf/src/handlers/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn query_handler(deps: Deps, _env: Env, _etf: &EtfApp, msg: EtfQueryMsg) ->
EtfQueryMsg::State {} => {
let fee = FEE.load(deps.storage)?;
to_binary(&StateResponse {
liquidity_token: STATE.load(deps.storage)?.liquidity_token_addr.to_string(),
share_token_address: STATE.load(deps.storage)?.share_token_address.to_string(),
fee: fee.share(),
})
}
Expand Down
8 changes: 4 additions & 4 deletions contracts/etf/src/handlers/receive.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::contract::{EtfApp, EtfResult};
use crate::error::EtfError;
use crate::handlers::execute;
use crate::msg::DepositHookMsg;
use crate::msg::Cw20HookMsg;
use crate::state::{State, STATE};
use cosmwasm_std::from_binary;
use cosmwasm_std::DepsMut;
Expand All @@ -21,17 +21,17 @@ pub fn receive_cw20(
cw20_msg: Cw20ReceiveMsg,
) -> EtfResult {
match from_binary(&cw20_msg.msg)? {
DepositHookMsg::WithdrawLiquidity {} => {
Cw20HookMsg::Claim {} => {
let state: State = STATE.load(deps.storage)?;
if msg_info.sender != state.liquidity_token_addr {
if msg_info.sender != state.share_token_address {
return Err(EtfError::NotLPToken {
token: msg_info.sender.to_string(),
});
}
let sender = deps.as_ref().api.addr_validate(&cw20_msg.sender)?;
execute::try_withdraw_liquidity(deps, env, dapp, sender, cw20_msg.amount)
}
DepositHookMsg::ProvideLiquidity {} => {
Cw20HookMsg::Deposit {} => {
// Construct deposit asset
let asset = Asset {
info: AssetInfo::Cw20(msg_info.sender.clone()),
Expand Down
6 changes: 3 additions & 3 deletions contracts/etf/src/handlers/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ pub fn instantiate_reply(deps: DepsMut, _env: Env, etf: EtfApp, reply: Reply) ->
Message::parse_from_bytes(data.as_slice()).map_err(|_| {
StdError::parse_err("MsgInstantiateContractResponse", "failed to parse data")
})?;
let liquidity_token = res.get_contract_address();
let share_token_address = res.get_contract_address();

let api = deps.api;
STATE.update(deps.storage, |mut meta| -> StdResult<_> {
meta.liquidity_token_addr = api.addr_validate(liquidity_token)?;
meta.share_token_address = api.addr_validate(share_token_address)?;
Ok(meta)
})?;

Ok(etf.custom_tag_response(
Response::default(),
"instantiate_reply",
vec![("liquidity_token_addr", liquidity_token)],
vec![("share_token_address", share_token_address)],
))
}
5 changes: 3 additions & 2 deletions contracts/etf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub mod boot {
use boot_core::ContractWrapper;
use boot_core::{boot_contract, BootEnvironment, Contract};

#[boot_contract(EtfInstantiateMsg, EtfExecuteMsg, EtfQueryMsg, MigrateMsg)]
#[boot_contract(InstantiateMsg, ExecuteMsg, QueryMsg, MigrateMsg)]
pub struct ETF<Chain>;

impl<Chain: BootEnvironment> AppDeployer<Chain> for ETF<Chain> {}
Expand All @@ -28,7 +28,8 @@ pub mod boot {
crate::contract::execute,
crate::contract::instantiate,
crate::contract::query,
),
)
.with_reply(crate::contract::reply),
));
Self(contract)
}
Expand Down
15 changes: 9 additions & 6 deletions contracts/etf/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ use cosmwasm_schema::QueryResponses;
use cosmwasm_std::Decimal;
use cw_asset::AssetUnchecked;

pub type InstantiateMsg = app::InstantiateMsg<EtfInstantiateMsg>;
pub type ExecuteMsg = app::ExecuteMsg<EtfExecuteMsg>;
pub type QueryMsg = app::QueryMsg<EtfQueryMsg>;
pub type MigrateMsg = app::MigrateMsg;

impl app::AppExecuteMsg for EtfExecuteMsg {}

Expand All @@ -67,8 +69,9 @@ pub struct EtfInstantiateMsg {
#[cfg_attr(feature = "boot", derive(boot_core::ExecuteFns))]
#[cfg_attr(feature = "boot", impl_into(ExecuteMsg))]
pub enum EtfExecuteMsg {
/// Provide liquidity to the attached proxy using a native token.
ProvideLiquidity { asset: AssetUnchecked },
/// Deposit asset into the ETF
#[cfg_attr(feature = "boot", payable)]
Deposit { asset: AssetUnchecked },
/// Set the withdraw fee
SetFee { fee: Decimal },
}
Expand All @@ -85,13 +88,13 @@ pub enum EtfQueryMsg {
}

#[cosmwasm_schema::cw_serde]
pub enum DepositHookMsg {
WithdrawLiquidity {},
ProvideLiquidity {},
pub enum Cw20HookMsg {
Deposit {},
Claim {},
}

#[cosmwasm_schema::cw_serde]
pub struct StateResponse {
pub liquidity_token: String,
pub share_token_address: String,
pub fee: Decimal,
}
2 changes: 1 addition & 1 deletion contracts/etf/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cw_storage_plus::Item;
/// BaseState is initialized in contract
#[cosmwasm_schema::cw_serde]
pub struct State {
pub liquidity_token_addr: Addr,
pub share_token_address: Addr,
pub manager_addr: Addr,
}

Expand Down

0 comments on commit 8c5a6f7

Please sign in to comment.