Merge pull request #310 from Instadapp/Erc4626-updates

Erc4626 updates
This commit is contained in:
Shriya Tyagi 2023-08-22 20:45:47 +05:30 committed by GitHub
commit bc9c7c7ad1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 9941 additions and 130 deletions

View File

@ -6,6 +6,7 @@ contract Events {
address indexed token,
uint256 underlyingAmt,
uint256 minSharesPerToken,
uint256 sharesReceieved,
uint256 getId,
uint256 setId
);
@ -14,6 +15,7 @@ contract Events {
address indexed token,
uint256 shareAmt,
uint256 maxTokenPerShares,
uint256 tokensDeducted,
uint256 getId,
uint256 setId
);
@ -22,6 +24,7 @@ contract Events {
address indexed token,
uint256 underlyingAmt,
uint256 maxSharesPerToken,
uint256 sharedBurned,
address indexed to,
uint256 getId,
uint256 setId
@ -31,8 +34,9 @@ contract Events {
address indexed token,
uint256 shareAmt,
uint256 minTokenPerShares,
uint256 underlyingAmtReceieved,
address to,
uint256 getId,
uint256 setId
);
}
}

View File

@ -17,57 +17,56 @@ abstract contract BasicConnector is Events, DSMath, Basic {
/**
* @dev Deposit underlying asset to ERC4626 Vault.
* @notice Mints vault shares by depositing exactly amount of underlying assets
* @param token ERC4626 Token address.
* @param vaultToken ERC4626 Token address.
* @param underlyingAmt The amount of the underlying asset to deposit. (For max: `uint256(-1)`)
* @param minSharesPerToken The min share rate of deposit
* @param minSharesPerToken The min share rate of deposit. Should always be in 18 decimals.
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens deposited.
*/
function deposit(
address token,
address vaultToken,
uint256 underlyingAmt,
uint256 minSharesPerToken,
uint256 getId,
uint256 setId
) public returns (string memory _eventName, bytes memory _eventParam) {
uint256 _underlyingAmt = getUint(getId, underlyingAmt);
IERC4626 vaultTokenContract = IERC4626(token);
address _underlyingToken = vaultTokenContract.asset();
uint8 _vaultShareDecimal = vaultTokenContract.decimals();
TokenInterface underlyingTokenContract = TokenInterface(
_underlyingToken
IERC4626 vaultTokenContract = IERC4626(vaultToken);
TokenInterface _underlyingTokenContract = TokenInterface(
vaultTokenContract.asset()
);
_underlyingAmt = _underlyingAmt == uint256(-1)
? underlyingTokenContract.balanceOf(address(this))
? _underlyingTokenContract.balanceOf(address(this))
: _underlyingAmt;
uint256 _minShares = convert18ToDec(
_vaultShareDecimal,
wmul(minSharesPerToken, _underlyingAmt)
);
// Returns final amount in token decimals.
uint256 _minShares = wmul(minSharesPerToken, _underlyingAmt);
// Initial share balance
uint256 _initialVaultBal = vaultTokenContract.balanceOf(address(this));
approve(underlyingTokenContract, token, _underlyingAmt);
approve(_underlyingTokenContract, vaultToken, _underlyingAmt);
// Deposit tokens for shares
vaultTokenContract.deposit(_underlyingAmt, address(this));
uint256 _finalVaultBal = vaultTokenContract.balanceOf(address(this));
require(
_minShares <= sub(_finalVaultBal, _initialVaultBal),
"minShares-exceeds"
uint256 _sharesReceieved = sub(
vaultTokenContract.balanceOf(address(this)),
_initialVaultBal
);
setUint(setId, _underlyingAmt);
require(_minShares <= _sharesReceieved, "Less shares received");
_eventName = "LogDeposit(address,uint256,uint256,uint256,uint256)";
setUint(setId, _sharesReceieved);
_eventName = "LogDeposit(address,uint256,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
token,
vaultToken,
_underlyingAmt,
minSharesPerToken,
_sharesReceieved,
getId,
setId
);
@ -76,66 +75,61 @@ abstract contract BasicConnector is Events, DSMath, Basic {
/**
* @dev Mint underlying asset to ERC4626 Vault.
* @notice Mints vault shares by minting exactly amount of underlying assets
* @param token ERC4626 Token address.
* @param vaultToken ERC4626 Token address.
* @param shareAmt The amount of the share to mint. (For max: `uint256(-1)`)
* @param maxTokenPerShares The max underyling token rate of mint
* @param maxTokenPerShares The max underyling token rate of mint. Always in 18 decimals.
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens minted.
*/
function mint(
address token,
address vaultToken,
uint256 shareAmt,
uint256 maxTokenPerShares,
uint256 getId,
uint256 setId
) public returns (string memory _eventName, bytes memory _eventParam) {
uint256 _shareAmt = getUint(getId, shareAmt);
IERC4626 vaultTokenContract = IERC4626(token);
address _underlyingToken = vaultTokenContract.asset();
uint8 _vaultShareDecimal = vaultTokenContract.decimals();
IERC4626 vaultTokenContract = IERC4626(vaultToken);
TokenInterface underlyingTokenContract = TokenInterface(
_underlyingToken
vaultTokenContract.asset()
);
_shareAmt = _shareAmt == uint256(-1)
? vaultTokenContract.balanceOf(address(this))
: _shareAmt;
maxTokenPerShares = convertTo18(
_vaultShareDecimal,
wmul(maxTokenPerShares, _shareAmt)
);
// Returns final amount in token decimals.
uint256 _maxTokens = wmul(maxTokenPerShares, _shareAmt);
uint256 _approveUnderlyingTokenAmount = vaultTokenContract.previewMint(
uint256 _underlyingTokenAmount = vaultTokenContract.previewMint(
_shareAmt
);
uint256 _initalUnderlyingBal = IERC20(_underlyingToken).balanceOf(
uint256 _initalUnderlyingBal = underlyingTokenContract.balanceOf(
address(this)
);
approve(underlyingTokenContract, token, _approveUnderlyingTokenAmount);
approve(underlyingTokenContract, vaultToken, _underlyingTokenAmount);
// Mint shares for tokens
vaultTokenContract.mint(_shareAmt, address(this));
uint256 _finalUnderlyingBal = IERC20(_underlyingToken).balanceOf(
address(this)
uint256 _tokensDeducted = sub(
_initalUnderlyingBal,
underlyingTokenContract.balanceOf(address(this))
);
require(
maxTokenPerShares >= sub(_initalUnderlyingBal, _finalUnderlyingBal),
"maxTokenPerShares-exceeds"
);
require(_maxTokens >= _tokensDeducted, "maxTokenPerShares-exceeds");
setUint(setId, _shareAmt);
_eventName = "LogMint(address,uint256,uint256,uint256,uint256)";
_eventName = "LogMint(address,uint256,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(
token,
vaultToken,
_shareAmt,
maxTokenPerShares,
_tokensDeducted,
getId,
setId
);
@ -144,16 +138,15 @@ abstract contract BasicConnector is Events, DSMath, Basic {
/**
* @dev Withdraw underlying asset from ERC4626 Vault.
* @notice Withdraw vault shares with exactly amount of underlying assets
* @param token ERC4626 Token address.
* @param vaultToken ERC4626 Token address.
* @param underlyingAmt The amount of the token to withdraw. (For max: `uint256(-1)`)
* @param maxSharesPerToken The max share rate of withdrawn amount.
* @param maxSharesPerToken The max share rate of withdrawn amount. Always send in 18 decimals.
* @param to The address of receiver.
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens withdrawn.
*/
function withdraw(
address token,
address vaultToken,
uint256 underlyingAmt,
uint256 maxSharesPerToken,
address payable to,
@ -161,41 +154,36 @@ abstract contract BasicConnector is Events, DSMath, Basic {
uint256 setId
) public returns (string memory _eventName, bytes memory _eventParam) {
uint256 _underlyingAmt = getUint(getId, underlyingAmt);
IERC4626 vaultTokenContract = IERC4626(token);
address _underlyingToken = vaultTokenContract.asset();
uint8 _vaultShareDecimal = vaultTokenContract.decimals();
IERC4626 vaultTokenContract = IERC4626(vaultToken);
TokenInterface underlyingTokenContract = TokenInterface(
_underlyingToken
vaultTokenContract.asset()
);
_underlyingAmt = _underlyingAmt == uint256(-1)
? underlyingTokenContract.balanceOf(address(this))
: _underlyingAmt;
uint256 _maxShares = convert18ToDec(
_vaultShareDecimal,
wmul(maxSharesPerToken, _underlyingAmt)
);
// Returns final amount in token decimals.
uint256 _maxShares = wmul(maxSharesPerToken, _underlyingAmt);
uint256 _initialVaultBal = vaultTokenContract.balanceOf(to);
// Withdraw tokens for shares
vaultTokenContract.withdraw(_underlyingAmt, to, address(this));
uint256 _finalVaultBal = vaultTokenContract.balanceOf(to);
uint256 _sharesBurned = sub(_initialVaultBal, vaultTokenContract.balanceOf(to));
require(
_maxShares >= sub(_finalVaultBal, _initialVaultBal),
"maxShares-exceeds"
);
require(_maxShares >= _sharesBurned, "maxShares-exceeds");
setUint(setId, _underlyingAmt);
_eventName = "LogWithdraw(address,uint256,uint256,address,uint256,uint256)";
_eventName = "LogWithdraw(address,uint256,uint256,uint256,address,uint256,uint256)";
_eventParam = abi.encode(
token,
vaultToken,
_underlyingAmt,
maxSharesPerToken,
_sharesBurned,
to,
getId,
setId
@ -205,16 +193,16 @@ abstract contract BasicConnector is Events, DSMath, Basic {
/**
* @dev Redeem underlying asset from ERC4626 Vault.
* @notice Redeem vault shares with exactly amount of underlying assets
* @param token ERC4626 Token address.
* @param vaultToken ERC4626 Token address.
* @param shareAmt The amount of the token to redeem. (For max: `uint256(-1)`)
* @param minTokenPerShares The min underlying token rate of withdraw.
* @param minTokenPerShares The min underlying token rate of withdraw. Always in 18 decimals.
* @param to The address of receiver.
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens redeem.
*/
function redeem(
address token,
address vaultToken,
uint256 shareAmt,
uint256 minTokenPerShares,
address payable to,
@ -222,44 +210,39 @@ abstract contract BasicConnector is Events, DSMath, Basic {
uint256 setId
) public returns (string memory _eventName, bytes memory _eventParam) {
uint256 _shareAmt = getUint(getId, shareAmt);
IERC4626 vaultTokenContract = IERC4626(token);
address _underlyingToken = vaultTokenContract.asset();
uint8 _vaultShareDecimal = vaultTokenContract.decimals();
IERC4626 vaultTokenContract = IERC4626(vaultToken);
TokenInterface underlyingTokenContract = TokenInterface(
_underlyingToken
vaultTokenContract.asset()
);
_shareAmt = _shareAmt == uint256(-1)
? vaultTokenContract.balanceOf(address(this))
: _shareAmt;
uint256 _minUnderlyingAmt = convert18ToDec(
_vaultShareDecimal,
wmul(minTokenPerShares, _shareAmt)
);
uint256 _initalUnderlyingBal = IERC20(_underlyingToken).balanceOf(
to
);
// Returns final amount in token decimals.
uint256 _minUnderlyingAmt = wmul(minTokenPerShares, _shareAmt);
uint256 _initalUnderlyingBal = underlyingTokenContract.balanceOf(to);
// Redeem tokens for shares
vaultTokenContract.redeem(_shareAmt, to, address(this));
uint256 _finalUnderlyingBal = IERC20(_underlyingToken).balanceOf(
to
uint256 _underlyingAmtReceieved = sub(
underlyingTokenContract.balanceOf(to),
_initalUnderlyingBal
);
require(
_minUnderlyingAmt <= sub(_finalUnderlyingBal, _initalUnderlyingBal),
"_minUnderlyingAmt-exceeds"
);
require(_minUnderlyingAmt <= _underlyingAmtReceieved, "_minUnderlyingAmt-exceeds");
setUint(setId, _shareAmt);
_eventName = "LogRedeem(address,uint256,uint256,address,uint256,uint256)";
_eventName = "LogRedeem(address,uint256,uint256,uint256,address,uint256,uint256)";
_eventParam = abi.encode(
token,
vaultToken,
_shareAmt,
minTokenPerShares,
_underlyingAmtReceieved,
to,
getId,
setId
@ -269,4 +252,4 @@ abstract contract BasicConnector is Events, DSMath, Basic {
contract ConnectV2BasicERC4626 is BasicConnector {
string public constant name = "BASIC-ERC4626-v1.0";
}
}

View File

@ -57,7 +57,7 @@
"@types/node": "^16.11.11",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"ethereum-waffle": "^3.4.0",
"ethereum-waffle": "^4.0.10",
"ethers": "^5.5.2",
"hardhat": "^2.14.0",
"hardhat-deploy": "^0.9.14",

View File

@ -93,7 +93,7 @@ describe("BASIC-D", function () {
value: ethers.utils.parseEther("10")
});
let txRes = await daiContract.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseEther("10000"));
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"));
});
@ -108,15 +108,17 @@ describe("BASIC-D", function () {
// });
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);
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("beforebalance :>> ", beforebalance);
console.log("Share before balance :>> ", beforebalance.toString());
let spells = [
{
@ -130,7 +132,9 @@ describe("BASIC-D", function () {
let receipt = await tx.wait();
const afterbalance = await erc4626Contract.balanceOf(dsaWallet0.address);
console.log("afterbalance :>> ", afterbalance);
console.log("Share after balance :>> ", afterbalance.toString());
expect(afterbalance.sub(beforebalance)).to.be.lte(previewDeposit)
// In case of not satisfying min rate
minSharesPerToken = ethers.utils.parseUnits("1");
@ -146,11 +150,15 @@ describe("BASIC-D", function () {
});
it("should mint asset to ERC4626", async () => {
// const daiBalance = await daiContract.balanceOf(dsaWallet0.address);
// console.log("daiBalance :>> ", daiBalance);
const shares = ethers.utils.parseEther("1.03");
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("previewMint :>> ", previewMint);
console.log("Token amount preview Mint :>> ", previewMint.toString());
let maxTokenPerShares = ethers.utils.parseUnits("1.1");
@ -158,13 +166,18 @@ describe("BASIC-D", function () {
{
connector: connectorName,
method: "mint",
args: [sDAIaddress, previewMint, maxTokenPerShares, 0, 0]
args: [sDAIaddress, shares, maxTokenPerShares, 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());
// In case of not satisfying max rate
maxTokenPerShares = ethers.utils.parseUnits("1");
@ -172,26 +185,26 @@ describe("BASIC-D", function () {
{
connector: connectorName,
method: "mint",
args: [sDAIaddress, previewMint, maxTokenPerShares, 0, 0]
args: [sDAIaddress, shares, maxTokenPerShares, 0, 0]
}
];
await expect(dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address)).to.be.reverted;
});
it("should redeem asset to ERC4626", async () => {
// const shares = new BigNumber(1).toString()
// const balance = await erc4626Contract.balanceOf(dsaWallet0.address);
// console.log("balance :>> ", balance);
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);
console.log("maxRedeem :>> ", maxRedeem.toString());
const beforeUnderbalance = await daiContract.balanceOf(wallet0.address);
console.log("beforeUnderbalance :>> ", beforeUnderbalance);
const beforeUnderbalance = await daiContract.balanceOf(dsaWallet0.address);
console.log("beforeUnderbalance :>> ", beforeUnderbalance.toString());
const beforeVaultbalance = await erc4626Contract.balanceOf(wallet0.address);
console.log("beforeVaultbalance :>> ", beforeVaultbalance);
const beforeVaultbalance = await erc4626Contract.balanceOf(dsaWallet0.address);
console.log("beforeVaultbalance :>> ", beforeVaultbalance.toString());
let minTokenPerShares = ethers.utils.parseUnits("1.01");
@ -200,72 +213,101 @@ describe("BASIC-D", function () {
{
connector: connectorName,
method: "redeem",
args: [sDAIaddress, maxRedeem.div(2), minTokenPerShares, wallet0.address, 0, setId]
args: [sDAIaddress, ethers.constants.MaxUint256, minTokenPerShares, 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(wallet0.address);
console.log("afterUnderbalance :>> ", afterUnderbalance);
const afterUnderbalance = await daiContract.balanceOf(dsaWallet0.address);
console.log("afterUnderbalance :>> ", afterUnderbalance.toString());
const afterVaultbalance = await erc4626Contract.balanceOf(wallet0.address);
console.log("afterVaultbalance :>> ", afterVaultbalance);
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"), ethers.utils.parseUnits("0.95"), 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
minTokenPerShares = ethers.utils.parseUnits("1.2");
let minTokenPerShares = ethers.utils.parseUnits("1.5");
spells = [
{
connector: connectorName,
method: "redeem",
args: [sDAIaddress, maxRedeem.div(2), minTokenPerShares, wallet0.address, 0, setId]
args: [sDAIaddress, ethers.constants.MaxUint256, minTokenPerShares, 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);
console.log("maxWithdraw :>> ", maxWithdraw.toString());
const beforeUnderbalance = await daiContract.balanceOf(wallet0.address);
console.log("beforeUnderbalance :>> ", beforeUnderbalance);
const beforeUnderbalance = await daiContract.balanceOf(dsaWallet0.address);
console.log("beforeUnderbalance :>> ", beforeUnderbalance.toString());
const beforeVaultbalance = await erc4626Contract.balanceOf(wallet0.address);
console.log("beforeVaultbalance :>> ", beforeVaultbalance);
const beforeVaultbalance = await erc4626Contract.balanceOf(dsaWallet0.address);
console.log("beforeVaultbalance :>> ", beforeVaultbalance.toString());
let maxSharesPerToken = ethers.utils.parseUnits("0.95");
let maxSharesPerToken = ethers.utils.parseUnits("0.975");
const setId = "83478237";
let spells = [
{
connector: connectorName,
method: "withdraw",
args: [sDAIaddress, maxWithdraw, maxSharesPerToken, wallet0.address, 0, setId]
args: [sDAIaddress, maxWithdraw, maxSharesPerToken, 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(wallet0.address);
console.log("afterUnderbalance :>> ", afterUnderbalance);
const afterUnderbalance = await daiContract.balanceOf(dsaWallet0.address);
console.log("afterUnderbalance :>> ", afterUnderbalance.toString());
const afterVaultbalance = await erc4626Contract.balanceOf(wallet0.address);
console.log("afterVaultbalance :>> ", afterVaultbalance);
const afterVaultbalance = await erc4626Contract.balanceOf(dsaWallet0.address);
console.log("afterVaultbalance :>> ", afterVaultbalance.toString());
// In case of not satisfying min rate
maxSharesPerToken = ethers.utils.parseUnits("1");
maxSharesPerToken = ethers.utils.parseUnits("0.95");
spells = [
{
connector: connectorName,
method: "withdraw",
args: [sDAIaddress, maxWithdraw, maxSharesPerToken, wallet0.address, 0, setId]
args: [sDAIaddress, maxWithdraw, maxSharesPerToken, dsaWallet0.address, 0, setId]
}
];

9782
yarn.lock Normal file

File diff suppressed because it is too large Load Diff