Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support golden tokens and bloberts for free games #7

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions contracts/dojo_mainnet.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ world_address = "0x02b127646258e21186e6c7e6234f42583d0d19bf88a57776a404c2cefeb42

[init_call_args]
"tournament-LSTournament" = [
"0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
"0x0124aeb495b947201f5fac96fd1138e326ad86195b98df6dec9009158a533b49",
"0x018108b32cea514a78ef1b0e4a0753e855cdf620bc0565202c02456f618c4dc4",
"0x2a85bd616f912537c50a49a4076db02c00b29b2cdc8a197ce92ed1837fa875b",
"1", # safe_mode
"1" # test_mode
]
Expand Down
27 changes: 15 additions & 12 deletions contracts/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
mod ls15_components {
pub mod constants;
mod interfaces;
// mod loot_survivor;
mod loot_survivor;
mod libs {
pub mod store;
pub mod utils;
}
pub mod models {
// pub mod loot_survivor;
pub mod loot_survivor;
pub mod tournament;
}
pub mod tournament;
mod tests {
// pub mod eth_mock;
// pub mod lords_mock;
// pub mod erc20_mock;
// pub mod erc721_mock;
// #[cfg(test)]
// mod helpers;
// pub mod loot_survivor_mock;
// pub mod pragma_mock;
// pub mod tournament_mock;
pub mod libs {
pub mod store;
}
pub mod eth_mock;
pub mod lords_mock;
pub mod erc20_mock;
pub mod erc721_mock;
#[cfg(test)]
mod helpers;
pub mod loot_survivor_mock;
pub mod pragma_mock;
pub mod tournament_mock;
#[cfg(test)]
mod test_tournament;
// pub mod interfaces;
pub mod interfaces;
// #[cfg(test)]
// mod test_tournament_stress_tests;
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/ls15_components/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub const MIN_REGISTRATION_PERIOD: u32 = 300; // 5 minutes
pub const MAX_REGISTRATION_PERIOD: u32 = 2592000; // 1 month
pub const MIN_TOURNAMENT_LENGTH: u32 = 3600; // 1 hour
pub const MAX_TOURNAMENT_LENGTH: u32 = 15552000; // 6 months
pub const MIN_SUBMISSION_PERIOD: u32 = 1800; // 30 mins
pub const MIN_SUBMISSION_PERIOD: u32 = 86400; // 1 day
pub const MAX_SUBMISSION_PERIOD: u32 = 1209600; // 2 weeks
pub const GAME_EXPIRATION_PERIOD: u32 = 864000; // 10 days

Expand Down
2 changes: 2 additions & 0 deletions contracts/src/ls15_components/interfaces.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use starknet::ContractAddress;
use adventurer::{adventurer::Adventurer, adventurer_meta::AdventurerMetadata, bag::Bag};
use dojo::world::{WorldStorage, WorldStorageTrait, IWorldDispatcher};
use tournament::presets::ls_tournament::{ILSTournamentDispatcher};
use tournament::ls15_components::models::tournament::FreeGameTokenType;

use tournament::ls15_components::libs::utils::ZERO;

Expand Down Expand Up @@ -34,6 +35,7 @@ pub trait ILootSurvivor<TState> {
fn get_adventurer_meta(self: @TState, adventurer_id: felt252) -> AdventurerMetadata;
fn get_bag(self: @TState, adventurer_id: felt252) -> Bag;
fn get_cost_to_play(self: @TState) -> u128;
fn free_game_available(self: @TState, token_type: FreeGameTokenType, token_id: u128) -> bool;
fn new_game(
ref self: TState,
client_reward_address: ContractAddress,
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/ls15_components/libs/store.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub impl StoreImpl of StoreTrait {
}
// Setters
//

// Tournament

#[inline(always)]
Expand Down
75 changes: 62 additions & 13 deletions contracts/src/ls15_components/loot_survivor.cairo
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
use starknet::ContractAddress;
use tournament::ls15_components::models::loot_survivor::{Adventurer, AdventurerMetadata, Bag};
use tournament::ls15_components::models::loot_survivor::{
Adventurer, AdventurerMetadataStorage, Bag
};
use tournament::ls15_components::models::tournament::FreeGameTokenType;
use adventurer::{adventurer_meta::AdventurerMetadata};

#[starknet::interface]
trait ILootSurvivor<TState> {
fn get_adventurer(self: @TState, adventurer_id: felt252) -> Adventurer;
fn get_adventurer_meta(self: @TState, adventurer_id: felt252) -> AdventurerMetadata;
fn get_bag(self: @TState, adventurer_id: felt252) -> Bag;
fn get_cost_to_play(self: @TState) -> u128;
fn free_game_available(
self: @TState, free_game_type: FreeGameTokenType, token_id: u128
) -> bool;
fn new_game(
ref self: TState,
client_reward_address: ContractAddress,
Expand All @@ -20,9 +27,10 @@ trait ILootSurvivor<TState> {
) -> felt252;
fn set_adventurer(ref self: TState, adventurer_id: felt252, adventurer: Adventurer);
fn set_adventurer_meta(
ref self: TState, adventurer_id: felt252, adventurer_meta: AdventurerMetadata
ref self: TState, adventurer_id: felt252, adventurer_meta: AdventurerMetadataStorage
);
fn set_bag(ref self: TState, adventurer_id: felt252, bag: Bag);
fn set_free_game_available(self: @TState, free_game_type: FreeGameTokenType, token_id: u128);
}

///
Expand All @@ -37,12 +45,15 @@ pub mod loot_survivor_component {
use starknet::{ContractAddress, get_block_timestamp, get_caller_address, get_contract_address};
use dojo::contract::components::world_provider::{IWorldProvider};

use adventurer::{adventurer_meta::AdventurerMetadata};

use tournament::ls15_components::models::loot_survivor::{
Adventurer, AdventurerMetadata, Bag, Stats, Equipment, Item, AdventurerModel,
AdventurerMetaModel, BagModel, GameCountModel, Contracts
Adventurer, AdventurerMetadataStorage, Bag, Stats, Equipment, Item, AdventurerModel,
AdventurerMetaModel, BagModel, GameCountModel, FreeGameAvailableModel, Contracts
};
use tournament::ls15_components::models::tournament::FreeGameTokenType;
use tournament::ls15_components::interfaces::{WorldTrait, WorldImpl};
use tournament::ls15_components::libs::store::{Store, StoreTrait};
use tournament::ls15_components::tests::libs::store::{Store, StoreTrait};
use tournament::ls15_components::libs::utils::{pow};

use openzeppelin_introspection::src5::SRC5Component;
Expand Down Expand Up @@ -94,7 +105,18 @@ pub mod loot_survivor_component {
self.get_contract().world_dispatcher(), @"tournament"
);
let mut store: Store = StoreTrait::new(world);
store.get_adventurer_meta_model(adventurer_id).adventurer_meta
let adventurer_meta = store.get_adventurer_meta_model(adventurer_id).adventurer_meta;
let formatted_adventurer_meta = AdventurerMetadata {
birth_date: adventurer_meta.birth_date,
death_date: adventurer_meta.death_date,
level_seed: adventurer_meta.level_seed,
item_specials_seed: adventurer_meta.item_specials_seed,
rank_at_death: adventurer_meta.rank_at_death,
delay_stat_reveal: adventurer_meta.delay_stat_reveal,
golden_token_id: adventurer_meta.golden_token_id,
launch_tournament_winner_token_id: 0,
};
formatted_adventurer_meta
}

fn get_bag(self: @ComponentState<TContractState>, adventurer_id: felt252) -> Bag {
Expand All @@ -109,6 +131,16 @@ pub mod loot_survivor_component {
50000000000000000000
}

fn free_game_available(
self: @ComponentState<TContractState>, free_game_type: FreeGameTokenType, token_id: u128
) -> bool {
let mut world = WorldTrait::storage(
self.get_contract().world_dispatcher(), @"tournament"
);
let mut store: Store = StoreTrait::new(world);
store.get_free_game_available_model(free_game_type, token_id).available
}

fn new_game(
ref self: ComponentState<TContractState>,
client_reward_address: ContractAddress,
Expand All @@ -127,11 +159,15 @@ pub mod loot_survivor_component {
let contracts = store.get_contracts_model(get_contract_address());
let cost_to_play = self.get_cost_to_play();
// transfer base game cost
let lords_dispatcher: IERC20Dispatcher = IERC20Dispatcher {
contract_address: contracts.lords
};
lords_dispatcher
.transfer_from(get_caller_address(), get_contract_address(), cost_to_play.into());
if (golden_token_id.is_zero() && launch_tournament_winner_token_id.is_zero()) {
let lords_dispatcher: IERC20Dispatcher = IERC20Dispatcher {
contract_address: contracts.lords
};
lords_dispatcher
.transfer_from(
get_caller_address(), get_contract_address(), cost_to_play.into()
);
}

// transfer VRF cost
let eth_dispatcher: IERC20Dispatcher = IERC20Dispatcher {
Expand Down Expand Up @@ -187,7 +223,7 @@ pub mod loot_survivor_component {
@AdventurerModel { adventurer_id: adventurer_id.into(), adventurer }
);

let adventurer_meta = AdventurerMetadata {
let adventurer_meta = AdventurerMetadataStorage {
birth_date: get_block_timestamp().into(),
death_date: 0,
level_seed: 0,
Expand Down Expand Up @@ -222,7 +258,7 @@ pub mod loot_survivor_component {
fn set_adventurer_meta(
ref self: ComponentState<TContractState>,
adventurer_id: felt252,
adventurer_meta: AdventurerMetadata
adventurer_meta: AdventurerMetadataStorage
) {
let mut world = WorldTrait::storage(
self.get_contract().world_dispatcher(), @"tournament"
Expand All @@ -239,6 +275,19 @@ pub mod loot_survivor_component {
let mut store: Store = StoreTrait::new(world);
store.set_bag_model(@BagModel { adventurer_id, bag });
}

fn set_free_game_available(
self: @ComponentState<TContractState>, free_game_type: FreeGameTokenType, token_id: u128
) {
let mut world = WorldTrait::storage(
self.get_contract().world_dispatcher(), @"tournament"
);
let mut store: Store = StoreTrait::new(world);
store
.set_free_game_available_model(
@FreeGameAvailableModel { free_game_type, token_id, available: true }
);
}
}

#[generate_trait]
Expand Down
17 changes: 14 additions & 3 deletions contracts/src/ls15_components/models/loot_survivor.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use starknet::ContractAddress;
use tournament::ls15_components::models::tournament::FreeGameTokenType;

// dojo compatible structs

Expand Down Expand Up @@ -41,15 +42,15 @@ pub struct Adventurer {
}

#[derive(Drop, Copy, Serde, Introspect)]
pub struct AdventurerMetadata {
pub struct AdventurerMetadataStorage {
pub birth_date: u64, // 64 bits in storage
pub death_date: u64, // 64 bits in storage
pub level_seed: u64, // 64 bits in storage
pub item_specials_seed: u16, // 16 bits in storage
pub rank_at_death: u8, // 2 bits in storage
pub delay_stat_reveal: bool, // 1 bit in storage
pub golden_token_id: u8, // 8 bits in storage
// launch_tournament_winner_token_id: u128, // 32 bits in storage
// pub launch_tournament_winner_token_id: u128, // 32 bits in storage
}


Expand Down Expand Up @@ -97,7 +98,7 @@ pub struct AdventurerModel {
pub struct AdventurerMetaModel {
#[key]
pub adventurer_id: felt252,
pub adventurer_meta: AdventurerMetadata,
pub adventurer_meta: AdventurerMetadataStorage,
}

#[dojo::model]
Expand All @@ -116,6 +117,16 @@ pub struct GameCountModel {
pub game_count: u128,
}

#[dojo::model]
#[derive(Copy, Drop, Serde)]
pub struct FreeGameAvailableModel {
#[key]
pub free_game_type: FreeGameTokenType,
#[key]
pub token_id: u128,
pub available: bool,
}

#[dojo::model]
#[derive(Copy, Drop, Serde)]
pub struct Contracts {
Expand Down
7 changes: 7 additions & 0 deletions contracts/src/ls15_components/models/tournament.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ pub enum EntryStatus {
Submitted,
}

#[derive(Copy, Drop, PartialEq, Introspect, Serde)]
pub enum FreeGameTokenType {
GoldenToken,
LaunchTournamentChampion,
}

///
/// Model
Expand Down Expand Up @@ -203,6 +208,8 @@ pub struct TournamentConfig {
pub lords: ContractAddress,
pub loot_survivor: ContractAddress,
pub oracle: ContractAddress,
pub golden_token: ContractAddress,
pub blobert: ContractAddress,
pub safe_mode: bool,
pub test_mode: bool
}
16 changes: 13 additions & 3 deletions contracts/src/ls15_components/tests/helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tournament::ls15_components::tests::interfaces::{
ITournamentMockDispatcher, ITournamentMockDispatcherTrait
};
use adventurer::{adventurer::Adventurer, equipment::Equipment, item::Item, stats::Stats};
use tournament::ls15_components::models::loot_survivor::AdventurerMetadata;
use tournament::ls15_components::models::loot_survivor::AdventurerMetadataStorage;
use tournament::ls15_components::models::tournament::{ERC20Data, ERC721Data, Token, TokenDataType};

//
Expand Down Expand Up @@ -44,6 +44,16 @@ pub fn approve_game_costs(
eth.approve(tournament.contract_address, entries * 200000000000000);
}

pub fn approve_free_game_cost(
eth: IERC20MockDispatcher,
golden_token: IERC721MockDispatcher,
token_id: u256,
tournament: ITournamentMockDispatcher
) {
eth.approve(tournament.contract_address, 200000000000000);
golden_token.approve(tournament.contract_address, token_id);
}

pub fn create_dead_adventurer_with_xp(xp: u16) -> Adventurer {
Adventurer {
health: 0,
Expand All @@ -70,8 +80,8 @@ pub fn create_dead_adventurer_with_xp(xp: u16) -> Adventurer {
}
}

pub fn create_adventurer_metadata_with_death_date(death_date: u64) -> AdventurerMetadata {
AdventurerMetadata {
pub fn create_adventurer_metadata_with_death_date(death_date: u64) -> AdventurerMetadataStorage {
AdventurerMetadataStorage {
birth_date: get_block_timestamp().into(),
death_date: death_date,
level_seed: 0,
Expand Down
Loading