mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
feat/update core erc4626-connector & testscript
This commit is contained in:
parent
b791989836
commit
4b1bb60c95
|
@ -6,17 +6,18 @@ pragma solidity ^0.7.0;
|
||||||
* @dev Deposit & Mint & Withdraw & Redeem from DSA.
|
* @dev Deposit & Mint & Withdraw & Redeem from DSA.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
|
// import { IERC4626 } from "@openzeppelin/contracts-latest/interfaces/IERC4626.sol";
|
||||||
import { IERC4626 } from "./interface.sol";
|
import { IERC4626 } from "./interface.sol";
|
||||||
|
import { TokenInterface } from "../../common/interfaces.sol";
|
||||||
import { DSMath } from "../../common/math.sol";
|
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 BasicResolver is Events, DSMath, Basic {
|
||||||
/**
|
/**
|
||||||
* @dev Deposit ERC4626_Token asset.
|
* @dev Deposit asset to ERC4626_Token.
|
||||||
* @notice Deposit a token to DSA.
|
* @notice Deposit a token to DSA.
|
||||||
* @param token The address of the token to deposit.
|
* @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.
|
||||||
* @param setId ID stores the amount of tokens deposited.
|
* @param setId ID stores the amount of tokens deposited.
|
||||||
|
@ -27,23 +28,18 @@ abstract contract BasicResolver is Events, DSMath, Basic {
|
||||||
uint256 amt,
|
uint256 amt,
|
||||||
uint256 getId,
|
uint256 getId,
|
||||||
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 tokenContract = IERC4626(token);
|
||||||
|
|
||||||
_amt = _amt == uint256(-1)
|
address assetToken = tokenContract.asset();
|
||||||
? tokenContract.balanceOf(address(this))
|
TokenInterface assetTokenContract = TokenInterface(assetToken);
|
||||||
: _amt;
|
|
||||||
|
|
||||||
try tokenContract.approve(tokenContract.asset(), _amt) {} catch {
|
_amt = _amt == uint256(-1) ? assetTokenContract.balanceOf(token) : _amt;
|
||||||
tokenContract.approve(tokenContract.asset(), 0);
|
|
||||||
tokenContract.approve(tokenContract.asset(), _amt);
|
approve(assetTokenContract, token, _amt);
|
||||||
}
|
|
||||||
// approve(tokenContract, tokenContract.asset(), _amt);
|
tokenContract.deposit(_amt, msg.sender);
|
||||||
tokenContract.deposit(_amt, address(this));
|
|
||||||
setUint(setId, _amt);
|
setUint(setId, _amt);
|
||||||
|
|
||||||
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
|
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
|
||||||
|
@ -51,9 +47,9 @@ abstract contract BasicResolver is Events, DSMath, Basic {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev Mint ERC4626_Token share.
|
* @dev Mint asset to ERC4626_Token.
|
||||||
* @notice Mint a token to DSA.
|
* @notice Mint a token to DSA.
|
||||||
* @param token The address of the token to mint.
|
* @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.
|
||||||
* @param setId ID stores the amount of tokens minted.
|
* @param setId ID stores the amount of tokens minted.
|
||||||
|
@ -68,16 +64,14 @@ abstract contract BasicResolver is Events, DSMath, Basic {
|
||||||
uint256 _amt = getUint(getId, amt);
|
uint256 _amt = getUint(getId, amt);
|
||||||
IERC4626 tokenContract = IERC4626(token);
|
IERC4626 tokenContract = IERC4626(token);
|
||||||
|
|
||||||
_amt = _amt == uint256(-1)
|
address assetToken = tokenContract.asset();
|
||||||
? tokenContract.balanceOf(address(this))
|
TokenInterface assetTokenContract = TokenInterface(assetToken);
|
||||||
: _amt;
|
|
||||||
|
|
||||||
try tokenContract.approve(tokenContract.asset(), _amt) {} catch {
|
_amt = _amt == uint256(-1) ? assetTokenContract.balanceOf(token) : _amt;
|
||||||
tokenContract.approve(tokenContract.asset(), 0);
|
|
||||||
tokenContract.approve(tokenContract.asset(), _amt);
|
approve(assetTokenContract, token, _amt);
|
||||||
}
|
|
||||||
// approve(tokenContract, tokenContract.asset(), _amt);
|
tokenContract.mint(_amt, msg.sender);
|
||||||
tokenContract.mint(_amt, address(this));
|
|
||||||
setUint(setId, _amt);
|
setUint(setId, _amt);
|
||||||
|
|
||||||
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
|
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
|
||||||
|
@ -85,10 +79,11 @@ abstract contract BasicResolver is Events, DSMath, Basic {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev Withdraw ERC4626_Token.
|
* @dev Withdraw asset from ERC4626_Token.
|
||||||
* @notice Withdraw a token to DSA.
|
* @notice Withdraw a token to DSA.
|
||||||
* @param token The address of the token to withdraw.
|
* @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 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.
|
||||||
*/
|
*/
|
||||||
|
@ -99,18 +94,18 @@ abstract contract BasicResolver is Events, DSMath, Basic {
|
||||||
address payable to,
|
address payable to,
|
||||||
uint256 getId,
|
uint256 getId,
|
||||||
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 tokenContract = IERC4626(token);
|
||||||
|
|
||||||
|
address assetToken = tokenContract.asset();
|
||||||
|
TokenInterface assetTokenContract = TokenInterface(assetToken);
|
||||||
|
|
||||||
_amt = _amt == uint256(-1)
|
_amt = _amt == uint256(-1)
|
||||||
? tokenContract.balanceOf(address(this))
|
? assetTokenContract.balanceOf(msg.sender)
|
||||||
: _amt;
|
: _amt;
|
||||||
|
|
||||||
tokenContract.withdraw(_amt, to, address(this));
|
tokenContract.withdraw(_amt, to, msg.sender);
|
||||||
setUint(setId, _amt);
|
setUint(setId, _amt);
|
||||||
|
|
||||||
_eventName = "LogWithdraw(address,uint256,address,uint256,uint256)";
|
_eventName = "LogWithdraw(address,uint256,address,uint256,uint256)";
|
||||||
|
@ -118,10 +113,11 @@ abstract contract BasicResolver is Events, DSMath, Basic {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev Redeem ERC4626_Token.
|
* @dev Redeem asset from ERC4626_Token.
|
||||||
* @notice Reddem a token to DSA.
|
* @notice Redeem a token to DSA.
|
||||||
* @param token The address of the token to redeem.
|
* @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 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 redeemed.
|
||||||
*/
|
*/
|
||||||
|
@ -132,21 +128,21 @@ abstract contract BasicResolver is Events, DSMath, Basic {
|
||||||
address payable to,
|
address payable to,
|
||||||
uint256 getId,
|
uint256 getId,
|
||||||
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 tokenContract = IERC4626(token);
|
||||||
|
|
||||||
|
address assetToken = tokenContract.asset();
|
||||||
|
TokenInterface assetTokenContract = TokenInterface(assetToken);
|
||||||
|
|
||||||
_amt = _amt == uint256(-1)
|
_amt = _amt == uint256(-1)
|
||||||
? tokenContract.balanceOf(address(this))
|
? assetTokenContract.balanceOf(msg.sender)
|
||||||
: _amt;
|
: _amt;
|
||||||
|
|
||||||
tokenContract.redeem(_amt, to, address(this));
|
tokenContract.redeem(_amt, to, msg.sender);
|
||||||
setUint(setId, _amt);
|
setUint(setId, _amt);
|
||||||
|
|
||||||
_eventName = "LogRedeem(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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,7 @@ export const connectors: Record<string, Array<string>> = {
|
||||||
"AAVE-V2-A",
|
"AAVE-V2-A",
|
||||||
"AUTHORITY-A",
|
"AUTHORITY-A",
|
||||||
"BASIC-A",
|
"BASIC-A",
|
||||||
|
"BASIC-D",
|
||||||
"COMP-A",
|
"COMP-A",
|
||||||
"COMPOUND-A",
|
"COMPOUND-A",
|
||||||
"DYDX-A",
|
"DYDX-A",
|
||||||
|
|
123
test/mainnet/basic_ERC4626/ERC4626.ts
Normal file
123
test/mainnet/basic_ERC4626/ERC4626.ts
Normal file
|
@ -0,0 +1,123 @@
|
||||||
|
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 { ConnectV2BasicERC4626__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: 12796965
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
masterSigner = await getMasterSigner();
|
||||||
|
instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2);
|
||||||
|
connector = await deployAndEnableConnector({
|
||||||
|
connectorName,
|
||||||
|
contractArtifact: ConnectV2BasicERC4626__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")
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10"));
|
||||||
|
|
||||||
|
let balance = await ethers.provider.getBalance(account);
|
||||||
|
console.log("1:", balance);
|
||||||
|
balance = await daiContract.balanceOf(account);
|
||||||
|
console.log("2:", balance);
|
||||||
|
let txRes = await daiContract.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseEther("10000"));
|
||||||
|
await txRes.wait();
|
||||||
|
expect(await daiContract.balanceOf(dsaWallet0.address)).to.be.eq(ethers.utils.parseEther("10000"));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("Main", function () {
|
||||||
|
it("should deposit asset to ERC4626", async () => {
|
||||||
|
const spells = [
|
||||||
|
{
|
||||||
|
connector: connectorName,
|
||||||
|
method: "deposit",
|
||||||
|
args: [sDAIaddress, ethers.utils.parseEther("1"), 0, 0]
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address);
|
||||||
|
const receipt = await tx.wait();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user