fn try_purchase(
    deps: DepsMut<ProvenanceQuery>,
    env: Env,
    info: MessageInfo,
    id: String,
) -> Result<Response<ProvenanceMsg>, ContractError> {
    // Ensure funds were sent with the message
    if info.funds.is_empty() {
        let err = "no purchase funds sent";
        return Err(ContractError::Std(StdError::generic_err(err)));
    }

    // Load state
    let state = config_read(deps.storage).load()?;
    let fee_pct = state.fee_percent;

    // Ensure the funds have the required amount and denomination
    for funds in info.funds.iter() {
        if funds.amount.is_zero() || funds.denom != state.purchase_denom {
            let err = format!("invalid purchase funds: {}{}", funds.amount, funds.denom);
            return Err(ContractError::Std(StdError::generic_err(err)));
        }
    }

    // Calculate amounts and create bank transfers to the merchant account
    let transfers = CosmosMsg::Bank(BankMsg::Send {
        to_address: state.merchant_address.to_string(),
        amount: info
            .funds
            .iter()
            .map(|sent| {
                let fees = sent.amount.mul(fee_pct).u128();
                coin(sent.amount.u128() - fees, sent.denom.clone())
            })
            .collect(),
    });

    // Calculate fees and create bank transfers to the fee collection account
    let fees = CosmosMsg::Bank(BankMsg::Send {
        to_address: state.fee_collection_address.to_string(),
        amount: info
            .funds
            .iter()
            .map(|sent| coin(sent.amount.mul(fee_pct).u128(), sent.denom.clone()))
            .collect(),
    });

    // Return a response that will dispatch the transfers to the bank module and emit events.
    Ok(Response::new()
        .add_message(transfers)
        .add_message(fees)
        .add_attribute("action", "purchase")
        .add_attribute("purchase_id", id)
        .add_attribute("purchase_time", env.block.time.to_string()))
}
// Calculates transfers and fees, then dispatches messages to the bank module.
Calculates transfers and fees, then dispatches messages to the bank module.
Calculates transfers and fees then dispatches messages to the bank module .
fn try_purchase(
    deps: DepsMut<ProvenanceQuery>,
    env: Env,
    info: MessageInfo,
    id: String,
) -> Result<Response<ProvenanceMsg>, ContractError> {
    // Ensure funds were sent with the message
    if info.funds.is_empty() {
        let err = "no purchase funds sent";
        return Err(ContractError::Std(StdError::generic_err(err)));
    }
    // Load state
    let state = config_read(deps.storage).load()?;
    let fee_pct = state.fee_percent;
    // Ensure the funds have the required amount and denomination
    for funds in info.funds.iter() {
        if funds.amount.is_zero() || funds.denom != state.purchase_denom {
            let err = format!("invalid purchase funds: {}{}", funds.amount, funds.denom);
            return Err(ContractError::Std(StdError::generic_err(err)));
        }
    }
    // Calculate amounts and create bank transfers to the merchant account
    let transfers = CosmosMsg::Bank(BankMsg::Send {
        to_address: state.merchant_address.to_string(),
        amount: info
            .funds
            .iter()
            .map(|sent| {
                let fees = sent.amount.mul(fee_pct).u128();
                coin(sent.amount.u128() - fees, sent.denom.clone())
            })
            .collect(),
    });
    // Calculate fees and create bank transfers to the fee collection account
    let fees = CosmosMsg::Bank(BankMsg::Send {
        to_address: state.fee_collection_address.to_string(),
        amount: info
            .funds
            .iter()
            .map(|sent| coin(sent.amount.mul(fee_pct).u128(), sent.denom.clone()))
            .collect(),
    });
    // Return a response that will dispatch the transfers to the bank module and emit events.
    Ok(Response::new()
        .add_message(transfers)
        .add_message(fees)
        .add_attribute("action", "purchase")
        .add_attribute("purchase_id", id)
        .add_attribute("purchase_time", env.block.time.to_string()))
}
fn try_purchase ( deps : DepsMut < ProvenanceQuery > , env : Env , info : MessageInfo , id : String , ) -> Result < Response < ProvenanceMsg > , ContractError > { // Ensure funds were sent with the message if info . funds . is_empty ( ) { let err = " " ; return Err ( ContractError :: Std ( StdError :: generic_err ( err ) ) ) ; } // Load state let state = config_read ( deps . storage ) . load ( ) ? ; let fee_pct = state . fee_percent ; // Ensure the funds have the required amount and denomination for funds in info . funds . iter ( ) { if funds . amount . is_zero ( ) || funds . denom != state . purchase_denom { let err = format ! ( " " funds amount funds denom ) ; return Err ( ContractError :: Std ( StdError :: generic_err ( err ) ) ) ; } } // Calculate amounts and create bank transfers to the merchant account let transfers = CosmosMsg :: Bank ( BankMsg :: Send { to_address : state . merchant_address . to_string ( ) , amount : info . funds . iter ( ) . map ( | sent | { let fees = sent . amount . mul ( fee_pct ) . u128 ( ) ; coin ( sent . amount . u128 ( ) - fees , sent . denom . clone ( ) ) } ) . collect ( ) , } ) ; // Calculate fees and create bank transfers to the fee collection account let fees = CosmosMsg :: Bank ( BankMsg :: Send { to_address : state . fee_collection_address . to_string ( ) , amount : info . funds . iter ( ) . map ( | sent | coin ( sent . amount . mul ( fee_pct ) . u128 ( ) , sent . denom . clone ( ) ) ) . collect ( ) , } ) ; // Return a response that will dispatch the transfers to the bank module and emit events. Ok ( Response :: new ( ) . add_message ( transfers ) . add_message ( fees ) . add_attribute ( " " , " " ) . add_attribute ( " " , id ) . add_attribute ( " " , env . block . time . to_string ( ) ) ) }
Calculates transfers and fees, then dispatches messages to the bank module.
Calculates transfers and fees then dispatches messages to the bank module .
deps
DepsMut<ProvenanceQuery>
env
Env
info
MessageInfo
id
String
deps
DepsMut<ProvenanceQuery>
env
Env
info
MessageInfo
id
String