code refactor

This commit is contained in:
Richa-iitr 2022-09-02 11:18:43 +05:30
parent eaf3a4cbce
commit 204c9f924e
3 changed files with 98 additions and 41 deletions

View File

@ -56,7 +56,7 @@ abstract contract Helpers is DSMath, Basic {
params.market != address(0) && params.token != address(0),
"invalid market/token address"
);
bool isEth = params.token == wethAddr;
bool isEth = params.token == ethAddr;
address token_ = isEth ? wethAddr : params.token;
TokenInterface tokenContract = TokenInterface(token_);

View File

@ -40,7 +40,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
"invalid market/token address"
);
bool isEth = token == ethAddr || token == wethAddr;
bool isEth = token == ethAddr;
address token_ = isEth ? wethAddr : token;
TokenInterface tokenContract = TokenInterface(token_);
@ -98,7 +98,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
);
require(to != address(0), "invalid to address");
bool isEth = token == ethAddr || token == wethAddr;
bool isEth = token == ethAddr;
address token_ = isEth ? wethAddr : token;
TokenInterface tokenContract = TokenInterface(token_);
@ -159,7 +159,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
);
require(to != address(0), "invalid to address");
bool isEth = token == ethAddr || token == wethAddr;
bool isEth = token == ethAddr;
address token_ = isEth ? wethAddr : token;
TokenInterface tokenContract = TokenInterface(token_);
@ -332,6 +332,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
*/
function borrow(
address market,
address token,
uint256 amt,
uint256 getId,
uint256 setId
@ -344,8 +345,9 @@ abstract contract CompoundV3Resolver is Events, Helpers {
require(market != address(0), "invalid market address");
bool isEth = token == ethAddr;
address token_ = getBaseToken(market);
bool isEth = token_ == wethAddr;
require(token == token_ || token == ethAddr, "invalid-token");
TokenInterface tokenContract = TokenInterface(token_);
@ -385,6 +387,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
*/
function borrowOnBehalf(
address market,
address token,
address to,
uint256 amt,
uint256 getId,
@ -394,10 +397,14 @@ abstract contract CompoundV3Resolver is Events, Helpers {
payable
returns (string memory eventName_, bytes memory eventParam_)
{
require(
token == ethAddr || token == getBaseToken(market),
"invalid-token"
);
(uint256 amt_, uint256 setId_) = _borrow(
BorrowWithdrawParams({
market: market,
token: getBaseToken(market),
token: token,
from: address(0),
to: to,
amt: amt,
@ -423,6 +430,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
address market,
address from,
address to,
address token,
uint256 amt,
uint256 getId,
uint256 setId
@ -431,10 +439,14 @@ abstract contract CompoundV3Resolver is Events, Helpers {
payable
returns (string memory eventName_, bytes memory eventParam_)
{
require(
token == ethAddr || token == getBaseToken(market),
"invalid-token"
);
(uint256 amt_, uint256 setId_) = _borrow(
BorrowWithdrawParams({
market: market,
token: getBaseToken(market),
token: token,
from: from,
to: to,
amt: amt,
@ -456,6 +468,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
*/
function payback(
address market,
address token,
uint256 amt,
uint256 getId,
uint256 setId
@ -467,8 +480,10 @@ abstract contract CompoundV3Resolver is Events, Helpers {
uint256 amt_ = getUint(getId, amt);
require(market != address(0), "invalid market address");
bool isEth = token == ethAddr;
address token_ = getBaseToken(market);
bool isEth = token_ == wethAddr;
require(token == token_ || token == ethAddr, "invalid-token");
TokenInterface tokenContract = TokenInterface(token_);
amt_ = amt_ == uint256(-1)
@ -503,6 +518,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
*/
function paybackOnBehalf(
address market,
address token,
address to,
uint256 amt,
uint256 getId,
@ -516,7 +532,9 @@ abstract contract CompoundV3Resolver is Events, Helpers {
require(market != address(0), "invalid market address");
address token_ = getBaseToken(market);
bool isEth = token_ == wethAddr;
bool isEth = token == ethAddr;
require(token == token_ || token == ethAddr, "invalid-token");
TokenInterface tokenContract = TokenInterface(token_);
amt_ = amt_ == uint256(-1)
@ -551,6 +569,7 @@ abstract contract CompoundV3Resolver is Events, Helpers {
*/
function paybackFromUsingManager(
address market,
address token,
address from,
address to,
uint256 amt,
@ -565,7 +584,9 @@ abstract contract CompoundV3Resolver is Events, Helpers {
require(market != address(0), "invalid market address");
address token_ = getBaseToken(market);
bool isEth = token_ == wethAddr;
bool isEth = token == ethAddr;
require(token == token_ || token == ethAddr, "invalid-token");
TokenInterface tokenContract = TokenInterface(token_);
amt_ = setAmt(market, token_, from, amt_, isEth, true);

View File

@ -272,12 +272,12 @@ describe("Compound III", function () {
{
connector: connectorName,
method: "borrow",
args: [market, amount, 0, 0]
args: [market, base, amount, 0, 0]
},
{
connector: connectorName,
method: "payback",
args: [market, ethers.utils.parseUnits("50", 6), 0, 0]
args: [market, base, ethers.utils.parseUnits("50", 6), 0, 0]
}
];
@ -316,7 +316,7 @@ describe("Compound III", function () {
{
connector: connectorName,
method: "paybackFromUsingManager",
args: [market, dsaWallet0.address, dsaWallet0.address, ethers.constants.MaxUint256, 0, 0]
args: [market, base, dsaWallet0.address, dsaWallet0.address, ethers.constants.MaxUint256, 0, 0]
}
];
@ -333,7 +333,7 @@ describe("Compound III", function () {
{
connector: connectorName,
method: "borrowOnBehalf",
args: [market, dsaWallet1.address, amount, 0, 0]
args: [market, base, dsaWallet1.address, amount, 0, 0]
}
];
@ -352,7 +352,7 @@ describe("Compound III", function () {
{
connector: connectorName,
method: "paybackOnBehalf",
args: [market, dsaWallet0.address, ethers.constants.MaxUint256, 0, 0]
args: [market, base, dsaWallet0.address, ethers.constants.MaxUint256, 0, 0]
}
];
@ -444,34 +444,69 @@ describe("Compound III", function () {
);
});
it("should buy collateral", async function () {
//deposit 10 usdc(base token) to dsa
await baseContract.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseUnits("10", 6));
console.log(await baseContract.connect(signer).balanceOf(dsaWallet0.address));
it("Should withdraw asset and transfer", async function () {
const spells1 = [
{
connector: connectorName,
method: "deposit",
args: [market, tokens.eth.address, ethers.utils.parseEther("3"), 0, 0]
}
];
//dsawallet0 --> collateral 0eth, balance 7eth
//dsaWallet1 --> balance 2eth coll: 5eth
//dsawallet0 --> collateral 0eth, balance 9eth 10usdc
//dsaWallet1 --> balance 2eth coll: 3eth
const amount = ethers.utils.parseEther("1");
const bal = await baseContract.connect(signer).balanceOf(dsaWallet0.address);
await baseContract.connect(wallet0).approve(market, bal);
const tx1 = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells1), wallet1.address);
let initialBal = await ethers.provider.getBalance(dsaWallet0.address);
const amount = ethers.utils.parseEther("2");
const spells = [
{
connector: connectorName,
method: "buyCollateral",
args: [market, tokens.link.address, dsaWallet0.address, amount, bal, 0, 0]
method: "withdrawOnBehalf",
args: [market, tokens.eth.address, dsaWallet0.address, amount, 0, 0]
}
];
//dsawallet0 --> collateral 0eth, balance 9eth
//dsaWallet1 --> balance 2eth coll: 3eth
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address);
const receipt = await tx.wait();
expect(new BigNumber(await linkContract.connect(signer).balanceOf(dsaWallet0.address)).toFixed()).to.be.gte(
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(initialBal.add(amount));
expect((await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.weth.address)).balance).to.be.gte(
ethers.utils.parseEther("1")
);
//dsawallet0 --> collateral 0eth, balance 9eth >1link
//dsaWallet1 --> balance 2eth coll: 3eth
});
//can buy only when target reserves not reached.
// it("should buy collateral", async function () {
// //deposit 10 usdc(base token) to dsa
// await baseContract.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseUnits("10", 6));
// console.log(await baseContract.connect(signer).balanceOf(dsaWallet0.address));
// //dsawallet0 --> collateral 0eth, balance 9eth 10usdc
// //dsaWallet1 --> balance 2eth coll: 3eth
// const amount = ethers.utils.parseUnits("1",6);
// const bal = await baseContract.connect(signer).balanceOf(dsaWallet0.address);
// const spells = [
// {
// connector: connectorName,
// method: "buyCollateral",
// args: [market, tokens.link.address, dsaWallet0.address, amount, bal, 0, 0]
// }
// ];
// const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address);
// const receipt = await tx.wait();
// expect(new BigNumber(await linkContract.connect(signer).balanceOf(dsaWallet0.address)).toFixed()).to.be.gte(
// ethers.utils.parseEther("1")
// );
// //dsawallet0 --> collateral 0eth, balance 9eth >1link
// //dsaWallet1 --> balance 2eth coll: 3eth
// });
it("should transfer eth from dsaWallet1 to dsaWallet0 position", async function () {
const spells = [
{
@ -494,27 +529,27 @@ describe("Compound III", function () {
//dsaWallet1 --> balance 2eth coll: 0eth
});
it("should deposit link from using manager", async function () {
it("should deposit eth from using manager", async function () {
await wallet0.sendTransaction({
to: dsaWallet0.address,
value: ethers.utils.parseEther("5")
});
const amount = ethers.utils.parseEther("1");
//approve market to access dsaWallet0
await baseContract.connect(dsa0Signer).approve(market, amount);
const spells = [
{
connector: connectorName,
method: "depositFromUsingManager",
args: [market, tokens.link.address, dsaWallet0.address, dsaWallet1.address, amount, 0, 0]
args: [market, tokens.eth.address, dsaWallet0.address, dsaWallet1.address, amount, 0, 0]
}
];
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address);
const tx = await dsaWallet2.connect(wallet0).cast(...encodeSpells(spells), wallet1.address);
const receipt = await tx.wait();
expect((await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.link.address)).balance).to.be.gte(
expect((await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.weth.address)).balance).to.be.gte(
ethers.utils.parseEther("1")
);
//dsawallet0 --> collateral 3eth, balance 9eth previous-1link
//dsaWallet1 --> balance 2eth coll: 0eth,1link
});
it("should borrow using manager", async function () {
@ -522,17 +557,16 @@ describe("Compound III", function () {
{
connector: connectorName,
method: "deposit",
args: [market, tokens.eth.address, ethers.utils.parseEther("6"), 0, 0]
args: [market, tokens.eth.address, ethers.utils.parseEther("5"), 0, 0]
}
];
const tx1 = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells1), wallet1.address);
const amount = ethers.utils.parseUnits("50", 6);
const spells = [
{
connector: connectorName,
method: "borrowFromUsingManager",
args: [market, dsaWallet0.address, dsaWallet1.address, amount, 0, 0]
args: [market, base, dsaWallet0.address, dsaWallet1.address, amount, 0, 0]
}
];
@ -550,6 +584,8 @@ describe("Compound III", function () {
});
it("should transferAsset using manager", async function () {
let bal1 = (await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.weth.address)).balance;
let bal0 = (await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance
const spells = [
{
connector: connectorName,