From ca8375b21c8bdbc4ec871de34755715ade52bb02 Mon Sep 17 00:00:00 2001 From: Shriya Tyagi Date: Mon, 11 Sep 2023 19:16:21 +0400 Subject: [PATCH] feat: new updates --- contracts/mainnet/connectors/crv_USD/main.sol | 116 ++++++++++++------ test/mainnet/crv_usd/crv_usd.test.ts | 30 ++--- 2 files changed, 87 insertions(+), 59 deletions(-) diff --git a/contracts/mainnet/connectors/crv_USD/main.sol b/contracts/mainnet/connectors/crv_USD/main.sol index 3337486d..8be545a1 100644 --- a/contracts/mainnet/connectors/crv_USD/main.sol +++ b/contracts/mainnet/connectors/crv_USD/main.sol @@ -16,8 +16,8 @@ abstract contract CurveUSDResolver is Helpers, Events { * @dev Create loan * @dev If a user already has an existing loan, the function will revert. * @param collateral Collateral token address.(For ETH: `0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE`) - * @param amount Amount of collateral (For max: `uint256(-1)`) - * @param debt Stablecoin debt to take (For max: `uint256(-1)`) + * @param amt Amount of collateral (For max: `uint256(-1)`) + * @param debtAmt Stablecoin debt to take (For max: `uint256(-1)`) * @param numBands Number of bands to deposit into (to do autoliquidation-deliquidation), can only be from MIN_TICKS(4) to MAX_TICKS(50) * @param controllerVersion Controller version, * @param getId ID to retrieve amt. @@ -25,14 +25,14 @@ abstract contract CurveUSDResolver is Helpers, Events { */ function createLoan( address collateral, - uint256 amount, - uint256 debt, + uint256 amt, + uint256 debtAmt, uint256 numBands, uint256 controllerVersion, uint256 getId, uint256 setId ) external returns (string memory _eventName, bytes memory _eventParam) { - uint256 _amt = getUint(getId, amount); + uint256 _amt = getUint(getId, amt); bool _isEth = collateral == ethAddr; address _collateralAddress = _isEth ? wethAddr : collateral; @@ -50,13 +50,13 @@ abstract contract CurveUSDResolver is Helpers, Events { approve(collateralContract, address(controller), _amt); - uint256 _debt = debt == uint256(-1) ? controller.max_borrowable(_amt, numBands) : debt; + uint256 _debtAmt = debtAmt == uint256(-1) ? controller.max_borrowable(_amt, numBands) : debtAmt; - controller.create_loan(_amt, _debt, numBands); + controller.create_loan(_amt, _debtAmt, numBands); - setUint(setId, _debt); + setUint(setId, _debtAmt); _eventName = "LogCreateLoan(address,uint256,uint256,uint256,uint256,uin256,uin256)"; - _eventParam = abi.encode(collateral, _amt, debt, numBands, controllerVersion, getId, setId); + _eventParam = abi.encode(collateral, _amt, _debtAmt, numBands, controllerVersion, getId, setId); } /** @@ -75,16 +75,17 @@ abstract contract CurveUSDResolver is Helpers, Events { uint256 getId, uint256 setId ) external returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + bool _isEth = collateral == ethAddr; address _collateralAddress = _isEth ? wethAddr : collateral; + + // Get controller address of collateral. IController controller = getController(_collateralAddress, controllerVersion); TokenInterface collateralContract = TokenInterface(_collateralAddress); - uint _amt = getUint(getId, amt); - uint ethAmt; if (_isEth) { _amt = _amt == uint(-1) ? address(this).balance : _amt; - ethAmt = _amt; convertEthToWeth(_isEth, collateralContract, _amt); } else { _amt = _amt == uint(-1) ? collateralContract.balanceOf(address(this)) : _amt; @@ -94,6 +95,7 @@ abstract contract CurveUSDResolver is Helpers, Events { controller.add_collateral(_amt, address(this)); setUint(setId, _amt); + _eventName = "LogAddCollateral(address,uint256,uint256,uint256,uint256)"; _eventParam = abi.encode(collateral, amt, controllerVersion, getId, setId); } @@ -114,15 +116,17 @@ abstract contract CurveUSDResolver is Helpers, Events { uint256 getId, uint256 setId ) external returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + bool _isEth = collateral == ethAddr; address _collateralAddress = _isEth ? wethAddr : collateral; + IController controller = getController(_collateralAddress, controllerVersion); - uint _amt = getUint(getId, amt); - controller.remove_collateral(_amt, collateral == ethAddr); + // remove_collateral will unwrap the eth. + controller.remove_collateral(_amt, _isEth); setUint(setId, _amt); - _eventName = "LogRemoveCollateral(address,uint256,uint256,uint256,uint256)"; _eventParam = abi.encode(collateral, amt, controllerVersion, getId, setId); } @@ -130,30 +134,64 @@ abstract contract CurveUSDResolver is Helpers, Events { /** * @dev Borrow more stablecoins while adding more collateral (not necessary) * @param collateral Collateral token address.(For ETH: `0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE`) - * @param amt Collateral amount for borrow more (For max: `uint256(-1)`) - * @param debt Stablecoin debt to take for borrow more (For max: `uint256(-1)`) + * @param debtAmt Stablecoin debt to take for borrow more (For max: `uint256(-1)`) * @param controllerVersion controller version * @param getId ID to retrieve amt. * @param setId ID stores the amount of tokens deposited. */ function borrowMore( address collateral, - uint256 amt, - uint256 debt, + uint256 debtAmt, uint256 controllerVersion, uint256 getId, uint256 setId ) external returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, debtAmt); + bool _isEth = collateral == ethAddr; + address _collateralAddress = _isEth ? wethAddr : collateral; IController controller = getController(_collateralAddress, controllerVersion); + + uint256[4] memory res = controller.user_state(address(this)); + uint256 _debtAmt = debtAmt == uint(-1) + ? controller.max_borrowable(res[0], res[3]) - res[2] + : debtAmt; + + controller.borrow_more(_amt, _debtAmt); + + setUint(setId, _amt); + _eventName = "LogBorrowMore(address,uint256,uint256,uin256,uin256)"; + _eventParam = abi.encode(collateral, debtAmt, controllerVersion, getId, setId); + } + + /** + * @dev Borrow more stablecoins while adding more collateral (not necessary) + * @param collateral Collateral token address.(For ETH: `0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE`) + * @param colAmt Collateral amount for borrow more (For max: `uint256(-1)`) + * @param debtAmt Stablecoin debt to take for borrow more (For max: `uint256(-1)`) + * @param controllerVersion controller version + * @param getId ID to retrieve amt. + * @param setId ID stores the amount of tokens deposited. + */ + function addCollateralAndBorrowMore( + address collateral, + uint256 colAmt, + uint256 debtAmt, + uint256 controllerVersion, + uint256 getId, + uint256 setId + ) external returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, colAmt); + + bool _isEth = collateral == ethAddr; + address _collateralAddress = _isEth ? wethAddr : collateral; TokenInterface collateralContract = TokenInterface(_collateralAddress); - uint _amt = getUint(getId, amt); - uint ethAmt; + IController controller = getController(_collateralAddress, controllerVersion); + if (_isEth) { _amt = _amt == uint(-1) ? address(this).balance : _amt; - ethAmt = _amt; convertEthToWeth(_isEth, collateralContract, _amt); } else { _amt = _amt == uint(-1) ? collateralContract.balanceOf(address(this)) : _amt; @@ -162,20 +200,22 @@ abstract contract CurveUSDResolver is Helpers, Events { approve(collateralContract, address(controller), _amt); uint256[4] memory res = controller.user_state(address(this)); - uint256 _debt = debt == uint(-1) ? controller.max_borrowable(_amt + res[0], res[3]) - res[2] : debt; + uint256 _debtAmt = debtAmt == uint(-1) + ? controller.max_borrowable(_amt + res[0], res[3]) - res[2] + : debtAmt; - controller.borrow_more(_amt, _debt); + controller.borrow_more(_amt, _debtAmt); setUint(setId, _amt); - _eventName = "LogBorrowMore(address,uint256,uint256,uint256,uin256,uin256)"; - _eventParam = abi.encode(collateral, amt, debt, controllerVersion, getId, setId); + _eventName = "LogAddCollateralAndBorrowMore(address,uint256,uint256,uint256,uin256,uin256)"; + _eventParam = abi.encode(collateral, colAmt, debtAmt, controllerVersion, getId, setId); } /** * @dev Repay Curve-USD. * @param collateral Collateral token address.(For ETH: `0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE`) * @param amt repay amount (For max: `uint256(-1)`) - * @param controllerVersion Controller version, + * @param controllerVersion Controller version. * @param getId ID to retrieve amt. * @param setId ID stores the amount of debt borrowed. */ @@ -186,10 +226,11 @@ abstract contract CurveUSDResolver is Helpers, Events { uint256 getId, uint256 setId ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _amt = getUint(getId, amt); + bool _isEth = collateral == ethAddr; address _collateralAddress = _isEth ? wethAddr : collateral; IController controller = getController(_collateralAddress, controllerVersion); - uint _amt = getUint(getId, amt); TokenInterface stableCoin = TokenInterface(CRV_USD); _amt = _amt == uint(-1) ? stableCoin.balanceOf(address(this)) : _amt; @@ -206,31 +247,32 @@ abstract contract CurveUSDResolver is Helpers, Events { /** * @dev Peform a bad liquidation (or self-liquidation) of user if health is not good * @param collateral collateral token address - * @param min_x Minimal amount of stablecoin to receive (to avoid liquidators being sandwiched) - * @param controllerVersion controller version + * @param minReceiveAmt Minimal amount of stablecoin to receive (to avoid liquidators being sandwiched) + * @param controllerVersion controller version. * @param getId ID to retrieve amt. * @param setId ID stores the amount of debt borrowed. */ - function liquidate( + function selfLiquidate( address collateral, - uint256 min_x, + uint256 minReceiveAmt, uint256 controllerVersion, uint256 getId, uint256 setId ) external payable returns (string memory _eventName, bytes memory _eventParam) { + uint _minReceiveAmt = getUint(getId, minReceiveAmt); + bool _isEth = collateral == ethAddr; address _collateralAddress = _isEth ? wethAddr : collateral; IController controller = getController(_collateralAddress, controllerVersion); - uint _min_x = getUint(getId, min_x); TokenInterface stableCoin = TokenInterface(CRV_USD); - approve(stableCoin, address(controller), _min_x); + approve(stableCoin, address(controller), _minReceiveAmt); - controller.liquidate(address(this), _min_x, _isEth); + controller.liquidate(address(this), _minReceiveAmt, _isEth); - setUint(setId, _min_x); + setUint(setId, _minReceiveAmt); _eventName = "LogLiquidate(address,uint256,uint256,uint256,uint256)"; - _eventParam = abi.encode(collateral, _min_x, controllerVersion, getId, setId); + _eventParam = abi.encode(collateral, _minReceiveAmt, controllerVersion, getId, setId); } } diff --git a/test/mainnet/crv_usd/crv_usd.test.ts b/test/mainnet/crv_usd/crv_usd.test.ts index fab52797..039217cc 100644 --- a/test/mainnet/crv_usd/crv_usd.test.ts +++ b/test/mainnet/crv_usd/crv_usd.test.ts @@ -63,8 +63,6 @@ describe("CRV USD", function () { let signer: any; let sfrxSigner: any; - // const comet = new ethers.Contract(market, cometABI); - const wallets = provider.getWallets(); const [wallet0, wallet1, wallet2, wallet3] = wallets; @@ -99,20 +97,8 @@ describe("CRV USD", function () { }); signer = await ethers.getSigner(wst_whale); - - // await hre.network.provider.request({ - // method: "hardhat_impersonateAccount", - // params: [wethWhale] - // }); - // sfrxSigner = await ethers.getSigner(wethWhale); - - // const tmp = await ethers.getContractAt(ABI_Ctr, "0xa920de414ea4ab66b97da1bfe9e6eca7d4219635") - - // console.log("======1111111111111==========",(await tmp.max_borrowable("500000000000000000", 10)).toString()) - // await sfrxEth.connect(signer).approve(tmp.address, "999999999999999999999999999999") - // await tmp.connect(signer).create_loan("1000000000000000000", "50000000000000000000", "10") - // console.log("-----balance of CRV-USD-----", (await crvUSD.balanceOf(signer.address)).toString()) }); + it("Should have contracts deployed.", async function () { expect(!!instaConnectorsV2.address).to.be.true; expect(!!connector.address).to.be.true; @@ -185,7 +171,7 @@ describe("CRV USD", function () { }); it("add Collateral", async function () { - const balance = await sfrxEth.balanceOf(dsaWallet0.address) + const balanceBefore = await sfrxEth.balanceOf(dsaWallet0.address) const spells = [ { connector: connectorName, @@ -198,7 +184,7 @@ describe("CRV USD", function () { await tx.wait(); expect(await sfrxEth.balanceOf(dsaWallet0.address)).to.be.eq( - ethers.BigNumber.from(balance).sub(ethers.utils.parseEther('1')) + ethers.BigNumber.from(balanceBefore).sub(ethers.utils.parseEther('1')) ); }); @@ -226,7 +212,7 @@ describe("CRV USD", function () { { connector: connectorName, method: "borrowMore", - args: [tokens.sfrxeth.address, '0', ethers.utils.parseEther('50'), "1", 0, 0] + args: [tokens.sfrxeth.address, ethers.utils.parseEther('50'), "1", 0, 0] } ]; @@ -238,12 +224,12 @@ describe("CRV USD", function () { ); }); - it("borrow more with maximum value", async function () { + it("addCollateralAndBorrowMore with maximum value", async function () { const balance = await crvUSD.balanceOf(dsaWallet0.address) const spells = [ { connector: connectorName, - method: "borrowMore", + method: "addCollateralAndBorrowMore", args: [tokens.sfrxeth.address, ethers.utils.parseEther('2'), dsaMaxValue, 1, 0, 0] } ]; @@ -397,7 +383,7 @@ describe("CRV USD", function () { { connector: connectorName, method: "borrowMore", - args: [tokens.eth.address, '0', ethers.utils.parseEther('10'), 0, 0, 0] + args: [tokens.eth.address, ethers.utils.parseEther('10'), 0, 0, 0] } ]; @@ -415,7 +401,7 @@ describe("CRV USD", function () { { connector: connectorName, method: "borrowMore", - args: [tokens.eth.address, '0', dsaMaxValue, 0, 0, 0] + args: [tokens.eth.address, dsaMaxValue, 0, 0, 0] } ];