pub fn try_swap<S: Storage, A: Api, Q: Querier>(
    deps: &mut Extern<S, A, Q>,
    env: Env,
    src_amount: Uint128,
    src_token: HumanAddr,
    dst_token: HumanAddr,
    recipient: HumanAddr,
) -> HandleResult {
    let supported_tokens = read_all_assets(&deps.storage)?;

    let src_token = supported_tokens
        .iter()
        .find(|t| t.address == src_token)
        .unwrap() /* this was checked before going into try_swap */;
    let dst_token = supported_tokens
        .iter()
        .find(|t| t.address == dst_token)
        .unwrap() /* this was checked before going into try_swap */;

    // Normalize amount (due to decimals differences)
    let mut dst_amount = src_amount.u128();
    if src_token.decimals > dst_token.decimals {
        let factor: u128 = 10u128.pow((src_token.decimals - dst_token.decimals) as u32);
        dst_amount = dst_amount / factor;
    } else if dst_token.decimals > src_token.decimals {
        let factor: u128 = 10u128.pow((dst_token.decimals - src_token.decimals) as u32);
        dst_amount = dst_amount * factor;
    }

    // Take fee
    let config = read_config(&deps.storage)?;

    dst_amount = dst_amount * config.swap_fee_nom.u128() / config.swap_fee_denom.u128();

    // TODO

    let mut messages = Vec::<CosmosMsg>::new();
    messages.push(return_asset.clone().into_msg(
        &deps,
        env.contract.address.clone(),
        to.clone().unwrap_or(sender.clone()),
    )?);

    Ok(HandleResponse {
        messages,
        log: vec![
            log("action", "swap"),
            log("offer_asset", offer_asset.info.to_string()),
            log("ask_asset", ask_pool.info.to_string()),
            log("offer_amount", offer_amount.to_string()),
            log("return_amount", return_amount.to_string()),
            log("spread_amount", spread_amount.to_string()),
            log("commission_amount", commission_amount.to_string()),
        ],
        data: None,
    })
}
// CONTRACT - a user must do token approval
a user must do token approval
a user must do token approval
pub fn try_swap<S: Storage, A: Api, Q: Querier>(
    deps: &mut Extern<S, A, Q>,
    env: Env,
    src_amount: Uint128,
    src_token: HumanAddr,
    dst_token: HumanAddr,
    recipient: HumanAddr,
) -> HandleResult {
    let supported_tokens = read_all_assets(&deps.storage)?;
    let src_token = supported_tokens
        .iter()
        .find(|t| t.address == src_token)
        .unwrap() /* this was checked before going into try_swap */;
    let dst_token = supported_tokens
        .iter()
        .find(|t| t.address == dst_token)
        .unwrap() /* this was checked before going into try_swap */;
    // Normalize amount (due to decimals differences)
    let mut dst_amount = src_amount.u128();
    if src_token.decimals > dst_token.decimals {
        let factor: u128 = 10u128.pow((src_token.decimals - dst_token.decimals) as u32);
        dst_amount = dst_amount / factor;
    } else if dst_token.decimals > src_token.decimals {
        let factor: u128 = 10u128.pow((dst_token.decimals - src_token.decimals) as u32);
        dst_amount = dst_amount * factor;
    }
    // Take fee
    let config = read_config(&deps.storage)?;
    dst_amount = dst_amount * config.swap_fee_nom.u128() / config.swap_fee_denom.u128();
    // TODO
    let mut messages = Vec::<CosmosMsg>::new();
    messages.push(return_asset.clone().into_msg(
        &deps,
        env.contract.address.clone(),
        to.clone().unwrap_or(sender.clone()),
    )?);
    Ok(HandleResponse {
        messages,
        log: vec![
            log("action", "swap"),
            log("offer_asset", offer_asset.info.to_string()),
            log("ask_asset", ask_pool.info.to_string()),
            log("offer_amount", offer_amount.to_string()),
            log("return_amount", return_amount.to_string()),
            log("spread_amount", spread_amount.to_string()),
            log("commission_amount", commission_amount.to_string()),
        ],
        data: None,
    })
}
pub fn try_swap < S : Storage , A : Api , Q : Querier > ( deps : & mut Extern < S , A , Q > , env : Env , src_amount : Uint128 , src_token : HumanAddr , dst_token : HumanAddr , recipient : HumanAddr , ) -> HandleResult { let supported_tokens = read_all_assets ( & deps . storage ) ? ; let src_token = supported_tokens . iter ( ) . find ( | t | t . address == src_token ) . unwrap ( ) /* this was checked before going into try_swap */ ; let dst_token = supported_tokens . iter ( ) . find ( | t | t . address == dst_token ) . unwrap ( ) /* this was checked before going into try_swap */ ; // Normalize amount (due to decimals differences) let mut dst_amount = src_amount . u128 ( ) ; if src_token . decimals > dst_token . decimals { let factor : u128 = 10u128 . pow ( ( src_token . decimals - dst_token . decimals ) as u32 ) ; dst_amount = dst_amount / factor ; } else if dst_token . decimals > src_token . decimals { let factor : u128 = 10u128 . pow ( ( dst_token . decimals - src_token . decimals ) as u32 ) ; dst_amount = dst_amount * factor ; } // Take fee let config = read_config ( & deps . storage ) ? ; dst_amount = dst_amount * config . swap_fee_nom . u128 ( ) / config . swap_fee_denom . u128 ( ) ; // TODO let mut messages = Vec :: < CosmosMsg > :: new ( ) ; messages . push ( return_asset . clone ( ) . into_msg ( & deps , env . contract . address . clone ( ) , to . clone ( ) . unwrap_or ( sender . clone ( ) ) , ) ? ) ; Ok ( HandleResponse { messages , log : vec ! [ log ( " " " " ) log ( " " offer_asset info to_string ( ) ) log ( " " ask_pool info to_string ( ) ) log ( " " offer_amount to_string ( ) ) log ( " " return_amount to_string ( ) ) log ( " " spread_amount to_string ( ) ) log ( " " commission_amount to_string ( ) ) ] , data : None , } ) }
CONTRACT - a user must do token approval
CONTRACT - a user must do token approval
deps
&mut Extern<S, A, Q>
env
Env
src_amount
Uint128
src_token
HumanAddr
dst_token
HumanAddr
recipient
HumanAddr
deps
&mut Extern<S, A, Q>
env
Env
src_amount
Uint128
src_token
HumanAddr
dst_token
HumanAddr
recipient
HumanAddr