btc20271457 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 +26 -0
  23. package/tea.yaml +96 -0
  24. package/tranpack.sh +23 -0
@@ -0,0 +1,258 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^0.8.0;
3
+
4
+
5
+ /*
6
+ 资产存入
7
+ 资产借出
8
+ 利息计算
9
+ 资产归还
10
+ 资产清算
11
+ */
12
+
13
+ import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
14
+
15
+ contract liquidity_protocol
16
+ {
17
+ /*
18
+ 计算的过程
19
+ 用户存入资产记住存入标签
20
+ 用户取出资产记录取出时间(主要记录用户在不同时间的资产存量)
21
+ 计算时用每段用户的 ∑ i>>n debt(i) * profitrate(i) * timespan(i)
22
+ */
23
+ struct ProfitStruct{
24
+ uint starTime;
25
+ uint endTime;
26
+ uint ratePerSecond;
27
+ }
28
+
29
+ ProfitStruct [] profitStructArray;
30
+
31
+ uint public index;
32
+ mapping (uint => ProfitStruct) public profitStructSort;
33
+ mapping (address =>mapping (uint => uint)) public indexBorrowed;
34
+
35
+ IERC20 asset;
36
+
37
+ uint public AllBalance;
38
+ uint public AllBorrowed;
39
+
40
+ mapping (address => uint) balance;
41
+ mapping (address => uint) borrowed;
42
+
43
+
44
+
45
+
46
+
47
+ constructor(address _asset){
48
+ asset = IERC20(_asset);
49
+ index = 0;
50
+ }
51
+
52
+ function deposit(uint _amount) public
53
+ {
54
+ index ++;
55
+ asset.transferFrom(msg.sender, address(this), _amount);
56
+
57
+ profitStructSort[index - 1].endTime = block.timestamp;
58
+ profitStructSort[index].starTime = block.timestamp;
59
+
60
+ profitStructSort[index -1].ratePerSecond = calProfitRatePerSecond();
61
+
62
+ balance[msg.sender] += _amount;
63
+ AllBalance += _amount;
64
+
65
+
66
+ }
67
+
68
+ function borrow(uint _amount) public
69
+ {
70
+ index ++;
71
+ require(borrowed[msg.sender] + _amount <= balance[msg.sender],"fuck");
72
+ asset.transfer(msg.sender, _amount);
73
+
74
+ profitStructSort[index - 1].endTime = block.timestamp;
75
+ profitStructSort[index].starTime = block.timestamp;
76
+
77
+ profitStructSort[index -1].ratePerSecond = calProfitRatePerSecond();
78
+
79
+ borrowed[msg.sender] += _amount;
80
+ AllBorrowed += _amount;
81
+ indexBorrowed[msg.sender][index] = borrowed[msg.sender];
82
+ }
83
+ /*
84
+ 1.计算用户在各个时间点的存入资产
85
+ 2.每个时间段的利息
86
+ 3.计算 利息*时间 得到总利息
87
+ */
88
+ function calAllProfit() public view returns(uint)
89
+ {
90
+ uint profit;
91
+ for(uint i = 1; i <= index; i ++)
92
+ {
93
+ uint spanProfit;
94
+ if(i == index)
95
+ {
96
+ spanProfit = indexBorrowed[msg.sender][i] * calProfitRatePerSecond() * (block.timestamp - profitStructSort[i].starTime);
97
+ }else{
98
+ spanProfit = indexBorrowed[msg.sender][i] * profitStructSort[i].ratePerSecond * (profitStructSort[i].endTime - profitStructSort[i].starTime);
99
+ }
100
+ profit += spanProfit;
101
+ }
102
+ return profit;
103
+
104
+
105
+
106
+
107
+ }
108
+ /*
109
+ 用户可以直接偿还本金从而减少贷款利息
110
+ 偿还的过程是先pay本金再还利息
111
+ (考虑利息会加入本金系列)
112
+ Q:how to cal the profit with
113
+ every weeks the cal function add the frofit to all balance
114
+
115
+ 本金利息分离的优势
116
+
117
+ 可以采用分离计利息
118
+ 本金100%
119
+ 利息50%
120
+
121
+ 需要做的是
122
+ 1.计算本金的利息
123
+ 2.计算利息的利息
124
+ 3.偿还机制的问题
125
+ 4.偿还先偿还本金
126
+ 5.再偿还利息(最简单就是用户的加一个变量browed + profit,计算的时候直接borrowed+profit)
127
+ 5.1.利息的偿还时间分段问题,需要用到数据来计算用户每个时间段的利息
128
+ */
129
+
130
+ function calBorrowedAndProfit() public view returns(uint)
131
+ {
132
+ borrowed[user] + calProfitReal();
133
+
134
+ }
135
+
136
+ function calAllProfi() public view returns(uint) {
137
+ uint profit;
138
+ for(uint i = 1; i <= index; i ++)
139
+ {
140
+ uint spanProfit;
141
+ if(i == index)
142
+ {
143
+ spanProfit = indexBorrowed[msg.sender][i] * calProfitRatePerSecond() * (block.timestamp - profitStructSort[i].starTime);
144
+ }else{
145
+ spanProfit = indexBorrowed[msg.sender][i] * profitStructSort[i].ratePerSecond * (profitStructSort[i].endTime - profitStructSort[i].starTime);
146
+ }
147
+ profit += spanProfit;
148
+ }
149
+ return profit;
150
+
151
+ }
152
+
153
+
154
+
155
+
156
+
157
+ function calProfitReal()public view returns(uint)
158
+ {
159
+ return calProfit()/10e18;
160
+ }
161
+ /*
162
+ rate = allBorrowed/AllBalance (0 < rate <1);
163
+ 如果rate是apr的话
164
+ 可以添加一个新函数来计算每秒利息
165
+ */
166
+ function calProfitRate() public view returns(uint)
167
+ {
168
+ if(AllBalance == 0){
169
+ return 0;
170
+ }else{
171
+ uint rate = 10e18 * AllBorrowed / AllBalance;
172
+
173
+ return rate;
174
+ }
175
+ }
176
+
177
+ function calProfitRatePerSecond() public view returns (uint)
178
+ {
179
+ return calProfitRate() / 86400;
180
+ }
181
+
182
+ function assetLiquidation()public
183
+ {
184
+
185
+ }
186
+
187
+ //计算收益
188
+ /*
189
+ 方法一:
190
+ staking reward 方式来灵活分配
191
+ 1.抵押tokenA 得到 ptokenA
192
+ 2.质押ptokenA到pool来进行staking reward 进行token到分配
193
+
194
+ 方法二:
195
+ All pool profit share
196
+
197
+ */
198
+
199
+ function calUserBonus() {
200
+
201
+ }
202
+
203
+
204
+ //清算资产
205
+
206
+ function liquidationConditionFactor() public view returns(uint)
207
+ {
208
+ uint factor = 10e18 * (calProfitReal() + borrowed[msg.sender] ) / balance[msg.sender];
209
+ return factor;
210
+ }
211
+
212
+ function liquidationCondition() public view returns(bool)
213
+ {
214
+ if(liquidationConditionFactor() > (10e17 * 8))
215
+ {
216
+ return true;
217
+ }
218
+ else{
219
+ return false;
220
+ }
221
+ }
222
+ //用户偿还资产
223
+
224
+ function payDebt(uint _amount)public
225
+
226
+ {
227
+ index ++;
228
+ require(borrowed[msg.sender] >= _amount,"what are you doing?");
229
+ asset.transferFrom(msg.sender,address(this),_amount);
230
+
231
+ profitStructSort[index - 1].endTime = block.timestamp;
232
+ profitStructSort[index].starTime = block.timestamp;
233
+
234
+ profitStructSort[index -1].ratePerSecond = calProfitRatePerSecond();
235
+
236
+ borrowed[msg.sender] -= _amount;
237
+ AllBorrowed -= _amount;
238
+
239
+ //可以把利息放在index里面吗
240
+ indexBorrowed[msg.sender][index] = borrowed[msg.sender];
241
+ /*
242
+ 可以开启双轨模式
243
+ */
244
+ //indexProfit[msg.sender][index] = calProfitReal();
245
+
246
+
247
+
248
+ }
249
+
250
+ //用户撤回资产
251
+
252
+
253
+
254
+
255
+
256
+
257
+
258
+ }
@@ -0,0 +1,29 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^0.8.20;
3
+ /*
4
+ 存入
5
+ 取出
6
+ */
7
+ //存入erc20
8
+ import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
9
+
10
+ contract bank0{
11
+
12
+ mapping (address => uint) _balance;
13
+
14
+ receive() external payable { }
15
+
16
+ function depositEth() public payable {
17
+ _balance[msg.sender] += msg.value;
18
+
19
+ }
20
+
21
+ function withdrawEth(uint _amount) public payable {
22
+ address payable user = payable (msg.sender);
23
+ user.transfer(_amount);
24
+ _balance[msg.sender] -= msg.value;
25
+ }
26
+
27
+
28
+
29
+ }
@@ -0,0 +1,58 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^0.8.20;
3
+ /*
4
+ 存入
5
+ 取出
6
+ */
7
+ //存入erc20
8
+ import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
9
+
10
+ contract bank1{
11
+
12
+ mapping (address => uint) _balance;//user => amount
13
+ mapping (address => mapping (address => uint)) _erc20Balance;//user => erc20 => amount
14
+
15
+ receive() external payable { }
16
+
17
+ function depositEth() public payable {
18
+ _balance[msg.sender] += msg.value;
19
+
20
+ }
21
+
22
+ function withdrawEth(uint _amount) public payable {
23
+ address payable user = payable (msg.sender);
24
+ user.transfer(_amount);
25
+ _balance[msg.sender] -= msg.value;
26
+ }
27
+
28
+ function depositErc20(address _erc20,uint _amount) public {
29
+ /*
30
+
31
+ */
32
+ address user = msg.sender;
33
+
34
+ IERC20 erc20 = IERC20(_erc20);
35
+ erc20.transferFrom(user, address(this), _amount);
36
+
37
+ _erc20Balance[user][_erc20] += _amount;
38
+
39
+ }
40
+
41
+ function withdrawErc20(address _erc20,uint _amount) public {
42
+ /*
43
+ 1.检查数量
44
+ 2.合约发送erc20
45
+ 3.改写钱包数量
46
+ */
47
+ address user = msg.sender;
48
+ require(_erc20Balance[user][_erc20] >= _amount,"???");
49
+
50
+ IERC20 erc20 = IERC20(_erc20);
51
+ erc20.transfer(user, _amount);
52
+
53
+ _erc20Balance[user][_erc20] -= _amount;
54
+ }
55
+
56
+
57
+
58
+ }
@@ -0,0 +1,124 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^0.8.20;
3
+ /*
4
+ 存入
5
+ 取出
6
+ 计算利息
7
+ */
8
+ //存入erc20
9
+ import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
10
+
11
+ contract bank2{
12
+ struct publicVariable{
13
+ uint starTime;
14
+ uint endTime;
15
+ uint reserveRate;
16
+ uint debtRate;
17
+ }
18
+
19
+
20
+ uint index;
21
+ mapping (uint => publicVariable) publicVariableIndex;
22
+ //mapping (uint => userVariable) userVariableIndex;
23
+
24
+ mapping (address => uint) AllReserve;//erc20 => amount
25
+ mapping (address => uint) AllBorrowed;
26
+
27
+ mapping (uint =>mapping (address => mapping (address => uint))) userReserveIndex;//index => user => assetType => amount
28
+ mapping (uint =>mapping (address => mapping (address => uint))) userDebtIndex;
29
+
30
+ uint AllETHReserve;
31
+ uint ALlETHBorrowed;
32
+
33
+ mapping (address => uint) _balance;//user => amount
34
+ mapping (address => mapping (address => uint)) _erc20Balance;//user => erc20 => amount
35
+
36
+ receive() external payable { }
37
+
38
+ function depositEth() public payable {
39
+ _balance[msg.sender] += msg.value;
40
+
41
+ }
42
+
43
+ function withdrawEth(uint _amount) public payable {
44
+ address payable user = payable (msg.sender);
45
+ user.transfer(_amount);
46
+ _balance[msg.sender] -= msg.value;
47
+ }
48
+
49
+ function depositErc20(address _erc20,uint _amount) public {
50
+ /*
51
+
52
+ */
53
+ address user = msg.sender;
54
+
55
+ IERC20 erc20 = IERC20(_erc20);
56
+ erc20.transferFrom(user, address(this), _amount);
57
+
58
+ _erc20Balance[user][_erc20] += _amount;
59
+
60
+ }
61
+
62
+ function withdrawErc20(address _erc20,uint _amount) public {
63
+ /*
64
+ 1.检查数量
65
+ 2.合约发送erc20
66
+ 3.改写钱包数量
67
+ */
68
+ address user = msg.sender;
69
+ require(_erc20Balance[user][_erc20] >= _amount,"???");
70
+
71
+ IERC20 erc20 = IERC20(_erc20);
72
+ erc20.transfer(user, _amount);
73
+
74
+ _erc20Balance[user][_erc20] -= _amount;
75
+ }
76
+ function calProfitForETH() public view returns(uint)
77
+ {
78
+
79
+
80
+ }
81
+
82
+ function calProfitForErc20(address erc20) public view returns(uint)
83
+ {
84
+ address user = msg.sender;
85
+ uint profit;
86
+ for (uint i;i <= index;i++){
87
+ if(i==index){
88
+ uint endTime = block.timestamp;
89
+ uint deltaT = endTime - publicVariableIndex[index].starTime;
90
+ profit += deltaT * publicVariableIndex[index].reserveRate * userReserveIndex[index][user][erc20];
91
+
92
+ }
93
+ else{
94
+ profit += calProfitForErc20S(i,erc20);
95
+ }
96
+ }
97
+ return profit;
98
+
99
+
100
+ }
101
+
102
+ function calProfitForErc20S(uint _index,address erc20) public view returns(uint)
103
+ {
104
+ address user = msg.sender;
105
+ publicVariableIndex[index];
106
+ //userVariableIndex[index];
107
+ uint deltaT = (publicVariableIndex[_index].endTime - publicVariableIndex[_index].starTime);
108
+ uint profitS = deltaT * publicVariableIndex[_index].reserveRate * userReserveIndex[_index][user][erc20];
109
+ return profitS;
110
+
111
+ }
112
+ function calReserveRate(address erc20) public view returns(uint)
113
+ {
114
+ return 8*10e17*AllBorrowed[erc20]/AllReserve[erc20];
115
+ //计算时再除于10e18
116
+ }
117
+ function calDebtRate(address erc20) public view returns(uint)
118
+ {
119
+ return 10e18*AllBorrowed[erc20]/AllReserve[erc20];
120
+ }
121
+
122
+
123
+
124
+ }
@@ -0,0 +1,210 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^0.8.20;
3
+
4
+ /*
5
+
6
+ 目前问题:
7
+ 用户资产存在不同的index
8
+ 合理的方式应该是用到跟index不同的index1来记录,
9
+ 并且把index和index1关联起来知道第几个index1用户资产发生变动
10
+ */
11
+ /*
12
+ 存入
13
+ 取出
14
+ 计算利息
15
+ * 资产借出
16
+ */
17
+ //存入erc20
18
+
19
+ /*
20
+ 关于私有变量index的统一问题
21
+ 插入数据来记录第几个index改变资产就ok
22
+ erc20index
23
+ userReserve
24
+ 有一个index来记录第几个index存入或取出多少资产
25
+
26
+ */
27
+ import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
28
+
29
+ contract bank3{
30
+ struct publicVariable{
31
+ uint starTime;
32
+ uint endTime;
33
+ uint reserveRate;
34
+ uint debtRate;
35
+ }
36
+
37
+
38
+ //uint _index;
39
+ mapping (address => uint) erc20Index;
40
+ mapping (uint => publicVariable) publicVariableIndex;
41
+ mapping (uint => mapping (address => publicVariable)) public erc20PublicVariableIndex;//index => assetType => publiVariable
42
+ //mapping (uint => userVariable) userVariableIndex;
43
+
44
+ mapping (address => uint) AllReserve;//erc20 => amount
45
+ mapping (address => uint) AllBorrowed;
46
+
47
+ mapping (uint =>mapping (address => mapping (address => uint))) userReserveIndex;//index => user => assetType => amount
48
+
49
+ //大于index找最后一个插入index的数额
50
+ mapping (uint =>mapping (address => mapping (address => uint))) userDebtIndex;
51
+ mapping (address => mapping (address => uint)) userReserve;
52
+ mapping (address => mapping (address => uint)) userDebt;
53
+
54
+ uint AllETHReserve;
55
+ uint ALlETHBorrowed;
56
+
57
+ mapping (address => uint) _balance;//user => amount
58
+ mapping (address => mapping (address => uint)) _erc20Balance;//user => erc20 => amount
59
+
60
+ receive() external payable { }
61
+
62
+
63
+
64
+ function depositEth() public payable {
65
+ _balance[msg.sender] += msg.value;
66
+
67
+ }
68
+
69
+ function withdrawEth(uint _amount) public payable {
70
+ address payable user = payable (msg.sender);
71
+ user.transfer(_amount);
72
+ _balance[msg.sender] -= msg.value;
73
+ }
74
+
75
+ function depositErc20(address _erc20,uint _amount) public {
76
+ /*
77
+
78
+ */
79
+
80
+ erc20Index[_erc20]++;
81
+ uint index = erc20Index[_erc20];
82
+
83
+ if(index == 1){
84
+ erc20PublicVariableIndex[index-1][_erc20].starTime = block.timestamp;
85
+ }
86
+
87
+ address user = msg.sender;
88
+
89
+ IERC20 erc20 = IERC20(_erc20);
90
+ erc20.transferFrom(user, address(this), _amount);
91
+
92
+ //_erc20Balance[user][_erc20] += _amount;
93
+
94
+ //userReserveIndex[index][user][_erc20] += _amount;
95
+
96
+ erc20PublicVariableIndex[index-1][_erc20].endTime = block.timestamp;
97
+ erc20PublicVariableIndex[index-1][_erc20].reserveRate = calReserveRate(_erc20);
98
+ erc20PublicVariableIndex[index-1][_erc20].debtRate = calDebtRate(_erc20);
99
+ erc20PublicVariableIndex[index][_erc20].starTime = block.timestamp;
100
+
101
+ //资产数据更新
102
+ AllReserve[_erc20] += _amount;
103
+ userReserveIndex[index][user][_erc20] += _amount;
104
+
105
+
106
+
107
+ }
108
+
109
+ function withdrawErc20(address _erc20,uint _amount) public {
110
+ /*
111
+ 1.检查数量
112
+ 2.合约发送erc20
113
+ 3.改写钱包数量
114
+ */
115
+ address user = msg.sender;
116
+ require(_erc20Balance[user][_erc20] >= _amount,"???");
117
+
118
+ IERC20 erc20 = IERC20(_erc20);
119
+ erc20.transfer(user, _amount);
120
+
121
+ _erc20Balance[user][_erc20] -= _amount;
122
+ }
123
+
124
+ function borrowErc20Asset(address _erc20, uint _amount) public
125
+ {
126
+ erc20Index[_erc20] ++;
127
+ uint index = erc20Index[_erc20];
128
+
129
+ address user = msg.sender;
130
+ uint reserve = userReserveIndex[index][user][_erc20];
131
+ uint debt = userDebtIndex[index][user][_erc20];
132
+ require((debt + _amount) <= (reserve/2),"debt must less reserve/2");
133
+
134
+ //userDebtIndex[index][user][_erc20] += _amount;
135
+
136
+ erc20PublicVariableIndex[index-1][_erc20].endTime = block.timestamp;
137
+ erc20PublicVariableIndex[index-1][_erc20].reserveRate = calReserveRate(_erc20);
138
+ erc20PublicVariableIndex[index-1][_erc20].debtRate = calDebtRate(_erc20);
139
+ erc20PublicVariableIndex[index][_erc20].starTime = block.timestamp;
140
+
141
+ //资产数据更新
142
+ AllBorrowed[_erc20] += _amount;
143
+ userDebtIndex[index][user][_erc20] += _amount;
144
+
145
+
146
+
147
+
148
+
149
+
150
+
151
+ }
152
+
153
+
154
+ function calProfitForETH() public view returns(uint)
155
+ {
156
+
157
+
158
+ }
159
+
160
+ function calProfitForErc20(address _erc20) public view returns(uint)
161
+ {
162
+ address user = msg.sender;
163
+ uint profit;
164
+ for (uint i;i <= erc20Index[_erc20];i++){
165
+ if(i==erc20Index[_erc20]){
166
+ uint endTime = block.timestamp;
167
+ uint deltaT = endTime - erc20PublicVariableIndex[i][_erc20].starTime;
168
+ profit += deltaT * erc20PublicVariableIndex[i][_erc20].reserveRate * userReserveIndex[i][user][_erc20];
169
+
170
+ }
171
+ else{
172
+ profit += calProfitForErc20S(i,_erc20);
173
+ }
174
+ }
175
+ return profit;
176
+
177
+
178
+ }
179
+
180
+ function calProfitForErc20S(uint _index,address _erc20) public view returns(uint)
181
+ {
182
+ address user = msg.sender;
183
+ publicVariableIndex[_index];
184
+ //userVariableIndex[index];
185
+ uint deltaT = (erc20PublicVariableIndex[_index][_erc20].endTime - erc20PublicVariableIndex[_index][_erc20].starTime);
186
+ uint profitS = deltaT * erc20PublicVariableIndex[_index][_erc20].reserveRate * userReserveIndex[_index][user][_erc20];
187
+ return profitS;
188
+
189
+ }
190
+ function calReserveRate(address _erc20) public view returns(uint)
191
+ {
192
+ if(AllReserve[_erc20] == 0){
193
+ return 0;
194
+ }else{
195
+ return 8*10e17*AllBorrowed[_erc20]/AllReserve[_erc20];
196
+ //计算时再除于10e18
197
+ }
198
+ }
199
+ function calDebtRate(address _erc20) public view returns(uint)
200
+ {
201
+ if(AllReserve[_erc20] == 0){
202
+ return 0;
203
+ }else{
204
+ return 10e18*AllBorrowed[_erc20]/AllReserve[_erc20];
205
+ }
206
+ }
207
+
208
+
209
+
210
+ }