/
FactoryRouter.sol
768 lines (709 loc) Β· 27.4 KB
/
FactoryRouter.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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
pragma solidity 0.8.12;
// Copyright BigchainDB GmbH and Ocean Protocol contributors
// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
// Code is Apache-2.0 and docs are CC-BY-4.0
import "./balancer/BFactory.sol";
import "../interfaces/IFactory.sol";
import "../interfaces/IERC20.sol";
import "../interfaces/IFixedRateExchange.sol";
import "../interfaces/IPool.sol";
import "../interfaces/IDispenser.sol";
import "../utils/SafeERC20.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
contract FactoryRouter is BFactory, IFactoryRouter {
using SafeERC20 for IERC20;
using SafeMath for uint256;
address public routerOwner;
address public factory;
address public fixedRate;
uint256 public minVestingPeriodInBlocks = 2426000;
uint256 public swapOceanFee = 1e15; //0.1%
uint256 public swapNonOceanFee = 2e15; // 0.2%
uint256 public consumeFee = 3e16; // 0.03 DT
uint256 public providerFee = 0; // 0%
address[] public approvedTokens;
address[] public ssContracts;
address[] public fixedrates;
address[] public dispensers;
// mapping(address => bool) public approvedTokens;
// mapping(address => bool) public ssContracts;
// mapping(address => bool) public fixedPrice;
// mapping(address => bool) public dispenser;
event NewPool(address indexed poolAddress, bool isOcean);
event VestingPeriodChanges(address indexed caller, uint256 minVestingPeriodInBlocks);
event RouterChanged(address indexed caller, address indexed newRouter);
event FactoryContractChanged(
address indexed caller,
address indexed contractAddress
);
event TokenAdded(address indexed caller, address indexed token);
event TokenRemoved(address indexed caller, address indexed token);
event SSContractAdded(
address indexed caller,
address indexed contractAddress
);
event SSContractRemoved(
address indexed caller,
address indexed contractAddress
);
event FixedRateContractAdded(
address indexed caller,
address indexed contractAddress
);
event FixedRateContractRemoved(
address indexed caller,
address indexed contractAddress
);
event DispenserContractAdded(
address indexed caller,
address indexed contractAddress
);
event DispenserContractRemoved(
address indexed caller,
address indexed contractAddress
);
event OPCFeeChanged(address indexed caller, uint256 newSwapOceanFee,
uint256 newSwapNonOceanFee, uint256 newConsumeFee, uint256 newProviderFee);
modifier onlyRouterOwner() {
require(routerOwner == msg.sender, "OceanRouter: NOT OWNER");
_;
}
event OPCCollectorChanged(address indexed caller, address indexed _newOpcCollector);
constructor(
address _routerOwner,
address _oceanToken,
address _bpoolTemplate,
address _opcCollector,
address[] memory _preCreatedPools
) BFactory(_bpoolTemplate, _opcCollector, _preCreatedPools) {
require(
_routerOwner != address(0),
"FactoryRouter: Invalid router owner"
);
require(
_opcCollector != address(0),
"FactoryRouter: Invalid opcCollector"
);
require(
_oceanToken != address(0),
"FactoryRouter: Invalid Ocean Token address"
);
routerOwner = _routerOwner;
opcCollector = _opcCollector;
_addApprovedToken(_oceanToken);
}
function changeRouterOwner(address _routerOwner) external onlyRouterOwner {
require(_routerOwner != address(0), "Invalid new router owner");
routerOwner = _routerOwner;
emit RouterChanged(msg.sender, _routerOwner);
}
/**
* @dev addApprovedToken
* Adds a token to the list of tokens with reduced fees
* @param tokenAddress address Token to be added
*/
function addApprovedToken(address tokenAddress) external onlyRouterOwner {
_addApprovedToken(tokenAddress);
}
function _addApprovedToken(address tokenAddress) internal {
if(!isApprovedToken(tokenAddress)){
approvedTokens.push(tokenAddress);
emit TokenAdded(msg.sender, tokenAddress);
}
}
/**
* @dev removeApprovedToken
* Removes a token if exists from the list of tokens with reduced fees
* @param tokenAddress address Token to be removed
*/
function removeApprovedToken(address tokenAddress)
external
onlyRouterOwner
{
require(
tokenAddress != address(0),
"FactoryRouter: Invalid Ocean Token address"
);
uint256 i;
for (i = 0; i < approvedTokens.length; i++) {
if(approvedTokens[i] == tokenAddress) break;
}
if(i < approvedTokens.length){
approvedTokens[i] = approvedTokens[approvedTokens.length -1];
approvedTokens.pop();
emit TokenRemoved(msg.sender, tokenAddress);
}
}
/**
* @dev isApprovedToken
* Returns true if token exists in the list of tokens with reduced fees
* @param tokenAddress address Token to be checked
*/
function isApprovedToken(address tokenAddress) public view returns(bool) {
for (uint256 i = 0; i < approvedTokens.length; i++) {
if(approvedTokens[i] == tokenAddress) return true;
}
return false;
}
/**
* @dev getApprovedTokens
* Returns the list of tokens with reduced fees
*/
function getApprovedTokens() public view returns(address[] memory) {
return(approvedTokens);
}
/**
* @dev addSSContract
* Adds a token to the list of ssContracts
* @param _ssContract address Contract to be added
*/
function addSSContract(address _ssContract) external onlyRouterOwner {
require(
_ssContract != address(0),
"FactoryRouter: Invalid _ssContract address"
);
if(!isSSContract(_ssContract)){
ssContracts.push(_ssContract);
emit SSContractAdded(msg.sender, _ssContract);
}
}
/**
* @dev removeSSContract
* Removes a token if exists from the list of ssContracts
* @param _ssContract address Contract to be removed
*/
function removeSSContract(address _ssContract) external onlyRouterOwner {
require(
_ssContract != address(0),
"FactoryRouter: Invalid _ssContract address"
);
uint256 i;
for (i = 0; i < ssContracts.length; i++) {
if(ssContracts[i] == _ssContract) break;
}
if(i < ssContracts.length){
// it's in the array
ssContracts[i] = ssContracts[ssContracts.length -1];
ssContracts.pop();
emit SSContractRemoved(msg.sender, _ssContract);
}
}
/**
* @dev isSSContract
* Returns true if token exists in the list of ssContracts
* @param _ssContract address Contract to be checked
*/
function isSSContract(address _ssContract) public view returns(bool) {
for (uint256 i = 0; i < ssContracts.length; i++) {
if(ssContracts[i] == _ssContract) return true;
}
return false;
}
/**
* @dev getSSContracts
* Returns the list of ssContracts
*/
function getSSContracts() public view returns(address[] memory) {
return(ssContracts);
}
function addFactory(address _factory) external onlyRouterOwner {
require(
_factory != address(0),
"FactoryRouter: Invalid _factory address"
);
require(factory == address(0), "FACTORY ALREADY SET");
factory = _factory;
emit FactoryContractChanged(msg.sender, _factory);
}
/**
* @dev addFixedRateContract
* Adds an address to the list of fixed rate contracts
* @param _fixedRate address Contract to be added
*/
function addFixedRateContract(address _fixedRate) external onlyRouterOwner {
require(
_fixedRate != address(0),
"FactoryRouter: Invalid _fixedRate address"
);
if(!isFixedRateContract(_fixedRate)){
fixedrates.push(_fixedRate);
emit FixedRateContractAdded(msg.sender, _fixedRate);
}
}
/**
* @dev removeFixedRateContract
* Removes an address from the list of fixed rate contracts
* @param _fixedRate address Contract to be removed
*/
function removeFixedRateContract(address _fixedRate)
external
onlyRouterOwner
{
require(
_fixedRate != address(0),
"FactoryRouter: Invalid _fixedRate address"
);
uint256 i;
for (i = 0; i < fixedrates.length; i++) {
if(fixedrates[i] == _fixedRate) break;
}
if(i < fixedrates.length){
// it's in the array
fixedrates[i] = fixedrates[fixedrates.length -1];
fixedrates.pop();
emit FixedRateContractRemoved(msg.sender, _fixedRate);
}
}
/**
* @dev isFixedRateContract
* Removes true if address exists in the list of fixed rate contracts
* @param _fixedRate address Contract to be checked
*/
function isFixedRateContract(address _fixedRate) public view returns(bool) {
for (uint256 i = 0; i < fixedrates.length; i++) {
if(fixedrates[i] == _fixedRate) return true;
}
return false;
}
/**
* @dev getFixedRatesContracts
* Returns the list of fixed rate contracts
*/
function getFixedRatesContracts() public view returns(address[] memory) {
return(fixedrates);
}
/**
* @dev addDispenserContract
* Adds an address to the list of dispensers
* @param _dispenser address Contract to be added
*/
function addDispenserContract(address _dispenser) external onlyRouterOwner {
require(
_dispenser != address(0),
"FactoryRouter: Invalid _dispenser address"
);
if(!isDispenserContract(_dispenser)){
dispensers.push(_dispenser);
emit DispenserContractAdded(msg.sender, _dispenser);
}
}
/**
* @dev removeDispenserContract
* Removes an address from the list of dispensers
* @param _dispenser address Contract to be removed
*/
function removeDispenserContract(address _dispenser)
external
onlyRouterOwner
{
require(
_dispenser != address(0),
"FactoryRouter: Invalid _dispenser address"
);
uint256 i;
for (i = 0; i < dispensers.length; i++) {
if(dispensers[i] == _dispenser) break;
}
if(i < dispensers.length){
// it's in the array
dispensers[i] = dispensers[dispensers.length -1];
dispensers.pop();
emit DispenserContractRemoved(msg.sender, _dispenser);
}
}
/**
* @dev isDispenserContract
* Returns true if address exists in the list of dispensers
* @param _dispenser address Contract to be checked
*/
function isDispenserContract(address _dispenser) public view returns(bool) {
for (uint256 i = 0; i < dispensers.length; i++) {
if(dispensers[i] == _dispenser) return true;
}
return false;
}
/**
* @dev getDispensersContracts
* Returns the list of fixed rate contracts
*/
function getDispensersContracts() public view returns(address[] memory) {
return(dispensers);
}
/**
* @dev getOPCFee
* Gets OP Community Fees for a particular token
* @param baseToken address token to be checked
*/
function getOPCFee(address baseToken) public view returns (uint256) {
if (isApprovedToken(baseToken)) {
return swapOceanFee;
} else return swapNonOceanFee;
}
/**
* @dev getOPCFees
* Gets OP Community Fees for approved tokens and non approved tokens
*/
function getOPCFees() public view returns (uint256,uint256) {
return (swapOceanFee, swapNonOceanFee);
}
/**
* @dev getConsumeFee
* Gets OP Community Fee cuts for consume fees
*/
function getOPCConsumeFee() public view returns (uint256) {
return consumeFee;
}
/**
* @dev getOPCProviderFee
* Gets OP Community Fee cuts for provider fees
*/
function getOPCProviderFee() public view returns (uint256) {
return providerFee;
}
/**
* @dev updateOPCFee
* Updates OP Community Fees
* @param _newSwapOceanFee Amount charged for swapping with ocean approved tokens
* @param _newSwapNonOceanFee Amount charged for swapping with non ocean approved tokens
* @param _newConsumeFee Amount charged from consumeFees
* @param _newProviderFee Amount charged for providerFees
*/
function updateOPCFee(uint256 _newSwapOceanFee, uint256 _newSwapNonOceanFee,
uint256 _newConsumeFee, uint256 _newProviderFee) external onlyRouterOwner {
swapOceanFee = _newSwapOceanFee;
swapNonOceanFee = _newSwapNonOceanFee;
consumeFee = _newConsumeFee;
providerFee = _newProviderFee;
emit OPCFeeChanged(msg.sender, _newSwapOceanFee, _newSwapNonOceanFee, _newConsumeFee, _newProviderFee);
}
/*
* @dev getMinVestingPeriod
* Returns current minVestingPeriodInBlocks
@return minVestingPeriodInBlocks
*/
function getMinVestingPeriod() public view returns (uint256) {
return minVestingPeriodInBlocks;
}
/*
* @dev updateMinVestingPeriod
* Set new minVestingPeriodInBlocks
* @param _newPeriod
*/
function updateMinVestingPeriod(uint256 _newPeriod) external onlyRouterOwner {
minVestingPeriodInBlocks = _newPeriod;
emit VestingPeriodChanges(msg.sender, _newPeriod);
}
/**
* @dev Deploys a new `OceanPool` on Ocean Friendly Fork modified for 1SS.
This function cannot be called directly, but ONLY through the ERC20DT contract from a ERC20DEployer role
ssContract address
tokens [datatokenAddress, baseTokenAddress]
publisherAddress user which will be assigned the vested amount.
* @param tokens precreated parameter
* @param ssParams params for the ssContract.
* [0] = rate (wei)
* [1] = baseToken decimals
* [2] = vesting amount (wei)
* [3] = vested blocks
* [4] = initial liquidity in baseToken for pool creation
* @param swapFees swapFees (swapFee, swapMarketFee), swapOceanFee will be set automatically later
* [0] = swapFee for LP Providers
* [1] = swapFee for marketplace runner
.
* @param addresses refers to an array of addresses passed by user
* [0] = side staking contract address
* [1] = baseToken address for pool creation(OCEAN or other)
* [2] = baseTokenSender user which will provide the baseToken amount for initial liquidity
* [3] = publisherAddress user which will be assigned the vested amount
* [4] = marketFeeCollector marketFeeCollector address
[5] = poolTemplateAddress
@return pool address
*/
function deployPool(
address[2] calldata tokens,
// [datatokenAddress, baseTokenAddress]
uint256[] calldata ssParams,
uint256[] calldata swapFees,
address[] calldata addresses
)
external
returns (
//[controller,baseTokenAddress,baseTokenSender,publisherAddress, marketFeeCollector,poolTemplateAddress]
address
)
{
require(
IFactory(factory).erc20List(msg.sender),
"FACTORY ROUTER: NOT ORIGINAL ERC20 TEMPLATE"
);
require(isSSContract(addresses[0]),
"FACTORY ROUTER: invalid ssContract"
);
require(ssParams[1] > 0, "Wrong decimals");
// we pull baseToken for creating initial pool and send it to the controller (ssContract)
_pullUnderlying(tokens[1],addresses[2], addresses[0], ssParams[4]);
address pool = newBPool(tokens, ssParams, swapFees, addresses);
require(pool != address(0), "FAILED TO DEPLOY POOL");
if (isApprovedToken(tokens[1])) emit NewPool(pool, true);
else emit NewPool(pool, false);
return pool;
}
function _getLength(IERC20[] memory array) internal pure returns (uint256) {
return array.length;
}
/**
* @dev deployFixedRate
* Creates a new FixedRateExchange setup.
* As for deployPool, this function cannot be called directly,
* but ONLY through the ERC20DT contract from a ERC20DEployer role
* @param fixedPriceAddress fixedPriceAddress
* @param addresses array of addresses [baseToken,owner,marketFeeCollector]
* @param uints array of uints [baseTokenDecimals,datatokenDecimals, fixedRate, marketFee, withMint]
@return exchangeId
*/
function deployFixedRate(
address fixedPriceAddress,
address[] calldata addresses,
uint256[] calldata uints
) external returns (bytes32 exchangeId) {
require(
IFactory(factory).erc20List(msg.sender),
"FACTORY ROUTER: NOT ORIGINAL ERC20 TEMPLATE"
);
require(isFixedRateContract(fixedPriceAddress),
"FACTORY ROUTER: Invalid FixedPriceContract"
);
exchangeId = IFixedRateExchange(fixedPriceAddress).createWithDecimals(
msg.sender,
addresses,
uints
);
}
/**
* @dev deployDispenser
* Activates a new Dispenser
* As for deployPool, this function cannot be called directly,
* but ONLY through the ERC20DT contract from a ERC20DEployer role
* @param _dispenser dispenser contract address
* @param datatoken refers to datatoken address.
* @param maxTokens - max tokens to dispense
* @param maxBalance - max balance of requester.
* @param owner - owner
* @param allowedSwapper - if !=0, only this address can request DTs
*/
function deployDispenser(
address _dispenser,
address datatoken,
uint256 maxTokens,
uint256 maxBalance,
address owner,
address allowedSwapper
) external {
require(
IFactory(factory).erc20List(msg.sender),
"FACTORY ROUTER: NOT ORIGINAL ERC20 TEMPLATE"
);
require(isDispenserContract(_dispenser),
"FACTORY ROUTER: Invalid DispenserContract"
);
IDispenser(_dispenser).create(
datatoken,
maxTokens,
maxBalance,
owner,
allowedSwapper
);
}
/**
* @dev addPoolTemplate
* Adds an address to the list of pools templates
* @param poolTemplate address Contract to be added
*/
function addPoolTemplate(address poolTemplate) external onlyRouterOwner {
_addPoolTemplate(poolTemplate);
}
/**
* @dev removePoolTemplate
* Removes an address from the list of pool templates
* @param poolTemplate address Contract to be removed
*/
function removePoolTemplate(address poolTemplate) external onlyRouterOwner {
_removePoolTemplate(poolTemplate);
}
// If you need to buy multiple DT (let's say for a compute job which has multiple datasets),
// you have to send one transaction for each DT that you want to buy.
// Perks:
// one single call to buy multiple DT for multiple assets (better UX, better gas optimization)
// require tokenIn approvals for router from user. (except for dispenser operations)
function buyDTBatch(Operations[] calldata _operations) external {
// TODO: to avoid DOS attack, we set a limit to maximum orders (50?)
require(_operations.length <= 50, "FactoryRouter: Too Many Operations");
for (uint256 i = 0; i < _operations.length; i++) {
// address[] memory tokenInOutMarket = new address[](3);
address[3] memory tokenInOutMarket = [
_operations[i].tokenIn,
_operations[i].tokenOut,
_operations[i].marketFeeAddress
];
uint256[4] memory amountsInOutMaxFee = [
_operations[i].amountsIn,
_operations[i].amountsOut,
_operations[i].maxPrice,
_operations[i].swapMarketFee
];
// tokenInOutMarket[0] =
if (_operations[i].operation == operationType.SwapExactIn) {
// Get amountIn from user to router
_pullUnderlying(_operations[i].tokenIn,msg.sender,
address(this),
_operations[i].amountsIn);
// we approve pool to pull token from router
IERC20(_operations[i].tokenIn).safeIncreaseAllowance(
_operations[i].source,
_operations[i].amountsIn
);
// Perform swap
(uint256 amountReceived, ) = IPool(_operations[i].source)
.swapExactAmountIn(tokenInOutMarket, amountsInOutMaxFee);
// transfer token swapped to user
IERC20(_operations[i].tokenOut).safeTransfer(
msg.sender,
amountReceived
);
} else if (_operations[i].operation == operationType.SwapExactOut) {
// calculate how much amount In we need for exact Out
uint256 amountIn;
(amountIn, , , , ) = IPool(_operations[i].source)
.getAmountInExactOut(
_operations[i].tokenIn,
_operations[i].tokenOut,
_operations[i].amountsOut,
_operations[i].swapMarketFee
);
// pull amount In from user
_pullUnderlying(_operations[i].tokenIn,msg.sender,
address(this),
amountIn);
// we approve pool to pull token from router
IERC20(_operations[i].tokenIn).safeIncreaseAllowance(
_operations[i].source,
amountIn
);
// perform swap
(uint tokenAmountIn,) = IPool(_operations[i].source).swapExactAmountOut(
tokenInOutMarket,
amountsInOutMaxFee
);
require(tokenAmountIn <= amountsInOutMaxFee[0], 'TOO MANY TOKENS IN');
// send amount out back to user
IERC20(_operations[i].tokenOut).safeTransfer(
msg.sender,
_operations[i].amountsOut
);
} else if (_operations[i].operation == operationType.FixedRate) {
// get datatoken address
(, address datatoken, , , , , , , , , , ) = IFixedRateExchange(
_operations[i].source
).getExchange(_operations[i].exchangeIds);
// get tokenIn amount required for dt out
(uint256 baseTokenAmount, , , ) = IFixedRateExchange(
_operations[i].source
).calcBaseInGivenOutDT(
_operations[i].exchangeIds,
_operations[i].amountsOut,
_operations[i].swapMarketFee
);
// pull tokenIn amount
_pullUnderlying(_operations[i].tokenIn,msg.sender,
address(this),
baseTokenAmount);
// we approve pool to pull token from router
IERC20(_operations[i].tokenIn).safeIncreaseAllowance(
_operations[i].source,
baseTokenAmount
);
// perform swap
IFixedRateExchange(_operations[i].source).buyDT(
_operations[i].exchangeIds,
_operations[i].amountsOut,
_operations[i].amountsIn,
_operations[i].marketFeeAddress,
_operations[i].swapMarketFee
);
// send dt out to user
IERC20(datatoken).safeTransfer(
msg.sender,
_operations[i].amountsOut
);
} else {
IDispenser(_operations[i].source).dispense(
_operations[i].tokenOut,
_operations[i].amountsOut,
msg.sender
);
}
}
}
// require pool[].baseToken (for each pool) approvals for router from user.
function stakeBatch(Stakes[] calldata _stakes) external {
// TODO: to avoid DOS attack, we set a limit to maximum orders (50?)
require(_stakes.length <= 50, "FactoryRouter: Too Many Operations");
for (uint256 i = 0; i < _stakes.length; i++) {
address baseToken = IPool(_stakes[i].poolAddress).getBaseTokenAddress();
_pullUnderlying(baseToken,msg.sender,
address(this),
_stakes[i].tokenAmountIn);
uint256 balanceBefore = IERC20(_stakes[i].poolAddress).balanceOf(address(this));
// we approve pool to pull token from router
IERC20(baseToken).safeIncreaseAllowance(
_stakes[i].poolAddress,
_stakes[i].tokenAmountIn);
//now stake
uint poolAmountOut = IPool(_stakes[i].poolAddress).joinswapExternAmountIn(
_stakes[i].tokenAmountIn, _stakes[i].minPoolAmountOut
);
require(poolAmountOut >= _stakes[i].minPoolAmountOut,'NOT ENOUGH LP');
uint256 balanceAfter = IERC20(_stakes[i].poolAddress).balanceOf(address(this));
//send LP shares to user
IERC20(_stakes[i].poolAddress).safeTransfer(
msg.sender,
balanceAfter.sub(balanceBefore)
);
}
}
function _pullUnderlying(
address erc20,
address from,
address to,
uint256 amount
) internal {
uint256 balanceBefore = IERC20(erc20).balanceOf(to);
IERC20(erc20).safeTransferFrom(from, to, amount);
require(IERC20(erc20).balanceOf(to) >= balanceBefore.add(amount),
"Transfer amount is too low");
}
function getPoolTemplates() public view override(BFactory, IFactoryRouter) returns (address[] memory) {
return BFactory.getPoolTemplates();
}
function isPoolTemplate(address poolTemplate) public view override(BFactory, IFactoryRouter)
returns (bool) {
return BFactory.isPoolTemplate(poolTemplate);
}
/*
* @dev updateOPCCollector
* Set new opcCollector
* @param opcCollector
*/
function updateOPCCollector(address _opcCollector) external onlyRouterOwner {
require(_opcCollector != address(0), "New opcCollector cannot be ZERO_ADDR");
opcCollector = _opcCollector;
emit OPCCollectorChanged(msg.sender, _opcCollector);
}
/*
* @dev getOPCCollector
* getter for opcCollector
*/
function getOPCCollector() view public returns (address) {
return opcCollector;
}
}