pub fn execute_swap_operation(
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    operation: SwapOperation,
    to: Option<String>,
) -> StdResult<Response<TerraMsgWrapper>> {
    if env.contract.address != info.sender {
        return Err(StdError::generic_err("unauthorized"));
    }

    let messages: Vec<CosmosMsg<TerraMsgWrapper>> = match operation {
        SwapOperation::NativeSwap {
            offer_denom,
            ask_denom,
        } => {
            let amount =
                query_balance(&deps.querier, env.contract.address, offer_denom.to_string())?;
            if let Some(to) = to {
                // if the operation is last, and requires send
                // deduct tax from the offer_coin
                let amount =
                    amount.checked_sub(compute_tax(&deps.querier, amount, offer_denom.clone())?)?;
                vec![create_swap_send_msg(
                    to,
                    Coin {
                        denom: offer_denom,
                        amount,
                    },
                    ask_denom,
                )]
            } else {
                vec![create_swap_msg(
                    Coin {
                        denom: offer_denom,
                        amount,
                    },
                    ask_denom,
                )]
            }
        }
        SwapOperation::TerraSwap {
            offer_asset_info,
            ask_asset_info,
        } => {
            let config: Config = CONFIG.load(deps.as_ref().storage)?;
            let terraswap_factory = deps.api.addr_humanize(&config.terraswap_factory)?;
            let pair_info: PairInfo = query_pair_info(
                &deps.querier,
                terraswap_factory,
                &[offer_asset_info.clone(), ask_asset_info],
            )?;

            let amount = match offer_asset_info.clone() {
                AssetInfo::NativeToken { denom } => {
                    query_balance(&deps.querier, env.contract.address, denom)?
                }
                AssetInfo::Token { contract_addr } => query_token_balance(
                    &deps.querier,
                    deps.api.addr_validate(contract_addr.as_str())?,
                    env.contract.address,
                )?,
            };
            let offer_asset: Asset = Asset {
                info: offer_asset_info,
                amount,
            };

            vec![asset_into_swap_msg(
                deps.as_ref(),
                Addr::unchecked(pair_info.contract_addr),
                offer_asset,
                None,
                to,
            )?]
        }
    };

    Ok(Response::new().add_messages(messages))
}
/// Execute swap operation
/// swap all offer asset to ask asset
Execute swap operation
swap all offer asset to ask asset
Execute swap operation swap all offer asset to ask asset
pub fn execute_swap_operation(
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    operation: SwapOperation,
    to: Option<String>,
) -> StdResult<Response<TerraMsgWrapper>> {
    if env.contract.address != info.sender {
        return Err(StdError::generic_err("unauthorized"));
    }
    let messages: Vec<CosmosMsg<TerraMsgWrapper>> = match operation {
        SwapOperation::NativeSwap {
            offer_denom,
            ask_denom,
        } => {
            let amount =
                query_balance(&deps.querier, env.contract.address, offer_denom.to_string())?;
            if let Some(to) = to {
                // if the operation is last, and requires send
                // deduct tax from the offer_coin
                let amount =
                    amount.checked_sub(compute_tax(&deps.querier, amount, offer_denom.clone())?)?;
                vec![create_swap_send_msg(
                    to,
                    Coin {
                        denom: offer_denom,
                        amount,
                    },
                    ask_denom,
                )]
            } else {
                vec![create_swap_msg(
                    Coin {
                        denom: offer_denom,
                        amount,
                    },
                    ask_denom,
                )]
            }
        }
        SwapOperation::TerraSwap {
            offer_asset_info,
            ask_asset_info,
        } => {
            let config: Config = CONFIG.load(deps.as_ref().storage)?;
            let terraswap_factory = deps.api.addr_humanize(&config.terraswap_factory)?;
            let pair_info: PairInfo = query_pair_info(
                &deps.querier,
                terraswap_factory,
                &[offer_asset_info.clone(), ask_asset_info],
            )?;
            let amount = match offer_asset_info.clone() {
                AssetInfo::NativeToken { denom } => {
                    query_balance(&deps.querier, env.contract.address, denom)?
                }
                AssetInfo::Token { contract_addr } => query_token_balance(
                    &deps.querier,
                    deps.api.addr_validate(contract_addr.as_str())?,
                    env.contract.address,
                )?,
            };
            let offer_asset: Asset = Asset {
                info: offer_asset_info,
                amount,
            };
            vec![asset_into_swap_msg(
                deps.as_ref(),
                Addr::unchecked(pair_info.contract_addr),
                offer_asset,
                None,
                to,
            )?]
        }
    };
    Ok(Response::new().add_messages(messages))
}
pub fn execute_swap_operation ( deps : DepsMut , env : Env , info : MessageInfo , operation : SwapOperation , to : Option < String > , ) -> StdResult < Response < TerraMsgWrapper > > { if env . contract . address != info . sender { return Err ( StdError :: generic_err ( " " ) ) ; } let messages : Vec < CosmosMsg < TerraMsgWrapper > > = match operation { SwapOperation :: NativeSwap { offer_denom , ask_denom , } => { let amount = query_balance ( & deps . querier , env . contract . address , offer_denom . to_string ( ) ) ? ; if let Some ( to ) = to { // if the operation is last, and requires send // deduct tax from the offer_coin let amount = amount . checked_sub ( compute_tax ( & deps . querier , amount , offer_denom . clone ( ) ) ? ) ? ; vec ! [ create_swap_send_msg ( to Coin { denom offer_denom amount } ask_denom ) ] } else { vec ! [ create_swap_msg ( Coin { denom offer_denom amount } ask_denom ) ] } } SwapOperation :: TerraSwap { offer_asset_info , ask_asset_info , } => { let config : Config = CONFIG . load ( deps . as_ref ( ) . storage ) ? ; let terraswap_factory = deps . api . addr_humanize ( & config . terraswap_factory ) ? ; let pair_info : PairInfo = query_pair_info ( & deps . querier , terraswap_factory , & [ offer_asset_info . clone ( ) , ask_asset_info ] , ) ? ; let amount = match offer_asset_info . clone ( ) { AssetInfo :: NativeToken { denom } => { query_balance ( & deps . querier , env . contract . address , denom ) ? } AssetInfo :: Token { contract_addr } => query_token_balance ( & deps . querier , deps . api . addr_validate ( contract_addr . as_str ( ) ) ? , env . contract . address , ) ? , } ; let offer_asset : Asset = Asset { info : offer_asset_info , amount , } ; vec ! [ asset_into_swap_msg ( deps as_ref ( ) Addr unchecked ( pair_info contract_addr ) offer_asset None to ) ] } } ; Ok ( Response :: new ( ) . add_messages ( messages ) ) }
Execute swap operation
swap all offer asset to ask asset
Execute swap operation swap all offer asset to ask asset
deps
DepsMut
env
Env
info
MessageInfo
operation
SwapOperation
to
Option<String>
deps
DepsMut
env
Env
info
MessageInfo
operation
SwapOperation
to
Option<String>