feat: update erc4626 mainnet

This commit is contained in:
Shriya Tyagi 2024-02-12 02:43:59 +04:00
parent 7704816e27
commit 4dcd91b1e8
3 changed files with 298 additions and 83 deletions

View File

@ -5,7 +5,6 @@ contract Events {
event LogDeposit( event LogDeposit(
address indexed token, address indexed token,
uint256 underlyingAmt, uint256 underlyingAmt,
uint256 minSharesPerToken,
uint256 sharesReceieved, uint256 sharesReceieved,
uint256 getId, uint256 getId,
uint256 setId uint256 setId
@ -14,8 +13,7 @@ contract Events {
event LogMint( event LogMint(
address indexed token, address indexed token,
uint256 shareAmt, uint256 shareAmt,
uint256 maxTokenPerShares, uint256 tokensDeposited,
uint256 tokensDeducted,
uint256 getId, uint256 getId,
uint256 setId uint256 setId
); );
@ -23,7 +21,6 @@ contract Events {
event LogWithdraw( event LogWithdraw(
address indexed token, address indexed token,
uint256 underlyingAmt, uint256 underlyingAmt,
uint256 maxSharesPerToken,
uint256 sharedBurned, uint256 sharedBurned,
address indexed to, address indexed to,
uint256 getId, uint256 getId,
@ -33,7 +30,6 @@ contract Events {
event LogRedeem( event LogRedeem(
address indexed token, address indexed token,
uint256 shareAmt, uint256 shareAmt,
uint256 minTokenPerShares,
uint256 underlyingAmtReceieved, uint256 underlyingAmtReceieved,
address to, address to,
uint256 getId, uint256 getId,

View File

@ -2,7 +2,7 @@
pragma solidity ^0.7.0; pragma solidity ^0.7.0;
/** /**
* @title Basic D. * @title Basic D V2.
* @dev Deposit, Mint, Withdraw, & Redeem from ERC4626 DSA. * @dev Deposit, Mint, Withdraw, & Redeem from ERC4626 DSA.
*/ */
@ -14,19 +14,18 @@ import { Basic } from "../../common/basic.sol";
import { Events } from "./events.sol"; import { Events } from "./events.sol";
abstract contract BasicConnector is Events, DSMath, Basic { abstract contract BasicConnector is Events, DSMath, Basic {
/** /**
* @dev Deposit underlying asset to ERC4626 Vault. * @dev Deposit underlying asset to ERC4626 Vault.
* @notice Mints vault shares by depositing exactly amount of underlying assets * @notice Mints vault shares by depositing exactly amount of underlying assets
* @param vaultToken ERC4626 Token address. * @param vaultToken ERC4626 Token address.
* @param underlyingAmt The amount of the underlying asset to deposit. (For max: `uint256(-1)`) * @param underlyingAmt The amount of the underlying asset to deposit. (For max: `uint256(-1)`)
* @param minSharesPerToken The min share rate of deposit. Should always be in 18 decimals.
* @param getId ID to retrieve amt. * @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens deposited. * @param setId ID stores the amount of tokens deposited.
*/ */
function deposit( function deposit(
address vaultToken, address vaultToken,
uint256 underlyingAmt, uint256 underlyingAmt,
uint256 minSharesPerToken,
uint256 getId, uint256 getId,
uint256 setId uint256 setId
) public returns (string memory _eventName, bytes memory _eventParam) { ) public returns (string memory _eventName, bytes memory _eventParam) {
@ -41,31 +40,18 @@ abstract contract BasicConnector is Events, DSMath, Basic {
? _underlyingTokenContract.balanceOf(address(this)) ? _underlyingTokenContract.balanceOf(address(this))
: _underlyingAmt; : _underlyingAmt;
// Returns final amount in token decimals.
uint256 _minShares = wmul(minSharesPerToken, _underlyingAmt);
// Initial share balance
uint256 _initialVaultBal = vaultTokenContract.balanceOf(address(this));
approve(_underlyingTokenContract, vaultToken, _underlyingAmt); approve(_underlyingTokenContract, vaultToken, _underlyingAmt);
// Deposit tokens for shares // Deposit tokens for shares
uint256 _sharesReceieved =
vaultTokenContract.deposit(_underlyingAmt, address(this)); vaultTokenContract.deposit(_underlyingAmt, address(this));
uint256 _sharesReceieved = sub(
vaultTokenContract.balanceOf(address(this)),
_initialVaultBal
);
require(_minShares <= _sharesReceieved, "Less shares received");
setUint(setId, _sharesReceieved); setUint(setId, _sharesReceieved);
_eventName = "LogDeposit(address,uint256,uint256,uint256,uint256,uint256)"; _eventName = "LogDeposit(address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode( _eventParam = abi.encode(
vaultToken, vaultToken,
_underlyingAmt, _underlyingAmt,
minSharesPerToken,
_sharesReceieved, _sharesReceieved,
getId, getId,
setId setId
@ -77,14 +63,12 @@ abstract contract BasicConnector is Events, DSMath, Basic {
* @notice Mints vault shares by minting exactly amount of underlying assets * @notice Mints vault shares by minting exactly amount of underlying assets
* @param vaultToken ERC4626 Token address. * @param vaultToken ERC4626 Token address.
* @param shareAmt The amount of the share to mint. (For max: `uint256(-1)`) * @param shareAmt The amount of the share to mint. (For max: `uint256(-1)`)
* @param maxTokenPerShares The max underyling token rate of mint. Always in 18 decimals.
* @param getId ID to retrieve amt. * @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens minted. * @param setId ID stores the amount of tokens minted.
*/ */
function mint( function mint(
address vaultToken, address vaultToken,
uint256 shareAmt, uint256 shareAmt,
uint256 maxTokenPerShares,
uint256 getId, uint256 getId,
uint256 setId uint256 setId
) public returns (string memory _eventName, bytes memory _eventParam) { ) public returns (string memory _eventName, bytes memory _eventParam) {
@ -95,41 +79,23 @@ abstract contract BasicConnector is Events, DSMath, Basic {
vaultTokenContract.asset() vaultTokenContract.asset()
); );
_shareAmt = _shareAmt == uint256(-1)
? vaultTokenContract.balanceOf(address(this))
: _shareAmt;
// Returns final amount in token decimals.
uint256 _maxTokens = wmul(maxTokenPerShares, _shareAmt);
uint256 _underlyingTokenAmount = vaultTokenContract.previewMint( uint256 _underlyingTokenAmount = vaultTokenContract.previewMint(
_shareAmt _shareAmt
); );
uint256 _initalUnderlyingBal = underlyingTokenContract.balanceOf(
address(this)
);
approve(underlyingTokenContract, vaultToken, _underlyingTokenAmount); approve(underlyingTokenContract, vaultToken, _underlyingTokenAmount);
// Mint shares for tokens // Mint shares for tokens
uint256 _tokensDeposited =
vaultTokenContract.mint(_shareAmt, address(this)); vaultTokenContract.mint(_shareAmt, address(this));
uint256 _tokensDeducted = sub( setUint(setId, _tokensDeposited);
_initalUnderlyingBal,
underlyingTokenContract.balanceOf(address(this))
);
require(_maxTokens >= _tokensDeducted, "maxTokenPerShares-exceeds"); _eventName = "LogMint(address,uint256,uint256,uint256,uint256)";
setUint(setId, _shareAmt);
_eventName = "LogMint(address,uint256,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode( _eventParam = abi.encode(
vaultToken, vaultToken,
_shareAmt, _shareAmt,
maxTokenPerShares, _tokensDeposited,
_tokensDeducted,
getId, getId,
setId setId
); );
@ -140,7 +106,6 @@ abstract contract BasicConnector is Events, DSMath, Basic {
* @notice Withdraw vault shares with exactly amount of underlying assets * @notice Withdraw vault shares with exactly amount of underlying assets
* @param vaultToken ERC4626 Token address. * @param vaultToken ERC4626 Token address.
* @param underlyingAmt The amount of the token to withdraw. (For max: `uint256(-1)`) * @param underlyingAmt The amount of the token to withdraw. (For max: `uint256(-1)`)
* @param maxSharesPerToken The max share rate of withdrawn amount. Always send in 18 decimals.
* @param to The address of receiver. * @param to The address of receiver.
* @param getId ID to retrieve amt. * @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens withdrawn. * @param setId ID stores the amount of tokens withdrawn.
@ -148,7 +113,6 @@ abstract contract BasicConnector is Events, DSMath, Basic {
function withdraw( function withdraw(
address vaultToken, address vaultToken,
uint256 underlyingAmt, uint256 underlyingAmt,
uint256 maxSharesPerToken,
address payable to, address payable to,
uint256 getId, uint256 getId,
uint256 setId uint256 setId
@ -164,25 +128,16 @@ abstract contract BasicConnector is Events, DSMath, Basic {
? underlyingTokenContract.balanceOf(address(this)) ? underlyingTokenContract.balanceOf(address(this))
: _underlyingAmt; : _underlyingAmt;
// Returns final amount in token decimals.
uint256 _maxShares = wmul(maxSharesPerToken, _underlyingAmt);
uint256 _initialVaultBal = vaultTokenContract.balanceOf(to);
// Withdraw tokens for shares // Withdraw tokens for shares
uint256 _sharesBurned =
vaultTokenContract.withdraw(_underlyingAmt, to, address(this)); vaultTokenContract.withdraw(_underlyingAmt, to, address(this));
uint256 _sharesBurned = sub(_initialVaultBal, vaultTokenContract.balanceOf(to));
require(_maxShares >= _sharesBurned, "maxShares-exceeds");
setUint(setId, _underlyingAmt); setUint(setId, _underlyingAmt);
_eventName = "LogWithdraw(address,uint256,uint256,uint256,address,uint256,uint256)"; _eventName = "LogWithdraw(address,uint256,uint256,address,uint256,uint256)";
_eventParam = abi.encode( _eventParam = abi.encode(
vaultToken, vaultToken,
_underlyingAmt, _underlyingAmt,
maxSharesPerToken,
_sharesBurned, _sharesBurned,
to, to,
getId, getId,
@ -195,7 +150,6 @@ abstract contract BasicConnector is Events, DSMath, Basic {
* @notice Redeem vault shares with exactly amount of underlying assets * @notice Redeem vault shares with exactly amount of underlying assets
* @param vaultToken ERC4626 Token address. * @param vaultToken ERC4626 Token address.
* @param shareAmt The amount of the token to redeem. (For max: `uint256(-1)`) * @param shareAmt The amount of the token to redeem. (For max: `uint256(-1)`)
* @param minTokenPerShares The min underlying token rate of withdraw. Always in 18 decimals.
* @param to The address of receiver. * @param to The address of receiver.
* @param getId ID to retrieve amt. * @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens redeem. * @param setId ID stores the amount of tokens redeem.
@ -204,7 +158,6 @@ abstract contract BasicConnector is Events, DSMath, Basic {
function redeem( function redeem(
address vaultToken, address vaultToken,
uint256 shareAmt, uint256 shareAmt,
uint256 minTokenPerShares,
address payable to, address payable to,
uint256 getId, uint256 getId,
uint256 setId uint256 setId
@ -220,28 +173,16 @@ abstract contract BasicConnector is Events, DSMath, Basic {
? vaultTokenContract.balanceOf(address(this)) ? vaultTokenContract.balanceOf(address(this))
: _shareAmt; : _shareAmt;
// Returns final amount in token decimals.
uint256 _minUnderlyingAmt = wmul(minTokenPerShares, _shareAmt);
uint256 _initalUnderlyingBal = underlyingTokenContract.balanceOf(to);
// Redeem tokens for shares // Redeem tokens for shares
uint256 _underlyingAmtReceieved =
vaultTokenContract.redeem(_shareAmt, to, address(this)); vaultTokenContract.redeem(_shareAmt, to, address(this));
uint256 _underlyingAmtReceieved = sub(
underlyingTokenContract.balanceOf(to),
_initalUnderlyingBal
);
require(_minUnderlyingAmt <= _underlyingAmtReceieved, "_minUnderlyingAmt-exceeds");
setUint(setId, _shareAmt); setUint(setId, _shareAmt);
_eventName = "LogRedeem(address,uint256,uint256,uint256,address,uint256,uint256)"; _eventName = "LogRedeem(address,uint256,uint256,address,uint256,uint256)";
_eventParam = abi.encode( _eventParam = abi.encode(
vaultToken, vaultToken,
_shareAmt, _shareAmt,
minTokenPerShares,
_underlyingAmtReceieved, _underlyingAmtReceieved,
to, to,
getId, getId,
@ -250,6 +191,6 @@ abstract contract BasicConnector is Events, DSMath, Basic {
} }
} }
contract ConnectV2BasicERC4626 is BasicConnector { contract ConnectV2BasicERC4626V2 is BasicConnector {
string public constant name = "BASIC-ERC4626-v1.0"; string public constant name = "BASIC-ERC4626-v2.0";
} }

View File

@ -0,0 +1,278 @@
import { expect } from "chai";
import hre, { network } from "hardhat";
const { web3, deployments, waffle, ethers } = hre;
const { provider, deployContract } = waffle;
import type { Signer, Contract } from "ethers";
import { parseEther, parseUnits } from "ethers/lib/utils";
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 { abis } from "../../../scripts/constant/abis";
import { tokens } from "../../../scripts/tests/mainnet/tokens";
import { ConnectV2BasicERC4626V2__factory, IERC4626__factory, IERC20Minimal__factory } from "../../../typechain";
describe("BASIC-D", function () {
const connectorName = "BASIC-D";
let dsaWallet0: any;
let masterSigner: Signer;
let instaConnectorsV2: Contract;
let connector: any;
let wallet: any;
const account = "0x075e72a5edf65f0a5f44699c7654c1a76941ddc8";
const sDAIaddress = "0x83f20f44975d03b1b09e64809b757c47f942beea";
let signer: any;
const daiContract = new ethers.Contract(tokens.dai.address, IERC20Minimal__factory.abi, ethers.provider);
const erc4626Contract = new ethers.Contract(sDAIaddress, IERC4626__factory.abi, ethers.provider);
const wallets = provider.getWallets();
const [wallet0] = wallets;
before(async () => {
await hre.network.provider.request({
method: "hardhat_reset",
params: [
{
forking: {
// @ts-ignore
jsonRpcUrl: hre.config.networks.hardhat.forking?.url,
blockNumber: 17907926
}
}
]
});
masterSigner = await getMasterSigner();
instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2);
connector = await deployAndEnableConnector({
connectorName,
contractArtifact: ConnectV2BasicERC4626V2__factory,
signer: masterSigner,
connectors: instaConnectorsV2
});
console.log("Connector address", connector.address);
await hre.network.provider.send("hardhat_setBalance", [account, ethers.utils.parseEther("10").toHexString()]);
await hre.network.provider.request({
method: "hardhat_impersonateAccount",
params: [account]
});
signer = await ethers.getSigner(account);
});
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(wallet0.address);
expect(!!dsaWallet0.address).to.be.true;
wallet = await ethers.getSigner(dsaWallet0.address);
});
it("Deposit ETH into DSA wallet", async function () {
await hre.network.provider.request({
method: "hardhat_impersonateAccount",
params: [wallet.address]
});
await wallet0.sendTransaction({
to: dsaWallet0.address,
value: ethers.utils.parseEther("10")
});
let txRes = await daiContract.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseEther("1000"));
await txRes.wait();
// expect(await daiContract.balanceOf(dsaWallet0.address)).to.be.eq(ethers.utils.parseEther("10000"));
});
});
describe("Main", function () {
// it("Calculate Total Asset and Total Supply", async () => {
// const totalAsset = await erc4626Contract.totalAssets();
// const totalSupply = await erc4626Contract.totalSupply();
// console.log("totalAsset :>> ", totalAsset);
// console.log("totalSupply :>> ", totalSupply);
// });
it("should deposit asset to ERC4626", async () => {
const assets = ethers.utils.parseEther("1");
// Returns the amount of shares for assets
const previewDeposit = await erc4626Contract.previewDeposit(assets);
console.log("previewDeposit :>> ", previewDeposit.toString());
const maxDeposit = await erc4626Contract.maxDeposit(dsaWallet0.address);
let minSharesPerToken = ethers.utils.parseUnits("0.95");
const beforebalance = await erc4626Contract.balanceOf(dsaWallet0.address);
console.log("Share before balance :>> ", beforebalance.toString());
let spells = [
{
connector: connectorName,
method: "deposit",
args: [sDAIaddress, assets, 0, 0]
}
];
let tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address);
let receipt = await tx.wait();
const afterbalance = await erc4626Contract.balanceOf(dsaWallet0.address);
console.log("Share after balance :>> ", afterbalance.toString());
expect(afterbalance.sub(beforebalance)).to.be.lte(previewDeposit)
});
it("should mint asset to ERC4626", async () => {
const beforeBalance = await daiContract.balanceOf(dsaWallet0.address);
console.log("token balance before :>> ", beforeBalance.toString());
const beforeSharebalance = await erc4626Contract.balanceOf(dsaWallet0.address);
console.log("share balance before :>> ", beforeSharebalance.toString());
const shares = ethers.utils.parseEther("1");
// Returns token amount for shares
const previewMint = await erc4626Contract.previewMint(shares);
console.log("Token amount preview Mint :>> ", previewMint.toString());
let maxTokenPerShares = ethers.utils.parseUnits("1.1");
let spells = [
{
connector: connectorName,
method: "mint",
args: [sDAIaddress, shares, 0, 0]
}
];
let tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address);
let receipt = await tx.wait();
const afterbalance = await daiContract.balanceOf(dsaWallet0.address);
console.log("token balance after :>> ", afterbalance.toString());
const afterSharebalance = await erc4626Contract.balanceOf(dsaWallet0.address);
console.log("share balance after :>> ", afterSharebalance.toString());
});
it("should Max redeem", async () => {
const balance = await erc4626Contract.balanceOf(dsaWallet0.address);
console.log("Share balance :>> ", balance.toString());
// Returns max Shares
const maxRedeem: BigNumber = await erc4626Contract.maxRedeem(dsaWallet0.address);
console.log("maxRedeem :>> ", maxRedeem.toString());
const beforeUnderbalance = await daiContract.balanceOf(dsaWallet0.address);
console.log("beforeUnderbalance :>> ", beforeUnderbalance.toString());
const beforeVaultbalance = await erc4626Contract.balanceOf(dsaWallet0.address);
console.log("beforeVaultbalance :>> ", beforeVaultbalance.toString());
let minTokenPerShares = ethers.utils.parseUnits("1.01");
const setId = "83478237";
let spells = [
{
connector: connectorName,
method: "redeem",
args: [sDAIaddress, ethers.constants.MaxUint256, dsaWallet0.address, 0, setId]
}
];
let tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address);
let receipt = await tx.wait();
const afterUnderbalance = await daiContract.balanceOf(dsaWallet0.address);
console.log("afterUnderbalance :>> ", afterUnderbalance.toString());
const afterVaultbalance = await erc4626Contract.balanceOf(dsaWallet0.address);
console.log("afterVaultbalance :>> ", afterVaultbalance.toString());
});
// it("should Revert for not satisfying min redeem rate", async () => {
// const balance = await erc4626Contract.balanceOf(dsaWallet0.address);
// console.log("Share balance :>> ", balance.toString());
// let spells = [
// {
// connector: connectorName,
// method: "deposit",
// args: [sDAIaddress, ethers.utils.parseEther("1"), 0, 0]
// }
// ];
// let tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address);
// let receipt = await tx.wait();
// // Returns max Shares
// const maxRedeem: BigNumber = await erc4626Contract.maxRedeem(dsaWallet0.address);
// console.log("maxRedeem :>> ", maxRedeem.toString());
// const beforeUnderbalance = await daiContract.balanceOf(dsaWallet0.address);
// console.log("beforeUnderbalance :>> ", beforeUnderbalance.toString());
// const beforeVaultbalance = await erc4626Contract.balanceOf(dsaWallet0.address);
// console.log("beforeVaultbalance :>> ", beforeVaultbalance.toString());
// expect(beforeVaultbalance).to.be.gte("950000000000000000")
// // In case of not satisfying min rate
// let minTokenPerShares = ethers.utils.parseUnits("1.5");
// spells = [
// {
// connector: connectorName,
// method: "redeem",
// args: [sDAIaddress, ethers.constants.MaxUint256, dsaWallet0.address, 0, 0]
// }
// ];
// await expect(dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address)).to.be.reverted;
// });
it("should withdraw asset to ERC4626", async () => {
const maxWithdraw: BigNumber = await erc4626Contract.maxWithdraw(dsaWallet0.address);
console.log("maxWithdraw :>> ", maxWithdraw.toString());
const beforeUnderbalance = await daiContract.balanceOf(dsaWallet0.address);
console.log("beforeUnderbalance :>> ", beforeUnderbalance.toString());
const beforeVaultbalance = await erc4626Contract.balanceOf(dsaWallet0.address);
console.log("beforeVaultbalance :>> ", beforeVaultbalance.toString());
let maxSharesPerToken = ethers.utils.parseUnits("0.975");
const setId = "83478237";
let spells = [
{
connector: connectorName,
method: "withdraw",
args: [sDAIaddress, maxWithdraw, dsaWallet0.address, 0, setId]
}
];
let tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address);
let receipt = await tx.wait();
const afterUnderbalance = await daiContract.balanceOf(dsaWallet0.address);
console.log("afterUnderbalance :>> ", afterUnderbalance.toString());
const afterVaultbalance = await erc4626Contract.balanceOf(dsaWallet0.address);
console.log("afterVaultbalance :>> ", afterVaultbalance.toString());
});
});
});