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,9 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="WEB_MODULE" version="4">
3
+ <component name="Go" enabled="true" />
4
+ <component name="NewModuleRootManager">
5
+ <content url="file://$MODULE_DIR$" />
6
+ <orderEntry type="inheritedJdk" />
7
+ <orderEntry type="sourceFolder" forTests="false" />
8
+ </component>
9
+ </module>
@@ -0,0 +1,15 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="GitToolBoxProjectSettings">
4
+ <option name="commitMessageIssueKeyValidationOverride">
5
+ <BoolValueOverride>
6
+ <option name="enabled" value="true" />
7
+ </BoolValueOverride>
8
+ </option>
9
+ <option name="commitMessageValidationEnabledOverride">
10
+ <BoolValueOverride>
11
+ <option name="enabled" value="true" />
12
+ </BoolValueOverride>
13
+ </option>
14
+ </component>
15
+ </project>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/Web3-Financial-Engineering-Courses-main.iml" filepath="$PROJECT_DIR$/.idea/Web3-Financial-Engineering-Courses-main.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
package/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+ </component>
6
+ </project>
@@ -0,0 +1,29 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^0.8.0;
3
+
4
+
5
+ /*
6
+ 1.添加流动性
7
+ 2.交易
8
+ 3.移除流动性
9
+ */
10
+
11
+ /*
12
+ 配合杠杆的玩法
13
+ 1.lp杠杆:存lp贷出lp(做市杠杆)
14
+ 2。pair币杠杆: 存lp贷出A or B(做市同时可做多做空莫token,即借出买入)
15
+ 3. token杠杆 存A贷B (单token杠杆)
16
+ 3.1 流程
17
+ - 存入tokenA到pair 根据对应的价格贷出70%贷出tokenB(贷出利息分给lp)
18
+ - 单币做市的可能性(a跟b比例相同的时候)
19
+ */
20
+
21
+ import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
22
+
23
+ contract AMM
24
+ {
25
+
26
+ function addLiquidity(type name) {
27
+
28
+ }
29
+ }
@@ -0,0 +1,150 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ //import "./IAMM.sol";
4
+ import "./Idatastore.sol";
5
+ import "./Algorithm.sol";
6
+ import "./lptoken.sol";
7
+
8
+ pragma solidity ^0.8.9;
9
+
10
+
11
+ contract AMMData{
12
+ Idatastore amm;
13
+ uint constant ONE_ETH = 10 ** 18;
14
+ constructor(address _amm){
15
+ amm = Idatastore(_amm);
16
+
17
+ }
18
+
19
+ function resetAmm(address _amm) public {
20
+ amm = Idatastore(_amm);
21
+ }
22
+
23
+
24
+ function getTokenOutAmount(address _tokenIn, address _tokenOut, uint _amountIn) public view returns(uint amountOut){
25
+ require(
26
+ amm.getLpToken(_tokenIn,_tokenOut) != address(0),
27
+ "invalid token"
28
+ );
29
+ require(_amountIn > 0, "amount in = 0");
30
+ require(_tokenIn != _tokenOut);
31
+
32
+ address lptokenAddr = amm.getLpToken(_tokenIn,_tokenOut);
33
+ uint reserveIn = amm.getReserve(lptokenAddr,_tokenIn);
34
+ uint reserveOut = amm.getReserve(lptokenAddr,_tokenOut);
35
+
36
+
37
+
38
+
39
+ //交易税收
40
+ uint amountInWithFee = (_amountIn * (100000-amm.getLpFee()-amm.getFundFee())) / 100000;
41
+
42
+ amountOut = (reserveOut * amountInWithFee) / (reserveIn + amountInWithFee);
43
+
44
+ //检查滑点
45
+ //setSli(amountInWithFee,reserveIn,reserveOut,_disirSli);
46
+
47
+
48
+
49
+
50
+ //uint profit = lpFee * _amountIn / 100000;
51
+
52
+ //_lpProfit[lptokenAddr] += profit;
53
+
54
+ //_update(lptokenAddr,_tokenIn, _tokenOut, totalReserve0, totalReserve1);
55
+
56
+ }
57
+
58
+
59
+ function getTokenPrice(address _tokenA, address _tokenB) public view returns(uint reserveA,uint reserveB, uint one_tokenA_price,uint one_tokenB_price)
60
+ {
61
+ address lptokenAddr = amm.getLpToken(_tokenA,_tokenB);
62
+ reserveA = amm.getReserve(lptokenAddr, _tokenA);
63
+ reserveB = amm.getReserve(lptokenAddr,_tokenB);
64
+
65
+ one_tokenA_price = reserveB * ONE_ETH / reserveA;
66
+ one_tokenB_price = reserveA * ONE_ETH / reserveB;
67
+
68
+
69
+ }
70
+ /*
71
+ function getTokenPriceStableCoin(address _tokenA, address _tokenB, uint amountIn) public view returns(uint reserveA,uint reserveB, uint tokenA_price,uint tokenB_price)
72
+ {
73
+ address lptokenAddr = amm.getLptoken(_tokenA,_tokenB);
74
+ reserveA = amm.getReserve(lptokenAddr, _tokenA);
75
+ reserveB = amm.getReserve(lptokenAddr,_tokenB);
76
+ tokenA_price = StableAlgorithm.calOutput(amm.getA(lptokenAddr),reserveA + reserveB, reserveA,amountIn);
77
+ tokenB_price = StableAlgorithm.calOutput(amm.getA(lptokenAddr),reserveA + reserveB, reserveB,amountIn);
78
+
79
+
80
+ //tokenOutAmount = StableAlgorithm.calOutput(100,reserveA + reserveB, reserveA,_tokenInAmount);
81
+
82
+
83
+
84
+
85
+ }
86
+
87
+
88
+ function cacalTokenOutAmount(address _tokenIn, address _tokenOut, uint _tokenInAmount) public view returns(uint tokenOutAmount)
89
+ {
90
+ address lptokenAddr = amm.getLptoken(_tokenIn,_tokenOut);
91
+ uint reserveIn = amm.getReserve(lptokenAddr, _tokenIn);
92
+ uint reserveOut = amm.getReserve(lptokenAddr,_tokenOut);
93
+ if(amm.isStablePair(lptokenAddr)){
94
+
95
+ tokenOutAmount = StableAlgorithm.calOutput(amm.getA(lptokenAddr),reserveIn + reserveOut, reserveIn,_tokenInAmount);
96
+ }else{
97
+ tokenOutAmount = (reserveOut * _tokenInAmount) / (reserveIn + _tokenInAmount);
98
+ }
99
+ }
100
+ */
101
+ function cacalLpTokenAddAmount(address _tokenA, address _tokenB, uint _amountA) public view returns(uint _amountB)
102
+ {
103
+ address lptokenAddr = amm.getLpToken(_tokenA,_tokenB);
104
+ _amountB = amm.getReserve(lptokenAddr,_tokenB) * _amountA / amm.getReserve(lptokenAddr, _tokenA);
105
+ }
106
+
107
+
108
+
109
+ function getRemoveLiquidityAmount(
110
+ address _token0,
111
+ address _token1,
112
+ uint _shares
113
+ ) public view returns (uint amount0, uint amount1) {
114
+ //ILPToken lptoken;//lptoken接口,为了mint 和 burn lptoken
115
+ address lptokenAddr = amm.getLpToken(_token0,_token1);
116
+
117
+ //lptoken = ILPToken(lptokenAddr);
118
+
119
+
120
+ amount0 = (_shares * amm.getReserve(lptokenAddr,_token0)) / lptoken(lptokenAddr).totalSupply();//share * totalsuply/bal0
121
+ amount1 = (_shares * amm.getReserve(lptokenAddr,_token1)) / lptoken(lptokenAddr).totalSupply();
122
+ }
123
+
124
+
125
+ function getUserLeftingBorrowAmount(address user, address asset) public view returns(uint)
126
+ {
127
+ uint leftingBorrowAmount = amm.calUserTokenReserve(user,asset) / 2 - amm.getUserWhiteListBorrowed(user,asset);
128
+ return leftingBorrowAmount;
129
+ }
130
+
131
+ function _getUserWhiteListBorrowedAmount(address user,address asset) public view returns(uint)
132
+ {
133
+ return amm.getUserWhiteListBorrowed(user,asset);
134
+ }
135
+ function _getWhiteListBorrowedAmount(address asset)public view returns(uint)
136
+ {
137
+ return amm.getWhiteListBorrowed(asset);
138
+ }
139
+
140
+ function calAllTokenBorrowed() public view returns(uint)
141
+ {
142
+ return amm.calAllTokenBorrowed();
143
+
144
+ }
145
+
146
+
147
+
148
+
149
+
150
+ }
@@ -0,0 +1,97 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+
4
+
5
+ pragma solidity ^0.8.9;
6
+
7
+ library Algorithm{
8
+ /*
9
+ \frac{-4ADx^2-4x+4AD^2x+\sqrt{\left(4ADx^2+4x-4AD^2x\right)^2+16AD^3x}}{8ADx}
10
+
11
+ \frac{-4ADx^2-4x+4AD^2x+\sqrt{\left(4ADx^2+4x-4AD^2x\right)^2+16AD^3x}}{8ADx}
12
+
13
+ y=(4*A*D*D*X-4*X-4*A*D*X*X + calSqrt(A, D, X))/8*A*D*X
14
+ dy = y - (4*A*D*D*X-4*X-4*A*D*X*X + calSqrt(A, D, X))/8*A*D*X
15
+ */
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+ function calOutAmount(uint A, uint D, uint X)public pure returns(uint)
24
+ {
25
+ //return (4*A*D*D*X+calSqrt(A, D, X) -4*X-4*A*D*X*X) / (8*A*D*X);
26
+ uint a = 4*A*D*X+D*calSqrt(A, D, X)-4*A*X*X-D*X;
27
+ //uint amountOut2 = y - amountOut1;
28
+ return a/(8*A*X);
29
+
30
+ }
31
+
32
+ function calOutput(uint A, uint D, uint X,uint dx)public pure returns(uint)
33
+ {
34
+ //D = D * 10**18;
35
+ //X = X * 10**18;
36
+ //dx = dx* 10**18;
37
+ uint S = X + dx;
38
+ uint amount1 = calOutAmount(A, D, X);
39
+ uint amount2 = calOutAmount(A, D, S);
40
+
41
+ //uint amountOut2 = y - amountOut1;
42
+ return amount1 - amount2;
43
+
44
+ }
45
+
46
+
47
+
48
+
49
+ function calSqrt(uint A, uint D, uint X)public pure returns(uint)
50
+ {
51
+ //uint T = t(A,D,X);
52
+ //uint calSqrtNum = _sqrt((X*(4+T))*(X*(4+T))+T*T*D*D+4*T*D*D-2*X*T*D*(4+T));
53
+ //return calSqrtNum;
54
+ (uint a, uint b) = (4*A*X*X/D+X,4*A*X);
55
+ uint c;
56
+ if(a>=b){
57
+ c = a -b;
58
+ }else{
59
+ c = b-a;
60
+ }
61
+
62
+ return _sqrt(c*c+4*D*X*A);
63
+
64
+ }
65
+
66
+ function _min(uint x, uint y) public pure returns (uint) {
67
+ return x <= y ? x : y;
68
+ }
69
+
70
+
71
+
72
+
73
+ function _sqrt(uint y) public pure returns (uint z) {
74
+ if (y > 3) {
75
+ z = y;
76
+ uint x = y / 2 + 1;
77
+ while (x < z) {
78
+ z = x;
79
+ x = (y / x + x) / 2;
80
+ }
81
+ } else if (y != 0) {
82
+ z = 1;
83
+ }
84
+ }
85
+
86
+ function _sortAddr(address a, address b) public pure returns(address small,address big)
87
+ {
88
+ //a > b ? return(b,a) : return(a,b);
89
+ a > b ? (big=a,small = b) : (big=b,small = a);
90
+ }
91
+
92
+
93
+
94
+
95
+
96
+
97
+ }
@@ -0,0 +1,120 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity ^0.8.20;
4
+
5
+ //to do list pay liquidity
6
+
7
+ interface Idatastore{
8
+
9
+ function calAllTokenBorrowed() external view returns(uint);
10
+
11
+ //function calAllTokenBorrowed() external view returns(uint);
12
+ function getUserBorrowedAmount(address asset) external view returns(uint);
13
+
14
+ function userBorrowed(address asset, uint amount) external ;
15
+
16
+ function calTokenReserve(address token) external view returns(uint);
17
+
18
+ function calUserTokenReserve(address user,address token) external view returns(uint);
19
+
20
+ function calAllTokenValue() external view returns(uint);
21
+
22
+ function calUserAllTokenValue(address user) external view returns(uint);
23
+
24
+
25
+ function setWhiteListLpTokenList(address lpAddr) external ;
26
+ function getWhiteListTokenList() external view returns(address[] memory);
27
+ function getWhiteListLpTokenList() external view returns(address[] memory);
28
+ function getWhiteListHealthFactor(address token) external view returns(uint);
29
+
30
+ function getWhiteListBorrowed(address token)external view returns(uint);
31
+
32
+ function whiteListBorrowed(address token, uint amount)external ;
33
+
34
+ function getWhiteListReserve(address token)external view returns(uint);
35
+
36
+ function whiteListReserve(address token, uint amount)external ;
37
+
38
+ function getUserWhiteListReserve(address user, address asset) external view returns(uint);
39
+
40
+ function userWhiteListReserve(address user, address asset, uint amount) external ;
41
+
42
+ function getUserWhiteListBorrowed(address user, address asset) external view returns(uint);
43
+
44
+ function userWhiteListBorrowed(address user, address asset, uint amount) external ;
45
+
46
+ function whiteListToken(address token) external view returns(bool);
47
+
48
+ function setWhiteListToken(address token, bool isWhiteListToken) external ;
49
+
50
+ function pushTokenInWhiteList(address token) external ;
51
+ function getCtoken(address token) external view returns(address);
52
+
53
+ function ctoken (address token ,address c_token) external;
54
+
55
+ function getFundAddr()external view returns(address);
56
+
57
+
58
+ function getUserReserve(address user,address lpAddr,address token)external view returns(uint);
59
+
60
+ function userReserve(address user,address lpAddr,address token,uint amount) external ;
61
+
62
+ function getVirtualReserve(address lpAddr,address token) external view returns(uint);
63
+ function virtualReserve(address lpAddr,address token,uint amount) external ;
64
+
65
+
66
+ function getReserve(address lpAddr,address token) external view returns(uint);
67
+ function reserve(address lpAddr,address token,uint amount)external ;
68
+
69
+
70
+ function getPairCreator (address lpAddr) external view returns(address);
71
+
72
+ function pairCreator(address lpAddr, address user)external ;
73
+
74
+ function getBorrowedAmount(address userAddr, address lpAddr, uint assetType) external view returns(uint);
75
+
76
+
77
+ function borrowedAmount(address userAddr, address lpAddr, uint assetType, uint amount) external ;
78
+
79
+
80
+ function getPayDebtAmount(address userAddr, address lpAddr, uint assetType) external view returns(uint);
81
+
82
+
83
+ function payDebtAmount(address userAddr, address lpAddr, uint assetType, uint amount) external ;
84
+
85
+
86
+ function getLpToken(address tokena, address tokenb) external view returns(address);
87
+
88
+ function lpToken(address tokena, address tokenb,address lptokenAddr) external ;
89
+
90
+ function getLpFee() external view returns(uint);
91
+
92
+ function getFundFee() external view returns(uint);
93
+
94
+ function getLpProfit(address lpAddr) external view returns(uint);
95
+
96
+ function WETHAddr()external view returns(address);
97
+
98
+
99
+
100
+ //管理人员权限
101
+
102
+ function lpProfit(address lpAddr, uint profit)external ;
103
+
104
+ function setLpFee(uint fee) external ;
105
+
106
+
107
+
108
+ function setFundFee(uint fee)external ;
109
+
110
+ function setFundAddr(address _fundAddr) external ;
111
+
112
+
113
+
114
+ function setWeth(address _wethAddr) external ;
115
+ function setLpSwapStatic(address _lpAddr, bool _static) external ;
116
+
117
+ function updateReserve(address lptokenAddr,address tokena, address tokenb, uint reserve0, uint reserve1) external ;
118
+ //function updateUserReserve()
119
+
120
+ }