From 9e15e0eb2afc0f2f2fe7ac0637dfdd2989bd8bff Mon Sep 17 00:00:00 2001 From: yellow <8539006+yellowBirdy@users.noreply.github.com> Date: Thu, 31 Oct 2024 04:21:08 +0100 Subject: [PATCH] small refactors --- src/RaidGeld.sol | 54 +++++++++++++++++++++---------------------- src/RaidGeldUtils.sol | 21 +++++++++-------- 2 files changed, 37 insertions(+), 38 deletions(-) diff --git a/src/RaidGeld.sol b/src/RaidGeld.sol index efb9d0d..1da2f84 100644 --- a/src/RaidGeld.sol +++ b/src/RaidGeld.sol @@ -10,8 +10,8 @@ import "../src/Constants.sol"; contract RaidGeld is ERC20, Ownable, Constants { uint256 public constant MANTISSA = 1e4; - uint256 public constant BUY_IN_AMOUNT = 0.0005 ether; - uint256 public immutable BUY_IN_DAO_TOKEN_AMOUNT; + uint256 public constant BUY_IN_AMOUNT = 0.00005 ether; + uint256 public BUY_IN_DAO_TOKEN_AMOUNT; uint256 public constant INITIAL_GELD = 50 * MANTISSA; mapping(address => Player) private players; mapping(address => Army) private armies; @@ -28,6 +28,7 @@ contract RaidGeld is ERC20, Ownable, Constants { // Events event PlayerRegistered(address indexed player, uint256 initialGeld); + event PleyerStrikesAgain(address indexed player, uint256 totalRewards, uint256 initialGeld); event RaidPerformed(address indexed player, uint256 totalMinted, uint256 geldBalance); event UnitAdded( address indexed player, @@ -40,6 +41,7 @@ contract RaidGeld is ERC20, Ownable, Constants { uint16 anointedLevel, uint16 championLevel ); + event DaoTokenBuyInAmountSet(address indexed owner, uint256 oldAmount, uint256 newAmount); // Modifier for functions that should only be available to registered players modifier onlyPlayer() { @@ -53,7 +55,7 @@ contract RaidGeld is ERC20, Ownable, Constants { } modifier newPlay() { - Player memory player = players[msg.sender]; + Player storage player = players[msg.sender]; bool notRegistered = player.created_at == 0; bool returningPlayer = player.is_registered && !player.has_active_session; require(notRegistered || returningPlayer, "Active session already in progress"); @@ -72,44 +74,37 @@ contract RaidGeld is ERC20, Ownable, Constants { // Mint some starting tokens to the player _mint(player, INITIAL_GELD); // Reset or set player - reset_player(player); + resetPlayer(player); players[player].has_active_session = true; players[player].is_registered = true; players[player].n_runs += 1; if (existing_player) { // TODO: Emit new run + emit PleyerStrikesAgain(player, players[player].total_rewards, INITIAL_GELD); } else { // Emit event emit PlayerRegistered(msg.sender, INITIAL_GELD); } } - function reset_player(address addr) private { - Player memory player = players[addr]; - uint32 current_n_runs = player.n_runs; - uint256 current_total_rewards = player.total_rewards; - bool current_is_registered = player.is_registered; - uint32 current_prestige_level = player.prestige_level; + function resetPlayer(address _playerAddress) private { + Player storage player = players[_playerAddress]; // Set initial states - players[addr] = Player({ - total_minted: INITIAL_GELD, - created_at: block.timestamp, - last_raided_at: block.timestamp, - n_runs: current_n_runs, - total_rewards: current_total_rewards, - has_active_session: false, - is_registered: current_is_registered, - prestige_level: current_prestige_level - }); - armies[addr] = Army({ + + player.total_minted = INITIAL_GELD; + player.created_at = block.timestamp; + player.last_raided_at = block.timestamp; + player.has_active_session = false; + + armies[_playerAddress] = Army({ moloch_denier: Raider({level: 0}), apprentice: Raider({level: 0}), anointed: Raider({level: 0}), champion: Raider({level: 0}), profit_per_second: 0 }); - bosses[addr] = Boss({level: 0, variants: RaidGeldUtils.generate_boss_variants(block.prevrandao)}); + bosses[_playerAddress] = Boss({level: 0, variants: RaidGeldUtils.generate_boss_variants(block.prevrandao)}); } // New player want to register with ETH @@ -202,14 +197,11 @@ contract RaidGeld is ERC20, Ownable, Constants { uint256 cost = RaidGeldUtils.calculateUnitPrice(unit, currentLevel, n_units); - // First trigger a raid so player receives what he is due at to this moment - uint256 time_past = block.timestamp - players[msg.sender].last_raided_at; - uint256 new_geld = armies[msg.sender].profit_per_second * time_past; - require(balanceOf(msg.sender) + new_geld >= cost, "Not enough GELD to add this unit"); + performRaid(msg.sender); // TODO: Since we are first minting then burning the token, this could be simplified - // by first calculating the difference and then minting / burning in just one operation + require(balanceOf(msg.sender) >= cost, "Not enough GELD to add this unit"); // then burn the cost of the new army _burn(msg.sender, cost); @@ -278,11 +270,17 @@ contract RaidGeld is ERC20, Ownable, Constants { } function player_dies(address player) private { - reset_player(player); + resetPlayer(player); players[player].has_active_session = false; _burn(msg.sender, balanceOf(player)); } + function setDaoTokenBuyInAmount(uint256 newAmount) external onlyOwner { + require(newAmount > 0, "Amount must be greater than 0"); + emit DaoTokenBuyInAmountSet(msg.sender, BUY_IN_DAO_TOKEN_AMOUNT, newAmount); + BUY_IN_DAO_TOKEN_AMOUNT = newAmount; + } + receive() external payable { revert("No plain Ether accepted, use register() function to check in :)"); } diff --git a/src/RaidGeldUtils.sol b/src/RaidGeldUtils.sol index 6f51181..b680008 100644 --- a/src/RaidGeldUtils.sol +++ b/src/RaidGeldUtils.sol @@ -22,6 +22,8 @@ library RaidGeldUtils { uint256 constant RISK_BETA = 15e17; + uint256 constant NUMBER_OF_BOSSES = 7; + function getBossPower(uint8 level) internal pure returns (uint256 power) { require(level <= 7, "Bosses only go to level 7"); if (level == 0) return 9000000; @@ -31,7 +33,6 @@ library RaidGeldUtils { else if (level == 4) return 90000000000; else if (level == 5) return 900000000000; else if (level == 6) return 9000000000000; - else return 0; } function getBossCumulativeChance(uint8 level) internal pure returns (uint256 chance) { @@ -102,11 +103,11 @@ library RaidGeldUtils { } // Calculates whether user survives the fight - function calculateBossFight(uint8 bossLevel, uint256 geldBurnt, uint256 prevrandao) internal pure returns (bool) { + function calculateBossFight(uint8 bossLevel, uint256 geldBurnt, uint256 _randomSeed) internal pure returns (bool) { uint256 bossPower = getBossPower(bossLevel); uint256 bossRoll = getBossChance(bossLevel); require(geldBurnt <= bossPower, "Cant try to defeat boss with more than what boss power is"); - uint256 random_n = random(prevrandao, 1, 100); + uint256 random_n = random(_randomSeed, 1, 100); // Relative power as in, you can only put in 800 geld to defeat 900 geld boss, // but you will get exponentially worse chances uint256 relativePower = ((geldBurnt ** 2) * 100) / bossPower ** 2; @@ -114,25 +115,25 @@ library RaidGeldUtils { return roll < bossRoll; } - function generate_boss_variants(uint256 prevrandao) internal pure returns (uint8[7] memory boss_variants) { + function generate_boss_variants(uint256 _randomSeed) internal pure returns (uint8[7] memory boss_variants) { // We shuffle the possible variants so each run is a bit different - uint8[6] memory array = [0, 1, 2, 3, 4, 5]; - for (uint256 i = array.length - 1; i > 0; i--) { + uint8[NUMBER_OF_BOSSES] memory array = [0, 1, 2, 3, 4, 5, 6]; + for (uint256 i = NUMBER_OF_BOSSES - 2; i > 0; i--) { // generate a pseudo-random index based on the prevrandao - uint256 j = uint256(keccak256(abi.encodePacked(prevrandao, i))) % (i + 1); + uint256 j = uint256(keccak256(abi.encodePacked(_randomSeed, i))) % (i + 1); // swap elements at i and j (array[i], array[j]) = (array[j], array[i]); } // Last boss is always the same old super Moloch - return [array[0], array[1], array[2], array[3], array[4], array[5], 6]; + return array; } // TODO: Implement actual randomness - function random(uint256 prevrandao, uint256 min, uint256 max) internal pure returns (uint256) { + function random(uint256 _randomSeed, uint256 min, uint256 max) internal pure returns (uint256) { // returns 0 - 100 require(max > min, "Max must be greater than min"); require(max < type(uint256).max, "Dont use largest possible uint"); uint256 range = max - min + 1; - return min + (uint256(keccak256(abi.encodePacked(prevrandao))) % range); + return min + (uint256(keccak256(abi.encodePacked(_randomSeed))) % range); } }