pub fn execute_sponsor(
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    award: Option<bool>,
    prize_distribution: Option<[Decimal256; NUM_PRIZE_BUCKETS]>,
) -> Result<Response, ContractError> {
    let config = CONFIG.load(deps.storage)?;
    let mut state = STATE.load(deps.storage)?;
    let mut pool = POOL.load(deps.storage)?;

    // get the amount of funds sent in the base stable denom
    let sponsor_amount = info
        .funds
        .iter()
        .find(|c| c.denom == config.stable_denom)
        .map(|c| Uint256::from(c.amount))
        .unwrap_or_else(Uint256::zero);

    // validate that the sponsor amount is non zero
    if sponsor_amount.is_zero() {
        return Err(ContractError::ZeroSponsorshipAmount {});
    }

    compute_global_sponsor_reward(&mut state, &pool, env.block.height);

    let mut msgs: Vec<CosmosMsg> = vec![];

    if let None | Some(false) = award {
        // Can't specify prize distribution in this case
        if prize_distribution.is_some() {
            return Err(ContractError::InvalidPrizeDistribution {});
        }

        // Deduct taxes that will be payed when transferring to anchor
        let net_sponsor_amount = Uint256::from(
            deduct_tax(
                deps.as_ref(),
                coin(sponsor_amount.into(), config.stable_denom.clone()),
            )?
            .amount,
        );

        // query exchange_rate from anchor money market
        let epoch_state: EpochStateResponse = query_exchange_rate(
            deps.as_ref(),
            config.anchor_contract.to_string(),
            env.block.height,
        )?;

        // add amount of aUST entitled from the deposit
        let minted_aust = net_sponsor_amount / epoch_state.exchange_rate;

        // Get minted_aust_value
        let minted_aust_value = minted_aust * epoch_state.exchange_rate;

        // fetch sponsor_info
        let mut sponsor_info: SponsorInfo = read_sponsor_info(deps.storage, &info.sender);

        // update sponsor sponsor rewards
        compute_sponsor_reward(&state, &mut sponsor_info);

        // add sponsor_amount to depositor
        sponsor_info.lottery_deposit = sponsor_info.lottery_deposit.add(minted_aust_value);
        store_sponsor_info(deps.storage, &info.sender, sponsor_info)?;

        // update pool
        pool.total_sponsor_lottery_deposits =
            pool.total_sponsor_lottery_deposits.add(minted_aust_value);

        // Push message to deposit stable coins into anchor
        msgs.push(CosmosMsg::Wasm(WasmMsg::Execute {
            contract_addr: config.anchor_contract.to_string(),
            funds: vec![Coin {
                denom: config.stable_denom,
                amount: net_sponsor_amount.into(),
            }],
            msg: to_binary(&AnchorMsg::DepositStable {})?,
        }));
    } else {
        // Award is instant

        // Get the prize_distribution or the prize_distribution in the config
        let prize_distribution = prize_distribution.unwrap_or(config.prize_distribution);

        // Validate that the prize_distribution is of length NUM_PRIZE_BUCKETS
        if prize_distribution.len() != NUM_PRIZE_BUCKETS {
            return Err(ContractError::InvalidPrizeDistribution {});
        }

        // Validate that the prize_distributions sums to 1
        let mut sum = Decimal256::zero();
        for item in prize_distribution.iter() {
            sum += *item;
        }

        if sum != Decimal256::one() {
            return Err(ContractError::InvalidPrizeDistribution {});
        }

        // Distribute the sponsorship to the prize buckets according to the prize distribution
        for (index, fraction_of_prize) in prize_distribution.iter().enumerate() {
            // Add the proportional amount of the net redeemed amount to the relevant award bucket.
            state.prize_buckets[index] += sponsor_amount * *fraction_of_prize
        }
    }

    STATE.save(deps.storage, &state)?;
    POOL.save(deps.storage, &pool)?;

    Ok(Response::new().add_messages(msgs).add_attributes(vec![
        attr("action", "sponsorship"),
        attr("sponsor", info.sender.to_string()),
        attr("sponsorship_amount", sponsor_amount),
    ]))
}
// Make a donation deposit to the lottery pool
Make a donation deposit to the lottery pool
Make a donation deposit to the lottery pool
pub fn execute_sponsor(
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    award: Option<bool>,
    prize_distribution: Option<[Decimal256; NUM_PRIZE_BUCKETS]>,
) -> Result<Response, ContractError> {
    let config = CONFIG.load(deps.storage)?;
    let mut state = STATE.load(deps.storage)?;
    let mut pool = POOL.load(deps.storage)?;
    // get the amount of funds sent in the base stable denom
    let sponsor_amount = info
        .funds
        .iter()
        .find(|c| c.denom == config.stable_denom)
        .map(|c| Uint256::from(c.amount))
        .unwrap_or_else(Uint256::zero);
    // validate that the sponsor amount is non zero
    if sponsor_amount.is_zero() {
        return Err(ContractError::ZeroSponsorshipAmount {});
    }
    compute_global_sponsor_reward(&mut state, &pool, env.block.height);
    let mut msgs: Vec<CosmosMsg> = vec![];
    if let None | Some(false) = award {
        // Can't specify prize distribution in this case
        if prize_distribution.is_some() {
            return Err(ContractError::InvalidPrizeDistribution {});
        }
        // Deduct taxes that will be payed when transferring to anchor
        let net_sponsor_amount = Uint256::from(
            deduct_tax(
                deps.as_ref(),
                coin(sponsor_amount.into(), config.stable_denom.clone()),
            )?
            .amount,
        );
        // query exchange_rate from anchor money market
        let epoch_state: EpochStateResponse = query_exchange_rate(
            deps.as_ref(),
            config.anchor_contract.to_string(),
            env.block.height,
        )?;
        // add amount of aUST entitled from the deposit
        let minted_aust = net_sponsor_amount / epoch_state.exchange_rate;
        // Get minted_aust_value
        let minted_aust_value = minted_aust * epoch_state.exchange_rate;
        // fetch sponsor_info
        let mut sponsor_info: SponsorInfo = read_sponsor_info(deps.storage, &info.sender);
        // update sponsor sponsor rewards
        compute_sponsor_reward(&state, &mut sponsor_info);
        // add sponsor_amount to depositor
        sponsor_info.lottery_deposit = sponsor_info.lottery_deposit.add(minted_aust_value);
        store_sponsor_info(deps.storage, &info.sender, sponsor_info)?;
        // update pool
        pool.total_sponsor_lottery_deposits =
            pool.total_sponsor_lottery_deposits.add(minted_aust_value);
        // Push message to deposit stable coins into anchor
        msgs.push(CosmosMsg::Wasm(WasmMsg::Execute {
            contract_addr: config.anchor_contract.to_string(),
            funds: vec![Coin {
                denom: config.stable_denom,
                amount: net_sponsor_amount.into(),
            }],
            msg: to_binary(&AnchorMsg::DepositStable {})?,
        }));
    } else {
        // Award is instant
        // Get the prize_distribution or the prize_distribution in the config
        let prize_distribution = prize_distribution.unwrap_or(config.prize_distribution);
        // Validate that the prize_distribution is of length NUM_PRIZE_BUCKETS
        if prize_distribution.len() != NUM_PRIZE_BUCKETS {
            return Err(ContractError::InvalidPrizeDistribution {});
        }
        // Validate that the prize_distributions sums to 1
        let mut sum = Decimal256::zero();
        for item in prize_distribution.iter() {
            sum += *item;
        }
        if sum != Decimal256::one() {
            return Err(ContractError::InvalidPrizeDistribution {});
        }
        // Distribute the sponsorship to the prize buckets according to the prize distribution
        for (index, fraction_of_prize) in prize_distribution.iter().enumerate() {
            // Add the proportional amount of the net redeemed amount to the relevant award bucket.
            state.prize_buckets[index] += sponsor_amount * *fraction_of_prize
        }
    }
    STATE.save(deps.storage, &state)?;
    POOL.save(deps.storage, &pool)?;
    Ok(Response::new().add_messages(msgs).add_attributes(vec![
        attr("action", "sponsorship"),
        attr("sponsor", info.sender.to_string()),
        attr("sponsorship_amount", sponsor_amount),
    ]))
}
pub fn execute_sponsor ( deps : DepsMut , env : Env , info : MessageInfo , award : Option < bool > , prize_distribution : Option < [ Decimal256 ; NUM_PRIZE_BUCKETS ] > , ) -> Result < Response , ContractError > { let config = CONFIG . load ( deps . storage ) ? ; let mut state = STATE . load ( deps . storage ) ? ; let mut pool = POOL . load ( deps . storage ) ? ; // get the amount of funds sent in the base stable denom let sponsor_amount = info . funds . iter ( ) . find ( | c | c . denom == config . stable_denom ) . map ( | c | Uint256 :: from ( c . amount ) ) . unwrap_or_else ( Uint256 :: zero ) ; // validate that the sponsor amount is non zero if sponsor_amount . is_zero ( ) { return Err ( ContractError :: ZeroSponsorshipAmount { } ) ; } compute_global_sponsor_reward ( & mut state , & pool , env . block . height ) ; let mut msgs : Vec < CosmosMsg > = vec ! [ ] ; if let None | Some ( false ) = award { // Can't specify prize distribution in this case if prize_distribution . is_some ( ) { return Err ( ContractError :: InvalidPrizeDistribution { } ) ; } // Deduct taxes that will be payed when transferring to anchor let net_sponsor_amount = Uint256 :: from ( deduct_tax ( deps . as_ref ( ) , coin ( sponsor_amount . into ( ) , config . stable_denom . clone ( ) ) , ) ? . amount , ) ; // query exchange_rate from anchor money market let epoch_state : EpochStateResponse = query_exchange_rate ( deps . as_ref ( ) , config . anchor_contract . to_string ( ) , env . block . height , ) ? ; // add amount of aUST entitled from the deposit let minted_aust = net_sponsor_amount / epoch_state . exchange_rate ; // Get minted_aust_value let minted_aust_value = minted_aust * epoch_state . exchange_rate ; // fetch sponsor_info let mut sponsor_info : SponsorInfo = read_sponsor_info ( deps . storage , & info . sender ) ; // update sponsor sponsor rewards compute_sponsor_reward ( & state , & mut sponsor_info ) ; // add sponsor_amount to depositor sponsor_info . lottery_deposit = sponsor_info . lottery_deposit . add ( minted_aust_value ) ; store_sponsor_info ( deps . storage , & info . sender , sponsor_info ) ? ; // update pool pool . total_sponsor_lottery_deposits = pool . total_sponsor_lottery_deposits . add ( minted_aust_value ) ; // Push message to deposit stable coins into anchor msgs . push ( CosmosMsg :: Wasm ( WasmMsg :: Execute { contract_addr : config . anchor_contract . to_string ( ) , funds : vec ! [ Coin { denom config stable_denom amount net_sponsor_amount into ( ) } ] , msg : to_binary ( & AnchorMsg :: DepositStable { } ) ? , } ) ) ; } else { // Award is instant // Get the prize_distribution or the prize_distribution in the config let prize_distribution = prize_distribution . unwrap_or ( config . prize_distribution ) ; // Validate that the prize_distribution is of length NUM_PRIZE_BUCKETS if prize_distribution . len ( ) != NUM_PRIZE_BUCKETS { return Err ( ContractError :: InvalidPrizeDistribution { } ) ; } // Validate that the prize_distributions sums to 1 let mut sum = Decimal256 :: zero ( ) ; for item in prize_distribution . iter ( ) { sum += * item ; } if sum != Decimal256 :: one ( ) { return Err ( ContractError :: InvalidPrizeDistribution { } ) ; } // Distribute the sponsorship to the prize buckets according to the prize distribution for ( index , fraction_of_prize ) in prize_distribution . iter ( ) . enumerate ( ) { // Add the proportional amount of the net redeemed amount to the relevant award bucket. state . prize_buckets [ index ] += sponsor_amount * * fraction_of_prize } } STATE . save ( deps . storage , & state ) ? ; POOL . save ( deps . storage , & pool ) ? ; Ok ( Response :: new ( ) . add_messages ( msgs ) . add_attributes ( vec ! [ attr ( " " " " ) attr ( " " info sender to_string ( ) ) attr ( " " sponsor_amount ) ] ) ) }
Make a donation deposit to the lottery pool
Make a donation deposit to the lottery pool
deps
DepsMut
env
Env
info
MessageInfo
award
Option<bool>
prize_distribution
Option<[Decimal256; NUM_PRIZE_BUCKETS]>
deps
DepsMut
env
Env
info
MessageInfo
award
Option<bool>
prize_distribution
Option<[Decimal256; NUM_PRIZE_BUCKETS]>