Skip to content

Commit 03cfaf8

Browse files
committed
MASSIVE MONSTER REFACTORING
1 parent 207d67f commit 03cfaf8

File tree

4 files changed

+27
-64
lines changed

4 files changed

+27
-64
lines changed

contracts/SwapPair/RootSwapPairContract.sol

+1-12
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ contract RootSwapPairContract is
115115
swapPairID: uniqueID
116116
},
117117
code: swapPairCode
118-
}(address(this), msg.pubkey(), tip3Deployer);
118+
}(address(this), tip3Deployer);
119119

120120
// Storing info about deployed swap pair contracts
121121
SwapPairInfo info = SwapPairInfo(
@@ -126,7 +126,6 @@ contract RootSwapPairContract is
126126
address.makeAddrStd(0, 0), // token wallet
127127
address.makeAddrStd(0, 0), // token wallet
128128
address.makeAddrStd(0, 0), // lp token wallet
129-
msg.pubkey(), // swap pair deployer
130129
currentTimestamp, // creation timestamp
131130
contractAddress, // address of swap pair
132131
uniqueID, // unique id of swap pair
@@ -314,16 +313,6 @@ contract RootSwapPairContract is
314313
_;
315314
}
316315

317-
modifier onlyPairDeployer(uint256 uniqueID) {
318-
SwapPairInfo spi = swapPairDB.at(uniqueID);
319-
require(
320-
spi.deployerPubkey == msg.pubkey() ||
321-
ownerPubkey == msg.pubkey(),
322-
RootSwapPairContractErrors.ERROR_MESSAGE_SENDER_IS_NOT_DEPLOYER
323-
);
324-
_;
325-
}
326-
327316
modifier pairWithAddressExists(address pairAddress) {
328317
require(
329318
addressToUniqueID.exists(pairAddress),

contracts/SwapPair/SwapPairContract.sol

+22-51
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
2626
uint static swapPairID;
2727

2828
uint32 swapPairCodeVersion = 1;
29-
uint256 swapPairDeployer;
3029
address swapPairRootContract;
3130
address tip3Deployer;
3231

@@ -38,7 +37,6 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
3837

3938
uint256 liquidityTokensMinted = 0;
4039

41-
mapping(uint8 => address) tokens;
4240
mapping(address => uint8) tokenPositions;
4341

4442
//Deployed token wallets addresses
@@ -49,7 +47,6 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
4947

5048
//Liquidity Pools
5149
mapping(uint8 => uint128) private lps;
52-
uint256 public kLast; // lps[T1] * lps[T2] after most recent swap
5350

5451

5552
//Pair creation timestamp
@@ -72,46 +69,25 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
7269
IRootTokenContract.IRootTokenContractDetails T1Info;
7370
IRootTokenContract.IRootTokenContractDetails T2Info;
7471

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.";
7577

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.";
9878
//============Contract initialization functions============
9979

100-
constructor(address rootContract, uint spd, address tip3Deployer_) public {
80+
constructor(address rootContract, address tip3Deployer_) public {
10181
tvm.accept();
10282
creationTimestamp = now;
10383
swapPairRootContract = rootContract;
104-
swapPairDeployer = spd;
10584
tip3Deployer = tip3Deployer_;
10685

107-
tokens[T1] = token1;
108-
tokens[T2] = token2;
10986
tokenPositions[token1] = T1;
11087
tokenPositions[token2] = T2;
11188

11289
lps[T1] = 0;
11390
lps[T2] = 0;
114-
kLast = 0;
11591

11692
//Deploy tokens wallets
11793
_deployWallet(token1);
@@ -199,6 +175,7 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
199175
function _prepareDataForTIP3Deploy()
200176
external
201177
view
178+
onlySelf
202179
{
203180
tvm.accept();
204181
string res = string(T1Info.symbol);
@@ -210,6 +187,7 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
210187
function _deployTIP3LpToken(bytes name, bytes symbol)
211188
external
212189
view
190+
onlySelf
213191
{
214192
tvm.accept();
215193
ITIP3TokenDeployer(tip3Deployer).deployTIP3Token{
@@ -278,8 +256,7 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
278256
initialized
279257
returns (uint128 providedFirstTokenAmount, uint128 providedSecondTokenAmount)
280258
{
281-
uint256 _m = 0;
282-
(providedFirstTokenAmount, providedSecondTokenAmount, _m) = _calculateProvidingLiquidityInfo(maxFirstTokenAmount, maxSecondTokenAmount);
259+
(providedFirstTokenAmount, providedSecondTokenAmount,) = _calculateProvidingLiquidityInfo(maxFirstTokenAmount, maxSecondTokenAmount);
283260
}
284261

285262
// NOTICE: Requires a lot of gas, will only work with runLocal
@@ -290,8 +267,7 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
290267
initialized
291268
returns (uint128 withdrawedFirstTokenAmount, uint128 withdrawedSecondTokenAmount)
292269
{
293-
uint256 _b = 0;
294-
(withdrawedFirstTokenAmount, withdrawedSecondTokenAmount, _b) = _calculateWithdrawingLiquidityInfo(liquidityTokensAmount);
270+
(withdrawedFirstTokenAmount, withdrawedSecondTokenAmount,) = _calculateWithdrawingLiquidityInfo(liquidityTokensAmount);
295271
}
296272

297273
// NOTICE: Requires a lot of gas, will only work with runLocal
@@ -379,7 +355,7 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
379355

380356
uint128 fee = swappableTokenAmount - math.muldivc(swappableTokenAmount, feeNominator, feeDenominator);
381357
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) );
383359

384360
uint128 targetTokenAmount = lps[toK] - newToPool;
385361

@@ -403,7 +379,6 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
403379

404380
lps[fromK] = _si.newFromPool;
405381
lps[toK] = _si.newToPool;
406-
kLast = uint256(_si.newFromPool) * uint256(_si.newToPool);
407382

408383
return SwapInfo(swappableTokenAmount, _si.targetTokenAmount, _si.fee);
409384
}
@@ -445,7 +420,6 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
445420
(provided1, provided2, toMint) = _calculateProvidingLiquidityInfo(amount1, amount2);
446421
lps[T1] += provided1;
447422
lps[T2] += provided2;
448-
kLast = uint256(lps[T1]) * uint256(lps[T2]);
449423
liquidityTokensMinted += toMint;
450424

451425
if (lpWallet.value == 0) {
@@ -490,7 +464,6 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
490464
if (withdrawed1 != 0 && withdrawed2 != 0) {
491465
lps[T1] -= withdrawed1;
492466
lps[T2] -= withdrawed2;
493-
kLast = uint256(lps[T1]) * uint256(lps[T2]);
494467

495468
if (!tokensBurnt) {
496469
_burnTransferredLPTokens(tokenAmount);
@@ -530,7 +503,6 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
530503

531504
lps[T1] -= withdrawed1;
532505
lps[T2] -= withdrawed2;
533-
kLast = uint256(lps[T1]) * uint256(lps[T2]);
534506

535507
if (!tokensBurnt) {
536508
_burnTransferredLPTokens(tokenAmount);
@@ -993,36 +965,36 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
993965

994966
//============Payload manipulation functions============
995967

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);
998970
transferTokensTo = _decompressSwapPayload(tmpArgs);
999971
bool isContentOk = transferTokensTo.value != 0;
1000972
isPayloadOk = isSizeOk && isContentOk;
1001973
}
1002974

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);
1005977
lpTokenAddress = _decompressProvideLiquidityPayload(tmpArgs);
1006978
bool isContentOk = lpTokenAddress.value != 0;
1007979
isPayloadOk = isSizeOk && isContentOk;
1008980
}
1009981

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);
1012984
lpwi = _decompressWithdrawLiquidityPayload(tmpArgs);
1013985
bool isContentOk = lpwi.tr1.value != 0 && lpwi.tr2.value != 0 && lpwi.tw1.value != 0 && lpwi.tw2.value != 0;
1014986
isPayloadOk = isSizeOk && isContentOk;
1015987
}
1016988

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);
1019991
lpTokenAddress = _decompressProvideLiquidityOneTokenPayload(tmpArgs);
1020992
bool isContentOk = lpTokenAddress.value != 0;
1021993
isPayloadOk = isSizeOk && isContentOk;
1022994
}
1023995

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);
1026998
(tokenRoot, userWallet) = _decompresskWithdrawLiquidityOneTokenPayload(tmpArgs);
1027999
bool isContentOk = (tokenRoot.value != 0) && (userWallet.value != 0);
10281000
isPayloadOk = isSizeOk && isContentOk;
@@ -1162,7 +1134,6 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
11621134
swapPairRootContract,
11631135
token1, token2, lpTokenRootAddress,
11641136
tokenWallets[0], tokenWallets[1], lpTokenWalletAddress,
1165-
swapPairDeployer,
11661137
creationTimestamp,
11671138
address(this),
11681139
swapPairID, swapPairCodeVersion
@@ -1183,7 +1154,7 @@ contract SwapPairContract is ITokensReceivedCallback, ISwapPairInformation, IUpg
11831154
}
11841155

11851156
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;
11871158
}
11881159

11891160
function _sqrt(uint256 x) private pure /*inline*/ returns(uint256){

contracts/SwapPair/interfaces/swapPair/ISwapPairInformation.sol

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ interface ISwapPairInformation {
1313
address tokenWallet1;
1414
address tokenWallet2;
1515
address lpTokenWallet;
16-
uint256 deployerPubkey;
1716
uint256 deployTimestamp;
1817
address swapPairAddress;
1918
uint256 uniqueId;

contracts/SwapPair/libraries/swapPair/SwapPairConstants.sol

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ library SwapPairConstants {
1515
uint128 constant sendToTIP3TokenWallets = 110 milli;
1616
uint128 constant sendToRootToken = 500 milli;
1717

18+
// fee constants
19+
uint128 constant feeNominator = 997;
20+
uint128 constant feeDenominator = 1000;
21+
1822
// TIP-3 root contract parameters
1923
uint8 constant tip3LpDecimals = 0;
2024
uint8 constant contractFullyInitialized = 4;

0 commit comments

Comments
 (0)