mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
feat/update
This commit is contained in:
parent
4b1bb60c95
commit
a6110bfa5b
|
@ -3,7 +3,7 @@ pragma solidity ^0.7.0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @title Basic D.
|
* @title Basic D.
|
||||||
* @dev Deposit & Mint & Withdraw & Redeem from DSA.
|
* @dev Deposit, Mint, Withdraw, & Redeem from ERC4626 DSA.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// import { IERC4626 } from "@openzeppelin/contracts-latest/interfaces/IERC4626.sol";
|
// import { IERC4626 } from "@openzeppelin/contracts-latest/interfaces/IERC4626.sol";
|
||||||
|
@ -13,10 +13,10 @@ import { DSMath } from "../../common/math.sol";
|
||||||
import { Basic } from "../../common/basic.sol";
|
import { Basic } from "../../common/basic.sol";
|
||||||
import { Events } from "./events.sol";
|
import { Events } from "./events.sol";
|
||||||
|
|
||||||
abstract contract BasicResolver is Events, DSMath, Basic {
|
abstract contract BasicConnector is Events, DSMath, Basic {
|
||||||
/**
|
/**
|
||||||
* @dev Deposit asset to ERC4626_Token.
|
* @dev Deposit underlying asset to ERC4626 Vault.
|
||||||
* @notice Deposit a token to DSA.
|
* @notice Mints vault shares by depositing exactly amount of underlying assets
|
||||||
* @param token ERC4626 Token address.
|
* @param token ERC4626 Token address.
|
||||||
* @param amt The amount of the token to deposit. (For max: `uint256(-1)`)
|
* @param amt The amount of the token to deposit. (For max: `uint256(-1)`)
|
||||||
* @param getId ID to retrieve amt.
|
* @param getId ID to retrieve amt.
|
||||||
|
@ -30,25 +30,24 @@ abstract contract BasicResolver is Events, DSMath, Basic {
|
||||||
uint256 setId
|
uint256 setId
|
||||||
) public returns (string memory _eventName, bytes memory _eventParam) {
|
) public returns (string memory _eventName, bytes memory _eventParam) {
|
||||||
uint256 _amt = getUint(getId, amt);
|
uint256 _amt = getUint(getId, amt);
|
||||||
IERC4626 tokenContract = IERC4626(token);
|
IERC4626 vaultTokenContract = IERC4626(token);
|
||||||
|
|
||||||
address assetToken = tokenContract.asset();
|
address _underlyingToken = vaultTokenContract.asset();
|
||||||
TokenInterface assetTokenContract = TokenInterface(assetToken);
|
TokenInterface underlyingTokenContract = TokenInterface(_underlyingToken);
|
||||||
|
|
||||||
_amt = _amt == uint256(-1) ? assetTokenContract.balanceOf(token) : _amt;
|
_amt = _amt == uint256(-1) ? underlyingTokenContract.balanceOf(address(this)) : _amt;
|
||||||
|
|
||||||
approve(assetTokenContract, token, _amt);
|
approve(underlyingTokenContract, token, _amt);
|
||||||
|
|
||||||
tokenContract.deposit(_amt, msg.sender);
|
vaultTokenContract.deposit(_amt, address(this));
|
||||||
setUint(setId, _amt);
|
setUint(setId, _amt);
|
||||||
|
|
||||||
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
|
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
|
||||||
_eventParam = abi.encode(token, _amt, getId, setId);
|
_eventParam = abi.encode(token, _amt, getId, setId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev Mint asset to ERC4626_Token.
|
* @dev Mint underlying asset to ERC4626 Vault.
|
||||||
* @notice Mint a token to DSA.
|
* @notice Mints vault shares by minting exactly amount of underlying assets
|
||||||
* @param token ERC4626 Token address.
|
* @param token ERC4626 Token address.
|
||||||
* @param amt The amount of the token to mint. (For max: `uint256(-1)`)
|
* @param amt The amount of the token to mint. (For max: `uint256(-1)`)
|
||||||
* @param getId ID to retrieve amt.
|
* @param getId ID to retrieve amt.
|
||||||
|
@ -62,25 +61,28 @@ abstract contract BasicResolver is Events, DSMath, Basic {
|
||||||
uint256 setId
|
uint256 setId
|
||||||
) public returns (string memory _eventName, bytes memory _eventParam) {
|
) public returns (string memory _eventName, bytes memory _eventParam) {
|
||||||
uint256 _amt = getUint(getId, amt);
|
uint256 _amt = getUint(getId, amt);
|
||||||
IERC4626 tokenContract = IERC4626(token);
|
IERC4626 vaultTokenContract = IERC4626(token);
|
||||||
|
|
||||||
address assetToken = tokenContract.asset();
|
address _underlyingToken = vaultTokenContract.asset();
|
||||||
TokenInterface assetTokenContract = TokenInterface(assetToken);
|
TokenInterface underlyingTokenContract = TokenInterface(_underlyingToken);
|
||||||
|
|
||||||
_amt = _amt == uint256(-1) ? assetTokenContract.balanceOf(token) : _amt;
|
_amt = _amt == uint256(-1) ? underlyingTokenContract.balanceOf(address(this)) : _amt;
|
||||||
|
|
||||||
approve(assetTokenContract, token, _amt);
|
uint256 _approveUnderlyingTokenAmount = vaultTokenContract.previewMint(_amt);
|
||||||
|
|
||||||
tokenContract.mint(_amt, msg.sender);
|
approve(underlyingTokenContract, token, _approveUnderlyingTokenAmount);
|
||||||
|
|
||||||
|
vaultTokenContract.mint(_amt, address(this));
|
||||||
setUint(setId, _amt);
|
setUint(setId, _amt);
|
||||||
|
|
||||||
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
|
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
|
||||||
_eventParam = abi.encode(token, _amt, getId, setId);
|
_eventParam = abi.encode(token, _amt, getId, setId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev Withdraw asset from ERC4626_Token.
|
* @dev Withdraw underlying asset from ERC4626 Vault.
|
||||||
* @notice Withdraw a token to DSA.
|
* @notice Withdraw vault shares with exactly amount of underlying assets
|
||||||
* @param token ERC4626 Token address.
|
* @param token ERC4626 Token address.
|
||||||
* @param amt The amount of the token to withdraw. (For max: `uint256(-1)`)
|
* @param amt The amount of the token to withdraw. (For max: `uint256(-1)`)
|
||||||
* @param to The address of receiver.
|
* @param to The address of receiver.
|
||||||
|
@ -96,30 +98,31 @@ abstract contract BasicResolver is Events, DSMath, Basic {
|
||||||
uint256 setId
|
uint256 setId
|
||||||
) public returns (string memory _eventName, bytes memory _eventParam) {
|
) public returns (string memory _eventName, bytes memory _eventParam) {
|
||||||
uint256 _amt = getUint(getId, amt);
|
uint256 _amt = getUint(getId, amt);
|
||||||
IERC4626 tokenContract = IERC4626(token);
|
IERC4626 vaultTokenContract = IERC4626(token);
|
||||||
|
|
||||||
address assetToken = tokenContract.asset();
|
address _underlyingToken = vaultTokenContract.asset();
|
||||||
TokenInterface assetTokenContract = TokenInterface(assetToken);
|
TokenInterface underlyingTokenContract = TokenInterface(_underlyingToken);
|
||||||
|
|
||||||
_amt = _amt == uint256(-1)
|
_amt = _amt == uint256(-1)
|
||||||
? assetTokenContract.balanceOf(msg.sender)
|
? underlyingTokenContract.balanceOf(address(this))
|
||||||
: _amt;
|
: _amt;
|
||||||
|
|
||||||
tokenContract.withdraw(_amt, to, msg.sender);
|
vaultTokenContract.withdraw(_amt, to, address(this));
|
||||||
setUint(setId, _amt);
|
setUint(setId, _amt);
|
||||||
|
|
||||||
_eventName = "LogWithdraw(address,uint256,address,uint256,uint256)";
|
_eventName = "LogWithdraw(address,uint256,address,uint256,uint256)";
|
||||||
_eventParam = abi.encode(token, _amt, to, getId, setId);
|
_eventParam = abi.encode(token, _amt, to, getId, setId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev Redeem asset from ERC4626_Token.
|
* @dev Redeem underlying asset from ERC4626 Vault.
|
||||||
* @notice Redeem a token to DSA.
|
* @notice Redeem vault shares with exactly amount of underlying assets
|
||||||
* @param token ERC4626 Token address.
|
* @param token ERC4626 Token address.
|
||||||
* @param amt The amount of the token to redeem. (For max: `uint256(-1)`)
|
* @param amt The amount of the token to redeem. (For max: `uint256(-1)`)
|
||||||
* @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 redeemed.
|
* @param setId ID stores the amount of tokens redeem.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function redeem(
|
function redeem(
|
||||||
|
@ -130,16 +133,16 @@ abstract contract BasicResolver is Events, DSMath, Basic {
|
||||||
uint256 setId
|
uint256 setId
|
||||||
) public returns (string memory _eventName, bytes memory _eventParam) {
|
) public returns (string memory _eventName, bytes memory _eventParam) {
|
||||||
uint256 _amt = getUint(getId, amt);
|
uint256 _amt = getUint(getId, amt);
|
||||||
IERC4626 tokenContract = IERC4626(token);
|
IERC4626 vaultTokenContract = IERC4626(token);
|
||||||
|
|
||||||
address assetToken = tokenContract.asset();
|
address _underlyingToken = vaultTokenContract.asset();
|
||||||
TokenInterface assetTokenContract = TokenInterface(assetToken);
|
TokenInterface underlyingTokenContract = TokenInterface(_underlyingToken);
|
||||||
|
|
||||||
_amt = _amt == uint256(-1)
|
_amt = _amt == uint256(-1)
|
||||||
? assetTokenContract.balanceOf(msg.sender)
|
? underlyingTokenContract.balanceOf(address(this))
|
||||||
: _amt;
|
: _amt;
|
||||||
|
|
||||||
tokenContract.redeem(_amt, to, msg.sender);
|
vaultTokenContract.redeem(_amt, to, address(this));
|
||||||
setUint(setId, _amt);
|
setUint(setId, _amt);
|
||||||
|
|
||||||
_eventName = "LogWithdraw(address,uint256,address,uint256,uint256)";
|
_eventName = "LogWithdraw(address,uint256,address,uint256,uint256)";
|
||||||
|
@ -147,6 +150,6 @@ abstract contract BasicResolver is Events, DSMath, Basic {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
contract ConnectV2BasicERC4626 is BasicResolver {
|
contract ConnectV2BasicERC4626 is BasicConnector {
|
||||||
string public constant name = "BASIC-ERC4626-v1.0";
|
string public constant name = "BASIC-ERC4626-v1.0";
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ describe("BASIC-D", function () {
|
||||||
let signer: any;
|
let signer: any;
|
||||||
|
|
||||||
const daiContract = new ethers.Contract(tokens.dai.address, IERC20Minimal__factory.abi, ethers.provider);
|
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 erc4626Contract = new ethers.Contract(sDAIaddress, IERC4626__factory.abi, ethers.provider);
|
||||||
|
|
||||||
const wallets = provider.getWallets();
|
const wallets = provider.getWallets();
|
||||||
const [wallet0] = wallets;
|
const [wallet0] = wallets;
|
||||||
|
@ -107,17 +107,78 @@ describe("BASIC-D", function () {
|
||||||
|
|
||||||
describe("Main", function () {
|
describe("Main", function () {
|
||||||
it("should deposit asset to ERC4626", async () => {
|
it("should deposit asset to ERC4626", async () => {
|
||||||
|
const assets = ethers.utils.parseEther("1");
|
||||||
|
const previewDeposit = await erc4626Contract.previewDeposit(assets);
|
||||||
|
console.log("previewDeposit :>> ", previewDeposit);
|
||||||
|
|
||||||
|
const maxDeposit = await erc4626Contract.maxDeposit(dsaWallet0.address);
|
||||||
|
console.log("maxDeposit :>> ", maxDeposit);
|
||||||
|
// const maxMint = await erc4626Contract.maxMint();
|
||||||
|
// console.log("maxMint :>> ", maxMint);
|
||||||
|
|
||||||
const spells = [
|
const spells = [
|
||||||
{
|
{
|
||||||
connector: connectorName,
|
connector: connectorName,
|
||||||
method: "deposit",
|
method: "deposit",
|
||||||
args: [sDAIaddress, ethers.utils.parseEther("1"), 0, 0]
|
args: [sDAIaddress, assets, 0, 0]
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address);
|
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address);
|
||||||
const receipt = await tx.wait();
|
const receipt = await tx.wait();
|
||||||
});
|
});
|
||||||
|
it("should mint asset to ERC4626", async () => {
|
||||||
|
const daiBalance = await daiContract.balanceOf(dsaWallet0.address);
|
||||||
|
console.log("daiBalance :>> ", daiBalance);
|
||||||
|
const shares = ethers.utils.parseEther("1");
|
||||||
|
const previewMint = await erc4626Contract.previewMint(shares);
|
||||||
|
console.log("previewMint :>> ", previewMint);
|
||||||
|
const spells = [
|
||||||
|
{
|
||||||
|
connector: connectorName,
|
||||||
|
method: "mint",
|
||||||
|
args: [sDAIaddress, previewMint, 0, 0]
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address);
|
||||||
|
const receipt = await tx.wait();
|
||||||
|
});
|
||||||
|
it("should redeem asset to ERC4626", async () => {
|
||||||
|
// const shares = new BigNumber(1).toString()
|
||||||
|
const balance = await erc4626Contract.balanceOf(dsaWallet0.address);
|
||||||
|
console.log("balance :>> ", balance);
|
||||||
|
|
||||||
|
const maxRedeem: BigNumber = await erc4626Contract.maxRedeem(dsaWallet0.address);
|
||||||
|
console.log("maxRedeem :>> ", maxRedeem);
|
||||||
|
|
||||||
|
const setId = "83478237";
|
||||||
|
const spells = [
|
||||||
|
{
|
||||||
|
connector: connectorName,
|
||||||
|
method: "redeem",
|
||||||
|
args: [sDAIaddress, maxRedeem.div(2), wallet0.address, 0, setId]
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address);
|
||||||
|
const receipt = await tx.wait();
|
||||||
|
});
|
||||||
|
it("should withdraw asset to ERC4626", async () => {
|
||||||
|
const maxWithdraw = await erc4626Contract.maxWithdraw(dsaWallet0.address);
|
||||||
|
console.log("maxWithdraw :>> ", maxWithdraw);
|
||||||
|
|
||||||
|
const setId = "83478237";
|
||||||
|
const spells = [
|
||||||
|
{
|
||||||
|
connector: connectorName,
|
||||||
|
method: "withdraw",
|
||||||
|
args: [sDAIaddress, maxWithdraw, wallet0.address, 0, setId]
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address);
|
||||||
|
const receipt = await tx.wait();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user