pub fn execute_swap(
    deps: DepsMut,
    info: MessageInfo,
    env: Env,
    amount: Uint128,
) -> StdResult<Response> {
    let config: Config = read_config(deps.storage)?;

    // Check only owner can call
    if config.owner != deps.api.addr_canonicalize(info.sender.as_str())? {
        return Err(StdError::generic_err("Unauthorized"));
    }

    let glow_token = deps.api.addr_humanize(&config.glow_token)?;
    let terraswap_factory_addr = deps.api.addr_humanize(&config.terraswap_factory)?;

    let pair_info: PairInfo = query_pair_info(
        &deps.querier,
        terraswap_factory_addr,
        &[
            AssetInfo::NativeToken {
                denom: config.stable_denom.to_string(),
            },
            AssetInfo::Token {
                contract_addr: glow_token.to_string(),
            },
        ],
    )?;

    let contract_balance = query_balance(
        &deps.querier,
        env.contract.address,
        config.stable_denom.to_string(),
    )?;

    if amount > contract_balance {
        return Err(StdError::generic_err(
            "Amount of stable denom to spend cannot be greater than contract balance",
        ));
    }

    let swap_asset = Asset {
        info: AssetInfo::NativeToken {
            denom: config.stable_denom.to_string(),
        },
        amount,
    };

    Ok(Response::new()
        .add_message(CosmosMsg::Wasm(WasmMsg::Execute {
            contract_addr: pair_info.contract_addr,
            msg: to_binary(&TerraswapExecuteMsg::Swap {
                offer_asset: Asset {
                    amount,
                    ..swap_asset
                },
                max_spread: None,
                belief_price: None,
                to: None,
            })?,
            funds: vec![Coin {
                denom: config.stable_denom.clone(),
                amount,
            }],
        }))
        .add_attributes(vec![
            attr("action", "swap"),
            attr(
                "amount_spent",
                format!("{:?}{:?}", amount.to_string(), config.stable_denom),
            ),
        ]))
}
/// Swap
/// Owner can execute sweep function to swap
/// asset config native denom => GLOW token
Swap
Owner can execute sweep function to swap
asset config native denom => GLOW token
Swap Owner can execute sweep function to swap asset config native denom = > GLOW token
pub fn execute_swap(
    deps: DepsMut,
    info: MessageInfo,
    env: Env,
    amount: Uint128,
) -> StdResult<Response> {
    let config: Config = read_config(deps.storage)?;
    // Check only owner can call
    if config.owner != deps.api.addr_canonicalize(info.sender.as_str())? {
        return Err(StdError::generic_err("Unauthorized"));
    }
    let glow_token = deps.api.addr_humanize(&config.glow_token)?;
    let terraswap_factory_addr = deps.api.addr_humanize(&config.terraswap_factory)?;
    let pair_info: PairInfo = query_pair_info(
        &deps.querier,
        terraswap_factory_addr,
        &[
            AssetInfo::NativeToken {
                denom: config.stable_denom.to_string(),
            },
            AssetInfo::Token {
                contract_addr: glow_token.to_string(),
            },
        ],
    )?;
    let contract_balance = query_balance(
        &deps.querier,
        env.contract.address,
        config.stable_denom.to_string(),
    )?;
    if amount > contract_balance {
        return Err(StdError::generic_err(
            "Amount of stable denom to spend cannot be greater than contract balance",
        ));
    }
    let swap_asset = Asset {
        info: AssetInfo::NativeToken {
            denom: config.stable_denom.to_string(),
        },
        amount,
    };
    Ok(Response::new()
        .add_message(CosmosMsg::Wasm(WasmMsg::Execute {
            contract_addr: pair_info.contract_addr,
            msg: to_binary(&TerraswapExecuteMsg::Swap {
                offer_asset: Asset {
                    amount,
                    ..swap_asset
                },
                max_spread: None,
                belief_price: None,
                to: None,
            })?,
            funds: vec![Coin {
                denom: config.stable_denom.clone(),
                amount,
            }],
        }))
        .add_attributes(vec![
            attr("action", "swap"),
            attr(
                "amount_spent",
                format!("{:?}{:?}", amount.to_string(), config.stable_denom),
            ),
        ]))
}
pub fn execute_swap ( deps : DepsMut , info : MessageInfo , env : Env , amount : Uint128 , ) -> StdResult < Response > { let config : Config = read_config ( deps . storage ) ? ; // Check only owner can call if config . owner != deps . api . addr_canonicalize ( info . sender . as_str ( ) ) ? { return Err ( StdError :: generic_err ( " " ) ) ; } let glow_token = deps . api . addr_humanize ( & config . glow_token ) ? ; let terraswap_factory_addr = deps . api . addr_humanize ( & config . terraswap_factory ) ? ; let pair_info : PairInfo = query_pair_info ( & deps . querier , terraswap_factory_addr , & [ AssetInfo :: NativeToken { denom : config . stable_denom . to_string ( ) , } , AssetInfo :: Token { contract_addr : glow_token . to_string ( ) , } , ] , ) ? ; let contract_balance = query_balance ( & deps . querier , env . contract . address , config . stable_denom . to_string ( ) , ) ? ; if amount > contract_balance { return Err ( StdError :: generic_err ( " " , ) ) ; } let swap_asset = Asset { info : AssetInfo :: NativeToken { denom : config . stable_denom . to_string ( ) , } , amount , } ; Ok ( Response :: new ( ) . add_message ( CosmosMsg :: Wasm ( WasmMsg :: Execute { contract_addr : pair_info . contract_addr , msg : to_binary ( & TerraswapExecuteMsg :: Swap { offer_asset : Asset { amount , .. swap_asset } , max_spread : None , belief_price : None , to : None , } ) ? , funds : vec ! [ Coin { denom config stable_denom clone ( ) amount } ] , } ) ) . add_attributes ( vec ! [ attr ( " " " " ) attr ( " " format ( " " amount to_string ( ) config stable_denom ) ) ] ) ) }
Swap
Owner can execute sweep function to swap
asset config native denom => GLOW token
Swap Owner can execute sweep function to swap asset config native denom = > GLOW token
deps
DepsMut
info
MessageInfo
env
Env
amount
Uint128
deps
DepsMut
info
MessageInfo
env
Env
amount
Uint128