fn try_swap(
    deps: DepsMut,
    info: MessageInfo,
    amount: Uint128,
    denom: String,
    address: Option<String>,
) -> Result<Response<ProvenanceMsg>, ContractError> {
    // Ensure no funds were sent.
    if !info.funds.is_empty() {
        return Err(contract_err("no funds should be sent during reserve swap"));
    }

    // Ensure amount is non-zero.
    if amount.is_zero() {
        return Err(contract_err("invalid reserve amount"));
    }

    // Read state
    let state = config_read(deps.storage).load()?;

    // Ensure message sender is a member.
    if members_read(deps.storage)
        .may_load(info.sender.as_bytes())?
        .is_none()
    {
        return Err(ContractError::Unauthorized {});
    }

    // Ensure amount is non-zero and denom is supported by the consortium.
    if !get_members(deps.as_ref())?.iter().any(|m| m.denom == denom) {
        return Err(contract_err("invalid reserve denom"));
    }

    // Ensure member holds at least the indicated amount.
    let balance = deps.querier.query_balance(info.sender.clone(), &denom)?;
    if balance.amount < amount {
        return Err(contract_err("insufficient reserve amount in swap"));
    }

    // Transfer reserve token from member to marker. Note: This will eventually require an authz
    // call be made first to give the SC permission to move the tokens out of the member account.
    let querier = ProvenanceQuerier::new(&deps.querier);
    let mut res = Response::new()
        .add_message(transfer_marker_coins(
            amount.u128(),
            &denom,
            querier.get_marker_by_denom(&denom)?.address,
            info.sender.clone(),
        )?)
        .add_message(mint_marker_supply(amount.u128(), &state.dcc_denom)?); // Mint the required dcc.

    // Determine where to withdraw the dcc tokens.
    let mut withdraw_address = info.sender.clone();
    if let Some(a) = address {
        withdraw_address = deps.api.addr_validate(&a)?;
        if withdraw_address != info.sender {
            ensure_attributes(deps.as_ref(), withdraw_address.clone(), state.kyc_attrs)?;
        }
    }

    // Withdraw dcc from marker to sender.
    res = res
        .add_message(withdraw_coins(
            &state.dcc_denom,
            amount.u128(),
            &state.dcc_denom,
            withdraw_address.clone(),
        )?)
        .add_attribute("action", "swap") // Add wasm event attributes
        .add_attribute("member_id", info.sender)
        .add_attribute("amount", amount)
        .add_attribute("denom", &denom)
        .add_attribute("withdraw_address", withdraw_address);
    Ok(res)
}
// Swap member reserve tokens for dcc tokens.
Swap member reserve tokens for dcc tokens.
Swap member reserve tokens for dcc tokens .
fn try_swap(
    deps: DepsMut,
    info: MessageInfo,
    amount: Uint128,
    denom: String,
    address: Option<String>,
) -> Result<Response<ProvenanceMsg>, ContractError> {
    // Ensure no funds were sent.
    if !info.funds.is_empty() {
        return Err(contract_err("no funds should be sent during reserve swap"));
    }
    // Ensure amount is non-zero.
    if amount.is_zero() {
        return Err(contract_err("invalid reserve amount"));
    }
    // Read state
    let state = config_read(deps.storage).load()?;
    // Ensure message sender is a member.
    if members_read(deps.storage)
        .may_load(info.sender.as_bytes())?
        .is_none()
    {
        return Err(ContractError::Unauthorized {});
    }
    // Ensure amount is non-zero and denom is supported by the consortium.
    if !get_members(deps.as_ref())?.iter().any(|m| m.denom == denom) {
        return Err(contract_err("invalid reserve denom"));
    }
    // Ensure member holds at least the indicated amount.
    let balance = deps.querier.query_balance(info.sender.clone(), &denom)?;
    if balance.amount < amount {
        return Err(contract_err("insufficient reserve amount in swap"));
    }
    // Transfer reserve token from member to marker. Note: This will eventually require an authz
    // call be made first to give the SC permission to move the tokens out of the member account.
    let querier = ProvenanceQuerier::new(&deps.querier);
    let mut res = Response::new()
        .add_message(transfer_marker_coins(
            amount.u128(),
            &denom,
            querier.get_marker_by_denom(&denom)?.address,
            info.sender.clone(),
        )?)
        .add_message(mint_marker_supply(amount.u128(), &state.dcc_denom)?); // Mint the required dcc.
    // Determine where to withdraw the dcc tokens.
    let mut withdraw_address = info.sender.clone();
    if let Some(a) = address {
        withdraw_address = deps.api.addr_validate(&a)?;
        if withdraw_address != info.sender {
            ensure_attributes(deps.as_ref(), withdraw_address.clone(), state.kyc_attrs)?;
        }
    }
    // Withdraw dcc from marker to sender.
    res = res
        .add_message(withdraw_coins(
            &state.dcc_denom,
            amount.u128(),
            &state.dcc_denom,
            withdraw_address.clone(),
        )?)
        .add_attribute("action", "swap") // Add wasm event attributes
        .add_attribute("member_id", info.sender)
        .add_attribute("amount", amount)
        .add_attribute("denom", &denom)
        .add_attribute("withdraw_address", withdraw_address);
    Ok(res)
}
fn try_swap ( deps : DepsMut , info : MessageInfo , amount : Uint128 , denom : String , address : Option < String > , ) -> Result < Response < ProvenanceMsg > , ContractError > { // Ensure no funds were sent. if ! info . funds . is_empty ( ) { return Err ( contract_err ( " " ) ) ; } // Ensure amount is non-zero. if amount . is_zero ( ) { return Err ( contract_err ( " " ) ) ; } // Read state let state = config_read ( deps . storage ) . load ( ) ? ; // Ensure message sender is a member. if members_read ( deps . storage ) . may_load ( info . sender . as_bytes ( ) ) ? . is_none ( ) { return Err ( ContractError :: Unauthorized { } ) ; } // Ensure amount is non-zero and denom is supported by the consortium. if ! get_members ( deps . as_ref ( ) ) ? . iter ( ) . any ( | m | m . denom == denom ) { return Err ( contract_err ( " " ) ) ; } // Ensure member holds at least the indicated amount. let balance = deps . querier . query_balance ( info . sender . clone ( ) , & denom ) ? ; if balance . amount < amount { return Err ( contract_err ( " " ) ) ; } // Transfer reserve token from member to marker. Note: This will eventually require an authz // call be made first to give the SC permission to move the tokens out of the member account. let querier = ProvenanceQuerier :: new ( & deps . querier ) ; let mut res = Response :: new ( ) . add_message ( transfer_marker_coins ( amount . u128 ( ) , & denom , querier . get_marker_by_denom ( & denom ) ? . address , info . sender . clone ( ) , ) ? ) . add_message ( mint_marker_supply ( amount . u128 ( ) , & state . dcc_denom ) ? ) ; // Mint the required dcc. // Determine where to withdraw the dcc tokens. let mut withdraw_address = info . sender . clone ( ) ; if let Some ( a ) = address { withdraw_address = deps . api . addr_validate ( & a ) ? ; if withdraw_address != info . sender { ensure_attributes ( deps . as_ref ( ) , withdraw_address . clone ( ) , state . kyc_attrs ) ? ; } } // Withdraw dcc from marker to sender. res = res . add_message ( withdraw_coins ( & state . dcc_denom , amount . u128 ( ) , & state . dcc_denom , withdraw_address . clone ( ) , ) ? ) . add_attribute ( " " , " " ) // Add wasm event attributes . add_attribute ( " " , info . sender ) . add_attribute ( " " , amount ) . add_attribute ( " " , & denom ) . add_attribute ( " " , withdraw_address ) ; Ok ( res ) }
Swap member reserve tokens for dcc tokens.
Swap member reserve tokens for dcc tokens .
deps
DepsMut
info
MessageInfo
amount
Uint128
denom
String
address
Option<String>
deps
DepsMut
info
MessageInfo
amount
Uint128
denom
String
address
Option<String>