-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEurekaHandler.sol
213 lines (165 loc) · 7.38 KB
/
EurekaHandler.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import {IICS20TransferMsgs, IICS20Transfer} from "./interfaces/eureka/ICS20Transfer.sol";
import {IIBCVoucher} from "./interfaces/lombard/IIBCVoucher.sol";
import {IEurekaHandler} from "./interfaces/IEurekaHandler.sol";
contract EurekaHandler is IEurekaHandler, Initializable, UUPSUpgradeable, OwnableUpgradeable {
using SafeERC20 for IERC20;
address public ics20Transfer;
address public swapRouter;
address public lbtcVoucher;
address public lbtc;
event EurekaTransfer(address indexed token, uint256 amount, uint256 relayFee, address relayFeeRecipient);
constructor() {
_disableInitializers();
}
function initialize(
address _owner,
address _ics20Transfer,
address _swapRouter,
address _lbtcVoucher,
address _lbtc
) external initializer {
__UUPSUpgradeable_init();
__Ownable_init(_owner);
ics20Transfer = _ics20Transfer;
swapRouter = _swapRouter;
lbtcVoucher = _lbtcVoucher;
lbtc = _lbtc;
}
function transfer(uint256 amount, TransferParams memory transferParams, Fees memory fees) external {
require(block.timestamp < fees.quoteExpiry, "Fee quote expired");
// Collect fees
if (fees.relayFee > 0) {
IERC20(transferParams.token).safeTransferFrom(msg.sender, fees.relayFeeRecipient, fees.relayFee);
}
IERC20(transferParams.token).safeTransferFrom(msg.sender, address(this), amount);
_sendTransfer(
IICS20TransferMsgs.SendTransferMsg({
denom: transferParams.token,
amount: amount,
receiver: transferParams.recipient,
sourceClient: transferParams.sourceClient,
destPort: transferParams.destPort,
timeoutTimestamp: transferParams.timeoutTimestamp,
memo: transferParams.memo
})
);
emit EurekaTransfer(transferParams.token, amount, fees.relayFee, fees.relayFeeRecipient);
}
function swapAndTransfer(
address swapInputToken,
uint256 swapInputAmount,
bytes memory swapCalldata,
TransferParams memory transferParams,
Fees memory fees
) external payable {
require(block.timestamp < fees.quoteExpiry, "Fee quote expired");
uint256 amountOut;
if (swapInputToken == address(0)) {
require(msg.value == swapInputAmount, "Insufficient native token");
amountOut = _swapNative(transferParams.token, swapInputAmount, swapCalldata);
} else {
IERC20(swapInputToken).safeTransferFrom(msg.sender, address(this), swapInputAmount);
amountOut = _swap(swapInputToken, transferParams.token, swapInputAmount, swapCalldata);
}
if (amountOut <= _totalFees(fees)) {
revert("Insufficient amount out to cover fees");
}
// Collect fees
if (fees.relayFee > 0) {
IERC20(transferParams.token).safeTransfer(fees.relayFeeRecipient, fees.relayFee);
}
uint256 amountOutAfterFees = amountOut - _totalFees(fees);
_sendTransfer(
IICS20TransferMsgs.SendTransferMsg({
denom: transferParams.token,
amount: amountOutAfterFees,
receiver: transferParams.recipient,
sourceClient: transferParams.sourceClient,
destPort: transferParams.destPort,
timeoutTimestamp: transferParams.timeoutTimestamp,
memo: transferParams.memo
})
);
emit EurekaTransfer(transferParams.token, amountOutAfterFees, fees.relayFee, fees.relayFeeRecipient);
}
function lombardTransfer(
uint256 amount,
uint256 minAmountOut,
TransferParams memory transferParams,
Fees memory fees
) external {
require(block.timestamp < fees.quoteExpiry, "Fee quote expired");
// Collect fees
if (fees.relayFee > 0) {
IERC20(lbtc).safeTransferFrom(msg.sender, fees.relayFeeRecipient, fees.relayFee);
}
IERC20(lbtc).safeTransferFrom(msg.sender, address(this), amount);
IERC20(lbtc).approve(lbtcVoucher, amount);
uint256 voucherAmount = IIBCVoucher(lbtcVoucher).wrap(amount, minAmountOut);
_sendTransfer(
IICS20TransferMsgs.SendTransferMsg({
denom: lbtcVoucher,
amount: voucherAmount,
receiver: transferParams.recipient,
sourceClient: transferParams.sourceClient,
destPort: transferParams.destPort,
timeoutTimestamp: transferParams.timeoutTimestamp,
memo: transferParams.memo
})
);
emit EurekaTransfer(lbtcVoucher, voucherAmount, fees.relayFee, fees.relayFeeRecipient);
}
function lombardSpend(uint256 amount) external {
uint256 lbtcBalanceBefore = IERC20(lbtc).balanceOf(address(this));
IERC20(lbtcVoucher).safeTransferFrom(msg.sender, address(this), amount);
IIBCVoucher(lbtcVoucher).spend(amount);
uint256 lbtcBalanceAfter = IERC20(lbtc).balanceOf(address(this));
IERC20(lbtc).safeTransfer(msg.sender, lbtcBalanceAfter - lbtcBalanceBefore);
}
function _sendTransfer(IICS20TransferMsgs.SendTransferMsg memory transferMsg) internal {
IERC20(transferMsg.denom).approve(ics20Transfer, transferMsg.amount);
IICS20Transfer(ics20Transfer).sendTransferWithSender(transferMsg, msg.sender);
}
function _swap(address tokenIn, address tokenOut, uint256 amountIn, bytes memory swapCalldata)
internal
returns (uint256 amountOut)
{
uint256 tokenOutBalanceBefore = IERC20(tokenOut).balanceOf(address(this));
IERC20(tokenIn).approve(swapRouter, amountIn);
(bool success,) = swapRouter.call(swapCalldata);
if (!success) {
assembly {
returndatacopy(0, 0, returndatasize())
revert(0, returndatasize())
}
}
amountOut = IERC20(tokenOut).balanceOf(address(this)) - tokenOutBalanceBefore;
return amountOut;
}
function _swapNative(address tokenOut, uint256 amountIn, bytes memory swapCalldata)
internal
returns (uint256 amountOut)
{
uint256 tokenOutBalanceBefore = IERC20(tokenOut).balanceOf(address(this));
(bool success,) = swapRouter.call{value: amountIn}(swapCalldata);
if (!success) {
assembly {
returndatacopy(0, 0, returndatasize())
revert(0, returndatasize())
}
}
amountOut = IERC20(tokenOut).balanceOf(address(this)) - tokenOutBalanceBefore;
return amountOut;
}
function _totalFees(Fees memory fees) internal pure returns (uint256) {
return fees.relayFee;
}
function _authorizeUpgrade(address newImplementation) internal override onlyOwner {}
}