Skip to content

Instantly share code, notes, and snippets.

@aalavandhan
Last active January 16, 2025 17:29
Show Gist options
  • Save aalavandhan/8efe0aaa4f33f8101702079a6a71c90e to your computer and use it in GitHub Desktop.
Save aalavandhan/8efe0aaa4f33f8101702079a6a71c90e to your computer and use it in GitHub Desktop.
AmplGonBalanceChangeTest.sol
pragma solidity ^0.8.13;
import "forge-std/Test.sol";
import "forge-std/console2.sol";
contract AmplGonBalanceChangeTest is Test {
// The proxy address of the UFragments (AMPL) contract.
// Replace with the correct **proxy** address in your environment.
address internal constant AMPL_PROXY = 0xD46bA6D942050d489DBd938a2C909A5d5039A161;
// The user whose balances we’re inspecting
address internal constant USER = 0x223592a191ECfC7FDC38a9256c3BD96E771539A9;
// The known slot index for _gonBalances in UFragments is 158 (zero-based in the storage layout).
uint256 internal constant GON_BALANCES_SLOT_INDEX = 158;
function setUp() public {
vm.createSelectFork(vm.rpcUrl("mainnet"), 20127143);
}
function testGonBalanceChange() external {
// -----------------------------------------
// 1. Read old balanceOf(USER)
// -----------------------------------------
(bool ok, bytes memory data) =
AMPL_PROXY.staticcall(
abi.encodeWithSignature("balanceOf(address)", USER)
);
require(ok, "balanceOf call #1 failed");
uint256 oldBal = abi.decode(data, (uint256));
console2.log("=== Before writing to storage ===");
console2.log("old balanceOf(user) =", oldBal);
// -----------------------------------------
// 2. Read old gonBalance(USER)
// _gonBalances[USER] => keccak256(abi.encode(USER, 14))
// -----------------------------------------
bytes32 gonSlot = keccak256(abi.encode(USER, GON_BALANCES_SLOT_INDEX));
bytes32 rawGons = vm.load(AMPL_PROXY, gonSlot);
uint256 oldGons = uint256(rawGons);
console2.log("old gonBalance(user) =", oldGons);
// -----------------------------------------
// 3. Update gonBalance to some new value
// -----------------------------------------
uint256 newGons = 11164885435515777500657914160541365361715642327198475026108499964265553105050;
vm.store(AMPL_PROXY, gonSlot, bytes32(newGons));
// -----------------------------------------
// 4. Read new balanceOf(USER)
// -----------------------------------------
(ok, data) = AMPL_PROXY.staticcall(
abi.encodeWithSignature("balanceOf(address)", USER)
);
require(ok, "balanceOf call #2 failed");
uint256 newBal = abi.decode(data, (uint256));
console2.log("=== After writing to storage ===");
console2.log("new balanceOf(user) =", newBal);
// -----------------------------------------
// 5. Read new gonBalance(USER)
// -----------------------------------------
rawGons = vm.load(AMPL_PROXY, gonSlot);
uint256 updatedGons = uint256(rawGons);
console2.log("new gonBalance(user) =", updatedGons);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment