diff --git a/contracts/mainnet/connectors/compound/v3/events.sol b/contracts/mainnet/connectors/compound/v3/events.sol index 0e6ea07c..a58462be 100644 --- a/contracts/mainnet/connectors/compound/v3/events.sol +++ b/contracts/mainnet/connectors/compound/v3/events.sol @@ -37,7 +37,7 @@ contract Events { uint256 setId ); - event LogWithdrawOnBehalfOf( + event LogWithdrawTo( address indexed market, address indexed token, address to, diff --git a/contracts/mainnet/connectors/compound/v3/helpers.sol b/contracts/mainnet/connectors/compound/v3/helpers.sol index 5ec28f8c..2a6770ae 100644 --- a/contracts/mainnet/connectors/compound/v3/helpers.sol +++ b/contracts/mainnet/connectors/compound/v3/helpers.sol @@ -198,7 +198,8 @@ abstract contract Helpers is DSMath, Basic { .userCollateral(src, token) .balance; } - amt = bal_ < allowance_ ? bal_ : allowance_; + if (action == ACTION.transfer) amt = bal_; + else amt = bal_ < allowance_ ? bal_ : allowance_; } if (src == address(this)) convertEthToWeth(isEth, TokenInterface(token), amt); @@ -220,8 +221,8 @@ abstract contract Helpers is DSMath, Basic { .userCollateral(src, token) .balance; } - - amt = bal_ < allowance_ ? bal_ : allowance_; + if (action == ACTION.transfer) amt = bal_; + else amt = bal_ < allowance_ ? bal_ : allowance_; } } return amt; diff --git a/contracts/mainnet/connectors/compound/v3/main.sol b/contracts/mainnet/connectors/compound/v3/main.sol index 92ef9992..653b9153 100644 --- a/contracts/mainnet/connectors/compound/v3/main.sol +++ b/contracts/mainnet/connectors/compound/v3/main.sol @@ -278,7 +278,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { }) ); - eventName_ = "LogWithdrawOnBehalf(address,address,address,uint256,uint256,uint256)"; + eventName_ = "LogWithdrawTo(address,address,address,uint256,uint256,uint256)"; eventParam_ = abi.encode(market, token, to, amt_, getId, setId_); } @@ -739,6 +739,7 @@ abstract contract CompoundV3Resolver is Events, Helpers { { uint256 amt_ = getUint(getId, amount); require(market != address(0), "invalid market address"); + require(dest != address(0), "invalid destination address"); bool isEth = token == ethAddr; address token_ = isEth ? wethAddr : token; diff --git a/test/mainnet/compound/compound.iii.test.ts b/test/mainnet/compound/compound.iii.test.ts index 5ea71421..264f538d 100644 --- a/test/mainnet/compound/compound.iii.test.ts +++ b/test/mainnet/compound/compound.iii.test.ts @@ -23,6 +23,7 @@ describe("Compound III", function () { const market = "0xc3d688B66703497DAA19211EEdff47f25384cdc3"; const base = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"; const account = "0x72a53cdbbcc1b9efa39c834a540550e23463aacb"; + const wethWhale = "0x1c11ba15939e1c16ec7ca1678df6160ea2063bc5"; const ABI = [ "function balanceOf(address account) public view returns (uint256)", @@ -147,6 +148,7 @@ describe("Compound III", function () { let instaConnectorsV2: Contract; let connector: any; let signer: any; + let wethSigner: any; const comet = new ethers.Contract(market, cometABI); @@ -184,6 +186,12 @@ describe("Compound III", function () { }); signer = await ethers.getSigner(account); + + await hre.network.provider.request({ + method: "hardhat_impersonateAccount", + params: [wethWhale] + }); + wethSigner = await ethers.getSigner(wethWhale); }); it("Should have contracts deployed.", async function () { @@ -413,7 +421,7 @@ describe("Compound III", function () { const spells = [ { connector: connectorName, - method: "withdrawOnBehalf", + method: "withdrawTo", args: [market, tokens.eth.address, dsaWallet0.address, amount, 0, 0] } ]; @@ -443,7 +451,7 @@ describe("Compound III", function () { const spells = [ { connector: connectorName, - method: "withdrawOnBehalf", + method: "withdrawTo", args: [market, tokens.eth.address, dsaWallet0.address, amount, 0, 0] } ]; @@ -476,14 +484,57 @@ describe("Compound III", function () { ); }); - it("should deposit eth from using manager", async function () { - await wallet0.sendTransaction({ - to: dsaWallet0.address, - value: ethers.utils.parseEther("5") - }); - console.log("balance wallet 0", await ethers.provider.getBalance(dsaWallet0.address)); + it("should transfer base token from dsaWallet1 to dsaWallet0 position", async function () { + await baseContract.connect(signer).transfer(dsaWallet1.address, ethers.utils.parseUnits("10", 6)); + + const spells = [ + { + connector: connectorName, + method: "deposit", + args: [market, base, ethers.constants.MaxUint256, 0, 0] + } + ]; + const tx = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + let initialBal= (await baseContract.connect(signer).balanceOf(dsaWallet1.address)); + console.log(initialBal.toString()); + let spells1 = [ + { + connector: connectorName, + method: "transferAsset", + args: [market, base, dsaWallet0.address, ethers.constants.MaxUint256, 0, 0] + } + ]; + + const tx1 = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells1), wallet1.address); + const receipt1 = await tx.wait(); + expect(await comet.connect(signer).balanceOf(dsaWallet1.address)).to.be.lte(ethers.utils.parseUnits("0", 6)); + expect(await comet.connect(signer).balanceOf(dsaWallet0.address)).to.be.gte(initialBal); + }); + + it("should transfer base token using manager from dsaWallet0 to dsaWallet1 position", async function () { + const spells = [ + { + connector: connectorName, + method: "transferAssetFromUsingManager", + args: [market, base, dsaWallet0.address, dsaWallet1.address, ethers.constants.MaxUint256, 0, 0] + } + ]; + let initialBal= (await baseContract.connect(signer).balanceOf(dsaWallet0.address)); + console.log(initialBal.toString()); + + const tx = await dsaWallet2.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect(await comet.connect(signer).balanceOf(dsaWallet0.address)).to.be.lte(ethers.utils.parseUnits("0", 6)); + expect(await comet.connect(signer).balanceOf(dsaWallet1.address)).to.be.gte(initialBal); + }); + + it("should deposit weth from using manager", async function () { + await wethContract.connect(wethSigner).transfer(dsaWallet0.address, ethers.utils.parseEther("10")); + let initialBal = await wethContract.connect(wallet0).balanceOf(dsaWallet0.address); + const amount = ethers.utils.parseEther("1"); - await baseContract.connect(dsa0Signer).approve(market, amount); + await wethContract.connect(dsa0Signer).approve(market, amount); const spells = [ { @@ -498,18 +549,44 @@ describe("Compound III", function () { expect((await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.weth.address)).balance).to.be.gte( ethers.utils.parseEther("1") ); + expect(await wethContract.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.lte(initialBal.sub(amount)); + }); + + it("should deposit eth from using manager same as 'from'", async function () { + const amount = ethers.utils.parseEther("1"); + await wethContract.connect(dsa0Signer).approve(market, amount); + let initialBal = await ethers.provider.getBalance(dsaWallet0.address); + + const spells = [ + { + connector: connectorName, + method: "depositFromUsingManager", + args: [market, tokens.eth.address, dsaWallet0.address, dsaWallet1.address, amount, 0, 0] + } + ]; + + const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); + const receipt = await tx.wait(); + expect((await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.weth.address)).balance).to.be.gte( + ethers.utils.parseEther("2") + ); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte(initialBal.sub(amount)); }); it("should borrow using manager", async function () { + await wallet0.sendTransaction({ + to: dsaWallet0.address, + value: ethers.utils.parseEther("5") + }); const spells1 = [ { connector: connectorName, method: "deposit", - args: [market, tokens.eth.address, ethers.utils.parseEther("5"), 0, 0] + args: [market, tokens.eth.address, ethers.utils.parseEther("3"), 0, 0] } ]; const tx1 = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells1), wallet1.address); - const amount = ethers.utils.parseUnits("50", 6); + const amount = ethers.utils.parseUnits("10", 6); const spells = [ { connector: connectorName, @@ -521,56 +598,11 @@ describe("Compound III", function () { const tx = await dsaWallet2.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); const receipt = await tx.wait(); expect(new BigNumber(await comet.connect(signer).borrowBalanceOf(dsaWallet0.address)).toFixed()).to.be.equal( - ethers.utils.parseUnits("50", 6) - ); - expect(await baseContract.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.equal( - ethers.utils.parseUnits("50", 6) - ); - }); - - it("should transfer base token from dsaWallet1 to dsaWallet0 position", async function () { - await baseContract.connect(signer).transfer(dsaWallet1.address, ethers.utils.parseUnits("10", 6)); - - const spells = [ - { - connector: connectorName, - method: "deposit", - args: [market, base, ethers.constants.MaxUint256, 0, 0] - }, - { - connector: connectorName, - method: "transferAsset", - args: [market, base, dsaWallet0.address, ethers.constants.MaxUint256, 0, 0] - } - ]; - - const tx = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells), wallet1.address); - const receipt = await tx.wait(); - expect((await comet.connect(signer).userCollateral(dsaWallet1.address, tokens.weth.address)).balance).to.be.lte( - ethers.utils.parseUnits("0", 6) - ); - expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( ethers.utils.parseUnits("10", 6) ); - }); - - it("should transfer base token using manager from dsaWallet0 to dsaWallet1 position", async function () { - const spells = [ - { - connector: connectorName, - method: "transferAssetFromUsingManager", - args: [market, base, dsaWallet0.address, dsaWallet1.address, ethers.constants.MaxUint256, 0, 0] - } - ]; - - 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.weth.address)).balance).to.be.lte( + expect(await baseContract.connect(wallet0).balanceOf(dsaWallet1.address)).to.be.equal( ethers.utils.parseUnits("10", 6) ); - expect((await comet.connect(signer).userCollateral(dsaWallet0.address, tokens.weth.address)).balance).to.be.gte( - ethers.utils.parseUnits("0", 6) - ); }); it("should transferAsset collateral using manager", async function () {