-
Notifications
You must be signed in to change notification settings - Fork 15
/
HelloTokenNative.sol
102 lines (88 loc) · 3.14 KB
/
HelloTokenNative.sol
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
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import "wormhole-solidity-sdk/WormholeRelayerSDK.sol";
import "wormhole-solidity-sdk/interfaces/IERC20.sol";
import "wormhole-solidity-sdk/interfaces/IWETH.sol";
contract HelloTokenNative is TokenSender, TokenReceiver {
uint256 constant GAS_LIMIT = 250_000;
constructor(
address _wormholeRelayer,
address _tokenBridge,
address _wormhole
) TokenBase(_wormholeRelayer, _tokenBridge, _wormhole) {}
function quoteCrossChainDeposit(
uint16 targetChain
) public view returns (uint256 cost) {
// Cost of delivering token and payload to targetChain
uint256 deliveryCost;
(deliveryCost, ) = wormholeRelayer.quoteEVMDeliveryPrice(
targetChain,
0,
GAS_LIMIT
);
// Total cost: delivery cost + cost of publishing the 'sending token' wormhole message
cost = deliveryCost + wormhole.messageFee();
}
function sendCrossChainDeposit(
uint16 targetChain,
address targetHelloToken,
address recipient,
uint256 amount,
address token
) public payable {
uint256 cost = quoteCrossChainDeposit(targetChain);
require(
msg.value == cost,
"msg.value must be quoteCrossChainDeposit(targetChain)"
);
IERC20(token).transferFrom(msg.sender, address(this), amount);
bytes memory payload = abi.encode(recipient);
sendTokenWithPayloadToEvm(
targetChain,
targetHelloToken, // address (on targetChain) to send token and payload to
payload,
0, // receiver value
GAS_LIMIT,
token, // address of IERC20 token contract
amount
);
}
function sendNativeCrossChainDeposit(
uint16 targetChain,
address targetHelloToken,
address recipient,
uint256 amount
) public payable {
uint256 cost = quoteCrossChainDeposit(targetChain);
require(
msg.value == cost + amount,
"msg.value must be quoteCrossChainDeposit(targetChain) + amount"
);
IWETH wrappedNativeToken = tokenBridge.WETH();
wrappedNativeToken.deposit{value: amount}();
bytes memory payload = abi.encode(recipient);
sendTokenWithPayloadToEvm(
targetChain,
targetHelloToken, // address (on targetChain) to send token and payload to
payload,
0, // receiver value
GAS_LIMIT,
address(wrappedNativeToken), // address of IERC20 token contract
amount
);
}
function receivePayloadAndTokens(
bytes memory payload,
TokenReceived[] memory receivedTokens,
bytes32, // sourceAddress
uint16,
bytes32 // deliveryHash
) internal override onlyWormholeRelayer {
require(receivedTokens.length == 1, "Expected 1 token transfers");
address recipient = abi.decode(payload, (address));
IERC20(receivedTokens[0].tokenAddress).transfer(
recipient,
receivedTokens[0].amount
);
}
}