Ran forge fmt + solved client bug with amounts being much too high due to change in contract calculations
Some checks failed
CI / Foundry project (push) Has been cancelled
CI / Foundry project (pull_request) Has been cancelled

This commit is contained in:
mic0 2024-10-25 11:12:51 +02:00
parent a5f1bc7a57
commit 7a32129f7e
Signed by: mico
GPG Key ID: A3F8023524CF1C8D
7 changed files with 28 additions and 38 deletions

View File

@ -8,14 +8,12 @@ const calculateBalance = (balance: bigint, perSecond: bigint, lastRaidedAt: bigi
(new Date()).getTime() - parseInt(lastRaidedAt.toString()) * 1000; (new Date()).getTime() - parseInt(lastRaidedAt.toString()) * 1000;
return ( return (
balance + balance +
(BigInt(millisecondsSinceLastRaid) * (perSecond * BigInt(10000) /* decimals */) (BigInt(millisecondsSinceLastRaid) * perSecond
/ BigInt(1000) /* deduct milliseconds*/)) / BigInt(1000) /* deduct milliseconds*/))
} }
export const toReadable = (value: bigint, applyTokenDivision?: boolean) => { export const toReadable = (value: bigint) => {
if (applyTokenDivision) { value = value / BigInt(10000);
value = value / BigInt(10000);
}
const suffixes = [ const suffixes = [
{ value: BigInt('1000'), suffix: 'thousand' }, { value: BigInt('1000'), suffix: 'thousand' },
{ value: BigInt('1000000'), suffix: 'million' }, { value: BigInt('1000000'), suffix: 'million' },
@ -64,8 +62,8 @@ const Counter = () => {
balance, balance,
army?.profit_per_second ?? BigInt(0), army?.profit_per_second ?? BigInt(0),
player?.last_raided_at ?? BigInt(0) player?.last_raided_at ?? BigInt(0)
), true); ));
availableBalance.current = toReadable(balance, true); availableBalance.current = toReadable(balance);
render(); render();
}, 100); }, 100);
return () => clearInterval(tickInterval) return () => clearInterval(tickInterval)

View File

@ -91,7 +91,7 @@ const PlayerProvider = ({ children }: { children: ReactNode }) => {
} }
}); });
console.log(player) console.log(player, army)
const register = useCallback(() => { const register = useCallback(() => {
writeContract({ writeContract({

View File

@ -109,7 +109,7 @@ contract RaidGeld is ERC20, Ownable {
} }
uint256 cost = RaidGeldUtils.calculateUnitPrice(unit, currentLevel, n_units); uint256 cost = RaidGeldUtils.calculateUnitPrice(unit, currentLevel, n_units);
// First trigger a raid so player receives what he is due at to this moment // First trigger a raid so player receives what he is due at to this moment
performRaid(msg.sender); performRaid(msg.sender);
require(balanceOf(msg.sender) > cost, "Not enough GELD to add this much"); require(balanceOf(msg.sender) > cost, "Not enough GELD to add this much");

View File

@ -1,8 +1,6 @@
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
pragma solidity ^0.8.13; pragma solidity ^0.8.13;
struct Raider { struct Raider {
uint16 level; uint16 level;
} }

View File

@ -4,15 +4,12 @@ pragma solidity ^0.8.13;
import {Army} from "../src/RaidGeldStructs.sol"; import {Army} from "../src/RaidGeldStructs.sol";
library RaidGeldUtils { library RaidGeldUtils {
uint256 public constant PRECISION = 10000; uint256 public constant PRECISION = 10000;
uint256 constant BASE_PRICE = 380000; uint256 constant BASE_PRICE = 380000;
uint256 constant PRICE_FACTOR = 11500; uint256 constant PRICE_FACTOR = 11500;
uint256 constant APPRENTICE_PROFIT = 61000; uint256 constant APPRENTICE_PROFIT = 61000;
uint256 constant ANOINTED_PROFIT = 6 * 64000; uint256 constant ANOINTED_PROFIT = 6 * 64000;
uint256 constant CHAMPION_PROFIT = 67000 * 61000 * 64000/ PRECISION / PRECISION; uint256 constant CHAMPION_PROFIT = 67000 * 61000 * 64000 / PRECISION / PRECISION;
function calculateUnitPrice(uint8 unit, uint16 currentLevel, uint16 units) internal pure returns (uint256) { function calculateUnitPrice(uint8 unit, uint16 currentLevel, uint16 units) internal pure returns (uint256) {
require(unit <= 3, "No matching unit found"); require(unit <= 3, "No matching unit found");
@ -37,7 +34,7 @@ library RaidGeldUtils {
uint256 apprentice_profit = army.apprentice.level * APPRENTICE_PROFIT; uint256 apprentice_profit = army.apprentice.level * APPRENTICE_PROFIT;
uint256 anointed_profit = army.anointed.level * ANOINTED_PROFIT; uint256 anointed_profit = army.anointed.level * ANOINTED_PROFIT;
uint256 champion_profit = army.champion.level * CHAMPION_PROFIT; uint256 champion_profit = army.champion.level * CHAMPION_PROFIT;

View File

@ -3,7 +3,7 @@ pragma solidity ^0.8.13;
import {Test, console} from "forge-std/Test.sol"; import {Test, console} from "forge-std/Test.sol";
import {RaidGeld, Army, Player} from "../src/RaidGeld.sol"; import {RaidGeld, Army, Player} from "../src/RaidGeld.sol";
import "../src/RaidGeldUtils.sol"; import "../src/RaidGeldUtils.sol";
contract raid_geldTest is Test { contract raid_geldTest is Test {
RaidGeld public raid_geld; RaidGeld public raid_geld;

View File

@ -3,10 +3,10 @@ pragma solidity ^0.8.13;
import {Test, console} from "forge-std/Test.sol"; import {Test, console} from "forge-std/Test.sol";
import {Army, Raider} from "../src/RaidGeld.sol"; import {Army, Raider} from "../src/RaidGeld.sol";
import "../src/RaidGeldUtils.sol"; import "../src/RaidGeldUtils.sol";
contract raid_geldTest is Test { contract raid_geldTest is Test {
function test_0_unit_price() pure public { function test_0_unit_price() public pure {
// buying 1 unit of moloch_denier // buying 1 unit of moloch_denier
uint256 basePriceMolochDenier = RaidGeldUtils.calculateUnitPrice(0, 0, 1); uint256 basePriceMolochDenier = RaidGeldUtils.calculateUnitPrice(0, 0, 1);
assertEq(basePriceMolochDenier, RaidGeldUtils.BASE_PRICE); assertEq(basePriceMolochDenier, RaidGeldUtils.BASE_PRICE);
@ -30,28 +30,25 @@ contract raid_geldTest is Test {
function test_1_profits_per_second(uint16 _dLvl, uint16 _apLvl, uint16 _anLvl, uint16 _cLvl) public { function test_1_profits_per_second(uint16 _dLvl, uint16 _apLvl, uint16 _anLvl, uint16 _cLvl) public {
Army memory army = Army({ Army memory army = Army({
moloch_denier: Raider({ level: 1}), moloch_denier: Raider({level: 1}),
apprentice: Raider({ level: 0}), apprentice: Raider({level: 0}),
anointed: Raider({ level: 0}), anointed: Raider({level: 0}),
champion: Raider({ level: 0}), champion: Raider({level: 0}),
profit_per_second: 0 // irrelevant for this test profit_per_second: 0 // irrelevant for this test
}); });
uint256 profits_per_second = RaidGeldUtils.calculateProfitsPerSecond(army); uint256 profits_per_second = RaidGeldUtils.calculateProfitsPerSecond(army);
assertEq(profits_per_second, RaidGeldUtils.PRECISION); assertEq(profits_per_second, RaidGeldUtils.PRECISION);
army = Army({ army = Army({
moloch_denier: Raider({ level: _dLvl}), moloch_denier: Raider({level: _dLvl}),
apprentice: Raider({ level: _apLvl}), apprentice: Raider({level: _apLvl}),
anointed: Raider({ level: _anLvl}), anointed: Raider({level: _anLvl}),
champion: Raider({ level: _cLvl}), champion: Raider({level: _cLvl}),
profit_per_second: 0 // irrelevant for this test profit_per_second: 0 // irrelevant for this test
}); });
profits_per_second = RaidGeldUtils.calculateProfitsPerSecond(army); profits_per_second = RaidGeldUtils.calculateProfitsPerSecond(army);
uint256 expected = uint256 expected = _dLvl * RaidGeldUtils.PRECISION + _apLvl * RaidGeldUtils.APPRENTICE_PROFIT
_dLvl * RaidGeldUtils.PRECISION + + _anLvl * RaidGeldUtils.ANOINTED_PROFIT + _cLvl * RaidGeldUtils.CHAMPION_PROFIT;
_apLvl * RaidGeldUtils.APPRENTICE_PROFIT +
_anLvl * RaidGeldUtils.ANOINTED_PROFIT +
_cLvl * RaidGeldUtils.CHAMPION_PROFIT;
assertEq(profits_per_second, expected); assertEq(profits_per_second, expected);
} }
} }