Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
aroralanuk committed Nov 11, 2024
1 parent 670912b commit 3fec6a0
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 226 deletions.
126 changes: 88 additions & 38 deletions solidity/contracts/token/HypNative.sol
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.8.0;

/*@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@ HYPERLANE @@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@*/

// ============ Internal Imports ============
import {TokenRouter} from "./libs/TokenRouter.sol";
import {TokenMessage} from "./libs/TokenMessage.sol";
import {StandardHookMetadata} from "../hooks/libs/StandardHookMetadata.sol";

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

/**
* @title Hyperlane Native Token Router that extends ERC20 with remote transfer functionality.
* @title HypNative
* @author Abacus Works
* @dev Supply on each chain is not constant but the aggregate supply across all chains is.
* @notice This contract facilitates the transfer of value between chains using value transfer hooks
*/
contract HypNative is TokenRouter {
/**
Expand All @@ -17,90 +32,125 @@ contract HypNative is TokenRouter {
* @param amount The amount of native tokens donated.
*/
event Donation(address indexed sender, uint256 amount);
// ============ Errors ============

error InsufficientValue(uint256 requiredValue, uint256 providedValue);

constructor(address _mailbox) TokenRouter(_mailbox) {}

// ============ Initialization ============

/**
* @notice Initializes the Hyperlane router
* @param _hook The post-dispatch hook contract.
@param _interchainSecurityModule The interchain security module contract.
@param _owner The this contract.
* @notice Initializes the contract
* @param _valuehook The address of the value transfer hook
* @param _interchainSecurityModule The address of the interchain security module
* @param _owner The owner of the contract
*/
function initialize(
address _hook,
address _valuehook,
address _interchainSecurityModule,
address _owner
) public initializer {
_MailboxClient_initialize(_hook, _interchainSecurityModule, _owner);
_MailboxClient_initialize(
_valuehook,
_interchainSecurityModule,
_owner
);
}

/**
* @inheritdoc TokenRouter
* @dev uses (`msg.value` - `_amount`) as hook payment and `msg.sender` as refund address.
*/
function transferRemote(
uint32 _destination,
bytes32 _recipient,
uint256 _amount
) external payable virtual override returns (bytes32 messageId) {
require(msg.value >= _amount, "Native: amount exceeds msg.value");
uint256 _hookPayment = msg.value - _amount;
return _transferRemote(_destination, _recipient, _amount, _hookPayment);
}
// ============ External Functions ============

/**
* @inheritdoc TokenRouter
* @dev uses (`msg.value` - `_amount`) as hook payment.
* @dev use _hook with caution, make sure that this hook can handle msg.value transfer using the metadata.msgValue()
*/
function transferRemote(
uint32 _destination,
bytes32 _recipient,
uint256 _amount,
bytes calldata _hookMetadata,
address _hook
) external payable virtual override returns (bytes32 messageId) {
require(msg.value >= _amount, "Native: amount exceeds msg.value");
uint256 _hookPayment = msg.value - _amount;
) public payable virtual override returns (bytes32 messageId) {
uint256 quote = _GasRouter_quoteDispatch(
_destination,
_hookMetadata,
_hook
);
if (msg.value < _amount + quote) {
revert InsufficientValue(_amount + quote, msg.value);
}

bytes memory hookMetadata = StandardHookMetadata.overrideMsgValue(
_hookMetadata,
_amount
);

return
_transferRemote(
_destination,
_recipient,
_amount,
_hookPayment,
_hookMetadata,
_amount + quote,
hookMetadata,
_hook
);
}

function balanceOf(
address _account
) external view override returns (uint256) {
return _account.balance;
/// @inheritdoc TokenRouter
function transferRemote(
uint32 _destination,
bytes32 _recipient,
uint256 _amount
) external payable virtual override returns (bytes32 messageId) {
bytes calldata emptyBytes;
assembly {
emptyBytes.length := 0
emptyBytes.offset := 0
}
return
transferRemote(
_destination,
_recipient,
_amount,
emptyBytes,
address(hook)
);
}

// ============ Internal Functions ============

/**
* @inheritdoc TokenRouter
* @dev No-op because native amount is transferred in `msg.value`
* @dev Compiler will not include this in the bytecode.
* @dev No token metadata is needed for value transfers
*/
function _transferFromSender(
uint256
) internal pure override returns (bytes memory) {
return bytes(""); // no metadata
return bytes(""); // no token metadata
}

/**
* @dev Sends `_amount` of native token to `_recipient` balance.
* @inheritdoc TokenRouter
* @dev Sends the value to the recipient
*/
function _transferTo(
address _recipient,
uint256 _amount,
bytes calldata // no metadata
bytes calldata // no token metadata
) internal virtual override {
Address.sendValue(payable(_recipient), _amount);
}

/**
* @inheritdoc TokenRouter
* @dev The user will hold native value
*/
function balanceOf(
address /* _account */
) external pure override returns (uint256) {
return 0;
}

receive() external payable {
emit Donation(msg.sender, msg.value);
}
Expand Down
107 changes: 107 additions & 0 deletions solidity/contracts/token/HypNativeCollateral.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity >=0.8.0;

import {TokenRouter} from "./libs/TokenRouter.sol";
import {TokenMessage} from "./libs/TokenMessage.sol";
import {Address} from "@openzeppelin/contracts/utils/Address.sol";

/**
* @title Hyperlane Native Token Router that extends ERC20 with remote transfer functionality.
* @author Abacus Works
* @dev Supply on each chain is not constant but the aggregate supply across all chains is.
*/
contract HypNativeCollateral is TokenRouter {
/**
* @dev Emitted when native tokens are donated to the contract.
* @param sender The address of the sender.
* @param amount The amount of native tokens donated.
*/
event Donation(address indexed sender, uint256 amount);

constructor(address _mailbox) TokenRouter(_mailbox) {}

/**
* @notice Initializes the Hyperlane router
* @param _hook The post-dispatch hook contract.
* @param _interchainSecurityModule The interchain security module contract.
* @param _owner The this contract.
*/
function initialize(
address _hook,
address _interchainSecurityModule,
address _owner
) public initializer {
_MailboxClient_initialize(_hook, _interchainSecurityModule, _owner);
}

/**
* @inheritdoc TokenRouter
* @dev uses (`msg.value` - `_amount`) as hook payment and `msg.sender` as refund address.
*/
function transferRemote(
uint32 _destination,
bytes32 _recipient,
uint256 _amount
) external payable virtual override returns (bytes32 messageId) {
require(msg.value >= _amount, "Native: amount exceeds msg.value");
uint256 _hookPayment = msg.value - _amount;
return _transferRemote(_destination, _recipient, _amount, _hookPayment);
}

/**
* @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 virtual override returns (bytes32 messageId) {
require(msg.value >= _amount, "Native: amount exceeds msg.value");
uint256 _hookPayment = msg.value - _amount;
return
_transferRemote(
_destination,
_recipient,
_amount,
_hookPayment,
_hookMetadata,
_hook
);
}

function balanceOf(
address _account
) external view override returns (uint256) {
return _account.balance;
}

/**
* @inheritdoc TokenRouter
* @dev No-op because native amount is transferred in `msg.value`
* @dev Compiler will not include this in the bytecode.
*/
function _transferFromSender(
uint256
) internal pure override returns (bytes memory) {
return bytes(""); // no metadata
}

/**
* @dev Sends `_amount` of native token to `_recipient` balance.
* @inheritdoc TokenRouter
*/
function _transferTo(
address _recipient,
uint256 _amount,
bytes calldata // no metadata
) internal virtual override {
Address.sendValue(payable(_recipient), _amount);
}

receive() external payable {
emit Donation(msg.sender, msg.value);
}
}
Loading

0 comments on commit 3fec6a0

Please sign in to comment.