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), params.market != address(0) && params.token != address(0),
"invalid market/token address" "invalid market/token address"
); );
bool isEth = params.token == wethAddr; bool isEth = params.token == ethAddr;
address token_ = isEth ? wethAddr : params.token; address token_ = isEth ? wethAddr : params.token;
TokenInterface tokenContract = TokenInterface(token_); TokenInterface tokenContract = TokenInterface(token_);

View File

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

View File

@ -272,12 +272,12 @@ describe("Compound III", function () {
{ {
connector: connectorName, connector: connectorName,
method: "borrow", method: "borrow",
args: [market, amount, 0, 0] args: [market, base, amount, 0, 0]
}, },
{ {
connector: connectorName, connector: connectorName,
method: "payback", 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, connector: connectorName,
method: "paybackFromUsingManager", 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, connector: connectorName,
method: "borrowOnBehalf", 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, connector: connectorName,
method: "paybackOnBehalf", 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 () { it("Should withdraw asset and transfer", async function () {
//deposit 10 usdc(base token) to dsa const spells1 = [
await baseContract.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseUnits("10", 6)); {
console.log(await baseContract.connect(signer).balanceOf(dsaWallet0.address)); 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 const tx1 = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells1), wallet1.address);
//dsaWallet1 --> balance 2eth coll: 3eth let initialBal = await ethers.provider.getBalance(dsaWallet0.address);
const amount = ethers.utils.parseEther("1");
const bal = await baseContract.connect(signer).balanceOf(dsaWallet0.address); const amount = ethers.utils.parseEther("2");
await baseContract.connect(wallet0).approve(market, bal);
const spells = [ const spells = [
{ {
connector: connectorName, connector: connectorName,
method: "buyCollateral", method: "withdrawOnBehalf",
args: [market, tokens.link.address, dsaWallet0.address, amount, bal, 0, 0] 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 tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address);
const receipt = await tx.wait(); 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") 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 () { it("should transfer eth from dsaWallet1 to dsaWallet0 position", async function () {
const spells = [ const spells = [
{ {
@ -494,27 +529,27 @@ describe("Compound III", function () {
//dsaWallet1 --> balance 2eth coll: 0eth //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"); const amount = ethers.utils.parseEther("1");
//approve market to access dsaWallet0
await baseContract.connect(dsa0Signer).approve(market, amount); await baseContract.connect(dsa0Signer).approve(market, amount);
const spells = [ const spells = [
{ {
connector: connectorName, connector: connectorName,
method: "depositFromUsingManager", 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(); 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") ethers.utils.parseEther("1")
); );
//dsawallet0 --> collateral 3eth, balance 9eth previous-1link
//dsaWallet1 --> balance 2eth coll: 0eth,1link
}); });
it("should borrow using manager", async function () { it("should borrow using manager", async function () {
@ -522,17 +557,16 @@ describe("Compound III", function () {
{ {
connector: connectorName, connector: connectorName,
method: "deposit", 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 tx1 = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells1), wallet1.address);
const amount = ethers.utils.parseUnits("50", 6); const amount = ethers.utils.parseUnits("50", 6);
const spells = [ const spells = [
{ {
connector: connectorName, connector: connectorName,
method: "borrowFromUsingManager", 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 () { 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 = [ const spells = [
{ {
connector: connectorName, connector: connectorName,