generated from multiversx/mx-template-sc
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from multiversx/game-sc
Game sc fixed clippy
- Loading branch information
Showing
16 changed files
with
960 additions
and
0 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
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,10 @@ | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
/target/ | ||
*/target/ | ||
|
||
# The mxpy output | ||
/output*/ | ||
|
||
# Mandos test trace | ||
trace*.scen.json |
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,15 @@ | ||
[package] | ||
name = "mvx-game-sc" | ||
version = "0.0.0" | ||
authors = [ "Mihai Calin Luca [email protected]",] | ||
edition = "2018" | ||
publish = false | ||
|
||
[lib] | ||
path = "src/lib.rs" | ||
|
||
[dependencies.multiversx-sc] | ||
version = "0.43.4" | ||
|
||
[dev-dependencies.multiversx-sc-scenario] | ||
version = "0.43.4" |
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,3 @@ | ||
# lib | ||
|
||
`lib` is a simple Smart Contract. |
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,14 @@ | ||
[package] | ||
name = "mvx-game-sc-meta" | ||
version = "0.0.0" | ||
edition = "2018" | ||
publish = false | ||
authors = [ "you",] | ||
|
||
[dev-dependencies] | ||
|
||
[dependencies.mvx-game-sc] | ||
path = ".." | ||
|
||
[dependencies.multiversx-sc-meta] | ||
version = "0.43.4" |
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,3 @@ | ||
fn main() { | ||
multiversx_sc_meta::cli_main::<mvx_game_sc::AbiProvider>(); | ||
} |
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,3 @@ | ||
{ | ||
"language": "rust" | ||
} |
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,87 @@ | ||
#![no_std] | ||
|
||
multiversx_sc::imports!(); | ||
multiversx_sc::derive_imports!(); | ||
|
||
pub mod owner; | ||
pub mod private; | ||
pub mod storage; | ||
pub mod types; | ||
|
||
#[multiversx_sc::contract] | ||
pub trait MvxGameSc: storage::StorageModule + owner::OwnerModule + private::PrivateModule { | ||
#[init] | ||
fn init( | ||
&self, | ||
enabled_opt: OptionalValue<bool>, | ||
game_start_fee_opt: OptionalValue<BigUint>, | ||
token_id_opt: OptionalValue<EgldOrEsdtTokenIdentifier>, | ||
) { | ||
match enabled_opt { | ||
OptionalValue::Some(_) => self.enabled().set(true), | ||
OptionalValue::None => {} | ||
} | ||
|
||
match game_start_fee_opt { | ||
OptionalValue::Some(val) => self.game_start_fee().set(val), | ||
OptionalValue::None => { | ||
require!(!self.game_start_fee().is_empty(), "game start fee not set") | ||
} | ||
} | ||
|
||
match token_id_opt { | ||
OptionalValue::Some(val) => self.token_id().set(val), | ||
OptionalValue::None => require!(!self.token_id().is_empty(), "fee token id not set"), | ||
} | ||
} | ||
|
||
#[payable("*")] | ||
#[endpoint(createGame)] | ||
fn create_game( | ||
&self, | ||
waiting_time: u64, | ||
number_of_players_min: u64, | ||
number_of_players_max: u64, | ||
wager: BigUint, | ||
) { | ||
self.require_enabled(); | ||
|
||
let (token_id, amount) = self.call_value().single_fungible_esdt(); | ||
self.validate_create_game_payment(&token_id, &amount, &wager, waiting_time); | ||
|
||
let (min, max) = self.get_min_max(number_of_players_min, number_of_players_max); | ||
|
||
let caller = self.blockchain().get_caller(); | ||
self.create_new_game(caller, waiting_time, min, max, wager); | ||
} | ||
|
||
#[payable("*")] | ||
#[endpoint(joinGame)] | ||
fn join_game(&self, game_id: u64) { | ||
self.require_enabled(); | ||
|
||
let (token_id, amount) = self.call_value().single_fungible_esdt(); | ||
let now = self.blockchain().get_block_timestamp(); | ||
let caller = self.blockchain().get_caller(); | ||
|
||
let game_settings = self.validate_join_game(&caller, now, &token_id, &amount, game_id); | ||
|
||
self.add_player(caller, game_id); | ||
|
||
self.refresh_game_status(game_id, game_settings); | ||
} | ||
|
||
//manually claim back wager if the game is invalid | ||
#[endpoint(claimBackWager)] | ||
fn claim_back_wager(&self, game_id: u64) { | ||
self.require_enabled(); | ||
|
||
let caller = self.blockchain().get_caller(); | ||
let wager = self.validate_claim_wager(&caller, game_id); | ||
|
||
let token_id = self.token_id().get(); | ||
self.send().direct(&caller, &token_id, 0u64, &wager); | ||
|
||
self.remove_player(caller, game_id); | ||
} | ||
} |
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,75 @@ | ||
use crate::types::Status; | ||
|
||
multiversx_sc::imports!(); | ||
multiversx_sc::derive_imports!(); | ||
|
||
#[multiversx_sc::module] | ||
pub trait OwnerModule: crate::private::PrivateModule + crate::storage::StorageModule { | ||
//u64 is percentage * 100 | ||
//function called by the owner when the winners have been decided | ||
#[only_owner] | ||
#[endpoint(sendReward)] | ||
fn send_reward( | ||
&self, | ||
game_id: u64, | ||
winners: OptionalValue<MultiValueEncoded<(ManagedAddress, u64)>>, | ||
) { | ||
self.require_enabled(); | ||
|
||
let game_settings = self.validate_send_reward(game_id); | ||
let token_id = self.token_id().get(); | ||
|
||
match game_settings.status { | ||
Status::Invalid => { | ||
self.send_back_wager(game_id, &game_settings.wager, &token_id); | ||
|
||
let game_creation_fee = self.game_start_fee().get(); | ||
self.send() | ||
.direct(&game_settings.creator, &token_id, 0u64, &game_creation_fee); | ||
} | ||
Status::Valid => { | ||
match winners { | ||
OptionalValue::Some(val) => { | ||
let len = self.players(game_id).len(); | ||
let total_wager = &BigUint::from(len) * &game_settings.wager; | ||
|
||
for (winner, percentage) in val.into_iter() { | ||
let reward_per_winner = | ||
&BigUint::from(percentage) * &total_wager / &BigUint::from(100u64); | ||
self.send() | ||
.direct(&winner, &token_id, 0u64, &reward_per_winner); | ||
} | ||
} | ||
//tie/draw | ||
OptionalValue::None => { | ||
self.send_back_wager(game_id, &game_settings.wager, &token_id); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[only_owner] | ||
#[endpoint(enableSC)] | ||
fn enable_sc(&self) { | ||
self.enabled().set(true) | ||
} | ||
|
||
#[only_owner] | ||
#[endpoint(disableSC)] | ||
fn disable_sc(&self) { | ||
self.enabled().clear() | ||
} | ||
|
||
#[only_owner] | ||
#[endpoint(setTokenId)] | ||
fn set_token_id(&self, token_id: EgldOrEsdtTokenIdentifier) { | ||
self.token_id().set(token_id) | ||
} | ||
|
||
#[only_owner] | ||
#[endpoint(setGameStartFee)] | ||
fn set_game_start_fee(&self, amount: BigUint) { | ||
self.game_start_fee().set(amount) | ||
} | ||
} |
Oops, something went wrong.