dsa-connectors/contracts/mainnet/connectors/instaLite/main.sol

249 lines
6.6 KiB
Solidity
Raw Normal View History

2022-04-01 17:03:30 +00:00
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
/**
* @title InstaLite Connector
2022-04-06 02:32:56 +00:00
* @dev Supply, Withdraw & Deleverage
2022-04-01 17:03:30 +00:00
*/
2022-04-06 02:32:56 +00:00
2022-04-02 18:24:00 +00:00
import { TokenInterface } from "../../common/interfaces.sol";
import { Basic } from "../../common/basic.sol";
2022-04-01 17:03:30 +00:00
import { Events } from "./events.sol";
2022-04-02 18:32:38 +00:00
import { IInstaLite } from "./interface.sol";
2022-04-01 17:03:30 +00:00
2022-04-02 17:11:33 +00:00
abstract contract InstaLiteConnector is Events, Basic {
2022-06-10 20:52:11 +00:00
TokenInterface internal constant astethToken =
TokenInterface(0x1982b2F5814301d4e9a8b0201555376e62F82428);
2022-06-10 21:44:59 +00:00
TokenInterface internal constant stethToken =
TokenInterface(0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84);
address internal constant ethVaultAddr =
2022-06-10 21:54:09 +00:00
0xc383a3833A87009fD9597F8184979AF5eDFad019;
2022-04-06 02:27:57 +00:00
2022-04-01 17:03:30 +00:00
/**
* @dev Supply ETH/ERC20
* @notice Supply a token into Instalite.
2022-04-06 02:27:57 +00:00
* @param vaultAddr Address of instaLite Contract.
* @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)`)
2022-04-01 17:03:30 +00:00
* @param getId ID to retrieve amt.
* @param setIds array of IDs to store the amount of tokens deposited.
2022-04-01 17:03:30 +00:00
*/
function supply(
2022-04-06 02:27:57 +00:00
address vaultAddr,
2022-04-01 17:03:30 +00:00
address token,
uint256 amt,
uint256 getId,
2022-04-02 18:24:00 +00:00
uint256[] memory setIds
2022-04-01 17:03:30 +00:00
)
public
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(getId, amt);
bool isEth = token == ethAddr;
2022-04-01 17:50:07 +00:00
uint256 vTokenAmt;
2022-04-02 17:11:33 +00:00
2022-04-01 17:03:30 +00:00
if (isEth) {
_amt = _amt == uint256(-1) ? address(this).balance : _amt;
2022-06-10 20:52:11 +00:00
vTokenAmt = IInstaLite(vaultAddr).supplyEth{ value: amt }(
address(this)
);
2022-04-01 17:03:30 +00:00
} else {
TokenInterface tokenContract = TokenInterface(token);
_amt = _amt == uint256(-1)
? tokenContract.balanceOf(address(this))
: _amt;
2022-04-06 02:27:57 +00:00
approve(tokenContract, vaultAddr, _amt);
2022-06-10 20:52:11 +00:00
vTokenAmt = IInstaLite(vaultAddr).supply(
token,
_amt,
address(this)
);
2022-04-01 17:03:30 +00:00
}
2022-06-10 21:44:59 +00:00
if (setIds.length >= 2) {
setUint(setIds[0], _amt);
setUint(setIds[1], vTokenAmt);
}
2022-04-01 17:03:30 +00:00
2022-04-02 22:01:38 +00:00
_eventName = "LogSupply(address,address,uint256,uint256,uint256,uint256[])";
2022-04-02 18:08:26 +00:00
_eventParam = abi.encode(
2022-04-06 02:27:57 +00:00
vaultAddr,
2022-04-02 18:08:26 +00:00
token,
vTokenAmt,
_amt,
getId,
2022-04-02 18:24:00 +00:00
setIds
2022-04-02 18:08:26 +00:00
);
2022-04-01 17:03:30 +00:00
}
/**
* @dev Withdraw ETH/ERC20
* @notice Withdraw deposited tokens from Instalite.
2022-04-06 02:27:57 +00:00
* @param vaultAddr Address of vaultAddress Contract.
2022-04-01 17:03:30 +00:00
* @param amt The amount of the token to withdraw.
* @param getId ID to retrieve amt.
* @param setIds array of IDs to stores the amount of tokens withdrawn.
2022-04-01 17:03:30 +00:00
*/
function withdraw(
2022-04-06 02:27:57 +00:00
address vaultAddr,
2022-04-01 17:03:30 +00:00
uint256 amt,
uint256 getId,
2022-04-02 18:24:00 +00:00
uint256[] memory setIds
2022-04-01 17:03:30 +00:00
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(getId, amt);
2022-06-10 20:52:11 +00:00
uint256 vTokenAmt = IInstaLite(vaultAddr).withdraw(_amt, address(this));
2022-04-01 17:03:30 +00:00
2022-06-10 21:44:59 +00:00
if (setIds.length >= 2) {
setUint(setIds[0], _amt);
setUint(setIds[1], vTokenAmt);
}
2022-04-01 17:03:30 +00:00
2022-04-02 22:01:38 +00:00
_eventName = "LogWithdraw(address,uint256,uint256,uint256,uint256[])";
2022-04-06 02:27:57 +00:00
_eventParam = abi.encode(vaultAddr, _amt, vTokenAmt, getId, setIds);
}
/**
2022-04-06 02:32:56 +00:00
* @dev Deleverage vault. Pays back ETH debt and get stETH collateral. 1:1 swap of ETH to stETH
* @notice Deleverage Instalite vault.
* @param vaultAddr Address of vaultAddress Contract.
* @param amt The amount of the token to deleverage.
2022-04-06 02:27:57 +00:00
* @param getId ID to retrieve amt.
2022-04-06 02:32:56 +00:00
* @param setId ID to set amt.
2022-04-06 02:27:57 +00:00
*/
function deleverage(
address vaultAddr,
uint256 amt,
uint256 getId,
uint256 setId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(getId, amt);
2022-06-10 20:52:11 +00:00
uint256 initialBal = astethToken.balanceOf(address(this));
2022-04-06 02:27:57 +00:00
approve(TokenInterface(wethAddr), vaultAddr, _amt);
IInstaLite(vaultAddr).deleverage(_amt);
2022-06-10 20:52:11 +00:00
uint256 finalBal = astethToken.balanceOf(address(this));
2022-04-06 02:27:57 +00:00
require(amt <= (1e9 + finalBal - initialBal), "lack-of-steth");
2022-04-06 02:27:57 +00:00
setUint(setId, _amt);
_eventName = "LogDeleverage(address,uint256,uint256,uint256)";
_eventParam = abi.encode(vaultAddr, _amt, getId, setId);
2022-04-01 17:03:30 +00:00
}
2022-04-06 02:32:56 +00:00
2022-06-10 12:46:45 +00:00
/**
2022-06-10 22:03:36 +00:00
* @dev Deleverage and Withdraw vault. Pays back weth debt and gets steth collateral (aSteth).
2022-06-10 12:46:45 +00:00
* @notice Deleverage Instalite vault.
* @param vaultAddr Address of vaultAddress Contract.
2022-06-10 22:03:36 +00:00
* @param deleverageAmt The amount of the weth to deleverage.
* @param withdrawAmt The amount of the token to withdraw.
* @param getIds IDs to retrieve amts.
* @param setIds IDs to set amts.
2022-06-10 12:46:45 +00:00
*/
function deleverageAndWithdraw(
address vaultAddr,
2022-06-10 22:03:36 +00:00
uint256 deleverageAmt,
uint256 withdrawAmt,
2022-06-10 12:46:45 +00:00
uint256[] memory getIds,
uint256[] memory setIds
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
2022-06-10 20:52:11 +00:00
{
2022-06-10 22:03:36 +00:00
if (getIds.length >= 2) {
deleverageAmt = getUint(getIds[0], deleverageAmt);
withdrawAmt = getUint(getIds[1], withdrawAmt);
}
2022-06-10 12:46:45 +00:00
2022-06-10 21:44:59 +00:00
uint256 _astethAmt;
uint256 _ethAmt;
uint256 _stethAmt;
uint256 _tokenAmt;
2022-06-10 12:46:45 +00:00
2022-06-10 22:03:36 +00:00
approve(TokenInterface(wethAddr), vaultAddr, deleverageAmt);
2022-06-10 21:44:59 +00:00
if (vaultAddr == ethVaultAddr) {
uint256 initialBalAsteth = astethToken.balanceOf(address(this));
uint256 initialBalEth = address(this).balance;
uint256 initialBalSteth = stethToken.balanceOf(address(this));
IInstaLite(vaultAddr).deleverageAndWithdraw(
2022-06-10 22:03:36 +00:00
deleverageAmt,
withdrawAmt,
2022-06-10 21:44:59 +00:00
address(this)
);
2022-06-10 12:46:45 +00:00
2022-06-10 21:53:08 +00:00
_astethAmt =
astethToken.balanceOf(address(this)) -
initialBalAsteth;
2022-06-10 21:44:59 +00:00
_ethAmt = address(this).balance - initialBalEth;
2022-06-10 21:53:08 +00:00
_stethAmt = stethToken.balanceOf(address(this)) - initialBalSteth;
2022-06-10 22:03:36 +00:00
require(deleverageAmt <= (1e9 + _astethAmt), "lack-of-steth");
2022-06-10 21:44:59 +00:00
if (setIds.length >= 3) {
setUint(setIds[0], _astethAmt);
setUint(setIds[1], _ethAmt);
setUint(setIds[2], _stethAmt);
}
} else {
TokenInterface tokenContract = TokenInterface(
IInstaLite(vaultAddr).token()
);
2022-06-10 12:46:45 +00:00
2022-06-10 21:44:59 +00:00
uint256 initialBalAsteth = astethToken.balanceOf(address(this));
uint256 initialBalToken = tokenContract.balanceOf(address(this));
2022-06-10 12:46:45 +00:00
2022-06-10 21:44:59 +00:00
IInstaLite(vaultAddr).deleverageAndWithdraw(
2022-06-10 22:03:36 +00:00
deleverageAmt,
withdrawAmt,
2022-06-10 21:44:59 +00:00
address(this)
);
2022-06-10 21:53:08 +00:00
_astethAmt =
astethToken.balanceOf(address(this)) -
2022-06-10 21:44:59 +00:00
initialBalAsteth;
2022-06-10 21:53:08 +00:00
_tokenAmt =
tokenContract.balanceOf(address(this)) -
2022-06-10 21:44:59 +00:00
initialBalToken;
2022-06-10 22:03:36 +00:00
require(deleverageAmt <= (1e9 + _astethAmt), "lack-of-steth");
2022-06-10 21:44:59 +00:00
if (setIds.length >= 2) {
setUint(setIds[0], _astethAmt);
setUint(setIds[1], _tokenAmt);
}
2022-06-10 12:46:45 +00:00
}
2022-06-10 21:44:59 +00:00
_eventName = "LogDeleverageAndWithdraw(address,uint256,uint256,uint256,uint256,uint256,uint256,uint256[],uint256[])";
2022-06-10 20:52:11 +00:00
_eventParam = abi.encode(
vaultAddr,
2022-06-10 22:03:36 +00:00
deleverageAmt,
withdrawAmt,
2022-06-10 21:53:08 +00:00
_astethAmt,
_ethAmt,
_stethAmt,
_tokenAmt,
2022-06-10 20:52:11 +00:00
getIds,
setIds
);
2022-06-10 12:46:45 +00:00
}
2022-04-01 17:03:30 +00:00
}
2022-04-07 17:17:02 +00:00
contract ConnectV2InstaLite is InstaLiteConnector {
2022-06-10 12:46:45 +00:00
string public constant name = "InstaLite-v1.1";
2022-06-10 20:52:11 +00:00
}