Skip to content

Commit

Permalink
fix: check address code length + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-something committed Mar 9, 2024
1 parent dd2bd84 commit 46f029f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/JBBuybackHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -560,12 +560,15 @@ contract JBBuybackHook is ERC165, JBPermissioned, IJBBuybackHook {
// Get a reference to the pool that'll be used to make the swap.
IUniswapV3Pool pool = poolOf[projectId][address(terminalToken)];

// Make sure the pool exists.
// Make sure the pool exists, if not, return an empty quote.
if (address(pool).code.length == 0) return 0;

// If there is a contract at the address, try to get the pool's slot 0.
try pool.slot0() returns (uint160, int24, uint16, uint16, uint16, uint8, bool unlocked) {
// If the pool hasn't been initialized, return an empty quote.
if (!unlocked) return 0;
} catch {
// If the address is invalid, or if the pool has not been deployed yet, return an empty quote.
// If the address is invalid, return an empty quote.
return 0;
}

Expand Down
29 changes: 29 additions & 0 deletions test/Unit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,35 @@ contract Test_BuybackHook_Unit is Test {
/// minting
/// from the terminal.
function test_beforePayRecordedContext_useTwapNonDeployedPool(uint256 tokenCount) public {
vm.etch(address(pool), "");
assert(address(pool).code.length == 0);

tokenCount = bound(tokenCount, 1, type(uint120).max);

// Set the relevant context.
beforePayRecordedContext.weight = tokenCount;
beforePayRecordedContext.metadata = "";

// Return values to catch:
JBPayHookSpecification[] memory specificationsReturned;
uint256 weightReturned;

// Test: call `beforePayRecordedWith` - notice we don't mock the pool, as the address should remain empty
vm.prank(terminalStore);
(weightReturned, specificationsReturned) = hook.beforePayRecordedWith(beforePayRecordedContext);

// No hook specifications should be returned.
assertEq(specificationsReturned.length, 0);

// The weight should be returned unchanged.
assertEq(weightReturned, tokenCount);
}

/// @notice Test `beforePayRecordedWith` with a TWAP but an invalid pool address, which should lead to the payment
/// minting from the terminal.
function test_beforePayRecordedContext_useTwapInvalidPool(uint256 tokenCount) public {
vm.etch(address(pool), "12345678");

tokenCount = bound(tokenCount, 1, type(uint120).max);

// Set the relevant context.
Expand Down

0 comments on commit 46f029f

Please sign in to comment.