Skip to content

Commit dff998d

Browse files
committed
changed stateless lib to helper contract
1 parent ce659ea commit dff998d

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed
Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@ import { Extsload } from "../lib/Extsload.sol";
55
import { ERC6909 } from "solady/tokens/ERC6909.sol";
66
import { Tstorish } from "../lib/Tstorish.sol";
77

8-
library UtilityLib {
8+
contract Utility {
99
address internal constant THE_COMPACT = address(0x00000000000000171ede64904551eeDF3C6C9788);
1010
address internal constant TSTORE_TEST_CONTRACT = address(0x627c1071d6A691688938Bb856659768398262690);
1111

1212
uint256 internal constant REENTRANCY_GUARD_SLOT = 0x929eee149b4bd21268;
1313

14+
bool internal immutable TSTORE_INITIAL_SUPPORT;
15+
1416
error TheCompactNotDeployed();
1517
error BalanceNotSettled();
1618

19+
constructor() {
20+
TSTORE_INITIAL_SUPPORT = checkTstoreAvailable();
21+
}
22+
1723
/// @notice Checks if the Compact is deployed and if transient storage is available on the chain.
1824
function checkTstoreAvailable() internal view returns (bool ok) {
1925
if (TSTORE_TEST_CONTRACT.code.length == 0) {
@@ -33,21 +39,23 @@ library UtilityLib {
3339
/// @notice Returns the users balance only if reentrancy protection is not active on the Compact. This eliminates in flight balances before the ERC6909 tokens were burned.
3440
/// @dev The function favors chains supporting eip-1153 (transient storage)
3541
function settledBalanceOf(address owner, uint256 id) internal view returns (uint256 amount) {
36-
// Favoring transient storage
37-
try Extsload(THE_COMPACT).exttload(bytes32(REENTRANCY_GUARD_SLOT)) returns (bytes32 reentrancySlotContent) {
38-
if (uint256(reentrancySlotContent) > 1) {
39-
revert BalanceNotSettled();
40-
}
41-
} catch {
42-
// If the call fails, assume transient storage is not available, so falling back to persistent storage
43-
try Extsload(THE_COMPACT).extsload(bytes32(REENTRANCY_GUARD_SLOT)) returns (bytes32 reentrancySlotContent) {
44-
if (uint256(reentrancySlotContent) > 1) {
45-
revert BalanceNotSettled();
46-
}
47-
} catch {
48-
// If the call fails as well, assume the compact is not deployed
49-
revert TheCompactNotDeployed();
50-
}
42+
bytes32 reentrancySlotContent;
43+
44+
if (TSTORE_INITIAL_SUPPORT) {
45+
// Only check the tstore reentrancy guard slot
46+
reentrancySlotContent = Extsload(THE_COMPACT).exttload(bytes32(REENTRANCY_GUARD_SLOT));
47+
} else {
48+
// Check both slots to cover the potential transition period
49+
try Extsload(THE_COMPACT).exttload(bytes32(REENTRANCY_GUARD_SLOT)) returns (bytes32 content) {
50+
reentrancySlotContent = content;
51+
} catch { }
52+
53+
// Independent of the result, check the persistent storage slot
54+
reentrancySlotContent |= Extsload(THE_COMPACT).extsload(bytes32(REENTRANCY_GUARD_SLOT));
55+
}
56+
57+
if (uint256(reentrancySlotContent) > 1) {
58+
revert BalanceNotSettled();
5159
}
5260

5361
// If we get here, the balance is settled, so returning the balance

test/utility/UtilityLib.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.0;
33

4-
import { UtilityLib } from "../../src/utility/UtilityLib.sol";
4+
import { Utility } from "../../src/utility/Utility.sol";
55
import { TheCompact } from "../../src/TheCompact.sol";
66
import { ERC20 } from "solady/tokens/ERC20.sol";
77

0 commit comments

Comments
 (0)