Skip to content

Commit

Permalink
Implement outbound and inbound amount transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
yorhodes committed Oct 22, 2024
1 parent 0301055 commit 2c1b772
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 58 deletions.
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(
uint256 _amountOrId
) internal view virtual returns (uint256) {
return _amountOrId;
}

function _inbound(
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

0 comments on commit 2c1b772

Please sign in to comment.