Skip to content

Commit

Permalink
reorgazing
Browse files Browse the repository at this point in the history
  • Loading branch information
alyn509 committed Dec 16, 2024
1 parent 00b5a4e commit 3da60f8
Show file tree
Hide file tree
Showing 15 changed files with 625 additions and 560 deletions.
4 changes: 4 additions & 0 deletions contracts/examples/lottery-esdt/src/basics/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub const PERCENTAGE_TOTAL: u32 = 100;
pub const THIRTY_DAYS_IN_SECONDS: u64 = 60 * 60 * 24 * 30;
pub const MAX_TICKETS: usize = 800;
pub const MAX_OPERATIONS: usize = 50;
4 changes: 4 additions & 0 deletions contracts/examples/lottery-esdt/src/basics/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod constants;
pub mod storage;
pub mod utils;
pub mod views;
42 changes: 42 additions & 0 deletions contracts/examples/lottery-esdt/src/basics/storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use multiversx_sc::imports::*;

#[multiversx_sc::module]
pub trait StorageModule {
#[storage_mapper("ticketHolder")]
fn ticket_holders(&self, lottery_name: &ManagedBuffer) -> VecMapper<u64>;

#[storage_mapper("accumulatedRewards")]
fn accumulated_rewards(
&self,
token_id: &EgldOrEsdtTokenIdentifier,
user_id: &u64,
) -> SingleValueMapper<BigUint>;

#[storage_mapper("totalWinning_tickets")]
fn total_winning_tickets(&self, lottery_name: &ManagedBuffer) -> SingleValueMapper<usize>;

#[storage_mapper("indexLastWinner")]
fn index_last_winner(&self, lottery_name: &ManagedBuffer) -> SingleValueMapper<usize>;

#[storage_mapper("accumulatedRewards")]
fn user_accumulated_token_rewards(
&self,
user_id: &u64,
) -> UnorderedSetMapper<EgldOrEsdtTokenIdentifier>;

#[storage_mapper("numberOfEntriesForUser")]
fn number_of_entries_for_user(
&self,
lottery_name: &ManagedBuffer,
user_id: &u64,
) -> SingleValueMapper<usize>;

#[storage_mapper("addressToIdMapper")]
fn addres_to_id_mapper(&self) -> AddressToIdMapper;

#[storage_mapper("burnPercentageForLottery")]
fn burn_percentage_for_lottery(
&self,
lottery_name: &ManagedBuffer,
) -> SingleValueMapper<BigUint>;
}
25 changes: 25 additions & 0 deletions contracts/examples/lottery-esdt/src/basics/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use multiversx_sc::imports::*;

use crate::constants::PERCENTAGE_TOTAL;

#[multiversx_sc::module]
pub trait UtilsModule {
fn sum_array(&self, array: &ManagedVec<u8>) -> u32 {
let mut sum = 0;

for item in array {
sum += item as u32;
}

sum
}

/// does not check if max - min >= amount, that is the caller's job
fn get_distinct_random(&self, min: usize, max: usize) -> usize {
let mut rand = RandomnessSource::new();
rand.next_usize_in_range(min, max)
}
fn calculate_percentage_of(&self, value: &BigUint, percentage: &BigUint) -> BigUint {
value * percentage / PERCENTAGE_TOTAL
}
}
31 changes: 31 additions & 0 deletions contracts/examples/lottery-esdt/src/basics/views.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::{LotteryInfo, Status};
use multiversx_sc::imports::*;

#[multiversx_sc::module]
pub trait ViewsModule {
#[view]
fn status(&self, lottery_name: &ManagedBuffer) -> Status {
if self.lottery_info(lottery_name).is_empty() {
return Status::Inactive;
}

let info = self.lottery_info(lottery_name).get();
let current_time = self.blockchain().get_block_timestamp();
if current_time > info.deadline || info.tickets_left == 0 {
return Status::Ended;
}

Status::Running
}

#[view(getLotteryInfo)]
#[storage_mapper("lotteryInfo")]
fn lottery_info(
&self,
lottery_name: &ManagedBuffer,
) -> SingleValueMapper<LotteryInfo<Self::Api>>;

#[view(getLotteryWhitelist)]
#[storage_mapper("lotteryWhitelist")]
fn lottery_whitelist(&self, lottery_name: &ManagedBuffer) -> UnorderedSetMapper<u64>;
}
Loading

0 comments on commit 3da60f8

Please sign in to comment.