-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathBridgingExample.sol
More file actions
121 lines (100 loc) · 4.64 KB
/
BridgingExample.sol
File metadata and controls
121 lines (100 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {CoreWriterLib, HLConstants, HLConversions} from "@hyper-evm-lib/src/CoreWriterLib.sol";
import {PrecompileLib} from "@hyper-evm-lib/src/PrecompileLib.sol";
/**
* @title BridgingExample
* @dev This contract demonstrates CoreWriterLib bridging functionality.
* Provides examples for bridging tokens between EVM and Core using both
* token IDs and token addresses.
*/
contract BridgingExample {
using CoreWriterLib for *;
/*//////////////////////////////////////////////////////////////
EVM to Core Bridging
//////////////////////////////////////////////////////////////*/
/**
* @notice Bridges tokens from EVM to Core using token ID
*/
function bridgeToCoreById(uint64 tokenId, uint256 evmAmount) external payable {
// Bridge tokens to core using token ID
CoreWriterLib.bridgeToCore(tokenId, evmAmount);
}
/**
* @notice Bridges tokens from EVM to Core using token address
*/
function bridgeToCoreByAddress(address tokenAddress, uint256 evmAmount) external payable {
// Bridge tokens to core using token address
CoreWriterLib.bridgeToCore(tokenAddress, evmAmount);
}
/**
* @notice Bridges USDC from EVM to Core for a specific recipient
* @param recipient The address that will receive the USDC on Core
* @param evmAmount The amount of USDC to bridge (in EVM decimals)
*/
function bridgeUsdcToCoreFor(address recipient, uint256 evmAmount) external payable {
// Bridge USDC to core for a specific recipient
CoreWriterLib.bridgeUsdcToCoreFor(recipient, evmAmount, HLConstants.SPOT_DEX);
}
/*//////////////////////////////////////////////////////////////
Core to EVM Bridging
//////////////////////////////////////////////////////////////*/
/**
* @notice Bridges tokens from Core to EVM using token ID
*/
function bridgeToEvmById(uint64 tokenId, uint256 evmAmount) external {
// Bridge tokens from core to EVM using token ID
// Note: For non-HYPE tokens, the contract must hold some HYPE on core for gas
CoreWriterLib.bridgeToEvm(tokenId, evmAmount, true);
}
/**
* @notice Bridges tokens from Core to EVM using token address
*/
function bridgeToEvmByAddress(address tokenAddress, uint256 evmAmount) external {
// Bridge tokens from core to EVM using token address
// Note: For non-HYPE tokens, the contract must hold some HYPE on core for gas
CoreWriterLib.bridgeToEvm(tokenAddress, evmAmount);
}
/**
* @notice Bridges HYPE tokens from Core to EVM
* @param evmAmount Amount of HYPE tokens to bridge (in EVM decimals)
*/
function bridgeHypeToEvm(uint256 evmAmount) external {
// Bridge HYPE tokens from core to EVM
CoreWriterLib.bridgeToEvm(HLConstants.hypeTokenIndex(), evmAmount, true);
}
/*//////////////////////////////////////////////////////////////
Advanced Bridging Example
//////////////////////////////////////////////////////////////*/
/**
* @notice Bridges tokens to core and then sends them to another address
*/
function bridgeToCoreAndSend(address tokenAddress, uint256 evmAmount, address recipient) external payable {
// Get token ID from address
uint64 tokenId = PrecompileLib.getTokenIndex(tokenAddress);
// Bridge tokens to core
CoreWriterLib.bridgeToCore(tokenAddress, evmAmount);
// Convert EVM amount to core amount
uint64 coreAmount = HLConversions.evmToWei(tokenId, evmAmount);
// Send tokens to recipient on core
CoreWriterLib.spotSend(recipient, tokenId, coreAmount);
}
function bridgeToCoreAndSendHype(uint256 evmAmount, address recipient) external payable {
// Bridge tokens to core
CoreWriterLib.bridgeToCore(HLConstants.hypeTokenIndex(), evmAmount);
// Convert EVM amount to core amount
uint64 coreAmount = HLConversions.evmToWei(HLConstants.hypeTokenIndex(), evmAmount);
// Send tokens to recipient on core
CoreWriterLib.spotSend(recipient, HLConstants.hypeTokenIndex(), coreAmount);
}
/*//////////////////////////////////////////////////////////////
Utility Functions
//////////////////////////////////////////////////////////////*/
/**
* @notice Gets the token index for a given token address
*/
function getTokenIndex(address tokenAddress) external view returns (uint64 tokenId) {
return PrecompileLib.getTokenIndex(tokenAddress);
}
receive() external payable {}
}