-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSwapPairContract.sol
1560 lines (1376 loc) · 58.4 KB
/
SwapPairContract.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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
pragma ton-solidity ^0.39.0;
pragma AbiHeader pubkey;
pragma AbiHeader expire;
pragma AbiHeader time;
import '../../ton-eth-bridge-token-contracts/free-ton/contracts/interfaces/IRootTokenContract.sol';
import '../../ton-eth-bridge-token-contracts/free-ton/contracts/interfaces/ITokensReceivedCallback.sol';
import '../../ton-eth-bridge-token-contracts/free-ton/contracts/interfaces/ITONTokenWallet.sol';
import "../../ton-eth-bridge-token-contracts/free-ton/contracts/interfaces/IBurnTokensCallback.sol";
import "../../ton-eth-bridge-token-contracts/free-ton/contracts/interfaces/IBurnableByOwnerTokenWallet.sol";
import './interfaces/swapPair/ISwapPairContract.sol';
import './interfaces/swapPair/ISwapPairInformation.sol';
import './interfaces/swapPair/IUpgradeSwapPairCode.sol';
import './interfaces/rootSwapPair/IRootContractCallback.sol';
import './interfaces/helpers/ITIP3TokenDeployer.sol';
import './libraries/swapPair/SwapPairErrors.sol';
import './libraries/swapPair/SwapPairConstants.sol';
// TODO: рефакторинг: добавить комментарии чтобы было понятно что за параметры
contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpgradeSwapPairCode, ISwapPairContract {
address static token1;
address static token2;
uint static swapPairID;
uint32 swapPairCodeVersion;
address swapPairRootContract;
address tip3Deployer;
address lpTokenRootAddress;
address lpTokenWalletAddress;
bytes LPTokenName;
uint128 constant feeNominator = 997;
uint128 constant feeDenominator = 1000;
uint256 liquidityTokensMinted = 0;
mapping(address => uint8) tokenPositions;
//Deployed token wallets addresses
mapping(uint8 => address) tokenWallets;
//Liquidity providers info for security reasons
mapping(uint256 => LPProvidingInfo) lpInputTokensInfo;
//Liquidity Pools
mapping(uint8 => uint128) private lps;
//Pair creation timestamp
uint256 creationTimestamp;
//Initialization status.
// 0 - new <- not initialized
// 1 - one wallet created <- not initialized
// 2 - both wallets for TIP-3 created <- not initialized
// 3 - deployed LP token contract <- not initialized
// 4 - deployed LP token wallet <- initialized
uint private initializedStatus = 0;
// Tokens positions
uint8 constant T1 = 0;
uint8 constant T2 = 1;
// Token info
uint8 tokenInfoCount;
IRootTokenContract.IRootTokenContractDetails T1Info;
IRootTokenContract.IRootTokenContractDetails T2Info;
uint8 constant wrongPayloadFormatMessage = 0; //Received payload is invalid or has wrong format.";
uint8 constant unknownOperationIdorWrongPayload = 1; //Received payload contains unknow Operation ID or is malformed.";
uint8 constant sumIsTooLowForSwap = 2; //Provided token amount is not enough for swap.";
uint8 constant noLiquidityProvidedMessage = 3; //No liquidity provided yet. Swaps are forbidden.";
uint8 constant sumIsTooLowForLPTokenWithdraw = 4; //Provided LP token amount is not enough to withdraw liquidity.";
//============Contract initialization functions============
constructor(address rootContract, address tip3Deployer_, uint32 swapPairCodeVersion_) public {
tvm.accept();
creationTimestamp = now;
swapPairRootContract = rootContract;
tip3Deployer = tip3Deployer_;
swapPairCodeVersion = swapPairCodeVersion_;
tokenPositions[token1] = T1;
tokenPositions[token2] = T2;
lps[T1] = 0;
lps[T2] = 0;
//Deploy tokens wallets
_deployWallet(token1);
_deployWallet(token2);
// Get information about tokens
_getTIP3Details(token1);
_getTIP3Details(token2);
}
/**
* Deploy wallet for swap pair.
* @dev You cannot get address from this function so _getWalletAddress is used to get address of wallet
* @param tokenRootAddress address of tip-3 root contract
*/
function _deployWallet(address tokenRootAddress) private view {
tvm.accept();
IRootTokenContract(tokenRootAddress).deployEmptyWallet{
value: SwapPairConstants.walletDeployMessageValue
}(SwapPairConstants.walletInitialBalanceAmount, tvm.pubkey(), address(this), address(this));
_getWalletAddress(tokenRootAddress);
}
/**
* Get address of future wallet address deployed using _deployWallet
* @dev getWalletAddressCallback is used to get wallet address
* @param token address of tip-3 root contract
*/
function _getWalletAddress(address token) private view {
tvm.accept();
IRootTokenContract(token).getWalletAddress{
value: SwapPairConstants.sendToRootToken,
callback: this.getWalletAddressCallback
}(tvm.pubkey(), address(this));
}
/**
* Deployed wallet address callback
* @dev can be called only by token root contracts
* @param walletAddress address of deployed token wallet
*/
function getWalletAddressCallback(address walletAddress) external onlyTokenRoot {
require(initializedStatus < SwapPairConstants.contractFullyInitialized, SwapPairErrors.CONTRACT_ALREADY_INITIALIZED);
tvm.accept();
if (msg.sender == token1) {
tokenWallets[T1] = walletAddress;
initializedStatus++;
}
if (msg.sender == token2) {
tokenWallets[T2] = walletAddress;
initializedStatus++;
}
if (msg.sender == lpTokenRootAddress) {
lpTokenWalletAddress = walletAddress;
initializedStatus++;
}
/*
For all deployed wallets we set callback address equal to swap pair address
*/
_setWalletsCallbackAddress(walletAddress);
/*
If all wallets were deployed and LP token root is deployed - swap pair is ready
We call swap pair root callback to update stored information
*/
if (initializedStatus == SwapPairConstants.contractFullyInitialized) {
_swapPairInitializedCall();
}
}
/**
* Set callback address for wallets
* @param walletAddress Address of TIP-3 wallet
*/
function _setWalletsCallbackAddress(address walletAddress) private pure {
tvm.accept();
ITONTokenWallet(walletAddress).setReceiveCallback{
value: 200 milliton
}(
address(this),
false
);
}
/**
* Get tip-3 details from root tip-3 contract
* @dev function _receiveTIP3Details is used for callback
* @param tokenRootAddress address of tip-3 root contract
*/
function _getTIP3Details(address tokenRootAddress) private pure {
tvm.accept();
IRootTokenContract(tokenRootAddress).getDetails{ value: SwapPairConstants.sendToRootToken, bounce: true, callback: this._receiveTIP3Details }();
}
/**
* Receive requested TIP-3 details from root contract
* @dev this function can be called only by know TIP-3 token root contracts (token1, token2, LP token)
* @param rtcd Details about TIP-3
*/
function _receiveTIP3Details(IRootTokenContract.IRootTokenContractDetails rtcd)
external
onlyTokenRoot
{
tvm.accept();
if (msg.sender == token1) {
T1Info = rtcd;
tokenInfoCount++;
} else {
T2Info = rtcd;
tokenInfoCount++;
}
/*
After we receive information about both tokens we can proceed to LP token creation
This is mainly done for nice name of future LP token such as "T1 <-> T2"
*/
if (tokenInfoCount == 2) {
this._prepareDataForTIP3Deploy();
}
}
/**
* Build name of future TIP-3 LP token
* @dev This function can be called only by contract itself
*/
function _prepareDataForTIP3Deploy() external view onlySelf {
tvm.accept();
string res = string(T1Info.symbol);
res.append("<->");
res.append(string(T2Info.symbol));
res.append(" LP");
this._deployTIP3LpToken(bytes(res), bytes(res));
}
/**
* Deploy TIP-3 LP token with created params
* @dev This function can be called only by contract itself
* @param name Name of future TIP-3 token, equal to symbol
* @param symbol Symbol of future TIP-3 token
*/
function _deployTIP3LpToken(bytes name, bytes symbol) external onlySelf {
tvm.accept();
LPTokenName = symbol;
/*
Another contract is required to deploy TIP-3 token
*/
ITIP3TokenDeployer(tip3Deployer).deployTIP3Token{
value: SwapPairConstants.tip3SendDeployGrams,
bounce: true,
callback: this._deployTIP3LpTokenCallback
}(name, symbol, SwapPairConstants.tip3LpDecimals, 0, address(this), SwapPairConstants.tip3SendDeployGrams/2);
}
/**
* Receive address of LP token root contract. Callback for _deployTIP3Lptoken
* @dev This function can be called only by TIP-3 deployer contract
* @param tip3RootContract Address of deployed LP token root contract
*/
function _deployTIP3LpTokenCallback(address tip3RootContract) external onlyTIP3Deployer {
tvm.accept();
lpTokenRootAddress = tip3RootContract;
initializedStatus++;
_deployWallet(tip3RootContract);
}
//============TON balance function============
receive() external {
// Thanks!
}
//============Get functions============
/**
* Get general information about swap pair
*/
function getPairInfo() override external responsible view returns (SwapPairInfo info) {
return _constructSwapPairInfo();
}
/**
* Get result of swap if swap would be performed right now
* @param swappableTokenRoot Root of tip-3 token used for swap
* @param swappableTokenAmount Amount of token for swap
*/
function getExchangeRate(address swappableTokenRoot, uint128 swappableTokenAmount)
override
external
responsible
view
initialized
tokenExistsInPair(swappableTokenRoot)
returns (SwapInfo)
{
if (swappableTokenAmount <= 0)
return SwapInfo(0, 0, 0);
_SwapInfoInternal si = _calculateSwapInfo(swappableTokenRoot, swappableTokenAmount);
return SwapInfo(swappableTokenAmount, si.targetTokenAmount, si.fee);
}
/**
* Get current pool states
*/
function getCurrentExchangeRate()
override
external
responsible
view
returns (LiquidityPoolsInfo lpi)
{
return LiquidityPoolsInfo(address(this), lps[T1], lps[T2], liquidityTokensMinted);
}
//============Functions for offchain execution============
/**
* Get information for liquidity providing - how much of first and second tokens will be added to
* liquidity pools
* @dev Requires a lot of gas, recommended to run with runLocal
* @notice This is just imitation of LP mechanism for offchain execution
* @param maxFirstTokenAmount amount of first token provided to LP
* @param maxSecondTokenAmount amount of second token provided to LP
*/
function getProvidingLiquidityInfo(uint128 maxFirstTokenAmount, uint128 maxSecondTokenAmount)
override
external
view
initialized
returns (uint128 providedFirstTokenAmount, uint128 providedSecondTokenAmount)
{
(providedFirstTokenAmount, providedSecondTokenAmount,) = _calculateProvidingLiquidityInfo(maxFirstTokenAmount, maxSecondTokenAmount);
}
/**
* Get information how much tokens you will receive if you burn given amount of LP tokens
* @dev Requires a lot of gas, recommended to run with runLocal
* @notice This is just imitation of LP mechanism for offchain execution
* @param liquidityTokensAmount Amount of liquidity tokens to be burnt
*/
function getWithdrawingLiquidityInfo(uint256 liquidityTokensAmount)
override
external
view
initialized
returns (uint128 withdrawedFirstTokenAmount, uint128 withdrawedSecondTokenAmount)
{
(withdrawedFirstTokenAmount, withdrawedSecondTokenAmount,) = _calculateWithdrawingLiquidityInfo(liquidityTokensAmount);
}
/**
* Calculate amount of another token you need to provide to LP
* @dev Requires a lot of gas, recommended to run with runLocal
* @notice This is just imitation of LP mechanism for offchain execution
* @param providingTokenRoot Address of provided tip-3 token
* @param providingTokenAmount Amount of provided tip-3 tokens
*/
function getAnotherTokenProvidingAmount(address providingTokenRoot, uint128 providingTokenAmount)
override
external
view
initialized
returns(uint128 anotherTokenAmount)
{
if (!_checkIsLiquidityProvided())
return 0;
uint8 fromK = _getTokenPosition(providingTokenRoot);
uint8 toK = fromK == T1 ? T2 : T1;
return providingTokenAmount != 0 ? math.muldivc(providingTokenAmount, lps[toK], lps[fromK]) : 0;
}
//============LP Functions============
/**
* Calculate LP providing information -> amount of first and second token provided and amount of LP token to mint
* @notice This function doesn't change LP volumes. It only calculates
* @param maxFirstTokenAmount Amount of first token user provided
* @param maxSecondTokenAmount Amount of second token user provided
*/
function _calculateProvidingLiquidityInfo(uint128 maxFirstTokenAmount, uint128 maxSecondTokenAmount)
private
view
returns (uint128 provided1, uint128 provided2, uint256 _minted)
{
// TODO: Антон: подумать что можно сделать с тем, что мы минтим uint256
/*
If no liquidity provided than you set the initial exchange rate
*/
if ( !_checkIsLiquidityProvided() ) {
provided1 = maxFirstTokenAmount;
provided2 = maxSecondTokenAmount;
_minted = uint256(provided1) * uint256(provided2);
} else {
uint128 maxToProvide1 = maxSecondTokenAmount != 0 ? math.muldiv(maxSecondTokenAmount, lps[T1], lps[T2]) : 0;
uint128 maxToProvide2 = maxFirstTokenAmount != 0 ? math.muldiv(maxFirstTokenAmount, lps[T2], lps[T1]) : 0;
if (maxToProvide1 <= maxFirstTokenAmount ) {
provided1 = maxToProvide1;
provided2 = maxSecondTokenAmount;
_minted = math.muldiv(uint256(provided2), liquidityTokensMinted, uint256(lps[T2]) );
} else {
provided1 = maxFirstTokenAmount;
provided2 = maxToProvide2;
_minted = math.muldiv(uint256(provided1), liquidityTokensMinted, uint256(lps[T1]) );
}
}
}
/**
* Calculate amount of tokens received if given amount of LP tokens is burnt
* @notice This function doesn't change LP volumes. It only calculates
* @param liquidityTokensAmount Amount of LP tokens burnt
*/
function _calculateWithdrawingLiquidityInfo(uint256 liquidityTokensAmount)
private
view
returns (uint128 withdrawed1, uint128 withdrawed2, uint256 _burned)
{
if (liquidityTokensMinted <= 0 || liquidityTokensAmount <= 0)
return (0, 0, 0);
withdrawed1 = uint128(math.muldiv(uint256(lps[T1]), liquidityTokensAmount, liquidityTokensMinted));
withdrawed2 = uint128(math.muldiv(uint256(lps[T2]), liquidityTokensAmount, liquidityTokensMinted));
_burned = liquidityTokensAmount;
}
/**
* Calculate LP providing information of only one token was provided
* @notice This function doesn't change LP volumes. It only calculates
* @param tokenRoot Root contract address of provided token
* @param tokenAmount Amount of provided token
*/
function _calculateOneTokenProvidingAmount(address tokenRoot, uint128 tokenAmount)
private
view
returns(uint128)
{
uint8 fromK = _getTokenPosition(tokenRoot);
uint256 f = uint256(lps[fromK]);
uint128 k = feeNominator+feeDenominator;
uint256 b = f*k;
uint256 v = f * _sqrt( k*k + math.muldiv(4*feeDenominator*feeNominator, tokenAmount, f));
return uint128((v-b)/(feeNominator+feeNominator));
}
/**
* Calculate swap results
* @notice This function doesn't change LP volumes. It only calculates swap results
* @param swappableTokenRoot Root contract address of token used for swap
* @param swappableTokenAmount Amount of token used for swap
*/
function _calculateSwapInfo(address swappableTokenRoot, uint128 swappableTokenAmount)
private
view
tokenExistsInPair(swappableTokenRoot)
returns (_SwapInfoInternal swapInfo)
{
uint8 fromK = _getTokenPosition(swappableTokenRoot);
uint8 toK = fromK == T1 ? T2 : T1;
uint128 fee = swappableTokenAmount - math.muldivc(swappableTokenAmount, feeNominator, feeDenominator);
uint128 newFromPool = lps[fromK] + swappableTokenAmount;
uint128 newToPool = uint128( math.divc(uint256(lps[T1]) * uint256(lps[T2]), newFromPool - fee) );
uint128 targetTokenAmount = lps[toK] - newToPool;
_SwapInfoInternal result = _SwapInfoInternal(fromK, toK, newFromPool, newToPool, targetTokenAmount, fee);
return result;
}
/**
* Wrapper for _calculateSwapInfo that changes the state of the contract
* @notice This function changes LP volumes
* @param swappableTokenRoot Root contract address of token used for swap
* @param swappableTokenAmount Amount of tokens used for swap
*/
function _swap(address swappableTokenRoot, uint128 swappableTokenAmount)
private
returns (SwapInfo)
{
_SwapInfoInternal _si = _calculateSwapInfo(swappableTokenRoot, swappableTokenAmount);
if (!notZeroLiquidity(swappableTokenAmount, _si.targetTokenAmount)) {
return SwapInfo(0, 0, 0);
}
lps[_si.fromKey] = _si.newFromPool;
lps[_si.toKey] = _si.newToPool;
return SwapInfo(swappableTokenAmount, _si.targetTokenAmount, _si.fee);
}
// TODO: Антон: проверка провайдинга ликвидности по одному токену
/**
* Internal function used for providing liquidity using one token
* @notice To provide liquidity using one token it's required to swap part of provided token amount
* @param tokenRoot Root contract address of token used for liquidity providing
* @param tokenAmount Amount of tokens used for liquidity providing
* @param senderPubkey Public key of user that provides liquidity
* @param senderAddress Address of TON wallet of user
* @param lpWallet Address of user's LP wallet
*/
function _provideLiquidityOneToken(address tokenRoot, uint128 tokenAmount, uint256 senderPubkey, address senderAddress, address lpWallet)
private
tokenExistsInPair(tokenRoot)
returns (uint128 provided1, uint128 provided2, uint256 toMint, uint128 inputTokenRemainder)
{
uint128 amount = _calculateOneTokenProvidingAmount(tokenRoot, tokenAmount);
if (amount <= 0)
return (0, 0, 0, 0);
SwapInfo si = _swap(tokenRoot, amount);
uint128 amount1 = 0;
uint128 amount2 = 0;
bool isT1 = (tokenRoot == token1);
if ( isT1 ) {
amount1 = tokenAmount - si.swappableTokenAmount;
amount2 = si.targetTokenAmount;
} else {
amount1 = si.targetTokenAmount;
amount2 = tokenAmount - si.swappableTokenAmount;
}
(provided1, provided2, toMint) = _provideLiquidity(amount1, amount2, senderPubkey, senderAddress, lpWallet);
inputTokenRemainder = isT1 ? (amount1 - provided1) : (amount2 - provided2);
}
/**
* Internal function for liquidity providing using both tokens
* @notice This function changes LP volumes
* @param amount1 Amount of first token provided by user
* @param amount2 Amount of second token provided by user
* @param senderPubkey Public key of user that provides liquidity
* @param senderAddress Address of TON wallet of user
* @param lpWallet Address of user's LP wallet
*/
function _provideLiquidity(uint128 amount1, uint128 amount2, uint256 senderPubkey, address senderAddress, address lpWallet)
private
returns (uint128 provided1, uint128 provided2, uint256 toMint)
{
(provided1, provided2, toMint) = _calculateProvidingLiquidityInfo(amount1, amount2);
lps[T1] += provided1;
lps[T2] += provided2;
liquidityTokensMinted += toMint;
/*
If user doesn't have wallet for LP tokens - we create one for user
*/
if (lpWallet.value == 0) {
IRootTokenContract(lpTokenRootAddress).deployWallet{
value: msg.value/2,
flag: 0
}(uint128(toMint), msg.value/4, senderPubkey, senderAddress, senderAddress);
} else {
IRootTokenContract(lpTokenRootAddress).mint(uint128(toMint), lpWallet);
}
}
/**
* Function to return tokens not used for lqiuidity providing
* @param providedByUser Amount of tokens transferred by user
* @param providedAmount Amount of tokens provided to LP
* @param tokenWallet Address of swap pair wallet that received tokens
* @param senderTokenWallet Address of user's token wallet
* @param original_gas_to Where to return remaining gas
* @param payloadTB Payload attached to message
*/
function _tryToReturnProvidingTokens(
uint128 providedByUser, uint128 providedAmount, address tokenWallet, address senderTokenWallet, address original_gas_to, TvmBuilder payloadTB
) private pure {
uint128 amount = providedByUser - providedAmount;
if (amount > 0) {
_sendTokens(tokenWallet, senderTokenWallet, amount, original_gas_to, false, payloadTB.toCell());
}
}
//============Withdraw LP tokens functionality============
/**
* Function to withdraw tokens from liquidity pool
* @notice To interact with swap pair you need to send TIP-3 tokens with specific payload and TONs
* @dev This function can be called only by contract itself
* @param tokenAmount Amount of LP tokens burnt/transferred
* @param lpwi Information used for token withdrawal. Contains root addresses and user's wallets
* @param walletAddress Address of user's LP wallet
* @param tokensBurnt If tokens were burnt or just transferred
* @param send_gas_to Where to send remaining gas
*/
function _withdrawTokensFromLP(
uint128 tokenAmount,
LPWithdrawInfo lpwi,
address walletAddress,
bool tokensBurnt,
address send_gas_to
) external onlySelf {
require(
_checkIsLiquidityProvided(),
SwapPairErrors.NO_LIQUIDITY_PROVIDED
);
(uint128 withdrawed1, uint128 withdrawed2, uint256 burned) = _calculateWithdrawingLiquidityInfo(tokenAmount);
if (withdrawed1 != 0 && withdrawed2 != 0) {
lps[T1] -= withdrawed1;
lps[T2] -= withdrawed2;
if (!tokensBurnt) {
_burnTransferredLPTokens(tokenAmount);
}
liquidityTokensMinted -= tokenAmount;
emit WithdrawLiquidity(burned, withdrawed1, withdrawed2);
SwapPairContract(this)._transferTokensToWallets{
flag: 64,
value: 0
}(lpwi, withdrawed1, withdrawed2, send_gas_to);
} else {
_fallbackWithdrawLP(walletAddress, tokenAmount, tokensBurnt);
}
}
/**
* Function to withdraw tokens from liquidity pool in one token
* @notice To interact with swap pair you need to send TIP-3 tokens with specific payload and TONs
* @dev This function can be called only by contract itself
* @param tokenAmount Amount of LP tokens burnt/transferred
* @param tokenRoot Address of desired TIP-3 token
* @param tokenWallet Address of tip-3 wallet to transfer tokens to
* @param lpWalletAddress Address of user's LP token wallet
* @param tokensBurnt If tokens were burnt or just transferred
* @param send_gas_to Where to send remaining gas
*/
function _withdrawOneTokenFromLP (
uint128 tokenAmount,
address tokenRoot,
address tokenWallet,
address lpWalletAddress,
bool tokensBurnt,
address send_gas_to
) external onlySelf {
// TODO: рефакторинг: общая часть с функцией _withdrawTokensFromLP, возможно стоит вынести в отдельный метод
require(
_checkIsLiquidityProvided(),
SwapPairErrors.NO_LIQUIDITY_PROVIDED
);
(uint128 withdrawed1, uint128 withdrawed2, uint256 burned) = _calculateWithdrawingLiquidityInfo(tokenAmount);
if (withdrawed1 == 0 || withdrawed2 == 0) {
_fallbackWithdrawLP(lpWalletAddress, tokenAmount, tokensBurnt);
return;
}
lps[T1] -= withdrawed1;
lps[T2] -= withdrawed2;
if (!tokensBurnt) {
_burnTransferredLPTokens(tokenAmount);
}
liquidityTokensMinted -= tokenAmount;
emit WithdrawLiquidity(burned, withdrawed1, withdrawed2);
bool isT1 = tokenRoot == token1;
address swapableToken = isT1 ? token2 : token1;
uint128 swapableAmount = isT1 ? withdrawed2 : withdrawed1;
SwapInfo si = _swap(swapableToken, swapableAmount);
uint128 resultAmount = si.targetTokenAmount + (isT1 ? withdrawed1 : withdrawed2);
address w = tokenRoot == token1 ? tokenWallets[T1] : tokenWallets[T2];
TvmCell payload;
_sendTokens(w, tokenWallet, resultAmount, send_gas_to, true, payload);
}
/**
* Function to transfer multiple tokens to wallets
* @dev This function can be called only by contract itself
* @param lpwi Struct with information for token withdraw
* @param t1Amount Amount of first token to transfer
* @param t2Amount Amount of second token to transfer
* @param send_gas_to Where to send remaining gas
*/
function _transferTokensToWallets(
LPWithdrawInfo lpwi, uint128 t1Amount, uint128 t2Amount, address send_gas_to
) external view onlySelf {
// TODO: рефакторинг: изменить названия
// TODO: рефакторинг: переделать функцию
bool t1ist1 = lpwi.tr1 == token1; // смотрим, не была ли перепутана последовательность адресов рут-контрактов
address w1 = t1ist1 ? tokenWallets[0] : tokenWallets[1];
address w2 = t1ist1 ? tokenWallets[1] : tokenWallets[0];
uint128 t1a = t1ist1 ? t1Amount : t2Amount;
uint128 t2a = t1ist1 ? t2Amount : t1Amount;
TvmCell payload = _createWithdrawResultPayload(w1, t1a, w2, t2a);
ITONTokenWallet(w1).transfer{value: msg.value/3}(lpwi.tw1, t1a, 0, send_gas_to, true, payload);
_sendTokens(w2, lpwi.tw2, t2a, send_gas_to, true, payload);
}
/**
* Fallback function if something went wrong during liquidity withdrawal
* @param walletAddress Address of user's LP token wallet
* @param tokensAmount Amount of LP tokens that were transferred/burnt
* @param mintRequired Were tokens burnt or just transferred
*/
function _fallbackWithdrawLP(
address walletAddress, uint128 tokensAmount, bool mintRequired
) private view {
if (mintRequired) {
IRootTokenContract(lpTokenRootAddress).mint{
value: 0,
flag: 64
}(tokensAmount, walletAddress);
} else {
TvmBuilder payload;
payload.store(sumIsTooLowForLPTokenWithdraw);
_sendTokens(lpTokenWalletAddress, walletAddress, tokensAmount, address(this), true, payload.toCell());
}
}
/**
* Burn transferred LP tokens
* @param tokenAmount Amount of LP tokens to burn
*/
function _burnTransferredLPTokens(uint128 tokenAmount) private view {
TvmCell payload;
IBurnableByOwnerTokenWallet(lpTokenWalletAddress).burnByOwner{
value: msg.value/4
}(tokenAmount, 0, address(this), address(this), payload);
}
//============Callbacks============
/**
* Function that is called when TIP-3 wallet receives tokens
* @dev This function can only be called by swap pair's TIP-3 wallets
* @notice This function is just a router for parsing initial payload and guessing which function to call
* @param token_wallet Address of wallet that received tokens
* @param token_root Root contract address of wallet
* @param amount Amount of tokens transferred
* @param sender_public_key Sender's public key
* @param sender_address Sender's TON wallet address
* @param sender_wallet Sender's token wallet address
* @param original_gas_to Original gas_back address
* @param updated_balance Balance of wallet after transfer
* @param payload Payload attached to message
*/
function tokensReceivedCallback(
address token_wallet,
address token_root,
uint128 amount,
uint256 sender_public_key,
address sender_address,
address sender_wallet,
address original_gas_to,
uint128 updated_balance,
TvmCell payload
) override public onlyOwnWallet {
TvmSlice tmp = payload.toSlice();
(UnifiedOperation uo) = tmp.decode(UnifiedOperation);
TvmBuilder failTB;
failTB.store(unknownOperationIdorWrongPayload);
// TODO: рефакторинг: разрулить if/else во что-то более нормальное
if (msg.sender != lpTokenWalletAddress) {
if (uo.operationId == SwapPairConstants.SwapPairOperation) {
SwapPairContract(this)._externalSwap{
flag: 64,
value: 0
}(
uo.operationArgs, msg.sender, token_root, amount, sender_wallet, original_gas_to
);
}
else if (uo.operationId == SwapPairConstants.ProvideLiquidity) {
SwapPairContract(this)._externalProvideLiquidity{
flag: 64,
value: 0
}(
uo.operationArgs, msg.sender, sender_public_key, amount, sender_wallet, sender_address, original_gas_to
);
}
else if (uo.operationId == SwapPairConstants.ProvideLiquidityOneToken) {
SwapPairContract(this)._externalProvideLiquidityOneToken{
flag: 64,
value: 0
}(
uo.operationArgs, token_root, msg.sender, sender_public_key, amount, sender_wallet, sender_address, original_gas_to
);
}
else {
_sendTokens(msg.sender, sender_wallet, amount, original_gas_to, true, failTB.toCell());
}
}
else {
if (uo.operationId == SwapPairConstants.WithdrawLiquidity) {
SwapPairContract(this)._externalWithdrawLiquidity{
flag: 64,
value: 0
}(
uo.operationArgs, amount, sender_wallet, original_gas_to, false
);
} else if (uo.operationId == SwapPairConstants.WithdrawLiquidityOneToken) {
SwapPairContract(this)._externalWithdrawLiquidityOneToken{
flag: 64,
value: 0
}(
uo.operationArgs, amount, sender_wallet, original_gas_to, false
);
} else {
_sendTokens(msg.sender, sender_wallet, amount, original_gas_to, true, failTB.toCell());
}
}
}
/**
* Function that is called when LP tokens are burnt
* @dev This function can only be called by LP token root contract
* @notice This function is just a router for parsing initial payload and guessing which function to call
* @param tokensBurnt Amount of burnt tokens
* @param payload Payload attached to burnt tokens
* @param sender_public_key Public key of user that burnt tokens
* @param sender_address Address of user's TON wallet
* @param wallet_address Address of user's LP token wallet
* @param send_gas_to Original gas_back address
*/
function burnCallback(
uint128 tokensBurnt,
TvmCell payload,
uint256 sender_public_key,
address sender_address,
address wallet_address,
address send_gas_to
) external view onlyLpTokenRoot {
TvmSlice tmp = payload.toSlice();
(UnifiedOperation uo) = tmp.decode(UnifiedOperation);
if (wallet_address == lpTokenWalletAddress) {
return;
}
if (uo.operationId == SwapPairConstants.WithdrawLiquidity) {
SwapPairContract(this)._externalWithdrawLiquidity{
flag: 64,
value: 0
}(
uo.operationArgs, tokensBurnt, wallet_address, send_gas_to, true
);
}
else if (uo.operationId == SwapPairConstants.WithdrawLiquidityOneToken) {
SwapPairContract(this)._externalWithdrawLiquidity{
flag: 64,
value: 0
}(
uo.operationArgs, tokensBurnt, wallet_address, send_gas_to, true
);
}
}
//============External LP functions============
/**
* Function for token swap. This is top-level wrapper.
* @dev This function can be called only by contract itself
* @param args Decoded payload from received message
* @param tokenReceiver TIP-3 wallet that received tokens
* @param token_root Root contract address of transferred token
* @param amount Amount of transferred tokens
* @param sender_wallet Address of user's TIP-3 wallet
* @param original_gas_to Where to send remaining gas
*/
function _externalSwap(
TvmCell args, address tokenReceiver, address token_root, uint128 amount, address sender_wallet, address original_gas_to
)
external
onlySelf
{
TvmSlice tmpArgs = args.toSlice();
(bool isPayloadOk, address transferTokensTo) = _checkAndDecompressSwapPayload(tmpArgs);
TvmBuilder failTB;
if ( !isPayloadOk ){
failTB.store(wrongPayloadFormatMessage);
_sendTokens(tokenReceiver, sender_wallet, amount, original_gas_to, true, failTB.toCell());
return;
}
if ( !_checkIsLiquidityProvided() ){
failTB.store(noLiquidityProvidedMessage);
_sendTokens(tokenReceiver, sender_wallet, amount, original_gas_to, true, failTB.toCell());
return;
}
SwapInfo si = _swap(token_root, amount);
if (si.targetTokenAmount != 0) {
emit Swap(token_root, _getOppositeToken(token_root), si.swappableTokenAmount, si.targetTokenAmount, si.fee);
address tokenWallet = tokenReceiver == tokenWallets[T1] ? tokenWallets[T2] : tokenWallets[T1];
_sendTokens(tokenWallet, transferTokensTo, si.targetTokenAmount, original_gas_to, true, _createSwapPayload(si));
} else {
_sendTokens(tokenReceiver, sender_wallet, amount, original_gas_to, true, _createSwapFallbackPayload());
}
}
/**
* Function for liquidity providing. This is top-level wrapper.
* @dev This function can be called only by contract itself
* @param args Decoded payload from received message
* @param tokenReceiver TIP-3 wallet that received tokens
* @param amount Amount of transferred tokens
* @param sender_wallet Address of user's TIP-3 wallet
* @param sender_address Address of user's TON wallet
* @param original_gas_to Where to send remaining gas
*/
function _externalProvideLiquidity(
TvmCell args,
address tokenReceiver,
uint256 sender_public_key,
uint128 amount,
address sender_wallet,
address sender_address,
address original_gas_to
)
external
onlySelf
{
TvmSlice tmpArgs = args.toSlice();
(bool isPayloadOk, address lpWallet) = _checkAndDecompressProvideLiquidityPayload(tmpArgs);
TvmBuilder failTB;
if ( !isPayloadOk ) {
failTB.store(wrongPayloadFormatMessage);
_sendTokens(tokenReceiver, sender_wallet, amount, original_gas_to, false, failTB.toCell());
return;
}
// TODO: рефакторинг: разнести в разные функции создание записи о пользователе
TvmBuilder tb;
tb.store(sender_public_key, sender_address);
uint256 uniqueID = tvm.hash(tb.toCell());
if (!lpInputTokensInfo.exists(uniqueID)) {
LPProvidingInfo _l = LPProvidingInfo(
sender_address,
sender_public_key,
address.makeAddrStd(0, 0),
0,
address.makeAddrStd(0, 0),
0
);
lpInputTokensInfo.add(uniqueID, _l);
}
// TODO: рефокторинг: изменение хранящихся данных
LPProvidingInfo lppi = lpInputTokensInfo[uniqueID];
if (tokenReceiver == tokenWallets[0]) {
lppi.a1 += amount;
lppi.w1 = sender_wallet;
}
if (tokenReceiver == tokenWallets[1]) {
lppi.a2 += amount;
lppi.w2 = sender_wallet;
}
if (lppi.a1 == 0 || lppi.a2 == 0) {
lpInputTokensInfo[uniqueID] = lppi;
address(sender_wallet).transfer({value: 0, flag: 64});
}
else {
(uint128 rtp1, uint128 rtp2, ) = _provideLiquidity(lppi.a1, lppi.a2, sender_public_key, sender_address, lpWallet);
emit ProvideLiquidity(liquidityTokensMinted, rtp1, rtp2);
TvmBuilder payloadTB;
payloadTB.store(lppi.a1, rtp1, lppi.a2, rtp2);
_tryToReturnProvidingTokens(lppi.a1, rtp1, tokenWallets[T1], lppi.w1, original_gas_to, payloadTB);
_tryToReturnProvidingTokens(lppi.a2, rtp2, tokenWallets[T2], lppi.w2, original_gas_to, payloadTB);
delete lpInputTokensInfo[uniqueID];
}
}
// TODO: Антон: проверка провайдинга ликвидности по одному токену
/**
* Function for liquidity providing using one token. This is top-level wrapper.
* @dev This function can be called only by contract itself
* @param args Decoded payload from received message
* @param tokenRoot Address of TIP-3 root contract
* @param tokenReceiver TIP-3 wallet that received tokens
* @param amount Amount of transferred tokens
* @param sender_wallet Address of user's TIP-3 wallet
* @param sender_address Address of user's TON wallet