/
HashflowPool.sol
649 lines (578 loc) · 19.1 KB
/
HashflowPool.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/**
* SPDX-License-Identifier: UNLICENSED
*/
pragma solidity 0.8.18;
import '@openzeppelin/contracts/proxy/utils/Initializable.sol';
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
import '@openzeppelin/contracts/utils/cryptography/ECDSA.sol';
import '@openzeppelin/contracts/utils/Context.sol';
import '../interfaces/external/IWETH.sol';
import '../interfaces/IHashflowPool.sol';
import '../interfaces/IHashflowRouter.sol';
interface IERC20AllowanceExtension {
function increaseAllowance(address spender, uint256 addedValue)
external
returns (bool);
function decreaseAllowance(address spender, uint256 subtractedValue)
external
returns (bool);
}
contract HashflowPool is IHashflowPool, Initializable, Context {
using Address for address payable;
using SafeERC20 for IERC20;
using ECDSA for bytes32;
string public name;
SignerConfiguration public signerConfiguration;
address public operations;
address public router;
mapping(address => uint256) public nonces;
mapping(bytes32 => uint256) public xChainNonces;
mapping(address => bool) internal _withrawalAccountAuth;
mapping(bytes32 => bool) internal _filledXChainTxids;
address public immutable _WETH;
constructor(address weth) {
require(
weth != address(0),
'HashflowPool::constructor WETH cannot be 0 address.'
);
_WETH = weth;
}
/// @dev Fallback function to receive native token.
receive() external payable {}
/// @inheritdoc IHashflowPool
function initialize(
string memory _name,
address _signer,
address _operations,
address _router
) public override initializer {
require(
_signer != address(0),
'HashflowPool::initialize Signer cannot be 0 address.'
);
require(
_operations != address(0),
'HashflowPool::initialize Operations cannot be 0 address.'
);
require(
_router != address(0),
'HashflowPool::initialize Router cannot be 0 address.'
);
require(
bytes(_name).length > 0,
'HashflowPool::initialize Name cannot be empty'
);
name = _name;
SignerConfiguration memory signerConfig;
signerConfig.enabled = true;
signerConfig.signer = _signer;
emit UpdateSigner(_signer, address(0));
signerConfiguration = signerConfig;
operations = _operations;
router = _router;
}
modifier authorizedOperations() {
require(
_msgSender() == operations,
'HashflowPool:authorizedOperations Sender must be operator.'
);
_;
}
modifier authorizedRouter() {
require(
_msgSender() == router,
'HashflowPool::authorizedRouter Sender must be Router.'
);
_;
}
/// @inheritdoc IHashflowPool
function tradeRFQT(RFQTQuote memory quote)
external
payable
override
authorizedRouter
{
/// Trust assumption: the Router has transferred baseToken.
require(
quote.baseToken != address(0) ||
quote.externalAccount != address(0) ||
msg.value == quote.effectiveBaseTokenAmount,
'HashflowPool::tradeRFQT msg.value must equal effectiveBaseTokenAmount'
);
bytes32 quoteHash = _hashQuoteRFQT(quote);
SignerConfiguration memory signerConfig = signerConfiguration;
require(signerConfig.enabled, 'HashflowPool::tradeRFQT Disabled.');
require(
quoteHash.recover(quote.signature) == signerConfig.signer,
'HashflowPool::tradeRFQT Invalid signer.'
);
_updateNonce(quote.effectiveTrader, quote.nonce);
uint256 quoteTokenAmount = quote.quoteTokenAmount;
if (quote.effectiveBaseTokenAmount < quote.baseTokenAmount) {
quoteTokenAmount =
(quote.effectiveBaseTokenAmount * quote.quoteTokenAmount) /
quote.baseTokenAmount;
}
emit Trade(
quote.trader,
quote.effectiveTrader,
quote.txid,
quote.baseToken,
quote.quoteToken,
quote.effectiveBaseTokenAmount,
quoteTokenAmount
);
if (quote.externalAccount == address(0)) {
_transferFromPool(quote.quoteToken, quote.trader, quoteTokenAmount);
} else {
_transferFromExternalAccount(
quote.externalAccount,
quote.quoteToken,
quote.trader,
quoteTokenAmount
);
}
}
/// @inheritdoc IHashflowPool
function tradeRFQM(RFQMQuote memory quote)
external
override
authorizedRouter
{
SignerConfiguration memory signerConfig = signerConfiguration;
require(signerConfig.enabled, 'HashflowPool::tradeRFQM Disabled.');
bytes32 quoteHash = _hashQuoteRFQM(quote);
require(
quoteHash.recover(quote.makerSignature) == signerConfig.signer,
'HashflowPool::tradeRFQM Invalid signer.'
);
emit Trade(
quote.trader,
quote.trader,
quote.txid,
quote.baseToken,
quote.quoteToken,
quote.baseTokenAmount,
quote.quoteTokenAmount
);
if (quote.externalAccount == address(0)) {
_transferFromPool(
quote.quoteToken,
quote.trader,
quote.quoteTokenAmount
);
} else {
_transferFromExternalAccount(
quote.externalAccount,
quote.quoteToken,
quote.trader,
quote.quoteTokenAmount
);
}
}
/// @inheritdoc IHashflowPool
function tradeXChainRFQT(XChainRFQTQuote memory quote, address trader)
external
payable
override
authorizedRouter
{
require(
quote.srcExternalAccount != address(0) ||
quote.baseToken != address(0) ||
msg.value == quote.effectiveBaseTokenAmount,
'HashflowPool::tradeXChainRFQT msg.value must = amount'
);
SignerConfiguration memory signerConfig = signerConfiguration;
require(
signerConfig.enabled,
'HashflowPool::tradeXChainRFQT Disabled.'
);
_updateNonceXChain(quote.dstTrader, quote.nonce);
bytes32 quoteHash = _hashXChainQuoteRFQT(quote);
require(
quoteHash.recover(quote.signature) == signerConfig.signer,
'HashflowPool::tradeXChainRFQT Invalid signer'
);
uint256 effectiveQuoteTokenAmount = quote.quoteTokenAmount;
if (quote.effectiveBaseTokenAmount < quote.baseTokenAmount) {
effectiveQuoteTokenAmount =
(quote.quoteTokenAmount * quote.effectiveBaseTokenAmount) /
quote.baseTokenAmount;
}
emit XChainTrade(
quote.dstChainId,
quote.dstPool,
trader,
quote.dstTrader,
quote.txid,
quote.baseToken,
quote.quoteToken,
quote.effectiveBaseTokenAmount,
effectiveQuoteTokenAmount
);
}
/// @inheritdoc IHashflowPool
function fillXChain(
address externalAccount,
bytes32 txid,
address trader,
address quoteToken,
uint256 quoteTokenAmount
) external override authorizedRouter {
require(
!_filledXChainTxids[txid],
'HashflowPool::fillXChain Quote has been executed previously.'
);
_filledXChainTxids[txid] = true;
emit XChainTradeFill(txid);
if (externalAccount == address(0)) {
_transferFromPool(quoteToken, trader, quoteTokenAmount);
} else {
_transferFromExternalAccount(
externalAccount,
quoteToken,
trader,
quoteTokenAmount
);
}
}
/// @inheritdoc IHashflowPool
function tradeXChainRFQM(XChainRFQMQuote memory quote)
external
override
authorizedRouter
{
SignerConfiguration memory signerConfig = signerConfiguration;
require(
signerConfig.enabled,
'HashflowPool::tradeXChainRFQM Disabled.'
);
bytes32 quoteHash = _hashXChainQuoteRFQM(quote);
require(
quoteHash.recover(quote.makerSignature) == signerConfig.signer,
'HashflowPool::tradeXChainRFQM Invalid signer'
);
emit XChainTrade(
quote.dstChainId,
quote.dstPool,
quote.trader,
quote.dstTrader,
quote.txid,
quote.baseToken,
quote.quoteToken,
quote.baseTokenAmount,
quote.quoteTokenAmount
);
}
/// @inheritdoc IHashflowPool
function updateXChainPoolAuthorization(
AuthorizedXChainPool[] calldata pools,
bool status
) external override authorizedOperations {
for (uint256 i = 0; i < pools.length; i++) {
require(pools[i].pool != bytes32(0));
IHashflowRouter(router).updateXChainPoolAuthorization(
pools[i].chainId,
pools[i].pool,
status
);
}
}
/// @inheritdoc IHashflowPool
function updateXChainMessengerAuthorization(
address xChainMessenger,
bool authorized
) external override authorizedOperations {
require(
xChainMessenger != address(0),
'HashflowPool::updateXChainMessengerAuthorization Invalid messenger address.'
);
IHashflowRouter(router).updateXChainMessengerAuthorization(
xChainMessenger,
authorized
);
}
/// @dev ERC1271 implementation.
function isValidSignature(bytes32 hash, bytes memory signature)
external
view
override
returns (bytes4 magicValue)
{
if (hash.recover(signature) == signerConfiguration.signer) {
magicValue = 0x1626ba7e;
}
}
/// @inheritdoc IHashflowPool
function approveToken(
address token,
address spender,
uint256 amount
) external override authorizedOperations {
IERC20(token).forceApprove(spender, amount);
}
/// @inheritdoc IHashflowPool
function increaseTokenAllowance(
address token,
address spender,
uint256 amount
) external override authorizedOperations {
IERC20(token).safeIncreaseAllowance(spender, amount);
}
/// @inheritdoc IHashflowPool
function decreaseTokenAllowance(
address token,
address spender,
uint256 amount
) external override authorizedOperations {
IERC20(token).safeDecreaseAllowance(spender, amount);
}
/// @inheritdoc IHashflowPool
function removeLiquidity(
address token,
address recipient,
uint256 amount
) external override authorizedOperations {
SignerConfiguration memory signerConfig = signerConfiguration;
require(
signerConfig.enabled,
'HashflowPool::removeLiquidity Disabled.'
);
require(amount > 0, 'HashflowPool::removeLiquidity Invalid amount');
address _recipient;
if (recipient != address(0)) {
require(
_withrawalAccountAuth[recipient],
'HashflowPool::removeLiquidity Recipient must be hedging account'
);
_recipient = recipient;
} else {
_recipient = _msgSender();
}
emit RemoveLiquidity(token, _recipient, amount);
_transferFromPool(token, _recipient, amount);
}
/// @inheritdoc IHashflowPool
function updateWithdrawalAccount(
address[] memory withdrawalAccounts,
bool authorized
) external override authorizedOperations {
for (uint256 i = 0; i < withdrawalAccounts.length; i++) {
require(withdrawalAccounts[i] != address(0));
_withrawalAccountAuth[withdrawalAccounts[i]] = authorized;
emit UpdateWithdrawalAccount(withdrawalAccounts[i], authorized);
}
}
/// @inheritdoc IHashflowPool
function updateSigner(address newSigner)
external
override
authorizedOperations
{
require(newSigner != address(0));
SignerConfiguration memory signerConfig = signerConfiguration;
emit UpdateSigner(newSigner, signerConfig.signer);
signerConfig.signer = newSigner;
signerConfiguration = signerConfig;
}
/// @inheritdoc IHashflowPool
function killswitchOperations(bool enabled)
external
override
authorizedRouter
{
SignerConfiguration memory signerConfig = signerConfiguration;
signerConfig.enabled = enabled;
signerConfiguration = signerConfig;
}
function getReserves(address token)
external
view
override
returns (uint256)
{
return _getReserves(token);
}
/**
* @dev Prevents against replay for RFQ-T. Checks that nonces are strictly increasing.
*/
function _updateNonce(address trader, uint256 nonce) internal {
require(
nonce > nonces[trader],
'HashflowPool::_updateNonce Invalid nonce.'
);
nonces[trader] = nonce;
}
/**
* @dev Prevents against replay for X-Chain RFQ-T. Checks that nonces are strictly increasing.
*/
function _updateNonceXChain(bytes32 trader, uint256 nonce) internal {
require(
nonce > xChainNonces[trader],
'HashflowPool::_updateNonceXChain Invalid nonce.'
);
xChainNonces[trader] = nonce;
}
function _transferFromPool(
address token,
address recipient,
uint256 value
) internal {
if (token == address(0)) {
payable(recipient).sendValue(value);
} else {
IERC20(token).safeTransfer(recipient, value);
}
}
/// @dev Helper function to transfer quoteToken from external account.
function _transferFromExternalAccount(
address externalAccount,
address token,
address receiver,
uint256 value
) private {
if (token == address(0)) {
IERC20(_WETH).safeTransferFrom(
externalAccount,
address(this),
value
);
IWETH(_WETH).withdraw(value);
payable(receiver).sendValue(value);
} else {
IERC20(token).safeTransferFrom(externalAccount, receiver, value);
}
}
function _getReserves(address token) internal view returns (uint256) {
return
token == address(0)
? address(this).balance
: IERC20(token).balanceOf(address(this));
}
/**
* @dev Generates a quote hash for RFQ-t.
*/
function _hashQuoteRFQT(RFQTQuote memory quote)
private
view
returns (bytes32)
{
return
keccak256(
abi.encodePacked(
'\x19Ethereum Signed Message:\n32',
keccak256(
abi.encodePacked(
address(this),
quote.trader,
quote.effectiveTrader,
quote.externalAccount,
quote.baseToken,
quote.quoteToken,
quote.baseTokenAmount,
quote.quoteTokenAmount,
quote.nonce,
quote.quoteExpiry,
quote.txid,
block.chainid
)
)
)
);
}
function _hashQuoteRFQM(RFQMQuote memory quote)
private
view
returns (bytes32)
{
return
keccak256(
abi.encodePacked(
'\x19Ethereum Signed Message:\n32',
keccak256(
abi.encodePacked(
quote.pool,
quote.externalAccount,
quote.trader,
quote.baseToken,
quote.quoteToken,
quote.baseTokenAmount,
quote.quoteTokenAmount,
quote.quoteExpiry,
quote.txid,
block.chainid
)
)
)
);
}
function _hashXChainQuoteRFQT(XChainRFQTQuote memory quote)
private
pure
returns (bytes32)
{
bytes32 digest = keccak256(
abi.encodePacked(
keccak256(
abi.encodePacked(
quote.srcChainId,
quote.dstChainId,
quote.srcPool,
quote.dstPool,
quote.srcExternalAccount,
quote.dstExternalAccount
)
),
quote.dstTrader,
quote.baseToken,
quote.quoteToken,
quote.baseTokenAmount,
quote.quoteTokenAmount,
quote.quoteExpiry,
quote.nonce,
quote.txid,
quote.xChainMessenger
)
);
return
keccak256(
abi.encodePacked('\x19Ethereum Signed Message:\n32', digest)
);
}
function _hashXChainQuoteRFQM(XChainRFQMQuote memory quote)
private
pure
returns (bytes32)
{
return
keccak256(
abi.encodePacked(
'\x19Ethereum Signed Message:\n32',
keccak256(
abi.encodePacked(
keccak256(
abi.encodePacked(
quote.srcChainId,
quote.dstChainId,
quote.srcPool,
quote.dstPool,
quote.srcExternalAccount,
quote.dstExternalAccount
)
),
quote.trader,
quote.baseToken,
quote.quoteToken,
quote.baseTokenAmount,
quote.quoteTokenAmount,
quote.quoteExpiry,
quote.txid,
quote.xChainMessenger
)
)
)
);
}
}