-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLiquidityExtension.sol
165 lines (137 loc) · 5.1 KB
/
LiquidityExtension.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
pragma solidity >=0.6.6;
import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/upgrades/contracts/Initializable.sol";
import '@uniswap/lib/contracts/libraries/Babylonian.sol';
import "./Permissions.sol";
import "./interfaces/IAuction.sol";
import "./interfaces/IDexHandler.sol";
import "./interfaces/IMaltDataLab.sol";
import "./libraries/UniswapV2Library.sol";
import "./interfaces/IBurnMintableERC20.sol";
/// @title Liquidity Extension
/// @author 0xScotch <scotch@malt.money>
/// @notice In charge of facilitating a premium with net supply contraction during auctions
contract LiquidityExtension is Initializable, Permissions {
ERC20 public collateralToken;
IBurnMintableERC20 public malt;
IAuction public auction;
IDexHandler public dexHandler;
IMaltDataLab public maltDataLab;
address public uniswapV2Factory;
uint256 public minReserveRatio = 40;
event SetAuction(address auction);
event SetDexHandler(address dexHandler);
event SetMaltDataLab(address dataLab);
event SetMinReserveRatio(uint256 ratio);
event BurnMalt(uint256 purchased);
function initialize(
address _timelock,
address initialAdmin,
address _auction,
address _collateralToken,
address _malt,
address _dexHandler,
address _maltDataLab,
address _uniswapV2Factory
) external initializer {
_adminSetup(_timelock);
_setupRole(ADMIN_ROLE, initialAdmin);
_setupRole(AUCTION_ROLE, _auction);
collateralToken = ERC20(_collateralToken);
malt = IBurnMintableERC20(_malt);
auction = IAuction(_auction);
dexHandler = IDexHandler(_dexHandler);
maltDataLab = IMaltDataLab(_maltDataLab);
uniswapV2Factory = _uniswapV2Factory;
}
/*
* PUBLIC VIEW METHODS
*/
function hasMinimumReserves() public view returns (bool) {
(uint256 rRatio, uint256 decimals) = reserveRatio();
return rRatio >= minReserveRatio.mul(10**decimals).div(100);
}
function collateralDeficit() public view returns (uint256 deficit, uint256 decimals) {
// Returns the amount of collateral token required to reach minimum reserves
// Returns 0 if liquidity extension contains minimum reserves.
uint256 balance = collateralToken.balanceOf(address(this));
uint256 collateralDecimals = collateralToken.decimals();
// TODO use data lab Mon 11 Oct 2021 16:48:08 BST
(uint256 maltSupply, uint256 collateralSupply) = UniswapV2Library.getReserves(
uniswapV2Factory,
address(malt),
address(collateralToken)
);
uint256 k = maltSupply.mul(collateralSupply);
uint256 priceTarget = maltDataLab.priceTarget();
uint256 fullCollateral = Babylonian.sqrt(k.mul(10**collateralDecimals).div(priceTarget));
uint256 minReserves = fullCollateral.mul(minReserveRatio).div(100);
if (minReserves > balance) {
return (minReserves - balance, collateralDecimals);
}
return (0, collateralDecimals);
}
function reserveRatio() public view returns (uint256, uint256) {
uint256 balance = collateralToken.balanceOf(address(this));
uint256 collateralDecimals = collateralToken.decimals();
// TODO use data lab Mon 11 Oct 2021 16:48:08 BST
(uint256 maltSupply, uint256 collateralSupply) = UniswapV2Library.getReserves(
uniswapV2Factory,
address(malt),
address(collateralToken)
);
uint256 k = maltSupply.mul(collateralSupply);
uint256 priceTarget = maltDataLab.priceTarget();
uint256 fullCollateral = Babylonian.sqrt(k.mul(10**collateralDecimals).div(priceTarget));
uint256 rRatio = balance.mul(10**collateralDecimals).div(fullCollateral);
return (rRatio, collateralDecimals);
}
/*
* PRIVILEDGED METHODS
*/
function purchaseAndBurn(uint256 amount)
external
onlyRole(AUCTION_ROLE, "Must have auction privs")
returns (uint256 purchased)
{
require(collateralToken.balanceOf(address(this)) >= amount, "Insufficient balance");
collateralToken.safeTransfer(address(dexHandler), amount);
purchased = dexHandler.buyMalt();
malt.burn(address(this), purchased);
emit BurnMalt(purchased);
}
function setAuction(address _auction)
external
onlyRole(ADMIN_ROLE, "Must have admin role")
{
require(_auction != address(0), "Not address 0");
auction = IAuction(_auction);
emit SetAuction(_auction);
}
function setDexHandler(address _dexHandler)
external
onlyRole(ADMIN_ROLE, "Must have admin role")
{
require(_dexHandler != address(0), "Not address 0");
dexHandler = IDexHandler(_dexHandler);
emit SetDexHandler(_dexHandler);
}
function setMaltDataLab(address _dataLab)
external
onlyRole(ADMIN_ROLE, "Must have admin role")
{
require(_dataLab != address(0), "Not address 0");
maltDataLab = IMaltDataLab(_dataLab);
emit SetMaltDataLab(_dataLab);
}
function setMinReserveRatio(uint256 _ratio)
external
onlyRole(ADMIN_ROLE, "Must have admin role")
{
require(_ratio > 0 && _ratio <= 100, "Must be between 0 and 100");
minReserveRatio = _ratio;
emit SetMinReserveRatio(_ratio);
}
}