-
Notifications
You must be signed in to change notification settings - Fork 75
feat: Add simple Hypercore Depositor handler #1181
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8ce6ac5
feat: Add simple Hypercore Depositor handler
nicholaspai 4e697e0
add package.json
nicholaspai f59ca75
remove from .gitmodules
nicholaspai 9b0732c
potential typo
nicholaspai 631a03d
remove drainLeftoverTokens logic
nicholaspai 786ffb9
remove permissioning
nicholaspai 8bfd133
fix import
nicholaspai a7790bd
add safeTransferFrom
nicholaspai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // SPDX-License-Identifier: GPL-3.0-only | ||
| pragma solidity ^0.8.0; | ||
|
|
||
| import "../interfaces/SpokePoolMessageHandler.sol"; | ||
| import "@openzeppelin/contracts-v4/token/ERC20/IERC20.sol"; | ||
| import "@openzeppelin/contracts-v4/token/ERC20/utils/SafeERC20.sol"; | ||
| import "@openzeppelin/contracts-v4/security/ReentrancyGuard.sol"; | ||
| import { CoreWriterLib, PrecompileLib } from "hyper-evm-lib/src/CoreWriterLib.sol"; | ||
| import { HLConversions } from "hyper-evm-lib/src/common/HLConversions.sol"; | ||
|
|
||
| /** | ||
| * @title Allows caller to bridge tokens from HyperEVM to Hypercore and send them to an end user's account | ||
| * on Hypercore. | ||
| * @dev This contract should only be deployed on HyperEVM. | ||
| * @dev This contract can replace a MulticallHandler on HyperEVM if the intent only wants to deposit tokens into | ||
| * Hypercore and bypass the other complex arbitrary calldata logic. | ||
| * @dev This contract can also be called directly to deposit tokens into Hypercore on behalf of an end user. | ||
| */ | ||
| contract HypercoreDepositorHandler is AcrossMessageHandler, ReentrancyGuard { | ||
| using SafeERC20 for IERC20; | ||
|
|
||
| /** | ||
| * @notice Bridges tokens from HyperEVM to Hypercore and sends them to the end user's account on Hypercore. | ||
| * @dev Requires msg.sender to have approved this contract to spend the tokens. | ||
| * @param token The address of the token to deposit. | ||
| * @param amount The amount of tokens on HyperEVM to deposit. | ||
| * @param user The address of the user on Hypercore to send the tokens to. | ||
| */ | ||
| function depositToHypercore(address token, uint256 amount, address user) external nonReentrant { | ||
| IERC20(token).safeTransferFrom(msg.sender, address(this), amount); | ||
| _bridgeToCore(token, amount); | ||
| _spotSend(user, token, amount); | ||
| } | ||
|
|
||
| /** | ||
| * @notice Entrypoint function if this contract is called by the SpokePool contract following an intent fill. | ||
| * @dev Deposits tokens into Hypercore and sends them to the end user's account on Hypercore. | ||
| * @param token The address of the token sent. | ||
| * @param amount The amount of tokens received by this contract. | ||
| * @param message Encoded end user address. | ||
| */ | ||
| function handleV3AcrossMessage( | ||
| address token, | ||
| uint256 amount, | ||
| address /* relayer */, | ||
| bytes memory message | ||
| ) external nonReentrant { | ||
| address user = abi.decode(message, (address)); | ||
| _bridgeToCore(token, amount); | ||
| _spotSend(user, token, amount); | ||
| } | ||
|
|
||
| function _bridgeToCore(address token, uint256 evmAmount) internal { | ||
| // Bridge tokens from HyperEVM to Hypercore. This call should revert if this contract has insufficient balance. | ||
| CoreWriterLib.bridgeToCore(token, evmAmount); | ||
| } | ||
|
|
||
| function _spotSend(address user, address token, uint256 evmAmount) internal { | ||
| // Convert EVM amount to wei amount (used in HyperCore) | ||
| uint64 tokenIndex = PrecompileLib.getTokenIndex(token); | ||
| uint64 coreAmount = HLConversions.evmToWei(tokenIndex, evmAmount); | ||
|
|
||
| // use CoreWriterLib to call the spotSend CoreWriter action and send tokens to end user. | ||
| // @dev: the following call does not execute atomically with the deposit into Hypercore. | ||
| // Therefore, this contract will maintain a balance of tokens for one block until the spot send into Hypercore | ||
| // is confirmed. | ||
| CoreWriterLib.spotSend(user, tokenIndex, coreAmount); | ||
| } | ||
|
|
||
| // Native tokens are not supported by this contract. | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10761,6 +10761,10 @@ husky@^4.2.3: | |
| slash "^3.0.0" | ||
| which-pm-runs "^1.0.0" | ||
|
|
||
| hyper-evm-lib@hyperliquid-dev/hyper-evm-lib: | ||
| version "0.0.0" | ||
| resolved "https://codeload.github.com/hyperliquid-dev/hyper-evm-lib/tar.gz/ee5e5e8593e9265fca35719e4efaa4fd3092123e" | ||
|
|
||
| [email protected]: | ||
| version "0.4.24" | ||
| resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b" | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This assumes that the tokens are dropped on the contract atomically, right?
What are the downsides of doing a
transferFromfrommsg.senderto pull the tokens in?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's a great call, I overlooked this flow. We definitely should pull funds from the msg.sender in
depositToHypercore, unlike inhandleAcrossV3Message