Skip to content

Commit

Permalink
Feature: Read private struct in contract
Browse files Browse the repository at this point in the history
  • Loading branch information
imsoso committed Nov 6, 2024
1 parent 50bbc6b commit 7f3ab77
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion solidity-scripting/src/W4D3/test/esRNTTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity ^0.8.20;
import {Test, console} from "forge-std/Test.sol";
import {esRNT} from "../esRNT.sol";

contract MyESRNTTest is Test {}
/*
// Contract to read
contract esRNT {
Expand All @@ -22,3 +21,52 @@ contract esRNT {
}
}
*/
contract MyESRNTTest is Test {
esRNT public esRNTContract;
uint256 public blockTime;

function setUp() public {
blockTime = block.timestamp;
esRNTContract = new esRNT();
}

function testReadLocks() public view {
uint256 initSlot = 0;

// The array is stored at the slot determined by:
bytes32 baseSlot = keccak256(abi.encode(initSlot));

for (uint256 i = 0; i < 11; i++) {
// LockInfo struct need 20 + 8 + 32 = 60 bytes, so we need 2 slots
uint256 slotCount = 2;
uint256 structBaseSlot = uint256(baseSlot) + (i * slotCount);

// load slot0 with:user + startTime, 28 bytes
bytes32 slot0Data = vm.load(
address(esRNTContract),
bytes32(structBaseSlot)
);

// address user has 160 bits,from low byte to high
address user = address(uint160(uint256(slot0Data)));
// uint64 startTime has 64 bits,from 160 bytes to 224 bytes
uint64 startTime = uint64(uint256(slot0Data) >> 160);

// load slot1 with:amount, 32 bytes
bytes32 slot1Data = vm.load(
address(esRNTContract),
bytes32(structBaseSlot + slotCount - 1)
);

// uint256 amount has 256 bits
uint256 amount = uint256(slot1Data);

// Log as required:
console.log("Lock", i);
console.log("User:", user);
console.log("StartTime:", startTime);
console.log("Amount:", amount);
console.log("----------------");
}
}
}

0 comments on commit 7f3ab77

Please sign in to comment.