-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
625 additions
and
560 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
Oops, something went wrong.