Skip to content

Commit

Permalink
revert
Browse files Browse the repository at this point in the history
  • Loading branch information
aroralanuk committed Nov 7, 2024
1 parent 4556e24 commit 99647fe
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 129 deletions.
5 changes: 0 additions & 5 deletions .changeset/friendly-owls-stare.md

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/green-kangaroos-whisper.md

This file was deleted.

25 changes: 0 additions & 25 deletions solidity/contracts/hooks/aggregation/StaticAggregationHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,11 @@ pragma solidity >=0.8.0;
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@*/

// ============ Internal Imports ============
import {StandardHookMetadata} from "../libs/StandardHookMetadata.sol";
import {Message} from "../../libs/Message.sol";
import {TypeCasts} from "../../libs/TypeCasts.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 Message for bytes;
using TypeCasts for bytes32;
using StandardHookMetadata for bytes;
using Address for address payable;

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

/// @inheritdoc IPostDispatchHook
Expand All @@ -44,29 +32,16 @@ contract StaticAggregationHook is AbstractPostDispatchHook {
) internal override {
address[] memory _hooks = hooks(message);
uint256 count = _hooks.length;
uint256 valueRemaining = msg.value;
for (uint256 i = 0; i < count; i++) {
uint256 quote = IPostDispatchHook(_hooks[i]).quoteDispatch(
metadata,
message
);
require(
valueRemaining >= quote,
"StaticAggregationHook: insufficient value"
);

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

valueRemaining -= quote;
}

if (valueRemaining > 0) {
payable(metadata.refundAddress(message.senderAddress())).sendValue(
valueRemaining
);
}
}

Expand Down
61 changes: 54 additions & 7 deletions solidity/contracts/token/extensions/HypNativeScaled.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,62 @@ contract HypNativeScaled is HypNative {
scale = _scale;
}

function _outboundAmount(
/**
* @inheritdoc HypNative
* @dev Sends scaled `msg.value` (divided by `scale`) to `_recipient`.
*/
function transferRemote(
uint32 _destination,
bytes32 _recipient,
uint256 _amount
) internal view override returns (uint256) {
return _amount / scale;
) external payable override returns (bytes32 messageId) {
require(msg.value >= _amount, "Native: amount exceeds msg.value");
uint256 _hookPayment = msg.value - _amount;
uint256 _scaledAmount = _amount / scale;
return
_transferRemote(
_destination,
_recipient,
_scaledAmount,
_hookPayment
);
}

function _inboundAmount(
uint256 _amount
) internal view override returns (uint256) {
return _amount * scale;
/**
* @inheritdoc TokenRouter
* @dev uses (`msg.value` - `_amount`) as hook payment.
*/
function transferRemote(
uint32 _destination,
bytes32 _recipient,
uint256 _amount,
bytes calldata _hookMetadata,
address _hook
) external payable override returns (bytes32 messageId) {
require(msg.value >= _amount, "Native: amount exceeds msg.value");
uint256 _hookPayment = msg.value - _amount;
uint256 _scaledAmount = _amount / scale;
return
_transferRemote(
_destination,
_recipient,
_scaledAmount,
_hookPayment,
_hookMetadata,
_hook
);
}

/**
* @dev Sends scaled `_amount` (multiplied by `scale`) to `_recipient`.
* @inheritdoc TokenRouter
*/
function _transferTo(
address _recipient,
uint256 _amount,
bytes calldata metadata // no metadata
) internal override {
uint256 scaledAmount = _amount * scale;
HypNative._transferTo(_recipient, scaledAmount, metadata);
}
}
34 changes: 3 additions & 31 deletions solidity/contracts/token/libs/TokenRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,9 @@ abstract contract TokenRouter is GasRouter {
address _hook
) internal virtual returns (bytes32 messageId) {
bytes memory _tokenMetadata = _transferFromSender(_amountOrId);

uint256 outboundAmount = _outboundAmount(_amountOrId);
bytes memory _tokenMessage = TokenMessage.format(
_recipient,
outboundAmount,
_amountOrId,
_tokenMetadata
);

Expand All @@ -132,29 +130,7 @@ abstract contract TokenRouter is GasRouter {
_hook
);

emit SentTransferRemote(_destination, _recipient, outboundAmount);
}

/**
* @dev Should return the amount of tokens to be encoded in the message amount (eg for scaling `_localAmount`).
* @param _localAmount The amount of tokens transferred on this chain in local denomination.
* @return _messageAmount The amount of tokens to be encoded in the message body.
*/
function _outboundAmount(
uint256 _localAmount
) internal view virtual returns (uint256 _messageAmount) {
_messageAmount = _localAmount;
}

/**
* @dev Should return the amount of tokens to be decoded from the message amount.
* @param _messageAmount The amount of tokens received in the message body.
* @return _localAmount The amount of tokens to be transferred on this chain in local denomination.
*/
function _inboundAmount(
uint256 _messageAmount
) internal view virtual returns (uint256 _localAmount) {
_localAmount = _messageAmount;
emit SentTransferRemote(_destination, _recipient, _amountOrId);
}

/**
Expand Down Expand Up @@ -187,11 +163,7 @@ abstract contract TokenRouter is GasRouter {
bytes32 recipient = _message.recipient();
uint256 amount = _message.amount();
bytes calldata metadata = _message.metadata();
_transferTo(
recipient.bytes32ToAddress(),
_inboundAmount(amount),
metadata
);
_transferTo(recipient.bytes32ToAddress(), amount, metadata);
emit ReceivedTransferRemote(_origin, recipient, amount);
}

Expand Down
55 changes: 0 additions & 55 deletions solidity/test/hooks/AggregationHook.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ pragma solidity ^0.8.13;

import {Test} from "forge-std/Test.sol";

import {Message} from "../../contracts/libs/Message.sol";
import {TypeCasts} from "../../contracts/libs/TypeCasts.sol";
import {StaticAggregationHook} from "../../contracts/hooks/aggregation/StaticAggregationHook.sol";
import {StaticAggregationHookFactory} from "../../contracts/hooks/aggregation/StaticAggregationHookFactory.sol";
import {TestPostDispatchHook} from "../../contracts/test/TestPostDispatchHook.sol";
import {IPostDispatchHook} from "../../contracts/interfaces/hooks/IPostDispatchHook.sol";

contract AggregationHookTest is Test {
using TypeCasts for address;

StaticAggregationHookFactory internal factory;
StaticAggregationHook internal hook;

Expand Down Expand Up @@ -78,55 +74,6 @@ contract AggregationHookTest is Test {
hook.postDispatch{value: _msgValue}("", message);
}

function test_postDispatch_refundsExcess(
uint8 _hooks,
bytes calldata body
) 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 = Message.formatMessage(
1,
0,
1,
address(this).addressToBytes32(),
2,
address(this).addressToBytes32(),
body
);
hook.postDispatch{value: overpaidValue}("", message);

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

function testPostDispatch_preventsUsingContractFunds(
uint8 _hooks,
bytes calldata body
) public {
uint256 fee = PER_HOOK_GAS_AMOUNT;
deployHooks(_hooks, fee);
vm.assume(_hooks > 0);

vm.prank(address(this));

uint256 additionalFunds = 1 ether;
vm.deal(address(hook), additionalFunds);

bytes memory message = abi.encodePacked("hello world");

vm.expectRevert("StaticAggregationHook: insufficient value");
hook.postDispatch{value: 0}("", message);

assertEq(address(hook).balance, additionalFunds);
}

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

receive() external payable {}
}
2 changes: 1 addition & 1 deletion solidity/test/token/HypNativeScaled.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ contract HypNativeScaledTest is Test {
environment.processNextPendingMessage();
}

function test_transferRemote(uint256 amount) public {
function test_tranferRemote(uint256 amount) public {
vm.assume(amount <= mintAmount);

uint256 nativeValue = amount * (10 ** nativeDecimals);
Expand Down

0 comments on commit 99647fe

Please sign in to comment.