added deleverage function in lite

This commit is contained in:
Samyak Jain 2022-04-06 06:27:57 +04:00
parent 373dcf59af
commit b191ac1710
2 changed files with 53 additions and 14 deletions

View File

@ -2,6 +2,7 @@
pragma solidity ^0.7.0; pragma solidity ^0.7.0;
interface IInstaLite { interface IInstaLite {
function supplyEth(address to_) external payable returns (uint256); function supplyEth(address to_) external payable returns (uint256);
function supply( function supply(
@ -11,4 +12,7 @@ interface IInstaLite {
) external returns (uint256); ) external returns (uint256);
function withdraw(uint256 amount_, address to_) external returns (uint256); function withdraw(uint256 amount_, address to_) external returns (uint256);
function deleverage(uint amt_) external;
} }

View File

@ -12,17 +12,20 @@ import { Events } from "./events.sol";
import { IInstaLite } from "./interface.sol"; import { IInstaLite } from "./interface.sol";
abstract contract InstaLiteConnector is Events, Basic { abstract contract InstaLiteConnector is Events, Basic {
TokenInterface internal constant astethToken = TokenInterface(0x1982b2F5814301d4e9a8b0201555376e62F82428);
/** /**
* @dev Supply ETH/ERC20 * @dev Supply ETH/ERC20
* @notice Supply a token into Instalite. * @notice Supply a token into Instalite.
* @param vaultAddress Address of instaLite Contract. * @param vaultAddr Address of instaLite Contract.
* @param token The address of the token to be supplied. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) * @param token The address of the token to be supplied. (For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of token to be supplied. (For max: `uint256(-1)`) * @param amt The amount of token to be supplied. (For max: `uint256(-1)`)
* @param getId ID to retrieve amt. * @param getId ID to retrieve amt.
* @param setIds array of IDs to store the amount of tokens deposited. * @param setIds array of IDs to store the amount of tokens deposited.
*/ */
function supply( function supply(
address vaultAddress, address vaultAddr,
address token, address token,
uint256 amt, uint256 amt,
uint256 getId, uint256 getId,
@ -36,11 +39,9 @@ abstract contract InstaLiteConnector is Events, Basic {
bool isEth = token == ethAddr; bool isEth = token == ethAddr;
uint256 vTokenAmt; uint256 vTokenAmt;
IInstaLite instaLite = IInstaLite(vaultAddress);
if (isEth) { if (isEth) {
_amt = _amt == uint256(-1) ? address(this).balance : _amt; _amt = _amt == uint256(-1) ? address(this).balance : _amt;
vTokenAmt = instaLite.supplyEth{ value: amt }(address(this)); vTokenAmt = IInstaLite(vaultAddr).supplyEth{ value: amt }(address(this));
} else { } else {
TokenInterface tokenContract = TokenInterface(token); TokenInterface tokenContract = TokenInterface(token);
@ -48,8 +49,8 @@ abstract contract InstaLiteConnector is Events, Basic {
? tokenContract.balanceOf(address(this)) ? tokenContract.balanceOf(address(this))
: _amt; : _amt;
approve(tokenContract, vaultAddress, _amt); approve(tokenContract, vaultAddr, _amt);
vTokenAmt = instaLite.supply(token, _amt, address(this)); vTokenAmt = IInstaLite(vaultAddr).supply(token, _amt, address(this));
} }
setUint(setIds[0], _amt); setUint(setIds[0], _amt);
@ -57,7 +58,7 @@ abstract contract InstaLiteConnector is Events, Basic {
_eventName = "LogSupply(address,address,uint256,uint256,uint256,uint256[])"; _eventName = "LogSupply(address,address,uint256,uint256,uint256,uint256[])";
_eventParam = abi.encode( _eventParam = abi.encode(
vaultAddress, vaultAddr,
token, token,
vTokenAmt, vTokenAmt,
_amt, _amt,
@ -69,13 +70,13 @@ abstract contract InstaLiteConnector is Events, Basic {
/** /**
* @dev Withdraw ETH/ERC20 * @dev Withdraw ETH/ERC20
* @notice Withdraw deposited tokens from Instalite. * @notice Withdraw deposited tokens from Instalite.
* @param vaultAddress Address of vaultAddress Contract. * @param vaultAddr Address of vaultAddress Contract.
* @param amt The amount of the token to withdraw. * @param amt The amount of the token to withdraw.
* @param getId ID to retrieve amt. * @param getId ID to retrieve amt.
* @param setIds array of IDs to stores the amount of tokens withdrawn. * @param setIds array of IDs to stores the amount of tokens withdrawn.
*/ */
function withdraw( function withdraw(
address vaultAddress, address vaultAddr,
uint256 amt, uint256 amt,
uint256 getId, uint256 getId,
uint256[] memory setIds uint256[] memory setIds
@ -86,15 +87,49 @@ abstract contract InstaLiteConnector is Events, Basic {
{ {
uint256 _amt = getUint(getId, amt); uint256 _amt = getUint(getId, amt);
IInstaLite instaLite = IInstaLite(vaultAddress); uint256 vTokenAmt = IInstaLite(vaultAddr).withdraw(_amt, address(this));
uint256 vTokenAmt = instaLite.withdraw(_amt, address(this));
setUint(setIds[0], _amt); setUint(setIds[0], _amt);
setUint(setIds[1], vTokenAmt); setUint(setIds[1], vTokenAmt);
_eventName = "LogWithdraw(address,uint256,uint256,uint256,uint256[])"; _eventName = "LogWithdraw(address,uint256,uint256,uint256,uint256[])";
_eventParam = abi.encode(vaultAddress, _amt, vTokenAmt, getId, setIds); _eventParam = abi.encode(vaultAddr, _amt, vTokenAmt, getId, setIds);
}
/**
* @dev Withdraw ETH/ERC20
* @notice Withdraw deposited tokens from Instalite.
* @param vaultAddr The amount of the token to withdraw.
* @param amt The amount of the token to withdraw.
* @param getId ID to retrieve amt.
* @param setId ID to retrieve amt.
*/
function deleverage(
address vaultAddr,
uint256 amt,
uint256 getId,
uint256 setId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(getId, amt);
uint initialBal = astethToken.balanceOf(address(this));
approve(TokenInterface(wethAddr), vaultAddr, _amt);
IInstaLite(vaultAddr).deleverage(_amt);
uint finalBal = astethToken.balanceOf(address(this));
require(amt <= (finalBal - initialBal), "lack-of-steth");
setUint(setId, _amt);
_eventName = "LogDeleverage(address,uint256,uint256,uint256)";
_eventParam = abi.encode(vaultAddr, _amt, getId, setId);
} }
} }