diff --git a/contracts/mainnet/connectors/instaLite/events.sol b/contracts/mainnet/connectors/instaLite/events.sol new file mode 100644 index 00000000..6b1e2827 --- /dev/null +++ b/contracts/mainnet/connectors/instaLite/events.sol @@ -0,0 +1,27 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +contract Events { + event LogSupply( + address vaultAddr, + address token, + uint256 vTokenAmt, + uint256 amt, + uint256 getId, + uint256[] setIds + ); + event LogWithdraw( + address vaultAddr, + uint256 amt, + uint256 vTokenAmt, + uint256 getId, + uint256[] setIds + ); + + event LogDeleverage( + address vaultAddr, + uint256 amt, + uint256 getId, + uint256 setId + ); +} diff --git a/contracts/mainnet/connectors/instaLite/interface.sol b/contracts/mainnet/connectors/instaLite/interface.sol new file mode 100644 index 00000000..ae20fed4 --- /dev/null +++ b/contracts/mainnet/connectors/instaLite/interface.sol @@ -0,0 +1,18 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +interface IInstaLite { + + function supplyEth(address to_) external payable returns (uint256); + + function supply( + address token_, + uint256 amount_, + address to_ + ) external returns (uint256); + + function withdraw(uint256 amount_, address to_) external returns (uint256); + + function deleverage(uint amt_) external; + +} diff --git a/contracts/mainnet/connectors/instaLite/main.sol b/contracts/mainnet/connectors/instaLite/main.sol new file mode 100644 index 00000000..d366cd69 --- /dev/null +++ b/contracts/mainnet/connectors/instaLite/main.sol @@ -0,0 +1,139 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.7.0; + +/** + * @title InstaLite Connector + * @dev Supply, Withdraw & Deleverage + */ + +import { TokenInterface } from "../../common/interfaces.sol"; +import { Basic } from "../../common/basic.sol"; +import { Events } from "./events.sol"; +import { IInstaLite } from "./interface.sol"; + +abstract contract InstaLiteConnector is Events, Basic { + + TokenInterface internal constant astethToken = TokenInterface(0x1982b2F5814301d4e9a8b0201555376e62F82428); + + /** + * @dev Supply ETH/ERC20 + * @notice Supply a token into Instalite. + * @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)`) + * @param getId ID to retrieve amt. + * @param setIds array of IDs to store the amount of tokens deposited. + */ + function supply( + address vaultAddr, + address token, + uint256 amt, + uint256 getId, + uint256[] memory setIds + ) + public + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(getId, amt); + bool isEth = token == ethAddr; + uint256 vTokenAmt; + + if (isEth) { + _amt = _amt == uint256(-1) ? address(this).balance : _amt; + vTokenAmt = IInstaLite(vaultAddr).supplyEth{ value: amt }(address(this)); + } else { + TokenInterface tokenContract = TokenInterface(token); + + _amt = _amt == uint256(-1) + ? tokenContract.balanceOf(address(this)) + : _amt; + + approve(tokenContract, vaultAddr, _amt); + vTokenAmt = IInstaLite(vaultAddr).supply(token, _amt, address(this)); + } + + setUint(setIds[0], _amt); + setUint(setIds[1], vTokenAmt); + + _eventName = "LogSupply(address,address,uint256,uint256,uint256,uint256[])"; + _eventParam = abi.encode( + vaultAddr, + token, + vTokenAmt, + _amt, + getId, + setIds + ); + } + + /** + * @dev Withdraw ETH/ERC20 + * @notice Withdraw deposited tokens from Instalite. + * @param vaultAddr Address of vaultAddress Contract. + * @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. + */ + function withdraw( + address vaultAddr, + uint256 amt, + uint256 getId, + uint256[] memory setIds + ) + external + payable + returns (string memory _eventName, bytes memory _eventParam) + { + uint256 _amt = getUint(getId, amt); + + uint256 vTokenAmt = IInstaLite(vaultAddr).withdraw(_amt, address(this)); + + setUint(setIds[0], _amt); + setUint(setIds[1], vTokenAmt); + + _eventName = "LogWithdraw(address,uint256,uint256,uint256,uint256[])"; + _eventParam = abi.encode(vaultAddr, _amt, vTokenAmt, getId, setIds); + } + + /** + * @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. + * @param getId ID to retrieve amt. + * @param setId ID to set 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); + } + +} + +contract ConnectV2InstaLite is InstaLiteConnector { + string public constant name = "InstaLite-v1"; +} diff --git a/test/mainnet/instaLite/instaLite.test.ts b/test/mainnet/instaLite/instaLite.test.ts new file mode 100644 index 00000000..a08847e3 --- /dev/null +++ b/test/mainnet/instaLite/instaLite.test.ts @@ -0,0 +1,101 @@ +import hre from "hardhat"; +import { expect } from "chai"; +const { ethers } = hre; //check +import { BigNumber } from "bignumber.js"; +import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector"; +import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2"; +import { encodeSpells } from "../../../scripts/tests/encodeSpells"; +import { getMasterSigner } from "../../../scripts/tests/getMasterSigner"; +import { addresses } from "../../../scripts/tests/mainnet/addresses"; +import { addLiquidity } from "../../../scripts/tests/addLiquidity"; +import { abis } from "../../../scripts/constant/abis"; +import { ConnectV2InstaLiteVault1__factory } from "../../../typechain"; +// import lido_abi from "./abi.json"; +import type { Signer, Contract } from "ethers"; +import { parseEther } from "ethers/lib/utils"; + +describe("instaLite", function () { + const connectorName = "instaLite-test"; + + let dsaWallet0: Contract; + let wallet0: Signer, wallet1: Signer; + let masterSigner: Signer; + let instaConnectorsV2: Contract; + let connector: Contract; + + before(async () => { + // await hre.network.provider.request({ + // method: "hardhat_reset", + // params: [ + // { + // forking: { + // // @ts-ignore + // jsonRpcUrl: hre.config.networks.hardhat.forking.url, + // blockNumber: 14334859 + // }, + // }, + // ], + // }); + [wallet0, wallet1] = await ethers.getSigners(); + masterSigner = await getMasterSigner(); + instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2); + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: ConnectV2InstaLiteVault1__factory, + signer: masterSigner, + connectors: instaConnectorsV2 + }); + console.log("Connector address", connector.address); + }); + + it("Should have contracts deployed.", async function () { + expect(!!instaConnectorsV2.address).to.be.true; + expect(!!connector.address).to.be.true; + expect(!!(await masterSigner.getAddress())).to.be.true; + }); + + describe("DSA wallet setup", function () { + it("Should build DSA v2", async function () { + dsaWallet0 = await buildDSAv2(await wallet0.getAddress()); + expect(!!dsaWallet0.address).to.be.true; + }); + + it("Deposit ETH into DSA wallet", async function () { + await wallet0.sendTransaction({ + to: dsaWallet0.address, + value: ethers.utils.parseEther("10") + }); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10")); + }); + }); + + describe("Main", function () { + it("should deposit the eth", async function () { + const _amt = ethers.utils.parseEther("5"); + const ethAddr = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"; + const spells = [ + { + connector: connectorName, + method: "supply", + args: ["0xc383a3833a87009fd9597f8184979af5edfad019", ethAddr, _amt, 0, [0, 0]] + } + ]; + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), await wallet1.getAddress()); + const receipt = await tx.wait(); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.eq(parseEther("5")); + }); + + it("should withdraw", async function () { + const _amt = ethers.utils.parseEther("1"); + const spells = [ + { + connector: connectorName, + method: "withdraw", + args: ["0xc383a3833a87009fd9597f8184979af5edfad019", _amt, 0, [0, 0]] + } + ]; + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), await wallet1.getAddress()); + const receipt = await tx.wait(); + }); + }); +});