diff --git a/README.md b/README.md index 9265b45..8f15761 100644 --- a/README.md +++ b/README.md @@ -1,66 +1,15 @@ -## Foundry +# SLAY THE MOLOCH -**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.** +Idle game & shitcoin advanture dedicated to cohort VII of Raid Guild. -Foundry consists of: +## Set up -- **Forge**: Ethereum testing framework (like Truffle, Hardhat and DappTools). -- **Cast**: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data. -- **Anvil**: Local Ethereum node, akin to Ganache, Hardhat Network. -- **Chisel**: Fast, utilitarian, and verbose solidity REPL. +1. Run `anvil` to setup local RPC -## Documentation +2. Deploy contract -https://book.getfoundry.sh/ +Either use `./deploy_contract.sh` script (!! change contract values and set private key to $DEV_PRIVATE_KEY for it to work) or call those things by hand. -## Usage +3. Local development requires mining blocks by hand -### Build - -```shell -$ forge build -``` - -### Test - -```shell -$ forge test -``` - -### Format - -```shell -$ forge fmt -``` - -### Gas Snapshots - -```shell -$ forge snapshot -``` - -### Anvil - -```shell -$ anvil -``` - -### Deploy - -```shell -$ forge script script/Counter.s.sol:CounterScript --rpc-url --private-key -``` - -### Cast - -```shell -$ cast -``` - -### Help - -```shell -$ forge --help -$ anvil --help -$ cast --help -``` +Call `cast rpc anvil_mine` to mine next block, otherwise it wont ever progress and time "stands still" as far as the game is concerned diff --git a/app/src/components/Tower.tsx b/app/src/components/Tower.tsx index 31418b5..7c3ca90 100644 --- a/app/src/components/Tower.tsx +++ b/app/src/components/Tower.tsx @@ -1,7 +1,9 @@ +import { usePlayer } from '../providers/PlayerProvider'; import styles from '../styles/Background.module.css'; const Tower = () => { - return
+ const { raid } = usePlayer(); + return
} export default Tower diff --git a/app/src/pages/index.tsx b/app/src/pages/index.tsx index 0cbc94f..eeab2bc 100644 --- a/app/src/pages/index.tsx +++ b/app/src/pages/index.tsx @@ -9,7 +9,7 @@ const Home: NextPage = () => { return (
- Slay Moloch + Slay The Moloch void; + raid: () => void } const PlayerContext = createContext({ @@ -18,11 +19,14 @@ const PlayerContext = createContext({ user: null, balance: BigInt(0), register: () => { }, + raid: () => { }, }); const PlayerProvider = ({ children }: { children: ReactNode }) => { const { address, isConnected } = useAccount(); - const { writeContract } = useWriteContract(); + const { writeContract, error } = useWriteContract(); + + const { data: isRegistered } = useReadContract({ address: contractAddress, abi, @@ -34,9 +38,7 @@ const PlayerProvider = ({ children }: { children: ReactNode }) => { } }); - console.log("IS REGISTERED", isRegistered, address) - - const { data: balance, error, refetch: refetchBalance } = useReadContract({ + const { data: balance, } = useReadContract({ address: contractAddress, abi, functionName: 'balanceOf', @@ -47,9 +49,7 @@ const PlayerProvider = ({ children }: { children: ReactNode }) => { } }); - console.log(balance, error) - - const { data: playerData, refetch: refetchPlayerData } = useReadContract({ + const { data: playerData } = useReadContract({ address: contractAddress, abi, functionName: 'getPlayer', @@ -60,15 +60,27 @@ const PlayerProvider = ({ children }: { children: ReactNode }) => { } }); + console.log(playerData) + + useEffect(() => { + console.warn(error, playerData) + }, [error]) + const register = useCallback(() => { - console.log("START") - const a = writeContract({ + writeContract({ abi, address: contractAddress, functionName: 'register', value: parseEther("0.0005"), }) - console.log(a) + }, [writeContract]) + + const raid = useCallback(() => { + writeContract({ + abi, + address: contractAddress, + functionName: 'raid', + }) }, [writeContract]) return ( @@ -76,7 +88,8 @@ const PlayerProvider = ({ children }: { children: ReactNode }) => { isRegistered: isRegistered as boolean, user: null, balance: balance as bigint, - register + register, + raid }}> {children} diff --git a/deploy_contract.sh b/deploy_contract.sh new file mode 100755 index 0000000..1d29a44 --- /dev/null +++ b/deploy_contract.sh @@ -0,0 +1,3 @@ +#!/bin/sh +cast rpc anvil_setBalance 0x3295CCA2d922c637d35b258fc6c9C7e471803b45 0xDE0B6B3A7640000 --rpc-url http://127.0.0.1:8545 +forge script script/RaidGeld.s.sol:RaidGeldScript --rpc-url 127.0.0.1:8545 --broadcast --private-key $DEV_PRIVATE_KEY diff --git a/src/RaidGeld.sol b/src/RaidGeld.sol index 1e63df8..377e4f1 100644 --- a/src/RaidGeld.sol +++ b/src/RaidGeld.sol @@ -4,6 +4,9 @@ pragma solidity ^0.8.13; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; +// DEV +import "forge-std/console.sol"; + struct Raider { uint16 level; } @@ -25,7 +28,7 @@ contract RaidGeld is ERC20, Ownable { uint8 constant DECIMALS = 4; uint256 public constant BUY_IN_AMOUNT = 0.0005 ether; uint256 public constant INITIAL_GELD = 50 * 10 ** DECIMALS; - uint256 public constant RAID_WAIT = 15; + uint256 public constant RAID_WAIT = 15 seconds; uint256 public total_minted = 0; mapping(address => Player) private players; @@ -71,24 +74,22 @@ contract RaidGeld is ERC20, Ownable { // Manual minting for itchy fingers function raid() external onlyPlayer { require(block.timestamp >= players[msg.sender].last_raided_at + RAID_WAIT, "Tried minting too soon"); - // TODO: Make real calculation based on army uint256 new_geld = 50 * 10 ** decimals(); - _mint(msg.sender, new_geld); - total_minted += new_geld; - + console.log(players[msg.sender].last_raided_at); players[msg.sender].last_raided_at = block.timestamp; + console.log(players[msg.sender].last_raided_at); } // Function to get Player struct - function getPlayer(address addr) public view onlyPlayer returns (Player memory) { + function getPlayer(address addr) public view returns (Player memory) { return players[addr]; } // Function to get Army struct - function getArmy(address addr) public view onlyPlayer returns (Army memory) { + function getArmy(address addr) public view returns (Army memory) { return armies[addr]; } diff --git a/test/RaidGeld.t.sol b/test/RaidGeld.t.sol index 8394da6..26207f0 100644 --- a/test/RaidGeld.t.sol +++ b/test/RaidGeld.t.sol @@ -106,6 +106,7 @@ contract raid_geldTest is Test { vm.startPrank(player1); registerPlayer(); + uint256 balance = raid_geld.balanceOf(player1); uint256 total_minted = raid_geld.total_minted(); @@ -115,6 +116,9 @@ contract raid_geldTest is Test { // New balance should be larger uint256 newBalance = raid_geld.balanceOf(player1); uint256 newTotalMinted = raid_geld.total_minted(); + Player memory player = raid_geld.getPlayer(player1); + uint256 last_raided_at = player.last_raided_at; + assertLt(balance, newBalance); assertLt(total_minted, newTotalMinted); @@ -129,8 +133,11 @@ contract raid_geldTest is Test { // Balance should reflect that uint256 newestBalance = raid_geld.balanceOf(player1); uint256 newestTotalMinted = raid_geld.total_minted(); + player = raid_geld.getPlayer(player1); + uint256 last_raided_at_2 = player.last_raided_at; assertLt(newTotalMinted, newestTotalMinted); assertLt(newBalance, newestBalance); + assertLt(last_raided_at, last_raided_at_2); } function test_06_is_registered() public {