Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(contracts): check for sufficient msgValue for AggregationHook #4673

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions solidity/contracts/hooks/aggregation/StaticAggregationHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@ pragma solidity >=0.8.0;
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@*/

// ============ Internal Imports ============
import {StandardHookMetadata} from "../libs/StandardHookMetadata.sol";
import {AbstractPostDispatchHook} from "../libs/AbstractPostDispatchHook.sol";
import {IPostDispatchHook} from "../../interfaces/hooks/IPostDispatchHook.sol";
import {MetaProxy} from "../../libs/MetaProxy.sol";

// ============ External Imports ============
import {Address} from "@openzeppelin/contracts/utils/Address.sol";

contract StaticAggregationHook is AbstractPostDispatchHook {
using StandardHookMetadata for bytes;
using Address for address payable;

// ============ External functions ============

/// @inheritdoc IPostDispatchHook
Expand All @@ -32,17 +40,26 @@ contract StaticAggregationHook is AbstractPostDispatchHook {
) internal override {
address[] memory _hooks = hooks(message);
uint256 count = _hooks.length;
uint256 gasRemaining = msg.value;
for (uint256 i = 0; i < count; i++) {
uint256 quote = IPostDispatchHook(_hooks[i]).quoteDispatch(
metadata,
message
);

gasRemaining -= quote;
IPostDispatchHook(_hooks[i]).postDispatch{value: quote}(
metadata,
message
);
}

if (gasRemaining > 0) {
address payable refundAddress = payable(
metadata.refundAddress(msg.sender)
);
refundAddress.sendValue(gasRemaining);
}
}

/// @inheritdoc AbstractPostDispatchHook
Expand Down
31 changes: 31 additions & 0 deletions solidity/test/hooks/AggregationHook.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,35 @@ contract AggregationHookTest is Test {
hook.postDispatch{value: _msgValue}("", message);
}

function test_postDispatch_refundsExcess(uint8 _hooks) public {
uint256 fee = PER_HOOK_GAS_AMOUNT;
address[] memory hooksDeployed = deployHooks(_hooks, fee);
uint256 requiredValue = hooksDeployed.length * fee;
uint256 overpaidValue = requiredValue + 1000;

vm.prank(address(this));

uint256 initialBalance = address(this).balance;

bytes memory message = abi.encodePacked("hello world");
hook.postDispatch{value: overpaidValue}("", message);

assertEq(address(hook).balance, 0);
assertEq(address(this).balance, initialBalance - requiredValue);
}

function testPostDispatch_preventsUsingContractFunds(uint8 _hooks) public {
vm.assume(_hooks > 0);

// aggregation hook has left over funds
uint256 additionalFunds = 1 ether;
vm.deal(address(hook), additionalFunds);

bytes memory message = abi.encodePacked("hello world");
vm.expectRevert(); // underflow
hook.postDispatch{value: 0}("", message);
}

function testQuoteDispatch(uint8 _hooks) public {
uint256 fee = PER_HOOK_GAS_AMOUNT;
address[] memory hooksDeployed = deployHooks(_hooks, fee);
Expand All @@ -94,4 +123,6 @@ contract AggregationHookTest is Test {
deployHooks(1, 0);
assertEq(hook.hookType(), uint8(IPostDispatchHook.Types.AGGREGATION));
}

receive() external payable {}
}
Loading