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

refactor: implement outbound and inbound warp route amount transforms #4729

Merged
merged 5 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
61 changes: 7 additions & 54 deletions solidity/contracts/token/extensions/HypNativeScaled.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,62 +17,15 @@ contract HypNativeScaled is HypNative {
scale = _scale;
}

/**
* @inheritdoc HypNative
* @dev Sends scaled `msg.value` (divided by `scale`) to `_recipient`.
*/
function transferRemote(
uint32 _destination,
bytes32 _recipient,
function _outbound(
uint256 _amount
) 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
);
) 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);
function _inbound(
uint256 _amount
) internal view override returns (uint256) {
return _amount * scale;
}
}
20 changes: 17 additions & 3 deletions solidity/contracts/token/libs/TokenRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,11 @@ abstract contract TokenRouter is GasRouter {
address _hook
) internal virtual returns (bytes32 messageId) {
bytes memory _tokenMetadata = _transferFromSender(_amountOrId);

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

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

emit SentTransferRemote(_destination, _recipient, _amountOrId);
emit SentTransferRemote(_destination, _recipient, outboundAmount);
}

function _outbound(
yorhodes marked this conversation as resolved.
Show resolved Hide resolved
uint256 _amountOrId
) internal view virtual returns (uint256) {
return _amountOrId;
}

function _inbound(
yorhodes marked this conversation as resolved.
Show resolved Hide resolved
uint256 _amountOrId
) internal view virtual returns (uint256) {
return _amountOrId;
}

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

Expand Down
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_tranferRemote(uint256 amount) public {
function test_transferRemote(uint256 amount) public {
vm.assume(amount <= mintAmount);

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