ghost-dao-contracts/test/gatekeeper/GatekeeperHistory.t.sol
Uncle Fatso fe9f92281b
make the dream a reality; treasury buyBack added
Signed-off-by: Uncle Fatso <uncle.fatso@ghostchain.io>
2026-09-06 16:58:29 +03:00

146 lines
5.7 KiB
Solidity

pragma solidity 0.8.20;
import {Test} from "forge-std/Test.sol";
import {Gatekeeper} from "../../src/Gatekeeper.sol";
import {IStorageHistory} from "../../src/interfaces/IStorageHistory.sol";
import {IGatekeeper} from "../../src/interfaces/IGatekeeper.sol";
import {RequestPacking} from "../../src/libraries/Packing.sol";
import {StorageHistory} from "../../src/types/StorageHistory.sol";
import {WETH9} from "../../src/mocks/WETH9.sol";
contract MockStaking is Test {
Gatekeeper public gatekeeper;
WETH9 public mockReserve;
mapping(address => uint256) private _recalledAmounts;
constructor() {
mockReserve = new WETH9();
StorageHistory history = new StorageHistory();
gatekeeper = new Gatekeeper(address(history));
gatekeeper.initialize(address(0));
history.setOwner(address(gatekeeper));
}
function redoGatekeeper() external {
Gatekeeper newGatekeeper = new Gatekeeper(address(0));
newGatekeeper.initialize(address(gatekeeper));
address storageHistory = IGatekeeper(gatekeeper).storageHistory();
IStorageHistory(storageHistory).setOwner(address(newGatekeeper));
gatekeeper = newGatekeeper;
}
function runGhost(bytes32 receiver, uint256 amount) external {
gatekeeper.ghost(receiver, amount);
}
function runRecall(uint256 exodusSession, uint256 amount, uint256 packed) external {
vm.prank(address(gatekeeper));
gatekeeper.recall(exodusSession, amount, packed);
}
function recall(address receiver, uint256 amount) external {
_recalledAmounts[receiver] += amount;
}
function recall(uint256 amount, uint256, uint256) external returns (address, uint256, uint256){
_recalledAmounts[tx.origin] += amount;
return (address(mockReserve), amount, 0);
}
function recalledAmount(address who) external view returns (uint256) {
return _recalledAmounts[who];
}
}
contract GatekeeperStorageHistoryTest is Test {
using RequestPacking for RequestPacking.RequestPayload;
address constant ALICE = 0x0000000000000000000000000000000000000001;
address constant BOB = 0x0000000000000000000000000000000000000002;
uint256 constant INIT_AMOUNT = 1337 * 1e18;
Gatekeeper gatekeeper;
MockStaking staking;
function setUp() public {
vm.prank(ALICE);
staking = new MockStaking();
staking.runGhost(bytes32(abi.encodePacked(ALICE)), INIT_AMOUNT);
gatekeeper = staking.gatekeeper();
}
function test_correctStorageHistoryInitialization() public view {
address storageHistory = gatekeeper.storageHistory();
IStorageHistory.DeploymentSnapshot memory snapshot = IStorageHistory(storageHistory).deploymentSnapshot();
assertEq(snapshot.deployedAt, 0);
assertEq(snapshot.amountIn, INIT_AMOUNT);
assertEq(snapshot.amountOut, 0);
assertEq(gatekeeper.ghostedSupply(), INIT_AMOUNT);
}
function test_historicalAmountsOnlyIncrease(uint256 ghostAmount, uint64 exodusSession) public {
vm.assume(ghostAmount > gatekeeper.EXISTENTIAL_DEPOSIT() && ghostAmount < INIT_AMOUNT);
uint256 amountToGhost = ghostAmount;
uint256 amountToMaterialize = ghostAmount / 2;
bytes32 receiver = bytes32(abi.encodePacked(ALICE));
address storageHistory = gatekeeper.storageHistory();
IStorageHistory.DeploymentSnapshot memory snapshot = IStorageHistory(storageHistory).deploymentSnapshot();
uint104 amountIn = snapshot.amountIn;
uint104 amountOut = snapshot.amountOut;
if (ghostAmount % 2 == 0) {
staking.runGhost(receiver, amountToGhost);
// forge-lint: disable-next-line(unsafe-typecast)
amountIn += uint104(amountToGhost);
} else {
// forge-lint: disable-next-line(unsafe-typecast)
uint256 packed = RequestPacking.pack(0, uint64(block.chainid), BOB);
uint256 previousAmount = staking.recalledAmount(BOB);
vm.prank(BOB, BOB);
staking.runRecall(exodusSession, amountToMaterialize, packed);
assertEq(previousAmount + amountToMaterialize, staking.recalledAmount(BOB));
// forge-lint: disable-next-line(unsafe-typecast)
amountOut += uint104(amountToMaterialize);
}
IStorageHistory.DeploymentSnapshot memory newSnapshot = IStorageHistory(storageHistory).deploymentSnapshot();
assertEq(newSnapshot.amountIn, amountIn);
assertEq(newSnapshot.amountOut, amountOut);
assertEq(gatekeeper.ghostedSupply(), amountIn - amountOut);
}
function test_inheritanceWorksForHistoricalStorge() public {
uint256 exodusSession = 69;
uint256 packed = RequestPacking.pack(0, uint64(block.chainid), BOB);
staking.runRecall(exodusSession, INIT_AMOUNT, packed);
staking.runGhost(bytes32(abi.encodePacked(ALICE)), INIT_AMOUNT);
address prevStorageHistory = gatekeeper.storageHistory();
IStorageHistory.DeploymentSnapshot memory prevSnapshot = IStorageHistory(prevStorageHistory).deploymentSnapshot();
staking.redoGatekeeper();
assert(gatekeeper != staking.gatekeeper());
address currStorageHistory = gatekeeper.storageHistory();
IStorageHistory.DeploymentSnapshot memory currSnapshot = IStorageHistory(currStorageHistory).deploymentSnapshot();
assertEq(prevSnapshot.amountIn, currSnapshot.amountIn);
assertEq(prevSnapshot.amountOut, currSnapshot.amountOut);
assertEq(prevSnapshot.deployedAt, currSnapshot.deployedAt);
assert(IStorageHistory(prevStorageHistory).isTransactionExecuted(exodusSession));
assert(IStorageHistory(currStorageHistory).isTransactionExecuted(exodusSession));
}
}