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

Merged
merged 8 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 20 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 @@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@*/

// ============ 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,16 +40,28 @@
) internal override {
address[] memory _hooks = hooks(message);
uint256 count = _hooks.length;
uint256 gasRemaining = msg.value;
Fixed Show fixed Hide fixed
for (uint256 i = 0; i < count; i++) {
uint256 quote = IPostDispatchHook(_hooks[i]).quoteDispatch(
metadata,
message
);

require(
gasRemaining >= quote,
"StaticAggregationHook: Insufficient value"
);

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

gasRemaining -= quote;
}

if (gasRemaining > 0) {
payable(metadata.refundAddress(msg.sender)).sendValue(gasRemaining);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
payable(metadata.refundAddress(msg.sender)).sendValue(gasRemaining);
payable(metadata.refundAddress(message.sender())).sendValue(gasRemaining);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you did not make this change...

}
}

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