web3chain041903 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (24) hide show
  1. package/.idea/Web3-Financial-Engineering-Courses-main.iml +9 -0
  2. package/.idea/git_toolbox_prj.xml +15 -0
  3. package/.idea/modules.xml +8 -0
  4. package/.idea/vcs.xml +6 -0
  5. package/0.web3_financial_infrastructure/dex_protocol/dex.sol +29 -0
  6. package/0.web3_financial_infrastructure/dex_protocol/dexInfra/AMMData.sol +150 -0
  7. package/0.web3_financial_infrastructure/dex_protocol/dexInfra/Algorithm.sol +97 -0
  8. package/0.web3_financial_infrastructure/dex_protocol/dexInfra/Idatastore.sol +120 -0
  9. package/0.web3_financial_infrastructure/dex_protocol/dexInfra/datastorage.sol +383 -0
  10. package/0.web3_financial_infrastructure/dex_protocol/dexInfra/lptoken.sol +33 -0
  11. package/0.web3_financial_infrastructure/dex_protocol/dexInfra/swap.sol +550 -0
  12. package/0.web3_financial_infrastructure/dex_protocol/dex_protocol.md +51 -0
  13. package/0.web3_financial_infrastructure/liquidity_protocol/bank.sol +258 -0
  14. package/0.web3_financial_infrastructure/liquidity_protocol/bank0.sol +29 -0
  15. package/0.web3_financial_infrastructure/liquidity_protocol/bank1.sol +58 -0
  16. package/0.web3_financial_infrastructure/liquidity_protocol/bank2.sol +124 -0
  17. package/0.web3_financial_infrastructure/liquidity_protocol/bank3.sol +210 -0
  18. package/0.web3_financial_infrastructure/liquidity_protocol/liquidity_protocol.md +58 -0
  19. package/0.web3_financial_infrastructure/liquidity_protocol/product.md +33 -0
  20. package/LICENSE +21 -0
  21. package/README.md +113 -0
  22. package/package.json +115 -0
  23. package/tea.yaml +96 -0
  24. package/tranpack.sh +23 -0
@@ -0,0 +1,383 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ import "./lptoken.sol";
4
+ import "./IWETH.sol";
5
+ import "@openzeppelin/contracts/access/AccessControl.sol";
6
+ import "./Algorithm.sol";
7
+
8
+ pragma solidity ^0.8.20;
9
+
10
+ //to do list pay liquidity
11
+
12
+ contract Datastore is AccessControl{
13
+
14
+
15
+ //lend
16
+ mapping (address => address) _ctoken;
17
+ mapping (address => bool) _whiteListToken;
18
+
19
+ mapping (address => uint) _whiteListReserve;
20
+ mapping (address => uint) _whiteListBorrowed;
21
+
22
+ mapping (address => bool) _whiteListLpAddr;
23
+
24
+ mapping (address => mapping (address => uint)) _userWhiteListReserve;
25
+ mapping (address => mapping (address => uint)) _userWhiteListBorrowed;
26
+
27
+
28
+ address[] _whiteListTokenList;
29
+
30
+
31
+ bytes32 public constant FEE_CONTROL_ROLE = keccak256("FEE_CONTROL_ROLE");
32
+ bytes32 public constant PAIR_CONTROL_ROLE = keccak256("PAIR_CONTROL_ROLE");
33
+ bytes32 public constant DATA_CONTROL_ROLE = keccak256("DATA_CONTROL_ROLE");
34
+ bytes32 public constant COMP_CONTROL_ROLE = keccak256("COMP_CONTROL_ROLE");
35
+
36
+
37
+ mapping (address => mapping (address => mapping (uint => uint))) public _borrowedAmount;//user => lp => assetType => amount
38
+ mapping (address => mapping (address => mapping (uint => uint))) public _payDebtAmount;
39
+ mapping (address => mapping (uint => uint)) public _allBorrowedAmount;//lp => assetType => amount
40
+ mapping (address => mapping (uint => uint)) public _allPayDebtAmount;
41
+
42
+
43
+
44
+ uint constant public ONE_ETH = 10 ** 18;
45
+
46
+
47
+ mapping(address => address) _pairCreator;//lpAddr pairCreator
48
+ address [] _lpTokenAddressList;//lptoken的数组
49
+ address [] _whiteListLpTokenList;
50
+
51
+ mapping(address => mapping(address => uint)) _virtualReserve;
52
+ mapping(address => mapping(address => uint)) _reserve;//第一个address是lptoken的address ,第2个是相应token的资产,uint是资产的amount
53
+ mapping(address => mapping(address => uint)) _borrowed;
54
+
55
+ mapping (address => mapping (address => mapping (address => uint))) _userReserve;
56
+ //mapping (address => mapping (address => mapping (address => uint))) _userBorrowed;
57
+ mapping(address => uint) _userBorrowed;
58
+
59
+
60
+ uint _lpFee;//fee to pool
61
+ uint _fundFee;
62
+ mapping(address => mapping(address => address)) _lpToken;
63
+ IWETH WETH;
64
+ address _WETHAddr;
65
+
66
+ //pair op
67
+
68
+ //可以遍历出user的所有lp address
69
+ mapping (address => address []) private _userPairList;
70
+
71
+ //mapping (address => bool) public isStablePair;
72
+ mapping (address => address[2]) _lpInfo;
73
+ mapping (address => bool) _lpSwapStatic;
74
+ mapping (address => uint) _lpProfit;
75
+ mapping (address => uint) _lpCreatedTime;
76
+ mapping (address => mapping (address => bool)) _userLpExist;
77
+ mapping (address => address[]) _userLpTokenList;
78
+
79
+
80
+
81
+ address public fundAddr;
82
+ //lend
83
+ constructor() {
84
+ address defaultAdmin = msg.sender;
85
+ _grantRole(DEFAULT_ADMIN_ROLE, defaultAdmin);
86
+ _grantRole(PAIR_CONTROL_ROLE, defaultAdmin);
87
+ _grantRole(DATA_CONTROL_ROLE, defaultAdmin);
88
+ _grantRole(FEE_CONTROL_ROLE, defaultAdmin);
89
+ }
90
+ /*
91
+ function iswhiteListLpAddr(address lpAddr)public view returns(uint)
92
+ {
93
+ return _whiteListBorrowed[token];
94
+ }
95
+
96
+ function whiteListBorrowed(address token, uint amount)public onlyRole(DATA_CONTROL_ROLE)
97
+ {
98
+ _whiteListBorrowed[token] = amount;
99
+ }*/
100
+
101
+ function calAllTokenBorrowed() public view returns(uint)
102
+ {
103
+ uint amount;
104
+
105
+ for(uint i=0;i < _whiteListTokenList.length;i ++)
106
+ {
107
+ amount += _whiteListBorrowed[_whiteListTokenList[i]];
108
+
109
+ }
110
+
111
+ return amount;
112
+
113
+ }
114
+
115
+ function getBorrowedAmount(address asset) public view returns(uint)
116
+ {
117
+ return _userBorrowed[asset];
118
+ }
119
+
120
+ function userBorrowed(address asset, uint amount) public
121
+ {
122
+ _userBorrowed[asset] = amount;
123
+ }
124
+
125
+ function calTokenReserve(address token) public view returns(uint)
126
+ {
127
+ uint amount;
128
+ for(uint i=0;i < _whiteListLpTokenList.length;i ++)
129
+ {
130
+ amount += _reserve[_whiteListLpTokenList[i]][token];
131
+ }
132
+ return amount;
133
+ }
134
+
135
+ function calUserTokenReserve(address user,address token) public view returns(uint)
136
+ {
137
+ uint amount;
138
+ for(uint i=0;i < _whiteListLpTokenList.length;i ++)
139
+ {
140
+ amount += _userReserve[user][_whiteListLpTokenList[i]][token];
141
+ }
142
+ return amount;
143
+ }
144
+
145
+
146
+
147
+ function calAllTokenValue() public view returns(uint)
148
+ {
149
+ uint amount;
150
+ for(uint i=0;i < _whiteListTokenList.length;i ++)
151
+ {
152
+ amount += calTokenReserve(_whiteListTokenList[i]);
153
+
154
+ }
155
+
156
+ return amount;
157
+ }
158
+
159
+ function calUserAllTokenValue(address user) public view returns(uint)
160
+ {
161
+ uint amount;
162
+ for(uint i=0;i < _whiteListTokenList.length;i ++)
163
+ {
164
+ amount += calUserTokenReserve(user,_whiteListTokenList[i] );
165
+ }
166
+
167
+ return amount;
168
+ }
169
+
170
+
171
+ function setWhiteListLpTokenList(address lpAddr) public
172
+ {
173
+ _whiteListLpTokenList.push(lpAddr);
174
+ }
175
+ function getWhiteListLpTokenList() public view returns(address[] memory)
176
+ {
177
+ return _whiteListLpTokenList;
178
+ }
179
+
180
+ function getWhiteListTokenList() public view returns(address[] memory)
181
+ {
182
+ return _whiteListTokenList;
183
+ }
184
+
185
+ function getWhiteListHealthFactor(address token) public view returns(uint)
186
+ {
187
+ uint tokenReserve = _whiteListReserve[token];
188
+ uint tokenBorrowed = _whiteListBorrowed[token];
189
+ uint initialReserve = tokenReserve + tokenBorrowed;
190
+ return 10e18 * tokenBorrowed / initialReserve;
191
+ }
192
+
193
+ function getWhiteListBorrowed(address token)public view returns(uint)
194
+ {
195
+ return _whiteListBorrowed[token];
196
+ }
197
+
198
+ function whiteListBorrowed(address token, uint amount)public onlyRole(DATA_CONTROL_ROLE)
199
+ {
200
+ _whiteListBorrowed[token] = amount;
201
+ }
202
+
203
+ function getWhiteListReserve(address token)public view returns(uint)
204
+ {
205
+ return _whiteListReserve[token];
206
+ }
207
+
208
+ function whiteListReserve(address token, uint amount)public onlyRole(DATA_CONTROL_ROLE)
209
+ {
210
+ _whiteListReserve[token] = amount;
211
+ }
212
+
213
+ function getUserWhiteListReserve(address user, address asset) public view returns(uint)
214
+ {
215
+ return _userWhiteListReserve[user][asset];
216
+ }
217
+
218
+ function userWhiteListReserve(address user, address asset, uint amount) public onlyRole(DATA_CONTROL_ROLE)
219
+ {
220
+ _userWhiteListReserve[user][asset] = amount;
221
+ }
222
+
223
+ function getUserWhiteListBorrowed(address user, address asset) public view returns(uint)
224
+ {
225
+ return _userWhiteListBorrowed[user][asset];
226
+ }
227
+
228
+ function userWhiteListBorrowed(address user, address asset, uint amount) public onlyRole(DATA_CONTROL_ROLE)
229
+ {
230
+ _userWhiteListBorrowed[user][asset] = amount;
231
+ }
232
+
233
+ function whiteListToken(address token) public view returns(bool)
234
+ {
235
+ return _whiteListToken[token];
236
+ }
237
+
238
+ function setWhiteListToken(address token, bool isWhiteListToken) public onlyRole(DATA_CONTROL_ROLE)
239
+ {
240
+ _whiteListToken[token] = isWhiteListToken;
241
+ }
242
+
243
+ function pushTokenInWhiteList(address token) public onlyRole(DATA_CONTROL_ROLE){
244
+ _whiteListTokenList.push(token);
245
+ }
246
+ function getCtoken(address token) public view returns(address)
247
+ {
248
+ return _ctoken[token];
249
+ }
250
+
251
+ function ctoken (address token ,address c_token) public onlyRole(COMP_CONTROL_ROLE)
252
+ {
253
+ _ctoken[token] = c_token;
254
+ }
255
+
256
+ function getUserReserve(address user,address lpAddr,address token) public view returns(uint)
257
+ {
258
+ return _userReserve[user][lpAddr][token];
259
+ }
260
+
261
+ function userReserve(address user,address lpAddr,address token,uint amount)public onlyRole(DATA_CONTROL_ROLE)
262
+ {
263
+ _userReserve[user][lpAddr][token] = amount;
264
+ }
265
+
266
+ function getVirtualReserve(address lpAddr,address token) public view returns(uint)
267
+ {
268
+ return _virtualReserve[lpAddr][token];
269
+ }
270
+ function virtualReserve(address lpAddr,address token,uint amount) public onlyRole(DATA_CONTROL_ROLE)
271
+ {
272
+ _virtualReserve[lpAddr][token] = amount;
273
+ }
274
+
275
+ function getReserve(address lpAddr,address token) public view returns(uint)
276
+ {
277
+ return _reserve[lpAddr][token];
278
+ }
279
+ function reserve(address lpAddr,address token,uint amount)public onlyRole(DATA_CONTROL_ROLE){
280
+ _reserve[lpAddr][token] = amount;
281
+ }
282
+
283
+
284
+ function getPairCreator (address lpAddr) public view returns(address)
285
+ {
286
+ return _pairCreator[lpAddr];
287
+ }
288
+
289
+ function pairCreator(address lpAddr, address user)public onlyRole(DATA_CONTROL_ROLE)
290
+ {
291
+ _pairCreator[lpAddr] = user;
292
+ }
293
+
294
+ function getBorrowedAmount(address userAddr, address lpAddr, uint assetType) public view returns(uint)
295
+ {
296
+ return _borrowedAmount[userAddr][lpAddr][assetType];
297
+ }
298
+
299
+ function borrowedAmount(address userAddr, address lpAddr, uint assetType, uint amount) public onlyRole(DATA_CONTROL_ROLE)
300
+ {
301
+ _borrowedAmount[userAddr][lpAddr][assetType] = amount;
302
+ }
303
+
304
+ function getPayDebtAmount(address userAddr, address lpAddr, uint assetType) public view returns(uint)
305
+ {
306
+ return _payDebtAmount[userAddr][lpAddr][assetType];
307
+ }
308
+
309
+ function payDebtAmount(address userAddr, address lpAddr, uint assetType, uint amount) public onlyRole(DATA_CONTROL_ROLE)
310
+ {
311
+ _payDebtAmount[userAddr][lpAddr][assetType] = amount;
312
+ }
313
+
314
+ function getLpToken(address tokena, address tokenb) public view returns(address)
315
+ {
316
+ return _lpToken[tokena][tokenb];
317
+ }
318
+ function lpToken(address tokena, address tokenb,address lptokenAddr) public onlyRole(DATA_CONTROL_ROLE)
319
+ {
320
+ _lpToken[tokena][tokenb] = lptokenAddr;
321
+ }
322
+
323
+ function getLpFee() public view returns(uint)
324
+ {
325
+ return _lpFee;
326
+ }
327
+
328
+ function getFundFee() public view returns(uint)
329
+ {
330
+ return _fundFee;
331
+ }
332
+
333
+ function getLpProfit(address lpAddr) public view returns(uint)
334
+ {
335
+ return _lpProfit[lpAddr];
336
+ }
337
+
338
+ function WETHAddr() public view returns(address)
339
+ {
340
+ return _WETHAddr;
341
+ }
342
+
343
+
344
+ //管理人员权限
345
+
346
+ function lpProfit(address lpAddr, uint profit)public onlyRole(FEE_CONTROL_ROLE)
347
+ {
348
+ _lpProfit[lpAddr] = profit;
349
+ }
350
+
351
+ function setLpFee(uint fee) external onlyRole(FEE_CONTROL_ROLE){
352
+ _lpFee = fee;// dx / 100000
353
+ }
354
+
355
+
356
+
357
+ function setFundFee(uint fee)external onlyRole(FEE_CONTROL_ROLE){
358
+ _fundFee = fee;
359
+ }
360
+
361
+ function setFundAddr(address _fundAddr) external onlyRole(FEE_CONTROL_ROLE){
362
+ fundAddr = _fundAddr;
363
+ }
364
+
365
+
366
+
367
+ function setWeth(address _wethAddr) public onlyRole(PAIR_CONTROL_ROLE){
368
+ WETH = IWETH(_wethAddr);
369
+ _WETHAddr = _wethAddr;
370
+ }
371
+ function setLpSwapStatic(address _lpAddr, bool _static) external onlyRole(PAIR_CONTROL_ROLE){
372
+ _lpSwapStatic[_lpAddr] = _static;
373
+ }
374
+
375
+ function updateReserve(address lptokenAddr,address tokena, address tokenb, uint reserve0, uint reserve1) public onlyRole(DATA_CONTROL_ROLE) {
376
+ _reserve[lptokenAddr][tokena] = reserve0;
377
+ _reserve[lptokenAddr][tokenb] = reserve1;
378
+ }
379
+
380
+
381
+
382
+
383
+ }
@@ -0,0 +1,33 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^0.8.9;
3
+
4
+ import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
5
+ import "@openzeppelin/contracts/access/Ownable.sol";
6
+
7
+ contract lptoken is ERC20, Ownable {
8
+
9
+ address token0;
10
+ address token1;
11
+
12
+
13
+
14
+ constructor(address _token0,address _token1 ) ERC20("lptoken", "LPT") {
15
+ token0 = _token0;
16
+ token1 = _token1;
17
+ }
18
+
19
+ function mint(address account,uint256 amount) public onlyOwner{
20
+ _mint(account, amount);
21
+ }
22
+
23
+ function burn(address account, uint256 amount) public onlyOwner
24
+ {
25
+ _burn(account,amount);
26
+ }
27
+ function getTokenAddr() public view returns(address,address){
28
+ return (token0,token1);
29
+ }
30
+
31
+
32
+
33
+ }