pub fn try_provide_liquidity(
    deps: DepsMut,
    msg_info: MessageInfo,
    asset: Asset,
    sender: Option<String>,
) -> VaultResult {
    // Load all needed states
    let pool: Pool = POOL.load(deps.storage)?;
    let state = STATE.load(deps.storage)?;
    let base_state: BaseState = BASESTATE.load(deps.storage)?;
    let memory = base_state.memory;

    // Get the liquidity provider address
    let liq_provider = match sender {
        Some(addr) => Addr::unchecked(addr),
        None => {
            // Check if deposit matches claimed deposit.
            if asset.is_native_token() {
                // If native token, assert claimed amount is correct
                asset.assert_sent_native_token_balance(&msg_info)?;
                msg_info.sender
            } else {
                // Can't add liquidity with cw20 if not using the hook
                return Err(VaultError::NotUsingCW20Hook {});
            }
        }
    };

    // Get all the required asset information from the memory contract
    let assets = memory.query_assets(deps.as_ref(), &pool.assets)?;

    // Construct deposit info
    let deposit_info = DepositInfo {
        asset_info: assets.get(&pool.deposit_asset).unwrap().clone(),
    };

    // Assert deposited asset and claimed asset infos are the same
    deposit_info.assert(&asset.info)?;

    // Init vector for logging
    let attrs = vec![
        ("Action:", String::from("Deposit to vault")),
        ("Received funds:", asset.to_string()),
    ];

    // Received deposit to vault
    let deposit: Uint128 = asset.amount;

    // Get total value in Vault
    let value = query_total_value(deps.as_ref(), &base_state.treasury_address)?;
    // Get total supply of LP tokens and calculate share
    let total_share = query_supply(&deps.querier, state.liquidity_token_addr.clone())?;

    let share = if total_share == Uint128::zero() || value.checked_sub(deposit)? == Uint128::zero()
    {
        // Initial share = deposit amount
        deposit
    } else {
        // lt: liquidity token
        // lt_to_receive = deposit * lt_price
        // lt_to_receive = deposit * lt_supply / previous_total_vault_value )
        // lt_to_receive = deposit * ( lt_supply / ( current_total_vault_value - deposit ) )
        deposit.multiply_ratio(total_share, value - deposit)
    };

    // mint LP token to liq_provider
    let mint_lp = CosmosMsg::Wasm(WasmMsg::Execute {
        contract_addr: state.liquidity_token_addr.to_string(),
        msg: to_binary(&Cw20ExecuteMsg::Mint {
            recipient: liq_provider.to_string(),
            amount: share,
        })?,
        funds: vec![],
    });

    // Send received asset to the vault.
    let send_to_vault = asset.into_msg(&deps.querier, base_state.treasury_address)?;

    let response = Response::new()
        .add_attributes(attrs)
        .add_message(mint_lp)
        .add_message(send_to_vault);

    Ok(response)
}
/// Called when either providing liquidity with a native token or when providing liquidity
/// with a CW20.
Called when either providing liquidity with a native token or when providing liquidity
with a CW20.
Called when either providing liquidity with a native token or when providing liquidity with a CW20 .
pub fn try_provide_liquidity(
    deps: DepsMut,
    msg_info: MessageInfo,
    asset: Asset,
    sender: Option<String>,
) -> VaultResult {
    // Load all needed states
    let pool: Pool = POOL.load(deps.storage)?;
    let state = STATE.load(deps.storage)?;
    let base_state: BaseState = BASESTATE.load(deps.storage)?;
    let memory = base_state.memory;
    // Get the liquidity provider address
    let liq_provider = match sender {
        Some(addr) => Addr::unchecked(addr),
        None => {
            // Check if deposit matches claimed deposit.
            if asset.is_native_token() {
                // If native token, assert claimed amount is correct
                asset.assert_sent_native_token_balance(&msg_info)?;
                msg_info.sender
            } else {
                // Can't add liquidity with cw20 if not using the hook
                return Err(VaultError::NotUsingCW20Hook {});
            }
        }
    };
    // Get all the required asset information from the memory contract
    let assets = memory.query_assets(deps.as_ref(), &pool.assets)?;
    // Construct deposit info
    let deposit_info = DepositInfo {
        asset_info: assets.get(&pool.deposit_asset).unwrap().clone(),
    };
    // Assert deposited asset and claimed asset infos are the same
    deposit_info.assert(&asset.info)?;
    // Init vector for logging
    let attrs = vec![
        ("Action:", String::from("Deposit to vault")),
        ("Received funds:", asset.to_string()),
    ];
    // Received deposit to vault
    let deposit: Uint128 = asset.amount;
    // Get total value in Vault
    let value = query_total_value(deps.as_ref(), &base_state.treasury_address)?;
    // Get total supply of LP tokens and calculate share
    let total_share = query_supply(&deps.querier, state.liquidity_token_addr.clone())?;
    let share = if total_share == Uint128::zero() || value.checked_sub(deposit)? == Uint128::zero()
    {
        // Initial share = deposit amount
        deposit
    } else {
        // lt: liquidity token
        // lt_to_receive = deposit * lt_price
        // lt_to_receive = deposit * lt_supply / previous_total_vault_value )
        // lt_to_receive = deposit * ( lt_supply / ( current_total_vault_value - deposit ) )
        deposit.multiply_ratio(total_share, value - deposit)
    };
    // mint LP token to liq_provider
    let mint_lp = CosmosMsg::Wasm(WasmMsg::Execute {
        contract_addr: state.liquidity_token_addr.to_string(),
        msg: to_binary(&Cw20ExecuteMsg::Mint {
            recipient: liq_provider.to_string(),
            amount: share,
        })?,
        funds: vec![],
    });
    // Send received asset to the vault.
    let send_to_vault = asset.into_msg(&deps.querier, base_state.treasury_address)?;
    let response = Response::new()
        .add_attributes(attrs)
        .add_message(mint_lp)
        .add_message(send_to_vault);
    Ok(response)
}
pub fn try_provide_liquidity ( deps : DepsMut , msg_info : MessageInfo , asset : Asset , sender : Option < String > , ) -> VaultResult { // Load all needed states let pool : Pool = POOL . load ( deps . storage ) ? ; let state = STATE . load ( deps . storage ) ? ; let base_state : BaseState = BASESTATE . load ( deps . storage ) ? ; let memory = base_state . memory ; // Get the liquidity provider address let liq_provider = match sender { Some ( addr ) => Addr :: unchecked ( addr ) , None => { // Check if deposit matches claimed deposit. if asset . is_native_token ( ) { // If native token, assert claimed amount is correct asset . assert_sent_native_token_balance ( & msg_info ) ? ; msg_info . sender } else { // Can't add liquidity with cw20 if not using the hook return Err ( VaultError :: NotUsingCW20Hook { } ) ; } } } ; // Get all the required asset information from the memory contract let assets = memory . query_assets ( deps . as_ref ( ) , & pool . assets ) ? ; // Construct deposit info let deposit_info = DepositInfo { asset_info : assets . get ( & pool . deposit_asset ) . unwrap ( ) . clone ( ) , } ; // Assert deposited asset and claimed asset infos are the same deposit_info . assert ( & asset . info ) ? ; // Init vector for logging let attrs = vec ! [ ( " " String from ( " " ) ) ( " " asset to_string ( ) ) ] ; // Received deposit to vault let deposit : Uint128 = asset . amount ; // Get total value in Vault let value = query_total_value ( deps . as_ref ( ) , & base_state . treasury_address ) ? ; // Get total supply of LP tokens and calculate share let total_share = query_supply ( & deps . querier , state . liquidity_token_addr . clone ( ) ) ? ; let share = if total_share == Uint128 :: zero ( ) || value . checked_sub ( deposit ) ? == Uint128 :: zero ( ) { // Initial share = deposit amount deposit } else { // lt: liquidity token // lt_to_receive = deposit * lt_price // lt_to_receive = deposit * lt_supply / previous_total_vault_value ) // lt_to_receive = deposit * ( lt_supply / ( current_total_vault_value - deposit ) ) deposit . multiply_ratio ( total_share , value - deposit ) } ; // mint LP token to liq_provider let mint_lp = CosmosMsg :: Wasm ( WasmMsg :: Execute { contract_addr : state . liquidity_token_addr . to_string ( ) , msg : to_binary ( & Cw20ExecuteMsg :: Mint { recipient : liq_provider . to_string ( ) , amount : share , } ) ? , funds : vec ! [ ] , } ) ; // Send received asset to the vault. let send_to_vault = asset . into_msg ( & deps . querier , base_state . treasury_address ) ? ; let response = Response :: new ( ) . add_attributes ( attrs ) . add_message ( mint_lp ) . add_message ( send_to_vault ) ; Ok ( response ) }
Called when either providing liquidity with a native token or when providing liquidity
with a CW20.
Called when either providing liquidity with a native token or when providing liquidity with a CW20 .
deps
DepsMut
msg_info
MessageInfo
asset
Asset
sender
Option<String>
deps
DepsMut
msg_info
MessageInfo
asset
Asset
sender
Option<String>