@@ -26,7 +26,6 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
26
26
uint static swapPairID;
27
27
28
28
uint32 swapPairCodeVersion = 1 ;
29
- uint256 swapPairDeployer;
30
29
address swapPairRootContract;
31
30
address tip3Deployer;
32
31
@@ -38,7 +37,6 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
38
37
39
38
uint256 liquidityTokensMinted = 0 ;
40
39
41
- mapping (uint8 => address ) tokens;
42
40
mapping (address => uint8 ) tokenPositions;
43
41
44
42
//Deployed token wallets addresses
@@ -49,7 +47,6 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
49
47
50
48
//Liquidity Pools
51
49
mapping (uint8 => uint128 ) private lps;
52
- uint256 public kLast; // lps[T1] * lps[T2] after most recent swap
53
50
54
51
55
52
//Pair creation timestamp
@@ -72,46 +69,25 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
72
69
IRootTokenContract.IRootTokenContractDetails T1Info;
73
70
IRootTokenContract.IRootTokenContractDetails T2Info;
74
71
72
+ uint8 constant wrongPayloadFormatMessage = 0 ; //Received payload is invalid or has wrong format.";
73
+ uint8 constant unknownOperationIdorWrongPayload = 1 ; //Received payload contains unknow Operation ID or is malformed.";
74
+ uint8 constant sumIsTooLowForSwap = 2 ; //Provided token amount is not enough for swap.";
75
+ uint8 constant noLiquidityProvidedMessage = 3 ; //No liquidity provided yet. Swaps are forbidden.";
76
+ uint8 constant sumIsTooLowForLPTokenWithdraw = 4 ; //Provided LP token amount is not enough to withdraw liquidity.";
75
77
76
- // Waiting for something except for numbers in libraries ...
77
- OperationSizeRequirements SwapOperationSize = OperationSizeRequirements (
78
- SwapPairConstants.SwapOperationBits, SwapPairConstants.SwapOperationRefs
79
- );
80
- OperationSizeRequirements WithdrawOperationSize = OperationSizeRequirements (
81
- SwapPairConstants.WithdrawOperationBits, SwapPairConstants.WithdrawOperationRefs
82
- );
83
- OperationSizeRequirements WithdrawOperationSizeOneToken = OperationSizeRequirements (
84
- SwapPairConstants.WithdrawOneOperationBits, SwapPairConstants.WithdrawOneOperationRefs
85
- );
86
- OperationSizeRequirements ProvideLiquidityOperationSize = OperationSizeRequirements (
87
- SwapPairConstants.ProvideLiquidityBits, SwapPairConstants.ProvideLiquidityRefs
88
- );
89
- OperationSizeRequirements ProvideLiquidityOperationSizeOneToken = OperationSizeRequirements (
90
- SwapPairConstants.ProvideLiquidityOneBits, SwapPairConstants.ProvideLiquidityOneRefs
91
- );
92
-
93
- string constant wrongPayloadFormatMessage = "Received payload is invalid or has wrong format. " ;
94
- string constant unknownOperationIdorWrongPayload = "Received payload contains unknow Operation ID or is malformed. " ;
95
- string constant sumIsTooLowForSwap = "Provided token amount is not enough for swap. " ;
96
- string constant noLiquidityProvidedMessage = "No liquidity provided yet. Swaps are forbidden. " ;
97
- string constant sumIsTooLowForLPTokenWithdraw = "Provided LP token amount is not enough to withdraw liquidity. " ;
98
78
//============Contract initialization functions============
99
79
100
- constructor (address rootContract , uint spd , address tip3Deployer_ ) public {
80
+ constructor (address rootContract , address tip3Deployer_ ) public {
101
81
tvm.accept ();
102
82
creationTimestamp = now ;
103
83
swapPairRootContract = rootContract;
104
- swapPairDeployer = spd;
105
84
tip3Deployer = tip3Deployer_;
106
85
107
- tokens[T1] = token1;
108
- tokens[T2] = token2;
109
86
tokenPositions[token1] = T1;
110
87
tokenPositions[token2] = T2;
111
88
112
89
lps[T1] = 0 ;
113
90
lps[T2] = 0 ;
114
- kLast = 0 ;
115
91
116
92
//Deploy tokens wallets
117
93
_deployWallet (token1);
@@ -199,6 +175,7 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
199
175
function _prepareDataForTIP3Deploy ()
200
176
external
201
177
view
178
+ onlySelf
202
179
{
203
180
tvm.accept ();
204
181
string res = string (T1Info.symbol);
@@ -210,6 +187,7 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
210
187
function _deployTIP3LpToken (bytes name , bytes symbol )
211
188
external
212
189
view
190
+ onlySelf
213
191
{
214
192
tvm.accept ();
215
193
ITIP3TokenDeployer (tip3Deployer).deployTIP3Token {
@@ -278,8 +256,7 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
278
256
initialized
279
257
returns (uint128 providedFirstTokenAmount , uint128 providedSecondTokenAmount )
280
258
{
281
- uint256 _m = 0 ;
282
- (providedFirstTokenAmount, providedSecondTokenAmount, _m) = _calculateProvidingLiquidityInfo (maxFirstTokenAmount, maxSecondTokenAmount);
259
+ (providedFirstTokenAmount, providedSecondTokenAmount,) = _calculateProvidingLiquidityInfo (maxFirstTokenAmount, maxSecondTokenAmount);
283
260
}
284
261
285
262
// NOTICE: Requires a lot of gas, will only work with runLocal
@@ -290,8 +267,7 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
290
267
initialized
291
268
returns (uint128 withdrawedFirstTokenAmount , uint128 withdrawedSecondTokenAmount )
292
269
{
293
- uint256 _b = 0 ;
294
- (withdrawedFirstTokenAmount, withdrawedSecondTokenAmount, _b) = _calculateWithdrawingLiquidityInfo (liquidityTokensAmount);
270
+ (withdrawedFirstTokenAmount, withdrawedSecondTokenAmount,) = _calculateWithdrawingLiquidityInfo (liquidityTokensAmount);
295
271
}
296
272
297
273
// NOTICE: Requires a lot of gas, will only work with runLocal
@@ -379,7 +355,7 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
379
355
380
356
uint128 fee = swappableTokenAmount - math.muldivc (swappableTokenAmount, feeNominator, feeDenominator);
381
357
uint128 newFromPool = lps[fromK] + swappableTokenAmount;
382
- uint128 newToPool = uint128 ( math.divc (kLast , newFromPool - fee) );
358
+ uint128 newToPool = uint128 ( math.divc (uint256 (lps[ 0 ]) * uint256 (lps[ 1 ]) , newFromPool - fee) );
383
359
384
360
uint128 targetTokenAmount = lps[toK] - newToPool;
385
361
@@ -403,7 +379,6 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
403
379
404
380
lps[fromK] = _si.newFromPool;
405
381
lps[toK] = _si.newToPool;
406
- kLast = uint256 (_si.newFromPool) * uint256 (_si.newToPool);
407
382
408
383
return SwapInfo (swappableTokenAmount, _si.targetTokenAmount, _si.fee);
409
384
}
@@ -445,7 +420,6 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
445
420
(provided1, provided2, toMint) = _calculateProvidingLiquidityInfo (amount1, amount2);
446
421
lps[T1] += provided1;
447
422
lps[T2] += provided2;
448
- kLast = uint256 (lps[T1]) * uint256 (lps[T2]);
449
423
liquidityTokensMinted += toMint;
450
424
451
425
if (lpWallet.value == 0 ) {
@@ -490,7 +464,6 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
490
464
if (withdrawed1 != 0 && withdrawed2 != 0 ) {
491
465
lps[T1] -= withdrawed1;
492
466
lps[T2] -= withdrawed2;
493
- kLast = uint256 (lps[T1]) * uint256 (lps[T2]);
494
467
495
468
if (! tokensBurnt) {
496
469
_burnTransferredLPTokens (tokenAmount);
@@ -530,7 +503,6 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
530
503
531
504
lps[T1] -= withdrawed1;
532
505
lps[T2] -= withdrawed2;
533
- kLast = uint256 (lps[T1]) * uint256 (lps[T2]);
534
506
535
507
if (! tokensBurnt) {
536
508
_burnTransferredLPTokens (tokenAmount);
@@ -993,36 +965,36 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
993
965
994
966
//============Payload manipulation functions============
995
967
996
- function _checkAndDecompressSwapPayload (TvmSlice tmpArgs ) private view returns (bool isPayloadOk , address transferTokensTo ) {
997
- bool isSizeOk = tmpArgs.hasNBitsAndRefs (SwapOperationSize.bits, SwapOperationSize.refs );
968
+ function _checkAndDecompressSwapPayload (TvmSlice tmpArgs ) private pure returns (bool isPayloadOk , address transferTokensTo ) {
969
+ bool isSizeOk = tmpArgs.hasNBitsAndRefs (SwapPairConstants.SwapOperationBits, SwapPairConstants.SwapOperationRefs );
998
970
transferTokensTo = _decompressSwapPayload (tmpArgs);
999
971
bool isContentOk = transferTokensTo.value != 0 ;
1000
972
isPayloadOk = isSizeOk && isContentOk;
1001
973
}
1002
974
1003
- function _checkAndDecompressProvideLiquidityPayload (TvmSlice tmpArgs ) private view returns (bool isPayloadOk , address lpTokenAddress ) {
1004
- bool isSizeOk = tmpArgs.hasNBitsAndRefs (ProvideLiquidityOperationSize.bits, ProvideLiquidityOperationSize.refs );
975
+ function _checkAndDecompressProvideLiquidityPayload (TvmSlice tmpArgs ) private pure returns (bool isPayloadOk , address lpTokenAddress ) {
976
+ bool isSizeOk = tmpArgs.hasNBitsAndRefs (SwapPairConstants.ProvideLiquidityBits, SwapPairConstants.ProvideLiquidityRefs );
1005
977
lpTokenAddress = _decompressProvideLiquidityPayload (tmpArgs);
1006
978
bool isContentOk = lpTokenAddress.value != 0 ;
1007
979
isPayloadOk = isSizeOk && isContentOk;
1008
980
}
1009
981
1010
- function _checkAndDecompressWithdrawLiquidityPayload (TvmSlice tmpArgs ) private view returns (bool isPayloadOk , LPWithdrawInfo lpwi ) {
1011
- bool isSizeOk = tmpArgs.hasNBitsAndRefs (WithdrawOperationSize.bits, WithdrawOperationSize.refs );
982
+ function _checkAndDecompressWithdrawLiquidityPayload (TvmSlice tmpArgs ) private pure returns (bool isPayloadOk , LPWithdrawInfo lpwi ) {
983
+ bool isSizeOk = tmpArgs.hasNBitsAndRefs (SwapPairConstants.WithdrawOperationBits, SwapPairConstants.WithdrawOperationRefs );
1012
984
lpwi = _decompressWithdrawLiquidityPayload (tmpArgs);
1013
985
bool isContentOk = lpwi.tr1.value != 0 && lpwi.tr2.value != 0 && lpwi.tw1.value != 0 && lpwi.tw2.value != 0 ;
1014
986
isPayloadOk = isSizeOk && isContentOk;
1015
987
}
1016
988
1017
- function _checkAndDecompressProvideLiquidityOneTokenPayload (TvmSlice tmpArgs ) private view returns (bool isPayloadOk , address lpTokenAddress ) {
1018
- bool isSizeOk = tmpArgs.hasNBitsAndRefs (ProvideLiquidityOperationSizeOneToken.bits, ProvideLiquidityOperationSizeOneToken.refs );
989
+ function _checkAndDecompressProvideLiquidityOneTokenPayload (TvmSlice tmpArgs ) private pure returns (bool isPayloadOk , address lpTokenAddress ) {
990
+ bool isSizeOk = tmpArgs.hasNBitsAndRefs (SwapPairConstants.ProvideLiquidityOneBits, SwapPairConstants.ProvideLiquidityOneRefs );
1019
991
lpTokenAddress = _decompressProvideLiquidityOneTokenPayload (tmpArgs);
1020
992
bool isContentOk = lpTokenAddress.value != 0 ;
1021
993
isPayloadOk = isSizeOk && isContentOk;
1022
994
}
1023
995
1024
- function _checkAndDecompressWithdrawLiquidityOneTokenPayload (TvmSlice tmpArgs ) private view returns (bool isPayloadOk , address tokenRoot , address userWallet ) {
1025
- bool isSizeOk = tmpArgs.hasNBitsAndRefs (WithdrawOperationSizeOneToken.bits, WithdrawOperationSizeOneToken.refs );
996
+ function _checkAndDecompressWithdrawLiquidityOneTokenPayload (TvmSlice tmpArgs ) private pure returns (bool isPayloadOk , address tokenRoot , address userWallet ) {
997
+ bool isSizeOk = tmpArgs.hasNBitsAndRefs (SwapPairConstants.WithdrawOneOperationBits, SwapPairConstants.WithdrawOneOperationRefs );
1026
998
(tokenRoot, userWallet) = _decompresskWithdrawLiquidityOneTokenPayload (tmpArgs);
1027
999
bool isContentOk = (tokenRoot.value != 0 ) && (userWallet.value != 0 );
1028
1000
isPayloadOk = isSizeOk && isContentOk;
@@ -1162,7 +1134,6 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
1162
1134
swapPairRootContract,
1163
1135
token1, token2, lpTokenRootAddress,
1164
1136
tokenWallets[0 ], tokenWallets[1 ], lpTokenWalletAddress,
1165
- swapPairDeployer,
1166
1137
creationTimestamp,
1167
1138
address (this ),
1168
1139
swapPairID, swapPairCodeVersion
@@ -1183,7 +1154,7 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
1183
1154
}
1184
1155
1185
1156
function _checkIsLiquidityProvided () private view /*inline*/ returns (bool ) {
1186
- return lps[T1] > 0 && lps[T2] > 0 && kLast > SwapPairConstants.kMin ;
1157
+ return lps[T1] > 0 && lps[T2] > 0 ;
1187
1158
}
1188
1159
1189
1160
function _sqrt (uint256 x ) private pure /*inline*/ returns (uint256 ){
0 commit comments