diff --git a/contracts/ConnectGelatoDebtBridge.sol b/contracts/ConnectGelatoDebtBridge.sol index cbe706e..006b88b 100644 --- a/contracts/ConnectGelatoDebtBridge.sol +++ b/contracts/ConnectGelatoDebtBridge.sol @@ -356,19 +356,19 @@ contract ConnectGelatoDebtBridge is ConnectGelatoDebtBridgeResolver { fees = _mul(GASLIMIT, _getGasPrice()); uint256 debt = getMakerVaultDebt(_vaultID); - uint256 collateral = _sub( - _wmul(getMakerVaultCollateralBalance(_vaultID), latestPrice), - fees + uint256 collateral = _wmul( + _sub(getMakerVaultCollateralBalance(_vaultID), fees), + latestPrice ); - collateralToWithdraw = _wcollateralToWithdraw( + collateralToWithdraw = wcollateralToWithdraw( _vaultLiquidationRatio, _compPosLiquidationRatio, collateral, debt, latestPrice ); - paybackAmount = _wborrowedTokenToPayback( + paybackAmount = wborrowedTokenToPayback( _vaultLiquidationRatio, _compPosLiquidationRatio, collateral, @@ -452,13 +452,13 @@ contract ConnectGelatoDebtBridge is ConnectGelatoDebtBridgeResolver { /// @param _bor amount of borrowed token2 on protocol 1. /// @param _colPrice price of the collateral. /// @return collateral to withdraw in wad standard - function _wcollateralToWithdraw( + function wcollateralToWithdraw( uint256 _p1LiqRatio, uint256 _p2LiqRatio, uint256 _col, uint256 _bor, uint256 _colPrice - ) internal pure returns (uint256) { + ) public pure returns (uint256) { return _wdiv( _sub( @@ -482,12 +482,12 @@ contract ConnectGelatoDebtBridge is ConnectGelatoDebtBridgeResolver { /// @param _col token1 collateral to put on protocol 1. /// @param _bor amount of borrowed token2 on protocol 1. /// @return amount of borrowed token to pay back in wad standard - function _wborrowedTokenToPayback( + function wborrowedTokenToPayback( uint256 _p1LiqRatio, uint256 _p2LiqRatio, uint256 _col, uint256 _bor - ) internal pure returns (uint256) { + ) public pure returns (uint256) { return _sub( _bor, diff --git a/contracts/Mocks/ConnectGelatoDebtBridgeMock.sol b/contracts/Mocks/ConnectGelatoDebtBridgeMock.sol deleted file mode 100644 index 7848349..0000000 --- a/contracts/Mocks/ConnectGelatoDebtBridgeMock.sol +++ /dev/null @@ -1,37 +0,0 @@ -pragma solidity 0.6.12; -pragma experimental ABIEncoderV2; - -import "../ConnectGelatoDEbtBridge.sol"; - -contract ConnectGelatoDebtBridgeMock is ConnectGelatoDebtBridge { - constructor(uint256 _iD, address _oracleAggregator) - public - ConnectGelatoDebtBridge(_iD, _oracleAggregator) - {} - - function wcollateralToWithdraw( - uint256 _p1LiqRatio, - uint256 _p2LiqRatio, - uint256 _col, - uint256 _bor, - uint256 _colPrice - ) public pure returns (uint256) { - return - _wcollateralToWithdraw( - _p1LiqRatio, - _p2LiqRatio, - _col, - _bor, - _colPrice - ); - } - - function wborrowedTokenToPayback( - uint256 _p1LiqRatio, - uint256 _p2LiqRatio, - uint256 _col, - uint256 _bor - ) public pure returns (uint256) { - return _wborrowedTokenToPayback(_p1LiqRatio, _p2LiqRatio, _col, _bor); - } -} diff --git a/test/2_Debt-Bridge-External-Provider.test.js b/test/2_Debt-Bridge-External-Provider.test.js index 7a42621..4c38ea5 100644 --- a/test/2_Debt-Bridge-External-Provider.test.js +++ b/test/2_Debt-Bridge-External-Provider.test.js @@ -604,13 +604,15 @@ describe("Debt Bridge with External Provider", function () { } ); + let makerVaultInitialBorrow = ethers.utils.parseUnits("1000", 18); + await dsa.cast( [bre.network.config.ConnectMaker], [ await bre.run("abi-encode-withselector", { abi: ConnectMaker.abi, functionname: "borrow", - inputs: [cdpId, ethers.utils.parseUnits("1000", 18), 0, 0], + inputs: [cdpId, makerVaultInitialBorrow, 0, 0], }), ], userAddress @@ -844,13 +846,15 @@ describe("Debt Bridge with External Provider", function () { let latestPrice = await oracleAggregator.getMakerTokenPrice(currencyPair); let fees = ethers.utils - .parseUnits("2000000", 0) + .parseUnits(String(1933090 + 19331 * 2), 0) .mul(await gelatoGasPriceOracle.latestAnswer()); let debt = await connectGelatoDebtBridge.getMakerVaultDebt(cdpId); let collateral = wmul( - await connectGelatoDebtBridge.getMakerVaultCollateralBalance(cdpId), + (await connectGelatoDebtBridge.getMakerVaultCollateralBalance(cdpId)).sub( + fees + ), latestPrice - ).sub(fees); + ); let expectedColWithdrawAmount = wcollateralToWithdraw( wantedLiquidationRatioOnProtocol1, @@ -867,6 +871,8 @@ describe("Debt Bridge with External Provider", function () { debt ); + //console.log(String(wdiv(collateral.sub(wmul(expectedColWithdrawAmount, latestPrice).add(fees)),debt.sub(expectedBorAmountToPayBack)))); + //#endregion let providerBalanceBeforeExecution = await providerWallet.getBalance(); @@ -898,30 +904,61 @@ describe("Debt Bridge with External Provider", function () { .sub(await cEthToken.totalReserves()) .div(await cEthToken.totalSupply()); - expect( - expectedBorAmountToPayBack.sub( - compoundPosition[0].borrowBalanceStoredUser - ) - ).to.be.gt(ethers.utils.parseUnits("1", 0)); + // Estimated amount to borrowed token should be equal to the actual one read on compound contracts + expect(expectedBorAmountToPayBack).to.be.equal( + compoundPosition[0].borrowBalanceStoredUser + ); + + // Estimated amount of collateral should be equal to the actual one read on compound contracts expect( expectedColWithdrawAmount.sub( compoundPosition[1].balanceOfUser.mul(exchangeRateCethToEth) ) - ).to.be.gt(ethers.utils.parseUnits("1", 0)); + ).to.be.lt(ethers.utils.parseUnits("1", 12)); + + debt = await connectGelatoDebtBridge.getMakerVaultDebt(cdpId); + collateral = await connectGelatoDebtBridge.getMakerVaultCollateralBalance( + cdpId + ); // in Ether. + + // Total Borrowed Amount on both protocol should equal to the initial borrowed amount on maker vault. expect( - expectedBorAmountToPayBack.sub( + debt + .add(compoundPosition[0].borrowBalanceStoredUser) + .sub(makerVaultInitialBorrow) + ).to.be.lte(ethers.utils.parseUnits("1", 0)); + // Total Ether col on Maker and Compound (+ fees) should equal to the initial col on maker vault + expect( + compoundPosition[1].balanceOfUser + .mul(exchangeRateCethToEth) + .add(fees) + .add(collateral) + .sub(ethers.utils.parseEther("10")) + ).to.be.lt(ethers.utils.parseUnits("1", 12)); + + // Check Collaterization Ratio of Maker and Compound + expect( + wdiv( + wmul( + compoundPosition[1].balanceOfUser.mul(exchangeRateCethToEth), + latestPrice + ), compoundPosition[0].borrowBalanceStoredUser - ) - ).to.be.lt(ethers.utils.parseUnits("1", 16)); + ).sub(wantedLiquidationRatioOnProtocol2) + ).to.be.lt(ethers.utils.parseUnits("1", 12)); expect( - expectedColWithdrawAmount.sub( - compoundPosition[1].balanceOfUser.mul(exchangeRateCethToEth) - ) - ).to.be.lt(ethers.utils.parseUnits("1", 14)); + wdiv( + wmul( + collateral, + await oracleAggregator.getMakerTokenPrice(currencyPair) + ), + debt + ).sub(wantedLiquidationRatioOnProtocol1) + ).to.be.lt(ethers.utils.parseUnits("1", 1)); // DSA contain 1000 DAI expect(await daiToken.balanceOf(dsa.address)).to.be.equal( - ethers.utils.parseUnits("1000", 18) + makerVaultInitialBorrow ); //#endregion diff --git a/test/7_ConnectGelatoDebtBridge.test.js b/test/7_ConnectGelatoDebtBridge.test.js index 1074fb2..cda5b9e 100644 --- a/test/7_ConnectGelatoDebtBridge.test.js +++ b/test/7_ConnectGelatoDebtBridge.test.js @@ -26,7 +26,7 @@ describe("Gelato Debt Bridge Connector unit test", function () { let connectGelatoDebtBridge; before(async function () { const ConnectGelatoDebtBridge = await ethers.getContractFactory( - "ConnectGelatoDebtBridgeMock" + "ConnectGelatoDebtBridge" ); connectGelatoDebtBridge = await ConnectGelatoDebtBridge.deploy( 0,