pub fn try_withdraw_liquidity(
    deps: DepsMut,
    _env: Env,
    sender: String,
    amount: Uint128,
) -> VaultResult {
    let pool: Pool = POOL.load(deps.storage)?;
    let state: State = STATE.load(deps.storage)?;
    let base_state: BaseState = BASESTATE.load(deps.storage)?;
    let memory = base_state.memory;
    let fee: Fee = FEE.load(deps.storage)?;
    // Get assets
    let assets = memory.query_assets(deps.as_ref(), &pool.assets)?;

    // Logging var
    let mut attrs = vec![
        ("Action:", String::from("Withdraw from vault")),
        ("Received 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())?;

    // Get treasury fee in LP tokens
    let treasury_fee = fee.compute(amount);

    // Share with fee deducted.
    let share_ratio: Decimal = Decimal::from_ratio(amount - treasury_fee, total_share);

    // Init response
    let mut response = Response::new();

    if !treasury_fee.is_zero() {
        // LP token fee
        let lp_token_treasury_fee = Asset {
            info: AssetInfo::Token {
                contract_addr: state.liquidity_token_addr.to_string(),
            },
            amount: treasury_fee,
        };

        // Construct treasury fee msg
        let treasury_fee_msg = fee.msg(
            deps.as_ref(),
            lp_token_treasury_fee,
            base_state.treasury_address.clone(),
        )?;

        // Transfer fee
        response = response.add_message(treasury_fee_msg);
    }
    attrs.push(("Treasury fee:", treasury_fee.to_string()));

    // Get asset holdings of vault and calculate amount to return
    let mut pay_back_assets: Vec<Asset> = vec![];
    // Get asset holdings of vault and calculate amount to return
    for (_, info) in assets.into_iter() {
        pay_back_assets.push(Asset {
            info: info.clone(),
            amount: share_ratio
                // query asset held in treasury
                * query_asset_balance(
                    deps.as_ref(),
                    &info.clone(),
                    base_state.treasury_address.clone(),
                )
                .unwrap(),
        });
    }

    // Construct repay msgs
    let mut refund_msgs: Vec<CosmosMsg> = vec![];
    for asset in pay_back_assets.into_iter() {
        if asset.amount != Uint128::zero() {
            // Unchecked ok as sender is already validated by VM
            refund_msgs.push(
                asset
                    .clone()
                    .into_msg(&deps.querier, Addr::unchecked(sender.clone()))?,
            );
            attrs.push(("Repaying:", asset.to_string()));
        }
    }

    // Msg that gets called on the vault address
    let vault_refund_msg = send_to_treasury(refund_msgs, &base_state.treasury_address)?;

    // LP burn msg
    let burn_msg = CosmosMsg::Wasm(WasmMsg::Execute {
        contract_addr: state.liquidity_token_addr.into(),
        // Burn exludes fee
        msg: to_binary(&Cw20ExecuteMsg::Burn {
            amount: (amount - treasury_fee),
        })?,
        funds: vec![],
    });

    Ok(response
        .add_attribute("Action:", "Withdraw Liquidity")
        // Burn LP tokens
        .add_message(burn_msg)
        // Send treasury funds to owner
        .add_message(vault_refund_msg)
        .add_attributes(attrs))
}
/// Attempt to withdraw deposits. Fees are calculated and deducted in liquidity tokens.
/// This allowes the war-chest to accumulate a stake in the vault.
/// The refund is taken out of Anchor if possible.
/// Luna holdings are not eligible for withdrawal.
Attempt to withdraw deposits. Fees are calculated and deducted in liquidity tokens.
This allowes the war-chest to accumulate a stake in the vault.
The refund is taken out of Anchor if possible.
Luna holdings are not eligible for withdrawal.
Attempt to withdraw deposits . Fees are calculated and deducted in liquidity tokens . This allowes the war - chest to accumulate a stake in the vault . The refund is taken out of Anchor if possible . Luna holdings are not eligible for withdrawal .
pub fn try_withdraw_liquidity(
    deps: DepsMut,
    _env: Env,
    sender: String,
    amount: Uint128,
) -> VaultResult {
    let pool: Pool = POOL.load(deps.storage)?;
    let state: State = STATE.load(deps.storage)?;
    let base_state: BaseState = BASESTATE.load(deps.storage)?;
    let memory = base_state.memory;
    let fee: Fee = FEE.load(deps.storage)?;
    // Get assets
    let assets = memory.query_assets(deps.as_ref(), &pool.assets)?;
    // Logging var
    let mut attrs = vec![
        ("Action:", String::from("Withdraw from vault")),
        ("Received 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())?;
    // Get treasury fee in LP tokens
    let treasury_fee = fee.compute(amount);
    // Share with fee deducted.
    let share_ratio: Decimal = Decimal::from_ratio(amount - treasury_fee, total_share);
    // Init response
    let mut response = Response::new();
    if !treasury_fee.is_zero() {
        // LP token fee
        let lp_token_treasury_fee = Asset {
            info: AssetInfo::Token {
                contract_addr: state.liquidity_token_addr.to_string(),
            },
            amount: treasury_fee,
        };
        // Construct treasury fee msg
        let treasury_fee_msg = fee.msg(
            deps.as_ref(),
            lp_token_treasury_fee,
            base_state.treasury_address.clone(),
        )?;
        // Transfer fee
        response = response.add_message(treasury_fee_msg);
    }
    attrs.push(("Treasury fee:", treasury_fee.to_string()));
    // Get asset holdings of vault and calculate amount to return
    let mut pay_back_assets: Vec<Asset> = vec![];
    // Get asset holdings of vault and calculate amount to return
    for (_, info) in assets.into_iter() {
        pay_back_assets.push(Asset {
            info: info.clone(),
            amount: share_ratio
                // query asset held in treasury
                * query_asset_balance(
                    deps.as_ref(),
                    &info.clone(),
                    base_state.treasury_address.clone(),
                )
                .unwrap(),
        });
    }
    // Construct repay msgs
    let mut refund_msgs: Vec<CosmosMsg> = vec![];
    for asset in pay_back_assets.into_iter() {
        if asset.amount != Uint128::zero() {
            // Unchecked ok as sender is already validated by VM
            refund_msgs.push(
                asset
                    .clone()
                    .into_msg(&deps.querier, Addr::unchecked(sender.clone()))?,
            );
            attrs.push(("Repaying:", asset.to_string()));
        }
    }
    // Msg that gets called on the vault address
    let vault_refund_msg = send_to_treasury(refund_msgs, &base_state.treasury_address)?;
    // LP burn msg
    let burn_msg = CosmosMsg::Wasm(WasmMsg::Execute {
        contract_addr: state.liquidity_token_addr.into(),
        // Burn exludes fee
        msg: to_binary(&Cw20ExecuteMsg::Burn {
            amount: (amount - treasury_fee),
        })?,
        funds: vec![],
    });
    Ok(response
        .add_attribute("Action:", "Withdraw Liquidity")
        // Burn LP tokens
        .add_message(burn_msg)
        // Send treasury funds to owner
        .add_message(vault_refund_msg)
        .add_attributes(attrs))
}
pub fn try_withdraw_liquidity ( deps : DepsMut , _env : Env , sender : String , amount : Uint128 , ) -> VaultResult { let pool : Pool = POOL . load ( deps . storage ) ? ; let state : State = STATE . load ( deps . storage ) ? ; let base_state : BaseState = BASESTATE . load ( deps . storage ) ? ; let memory = base_state . memory ; let fee : Fee = FEE . load ( deps . storage ) ? ; // Get assets let assets = memory . query_assets ( deps . as_ref ( ) , & pool . assets ) ? ; // Logging var let mut attrs = vec ! [ ( " " String from ( " " ) ) ( " " amount to_string ( ) ) ] ; // Calculate share of pool and requested pool value let total_share : Uint128 = query_supply ( & deps . querier , state . liquidity_token_addr . clone ( ) ) ? ; // Get treasury fee in LP tokens let treasury_fee = fee . compute ( amount ) ; // Share with fee deducted. let share_ratio : Decimal = Decimal :: from_ratio ( amount - treasury_fee , total_share ) ; // Init response let mut response = Response :: new ( ) ; if ! treasury_fee . is_zero ( ) { // LP token fee let lp_token_treasury_fee = Asset { info : AssetInfo :: Token { contract_addr : state . liquidity_token_addr . to_string ( ) , } , amount : treasury_fee , } ; // Construct treasury fee msg let treasury_fee_msg = fee . msg ( deps . as_ref ( ) , lp_token_treasury_fee , base_state . treasury_address . clone ( ) , ) ? ; // Transfer fee response = response . add_message ( treasury_fee_msg ) ; } attrs . push ( ( " " , treasury_fee . to_string ( ) ) ) ; // Get asset holdings of vault and calculate amount to return let mut pay_back_assets : Vec < Asset > = vec ! [ ] ; // Get asset holdings of vault and calculate amount to return for ( _ , info ) in assets . into_iter ( ) { pay_back_assets . push ( Asset { info : info . clone ( ) , amount : share_ratio // query asset held in treasury * query_asset_balance ( deps . as_ref ( ) , & info . clone ( ) , base_state . treasury_address . clone ( ) , ) . unwrap ( ) , } ) ; } // Construct repay msgs let mut refund_msgs : Vec < CosmosMsg > = vec ! [ ] ; for asset in pay_back_assets . into_iter ( ) { if asset . amount != Uint128 :: zero ( ) { // Unchecked ok as sender is already validated by VM refund_msgs . push ( asset . clone ( ) . into_msg ( & deps . querier , Addr :: unchecked ( sender . clone ( ) ) ) ? , ) ; attrs . push ( ( " " , asset . to_string ( ) ) ) ; } } // Msg that gets called on the vault address let vault_refund_msg = send_to_treasury ( refund_msgs , & base_state . treasury_address ) ? ; // LP burn msg let burn_msg = CosmosMsg :: Wasm ( WasmMsg :: Execute { contract_addr : state . liquidity_token_addr . into ( ) , // Burn exludes fee msg : to_binary ( & Cw20ExecuteMsg :: Burn { amount : ( amount - treasury_fee ) , } ) ? , funds : vec ! [ ] , } ) ; Ok ( response . add_attribute ( " " , " " ) // Burn LP tokens . add_message ( burn_msg ) // Send treasury funds to owner . add_message ( vault_refund_msg ) . add_attributes ( attrs ) ) }
Attempt to withdraw deposits.
Attempt to withdraw deposits .
deps
DepsMut
_env
Env
sender
String
amount
Uint128
deps
DepsMut
_env
Env
sender
String
amount
Uint128