Skip to content

Commit

Permalink
refacto tests + added oz components for ownable
Browse files Browse the repository at this point in the history
  • Loading branch information
ametel01 committed Dec 23, 2023
1 parent 7915d0a commit 92e9cd5
Show file tree
Hide file tree
Showing 27 changed files with 79 additions and 75 deletions.
9 changes: 0 additions & 9 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,3 @@ casm = true
[tool.snforge]
exit_first = false

[tool.sncast.katana]
account = "k_acc"
accounts-file = "/Users/alexmetelli/.starknet_accounts/starknet_open_zeppelin_accounts.json"
url = "http://0.0.0.0:5050"

[tool.sncast.testnet]
account = "cast_testnet"
accounts-file = "/Users/alexmetelli/.starknet_accounts/starknet_open_zeppelin_accounts.json"
url = "https://starknet-goerli.infura.io/v3/25371764a3e44191b39d3b3b98a8c55d"
3 changes: 1 addition & 2 deletions src/game/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ trait INoGame<TState> {
quartz: ContractAddress,
tritium: ContractAddress,
eth: ContractAddress,
receiver: ContractAddress,
owner: ContractAddress,
uni_speed: u128,
token_price: u128,
);
Expand All @@ -36,7 +36,6 @@ trait INoGame<TState> {
fn recall_fleet(ref self: TState, mission_id: usize);
fn collect_debris(ref self: TState, mission_id: usize);
// View functions
fn get_receiver(self: @TState) -> ContractAddress;
fn get_token_addresses(self: @TState) -> Tokens;
fn get_current_planet_price(self: @TState) -> u128;
fn get_number_of_planets(self: @TState) -> u16;
Expand Down
32 changes: 23 additions & 9 deletions src/game/main.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ mod NoGame {
};
use openzeppelin::token::erc20::interface::{IERC20CamelDispatcher, IERC20CamelDispatcherTrait};
use openzeppelin::upgrades::upgradeable::UpgradeableComponent;
use openzeppelin::access::ownable::OwnableComponent;
use openzeppelin::security::reentrancyguard::ReentrancyGuardComponent;
use nogame_fixed::f128::types::{Fixed, FixedTrait, ONE_u128 as ONE};

use nogame::game::interface::INoGame;
Expand All @@ -31,10 +33,17 @@ mod NoGame {
component!(path: UpgradeableComponent, storage: upgradeable, event: UpgradeableEvent);
impl UpgradableInteralImpl = UpgradeableComponent::InternalImpl<ContractState>;

component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);
#[abi(embed_v0)]
impl OwnableImpl = OwnableComponent::OwnableImpl<ContractState>;
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;

component!(path: ReentrancyGuardComponent, storage: reentrancyguard, event: ReentrancyGuardEvent);
impl ReentrancyGuardInternalImpl = ReentrancyGuardComponent::InternalImpl<ContractState>;

#[storage]
struct Storage {
initialized: bool,
receiver: ContractAddress,
token_price: u128,
uni_speed: u128,
// General.
Expand Down Expand Up @@ -65,6 +74,10 @@ mod NoGame {
hostile_missions_len: LegacyMap<u16, usize>,
#[substorage(v0)]
upgradeable: UpgradeableComponent::Storage,
#[substorage(v0)]
ownable: OwnableComponent::Storage,
#[substorage(v0)]
reentrancyguard: ReentrancyGuardComponent::Storage
}

#[event]
Expand All @@ -79,7 +92,11 @@ mod NoGame {
BattleReport: BattleReport,
DebrisCollected: DebrisCollected,
#[flat]
UpgradeableEvent: UpgradeableComponent::Event
UpgradeableEvent: UpgradeableComponent::Event,
#[flat]
OwnableEvent: OwnableComponent::Event,
#[flat]
ReentrancyGuardEvent: ReentrancyGuardComponent::Event
}

#[derive(Drop, starknet::Event)]
Expand Down Expand Up @@ -164,17 +181,17 @@ mod NoGame {
quartz: ContractAddress,
tritium: ContractAddress,
eth: ContractAddress,
receiver: ContractAddress,
owner: ContractAddress,
uni_speed: u128,
token_price: u128,
) {
assert(!self.initialized.read(), 'already initialized');
self.reentrancyguard.start();
self.ownable.initializer(owner);
self.erc721.write(IERC721NoGameDispatcher { contract_address: erc721 });
self.steel.write(IERC20NoGameDispatcher { contract_address: steel });
self.quartz.write(IERC20NoGameDispatcher { contract_address: quartz });
self.tritium.write(IERC20NoGameDispatcher { contract_address: tritium });
self.ETH.write(IERC20CamelDispatcher { contract_address: eth });
self.receiver.write(receiver);
self.uni_speed.write(uni_speed);
self.initialized.write(true);
self.token_price.write(token_price);
Expand All @@ -191,7 +208,7 @@ mod NoGame {
let caller = get_caller_address();
let time_elapsed = (get_block_timestamp() - self.universe_start_time.read()) / DAY;
let price: u256 = self.get_planet_price(time_elapsed).into();
self.ETH.read().transferFrom(caller, self.receiver.read(), price);
self.ETH.read().transferFrom(caller, self.ownable.owner(), price);
let number_of_planets = self.number_of_planets.read();
assert(number_of_planets != MAX_NUMBER_OF_PLANETS, 'max number of planets');
let token_id = number_of_planets + 1;
Expand Down Expand Up @@ -511,9 +528,6 @@ mod NoGame {
/////////////////////////////////////////////////////////////////////
// View Functions
/////////////////////////////////////////////////////////////////////
fn get_receiver(self: @ContractState) -> ContractAddress {
self.receiver.read()
}
fn get_token_addresses(self: @ContractState) -> Tokens {
self.get_tokens_addresses()
}
Expand Down
104 changes: 52 additions & 52 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -35,58 +35,58 @@ mod token {
}
}

mod tests {
mod internal_functions {
#[cfg(test)]
mod auction_price;
#[cfg(test)]
mod compound_cost;
#[cfg(test)]
mod fleet;
#[cfg(test)]
mod math;
#[cfg(test)]
mod mine_production;
#[cfg(test)]
mod tech_cost;
}
// mod tests {
// mod internal_functions {
// #[cfg(test)]
// mod auction_price;
// #[cfg(test)]
// mod compound_cost;
// #[cfg(test)]
// mod fleet;
// #[cfg(test)]
// mod math;
// #[cfg(test)]
// mod mine_production;
// #[cfg(test)]
// mod tech_cost;
// }

mod view_tests {
#[cfg(test)]
mod general_view;
#[cfg(test)]
mod compound_view;
#[cfg(test)]
mod defence_view;
#[cfg(test)]
mod dockyard_view;
#[cfg(test)]
mod fleet_view;
#[cfg(test)]
mod research_view;
}
// mod view_tests {
// #[cfg(test)]
// mod general_view;
// #[cfg(test)]
// mod compound_view;
// #[cfg(test)]
// mod defence_view;
// #[cfg(test)]
// mod dockyard_view;
// #[cfg(test)]
// mod fleet_view;
// #[cfg(test)]
// mod research_view;
// }

mod write_tests {
#[cfg(test)]
mod compound_write;
#[cfg(test)]
mod defence_write;
#[cfg(test)]
mod dockyard_write;
#[cfg(test)]
mod fleet_write;
#[cfg(test)]
mod general_write;
#[cfg(test)]
mod research_write;
}
// mod write_tests {
// #[cfg(test)]
// mod compound_write;
// #[cfg(test)]
// mod defence_write;
// #[cfg(test)]
// mod dockyard_write;
// #[cfg(test)]
// mod fleet_write;
// #[cfg(test)]
// mod general_write;
// #[cfg(test)]
// mod research_write;
// }

mod token {
#[cfg(test)]
mod test_erc721;
#[cfg(test)]
mod test_erc20;
}
#[cfg(test)]
mod utils;
}
// mod token {
// #[cfg(test)]
// mod test_erc721;
// #[cfg(test)]
// mod test_erc20;
// }
// #[cfg(test)]
// mod utils;
// }
2 changes: 1 addition & 1 deletion src/libraries/fleet.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ fn build_ships_array(
n_ships -= 1;
fleet.carrier -= 1;
}
let a = 0;
continue;
};
array
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use snforge_std::{start_prank, start_warp, CheatTarget, PrintTrait};
use nogame::game::interface::{INoGameDispatcher, INoGameDispatcherTrait};
use nogame::libraries::compounds::{Production, Compounds};
use nogame::libraries::types::{CompoundsLevels, UpgradeType};
use nogame::tests::utils::{
use tests::utils::{
E18, HOUR, Dispatchers, ACCOUNT1, ACCOUNT2, ACCOUNT3, ACCOUNT4, ACCOUNT5, init_game, set_up,
build_basic_mines
};
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn test_send_fleet_success() {

let tritium_before = dsp.game.get_spendable_resources(1).tritium;

dsp.game.send_fleet(fleet, p2_position, false);
// dsp.game.send_fleet(fleet, p2_position, false);

let tritium_after = dsp.game.get_spendable_resources(1).tritium;

Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 92e9cd5

Please sign in to comment.