pub fn try_arb_below_peg(
    deps: DepsMut,
    env: Env,
    msg_info: MessageInfo,
    details: ArbDetails,
) -> VaultResult {
    let state = STATE.load(deps.storage)?;
    let deposit_info = ARB_BASE_ASSET.load(deps.storage)?;

    // Ensure the caller is the vault
    if msg_info.sender != state.vault_address {
        return Err(StableArbError::Unauthorized {});
    }

    // Set vars
    let pool_address = POOLS.load(deps.storage, &details.pool_name)?;
    let denom = deposit_info.get_denom()?;
    let lent_coin = deduct_tax(
        deps.as_ref(),
        Coin::new(details.asset.amount.u128(), denom.clone()),
    )?;
    let ask_denom = LUNA_DENOM.to_string();
    let response: Response<TerraMsgWrapper> = Response::new();

    // Check if we have enough funds
    let balance = query_balance(&deps.querier, env.contract.address.clone(), denom)?;
    if balance < details.asset.amount {
        return Err(StableArbError::Broke {});
    }

    // Simulate first tx with Terra Market Module
    // lent_coin already takes transfer tax into account.
    let expected_luna_received =
        query_market_price(deps.as_ref(), lent_coin.clone(), ask_denom.clone())?;

    // Construct offer for Terraswap
    let offer_coin = Coin {
        denom: ask_denom.clone(),
        amount: expected_luna_received,
    };

    // Market swap msg, swap STABLE -> LUNA
    let swap_msg = create_swap_msg(lent_coin.clone(), ask_denom);

    // Terraswap msg, swap LUNA -> STABLE
    let terraswap_msg = CosmosMsg::Wasm(WasmMsg::Execute {
        contract_addr: pool_address.to_string(),
        funds: vec![offer_coin.clone()],
        msg: to_binary(&create_terraswap_msg(
            offer_coin,
            details.belief_price,
            Some(details.slippage),
        ))?,
    });

    let logs = vec![
        ("action", String::from("arb below peg")),
        ("offer_amount", lent_coin.amount.to_string()),
        ("expected_luna", expected_luna_received.to_string()),
    ];

    // Create callback, this will send the funds back to the vault.
    let callback_msg =
        CallbackMsg::AfterSuccessfulTradeCallback {}.to_cosmos_msg(&env.contract.address)?;

    Ok(response
        .add_attributes(logs)
        .add_message(swap_msg)
        .add_message(terraswap_msg)
        .add_message(callback_msg))
}
// Attempt to perform an arbitrage operation with the assumption that
// the currency to be arb'd is below peg. Needed funds should be provided
// by the earlier stablecoin vault flashloan call.
Attempt to perform an arbitrage operation with the assumption that
the currency to be arb'd is below peg. Needed funds should be provided
by the earlier stablecoin vault flashloan call.
Attempt to perform an arbitrage operation with the assumption that the currency to be arb ' d is below peg . Needed funds should be provided by the earlier stablecoin vault flashloan call .
pub fn try_arb_below_peg(
    deps: DepsMut,
    env: Env,
    msg_info: MessageInfo,
    details: ArbDetails,
) -> VaultResult {
    let state = STATE.load(deps.storage)?;
    let deposit_info = ARB_BASE_ASSET.load(deps.storage)?;
    // Ensure the caller is the vault
    if msg_info.sender != state.vault_address {
        return Err(StableArbError::Unauthorized {});
    }
    // Set vars
    let pool_address = POOLS.load(deps.storage, &details.pool_name)?;
    let denom = deposit_info.get_denom()?;
    let lent_coin = deduct_tax(
        deps.as_ref(),
        Coin::new(details.asset.amount.u128(), denom.clone()),
    )?;
    let ask_denom = LUNA_DENOM.to_string();
    let response: Response<TerraMsgWrapper> = Response::new();
    // Check if we have enough funds
    let balance = query_balance(&deps.querier, env.contract.address.clone(), denom)?;
    if balance < details.asset.amount {
        return Err(StableArbError::Broke {});
    }
    // Simulate first tx with Terra Market Module
    // lent_coin already takes transfer tax into account.
    let expected_luna_received =
        query_market_price(deps.as_ref(), lent_coin.clone(), ask_denom.clone())?;
    // Construct offer for Terraswap
    let offer_coin = Coin {
        denom: ask_denom.clone(),
        amount: expected_luna_received,
    };
    // Market swap msg, swap STABLE -> LUNA
    let swap_msg = create_swap_msg(lent_coin.clone(), ask_denom);
    // Terraswap msg, swap LUNA -> STABLE
    let terraswap_msg = CosmosMsg::Wasm(WasmMsg::Execute {
        contract_addr: pool_address.to_string(),
        funds: vec![offer_coin.clone()],
        msg: to_binary(&create_terraswap_msg(
            offer_coin,
            details.belief_price,
            Some(details.slippage),
        ))?,
    });
    let logs = vec![
        ("action", String::from("arb below peg")),
        ("offer_amount", lent_coin.amount.to_string()),
        ("expected_luna", expected_luna_received.to_string()),
    ];
    // Create callback, this will send the funds back to the vault.
    let callback_msg =
        CallbackMsg::AfterSuccessfulTradeCallback {}.to_cosmos_msg(&env.contract.address)?;
    Ok(response
        .add_attributes(logs)
        .add_message(swap_msg)
        .add_message(terraswap_msg)
        .add_message(callback_msg))
}
pub fn try_arb_below_peg ( deps : DepsMut , env : Env , msg_info : MessageInfo , details : ArbDetails , ) -> VaultResult { let state = STATE . load ( deps . storage ) ? ; let deposit_info = ARB_BASE_ASSET . load ( deps . storage ) ? ; // Ensure the caller is the vault if msg_info . sender != state . vault_address { return Err ( StableArbError :: Unauthorized { } ) ; } // Set vars let pool_address = POOLS . load ( deps . storage , & details . pool_name ) ? ; let denom = deposit_info . get_denom ( ) ? ; let lent_coin = deduct_tax ( deps . as_ref ( ) , Coin :: new ( details . asset . amount . u128 ( ) , denom . clone ( ) ) , ) ? ; let ask_denom = LUNA_DENOM . to_string ( ) ; let response : Response < TerraMsgWrapper > = Response :: new ( ) ; // Check if we have enough funds let balance = query_balance ( & deps . querier , env . contract . address . clone ( ) , denom ) ? ; if balance < details . asset . amount { return Err ( StableArbError :: Broke { } ) ; } // Simulate first tx with Terra Market Module // lent_coin already takes transfer tax into account. let expected_luna_received = query_market_price ( deps . as_ref ( ) , lent_coin . clone ( ) , ask_denom . clone ( ) ) ? ; // Construct offer for Terraswap let offer_coin = Coin { denom : ask_denom . clone ( ) , amount : expected_luna_received , } ; // Market swap msg, swap STABLE -> LUNA let swap_msg = create_swap_msg ( lent_coin . clone ( ) , ask_denom ) ; // Terraswap msg, swap LUNA -> STABLE let terraswap_msg = CosmosMsg :: Wasm ( WasmMsg :: Execute { contract_addr : pool_address . to_string ( ) , funds : vec ! [ offer_coin clone ( ) ] , msg : to_binary ( & create_terraswap_msg ( offer_coin , details . belief_price , Some ( details . slippage ) , ) ) ? , } ) ; let logs = vec ! [ ( " " String from ( " " ) ) ( " " lent_coin amount to_string ( ) ) ( " " expected_luna_received to_string ( ) ) ] ; // Create callback, this will send the funds back to the vault. let callback_msg = CallbackMsg :: AfterSuccessfulTradeCallback { } . to_cosmos_msg ( & env . contract . address ) ? ; Ok ( response . add_attributes ( logs ) . add_message ( swap_msg ) . add_message ( terraswap_msg ) . add_message ( callback_msg ) ) }
Attempt to perform an arbitrage operation with the assumption that
the currency to be arb'd is below peg.
Attempt to perform an arbitrage operation with the assumption that the currency to be arb ' d is below peg .
deps
DepsMut
env
Env
msg_info
MessageInfo
details
ArbDetails
deps
DepsMut
env
Env
msg_info
MessageInfo
details
ArbDetails