-
Notifications
You must be signed in to change notification settings - Fork 1
/
RootSwapPairContract.sol
382 lines (337 loc) · 12 KB
/
RootSwapPairContract.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
pragma ton-solidity ^0.39.0;
pragma AbiHeader expire;
pragma AbiHeader pubkey;
pragma AbiHeader time;
import './interfaces/swapPair/ISwapPairInformation.sol';
import './interfaces/rootSwapPair/IRootSwapPairContract.sol';
import './interfaces/rootSwapPair/IRootSwapPairUpgradePairCode.sol';
import './interfaces/rootSwapPair/IServiceInformation.sol';
import './interfaces/swapPair/IUpgradeSwapPairCode.sol';
import './libraries/rootSwapPair/RootSwapPairContractErrors.sol';
import './libraries/rootSwapPair/RootSwapPairConstants.sol';
import './SwapPairContract.sol';
contract RootSwapPairContract is
IRootSwapPairUpgradePairCode,
IRootSwapPairContract
{
//============Static variables============
// For debug purposes and for multiple instances of contract
uint256 static _randomNonce;
// Owner public key
uint256 static ownerPubkey;
//============Used variables============
// Swap pair code and it's version
TvmCell swapPairCode;
uint32 swapPairCodeVersion;
// Minimum required message value
uint256 minMessageValue;
// Minimum comission for contract
// This will be distributed if swap pair contract requires
uint256 contractServicePayment;
// Logic time of root contract creation
uint256 creationTimestamp;
// TIP-3 token root contract deployer
address tip3Deployer;
// Information about deployed swap pairs
// Required because we want only unique swap pairs
mapping (uint256 => SwapPairInfo) swapPairDB;
mapping (address => uint256) addressToUniqueID;
// Information about user balances
mapping (uint256 => uint128) userTONBalances;
//============Constructor===========
/**
* @param minMsgValue minimal value required for pair deployment
* @param contractSP payment for pair deployment
* @param tip3Deployer_ address of tip3 tokens deployer
*/
constructor(
uint256 minMsgValue,
uint256 contractSP,
address tip3Deployer_
) public {
tvm.accept();
creationTimestamp = now;
// Setting code
TvmCell empty;
swapPairCode = empty;
swapPairCodeVersion = 0;
// Setting payment options
minMessageValue = minMsgValue > RootSwapPairConstants.sendToNewSwapPair ?
minMsgValue :
RootSwapPairConstants.sendToNewSwapPair*RootSwapPairConstants.increaseNumerator/RootSwapPairConstants.increaseDenominator;
contractServicePayment = contractSP;
tip3Deployer = tip3Deployer_;
}
//============External functions============
/**
* @param tip3Deployer_ Address of new tip3 deployer contract
*/
function setTIP3DeployerAddress(
address tip3Deployer_
)
external
override
onlyOwner
{
tvm.accept();
tip3Deployer = tip3Deployer_;
}
/**
* Deploy swap pair with specified address of token root contract
* @param tokenRootContract1 Address of token root contract
* @param tokenRootContract2 Address of token root contract
*/
function deploySwapPair(
address tokenRootContract1,
address tokenRootContract2
)
external
override
onlyPaid
returns (address cA)
{
uint256 uniqueID = tokenRootContract1.value^tokenRootContract2.value;
require(
!swapPairDB.exists(uniqueID),
RootSwapPairContractErrors.ERROR_PAIR_ALREADY_EXISTS
);
tvm.accept();
uint256 currentTimestamp = now;
address contractAddress = new SwapPairContract{
value: RootSwapPairConstants.sendToNewSwapPair,
varInit: {
token1: tokenRootContract1,
token2: tokenRootContract2,
swapPairID: uniqueID
},
code: swapPairCode
}(address(this), tip3Deployer, swapPairCodeVersion);
// Storing info about deployed swap pair contracts
bytes tmp;
SwapPairInfo info = SwapPairInfo(
address(this), // root contract
tokenRootContract1, // token root
tokenRootContract2, // token root
address.makeAddrStd(0, 0), // lp token root
address.makeAddrStd(0, 0), // token wallet
address.makeAddrStd(0, 0), // token wallet
address.makeAddrStd(0, 0), // lp token wallet
currentTimestamp, // creation timestamp
contractAddress, // address of swap pair
uniqueID, // unique id of swap pair
swapPairCodeVersion, // code version of swap pair
tmp
);
swapPairDB.add(uniqueID, info);
addressToUniqueID.add(contractAddress, uniqueID);
emit DeploySwapPair(contractAddress, tokenRootContract1, tokenRootContract2);
return contractAddress;
}
//============Receive payments============
receive() external {
require(msg.value > minMessageValue);
TvmSlice ts = msg.data;
uint pubkey = ts.decode(uint);
userTONBalances[pubkey] += msg.value;
}
fallback() external {
}
//============Get functions============
/**
* Get all swap pairs
*/
function getAllSwapPairsID() external override view returns (uint256[] ids) {
// uint256 uniqueId;
for((uint256 uniqueId,): swapPairDB) {
ids.push(uniqueId);
}
}
/**
* Check if pair exists
* @param uniqueID unique ID of swap pair
*/
function getPairInfoByID(
uint256 uniqueID
) external view override returns(SwapPairInfo swapPairInfo) {
optional(SwapPairInfo) spi = swapPairDB.fetch(uniqueID);
require(
spi.hasValue(),
RootSwapPairContractErrors.ERROR_PAIR_DOES_NOT_EXIST
);
return spi.get();
}
/**
* Check if pair exists
* @param tokenRootContract1 Address of token root contract
* @param tokenRootContract2 Address of token root contract
*/
function getPairInfo(
address tokenRootContract1,
address tokenRootContract2
) external view override returns(SwapPairInfo) {
uint256 uniqueID = tokenRootContract1.value^tokenRootContract2.value;
optional(SwapPairInfo) spi = swapPairDB.fetch(uniqueID);
require(
spi.hasValue(),
RootSwapPairContractErrors.ERROR_PAIR_DOES_NOT_EXIST
);
return spi.get();
}
/**
* Get service information about root contract
*/
function getServiceInformation() external view override returns (ServiceInfo) {
return ServiceInfo(
ownerPubkey,
address(this).balance,
creationTimestamp,
swapPairCodeVersion,
swapPairCode
);
}
/**
* Check if pair exists
* @param tokenRootContract1 Address of token root contract
* @param tokenRootContract2 Address of token root contract
*/
function checkIfPairExists(
address tokenRootContract1,
address tokenRootContract2
) external view override returns(bool) {
uint256 uniqueID = tokenRootContract1.value^tokenRootContract2.value;
return swapPairDB.exists(uniqueID);
}
/**
* Get future address of swap pair
* @param tokenRootContract1 Address of token root contract
* @param tokenRootContract2 Address of token root contract
*/
function getFutureSwapPairAddress(
address tokenRootContract1,
address tokenRootContract2
) external view override returns(address) {
uint256 uniqueID = tokenRootContract1.value^tokenRootContract2.value;
return _calculateSwapPairContractAddress(tokenRootContract1, tokenRootContract2, uniqueID);
}
//============Callback functions============
/**
* Callback called when swap pair is fully intialized and ready for swaps
* @param spi Swap pair information
*/
function swapPairInitializedCallback(SwapPairInfo spi) external pairWithAddressExists(msg.sender) {
tvm.accept();
swapPairDB[addressToUniqueID[msg.sender]] = spi;
emit SwapPairInitialized(msg.sender);
}
//============Swap pair upgrade functionality============
/**
* Set new swap pair code
* @param code New swap pair code
* @param codeVersion New swap pair code version
*/
function setSwapPairCode(
TvmCell code,
uint32 codeVersion
) external override onlyOwner {
require(
codeVersion > swapPairCodeVersion,
RootSwapPairContractErrors.ERROR_CODE_IS_NOT_UPDATED_OR_IS_DOWNGRADED
);
tvm.accept();
swapPairCode = code;
swapPairCodeVersion = codeVersion;
emit SetSwapPairCode(codeVersion);
}
/**
* Upgrade swap pair code
* @param tokenRootContract1 Address of token root contract
* @param tokenRootContract2 Address of token root contract
*/
function upgradeSwapPair(
address tokenRootContract1,
address tokenRootContract2
)
external
override
pairWithTokensExist(tokenRootContract1, tokenRootContract2)
{
uint256 uniqueID = tokenRootContract1.value^tokenRootContract2.value;
SwapPairInfo info = swapPairDB.at(uniqueID);
require(
info.swapPairCodeVersion < swapPairCodeVersion,
RootSwapPairContractErrors.ERROR_CODE_IS_NOT_UPDATED_OR_IS_DOWNGRADED
);
require(
msg.value > RootSwapPairConstants.requiredForUpgrade,
RootSwapPairContractErrors.ERROR_MESSAGE_VALUE_IS_TOO_LOW
);
IUpgradeSwapPairCode(info.swapPairAddress).updateSwapPairCode{value: msg.value*3/4}(swapPairCode, swapPairCodeVersion);
info.swapPairCodeVersion = swapPairCodeVersion;
swapPairDB.replace(uniqueID, info);
emit UpgradeSwapPair(uniqueID, swapPairCodeVersion);
}
//============Private functions============
/**
* Calculate future swap pair contract address
* @param tokenRootContract1 Address of token root contract
* @param tokenRootContract2 Address of token root contract
* @param uniqueID ID of swap pair
*/
function _calculateSwapPairContractAddress(
address tokenRootContract1,
address tokenRootContract2,
uint256 uniqueID
) private view inline returns(address) {
TvmCell stateInit = tvm.buildStateInit({
contr: SwapPairContract,
varInit: {
token1: tokenRootContract1,
token2: tokenRootContract2,
swapPairID: uniqueID
},
code: swapPairCode
});
return address(tvm.hash(stateInit));
}
//============Modifiers============
modifier onlyOwner() {
require(
msg.pubkey() == ownerPubkey,
RootSwapPairContractErrors.ERROR_MESSAGE_SENDER_IS_NOT_OWNER
);
_;
}
modifier onlyPaid() {
require(
msg.value >= minMessageValue ||
userTONBalances[msg.pubkey()] >= minMessageValue ||
msg.pubkey() == ownerPubkey,
RootSwapPairContractErrors.ERROR_MESSAGE_VALUE_IS_TOO_LOW
);
_;
}
modifier pairWithTokensExist(address t1, address t2) {
uint256 uniqueID = t1.value^t2.value;
optional(SwapPairInfo) pairInfo = swapPairDB.fetch(uniqueID);
require(
pairInfo.hasValue(),
RootSwapPairContractErrors.ERROR_PAIR_DOES_NOT_EXIST
);
_;
}
modifier pairExists(uint256 uniqueID, bool exists) {
optional(SwapPairInfo) pairInfo = swapPairDB.fetch(uniqueID);
require(
pairInfo.hasValue() == exists,
RootSwapPairContractErrors.ERROR_PAIR_DOES_NOT_EXIST
);
_;
}
modifier pairWithAddressExists(address pairAddress) {
require(
addressToUniqueID.exists(pairAddress),
RootSwapPairContractErrors.ERROR_PAIR_WITH_ADDRESS_DOES_NOT_EXIST
);
_;
}
}