Skip to content

Commit

Permalink
add mock erc721
Browse files Browse the repository at this point in the history
  • Loading branch information
dpinones committed Nov 2, 2024
1 parent 8a088d8 commit 6b53ada
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 21 deletions.
3 changes: 3 additions & 0 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ mod systems {
mod game_system;
mod poker_hand_system;
mod rage_system;
mod mock {
mod erc721_system;
}
}

#[cfg(test)]
Expand Down
21 changes: 9 additions & 12 deletions src/models/status/round/adventurer.cairo
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
use jokers_of_neon::models::data::adventurer::{AdventurerConsumed, AdventurerConsumedStore};
use jokers_of_neon::utils::adventurer::get_adventurer_address;

use openzeppelin_token::erc721::interface::{IERC721Dispatcher, IERC721DispatcherTrait};
use jokers_of_neon::models::data::adventurer::{
AdventurerConsumed, AdventurerConsumedStore
};
use jokers_of_neon::utils::adventurer::get_adventurer_address;
use starknet::{ContractAddress, get_caller_address, get_tx_info};

mod errors {
Expand All @@ -14,27 +12,25 @@ mod errors {

#[generate_trait]
impl AdventurerImpl of AdventurerTrait {

fn use_adventurer(world: IWorldDispatcher, adventurer_id: u32) {
assert_adventurer_ownership(adventurer_id);
assert_adventurer_ownership(world, adventurer_id);
let mut adventurer_consumed = AdventurerConsumedStore::get(world, adventurer_id);
assert(!adventurer_consumed.consumed, errors::ADVENTURER_CONSUMED);
adventurer_consumed.consumed = true;
AdventurerConsumedStore::set(@adventurer_consumed, world);
}
}

fn assert_adventurer_ownership(token_id: u32) {
let owner = get_owner_of_adventurer(token_id);
fn assert_adventurer_ownership(world: IWorldDispatcher, token_id: u32) {
let owner = get_owner_of_adventurer(world, token_id);
assert(owner == get_caller_address(), errors::NOT_TOKEN_OWNER);
}

fn get_owner_of_adventurer(token_id: u32) -> ContractAddress {
let adventurer_address = get_adventurer_address(get_tx_info().unbox().chain_id);
fn get_owner_of_adventurer(world: IWorldDispatcher, token_id: u32) -> ContractAddress {
let adventurer_address = get_adventurer_address(world, get_tx_info().unbox().chain_id);
let erc721_dispatcher = IERC721Dispatcher { contract_address: adventurer_address };
erc721_dispatcher.owner_of(token_id.into())
}

// fn _get_adventurer(self: @ContractState, token_id: u32) -> Adventurer {
// let adventurer_address = utils::ADVENTURER_ADDRESS_MAINNET();
// let game_dispatcher = IGameDispatcher { contract_address: adventurer_address };
Expand All @@ -47,4 +43,5 @@ fn get_owner_of_adventurer(token_id: u32) -> ContractAddress {
// health: adventurer.health,
// rank_at_death: adventurer_meta.rank_at_death,
// }
// }
// }

3 changes: 1 addition & 2 deletions src/models/status/round/beast.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ impl BeastImpl of BeastTrait {
player_beast.energy -= game_mode_beast.cost_play;
PlayerBeastStore::set(@player_beast, world);


if beast.current_health.is_zero() {
let play_win_game_event = PlayWinGameEvent {
player: get_caller_address(), game_id, level: game.level, player_score: 0
Expand All @@ -112,7 +111,7 @@ impl BeastImpl of BeastTrait {
_ => Option::None
}.unwrap();
IRageSystemDispatcher { contract_address: rage_system_address.try_into().unwrap() }.calculate(game.id);
// create_level(world, ref store, game); TODO:
// create_level(world, ref store, game); TODO:
} else if player_beast.energy.is_zero() {
_attack_beast(world, ref store, ref game, ref player_beast, ref beast, ref game_mode_beast);
} else {
Expand Down
14 changes: 14 additions & 0 deletions src/systems/mock/erc721_system.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#[dojo::interface]
trait IERC721System {
fn owner_of(ref world: IWorldDispatcher, token_id: u256) -> bool;
}

#[dojo::contract]
mod erc721_system {
#[abi(embed_v0)]
impl ERC721Impl of super::IERC721System<ContractState> {
fn owner_of(ref world: IWorldDispatcher, token_id: u256) -> bool {
true
}
}
}
8 changes: 4 additions & 4 deletions src/tests/test_game_play_beast.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -278,23 +278,23 @@ mod test_play_beast_special_cards {
player_beast_after.energy == player_beast_before.energy - game_mode_beast.cost_play, 'wrong player energy'
);
}
// #[test]
// #[test]
// #[available_gas(30000000000000000)]
// fn test_play_special_initial_advantage() {
// let (world, systems) = setup::spawn_game();
// let mut store = StoreTrait::new(world);
// let mut game = mock_game(ref store, PLAYER());
// mock_round(ref store, @game, 300);

// // Mock special card
// // Mock special card
// let special_cards_ids = array![SPECIAL_INITIAL_ADVANTAGE_ID];
// mock_special_cards(ref store, ref game, special_cards_ids);

// // Mock hand
// // Mock hand
// let hand_cards_ids = array![ACE_CLUBS_ID, ACE_HEARTS_ID, ACE_DIAMONDS_ID, ACE_SPADES_ID];
// mock_current_hand_cards_ids(ref store, game.id, hand_cards_ids);

// set_contract_address(PLAYER());
// set_contract_address(PLAYER());
// systems.game_system.play(game.id, array![0, 1, 2, 3], array![100, 100, 100, 100]);
// // Four of a Kind - points: 60, multi: 7
// // points: 11 + 11 + 11 + 11 + 100
Expand Down
13 changes: 10 additions & 3 deletions src/utils/adventurer.cairo
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
use dojo::world::Resource::Contract;
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

use starknet::{ContractAddress, contract_address_const};

const MAINNET_CHAIN_ID: felt252 = 0x534e5f4d41494e;

fn get_adventurer_address(chain_id: felt252) -> ContractAddress {
fn get_adventurer_address(world: IWorldDispatcher, chain_id: felt252) -> ContractAddress {
if chain_id == MAINNET_CHAIN_ID {
ADVENTURER_ADDRESS_MAINNET()
} else {
panic_with_felt252('Chain not supported')
let (_, erc721_system_address) = match world.resource(selector_from_tag!("jokers_of_neon-erc721_system")) {
Contract((class_hash, contract_address)) => Option::Some((class_hash, contract_address)),
_ => Option::None
}.unwrap();
erc721_system_address
}
}

fn ADVENTURER_ADDRESS_MAINNET() -> ContractAddress {
contract_address_const::<0x018108b32cea514a78ef1b0e4a0753e855cdf620bc0565202c02456f618c4dc4>()
}
}

0 comments on commit 6b53ada

Please sign in to comment.