Fix collateral calculation bug

This commit is contained in:
Shivva 2020-10-21 14:44:56 +02:00 committed by Luis Schliesske
parent d04f327159
commit 36b2a4f6be
4 changed files with 65 additions and 65 deletions

View File

@ -356,19 +356,19 @@ contract ConnectGelatoDebtBridge is ConnectGelatoDebtBridgeResolver {
fees = _mul(GASLIMIT, _getGasPrice()); fees = _mul(GASLIMIT, _getGasPrice());
uint256 debt = getMakerVaultDebt(_vaultID); uint256 debt = getMakerVaultDebt(_vaultID);
uint256 collateral = _sub( uint256 collateral = _wmul(
_wmul(getMakerVaultCollateralBalance(_vaultID), latestPrice), _sub(getMakerVaultCollateralBalance(_vaultID), fees),
fees latestPrice
); );
collateralToWithdraw = _wcollateralToWithdraw( collateralToWithdraw = wcollateralToWithdraw(
_vaultLiquidationRatio, _vaultLiquidationRatio,
_compPosLiquidationRatio, _compPosLiquidationRatio,
collateral, collateral,
debt, debt,
latestPrice latestPrice
); );
paybackAmount = _wborrowedTokenToPayback( paybackAmount = wborrowedTokenToPayback(
_vaultLiquidationRatio, _vaultLiquidationRatio,
_compPosLiquidationRatio, _compPosLiquidationRatio,
collateral, collateral,
@ -452,13 +452,13 @@ contract ConnectGelatoDebtBridge is ConnectGelatoDebtBridgeResolver {
/// @param _bor amount of borrowed token2 on protocol 1. /// @param _bor amount of borrowed token2 on protocol 1.
/// @param _colPrice price of the collateral. /// @param _colPrice price of the collateral.
/// @return collateral to withdraw in wad standard /// @return collateral to withdraw in wad standard
function _wcollateralToWithdraw( function wcollateralToWithdraw(
uint256 _p1LiqRatio, uint256 _p1LiqRatio,
uint256 _p2LiqRatio, uint256 _p2LiqRatio,
uint256 _col, uint256 _col,
uint256 _bor, uint256 _bor,
uint256 _colPrice uint256 _colPrice
) internal pure returns (uint256) { ) public pure returns (uint256) {
return return
_wdiv( _wdiv(
_sub( _sub(
@ -482,12 +482,12 @@ contract ConnectGelatoDebtBridge is ConnectGelatoDebtBridgeResolver {
/// @param _col token1 collateral to put on protocol 1. /// @param _col token1 collateral to put on protocol 1.
/// @param _bor amount of borrowed token2 on protocol 1. /// @param _bor amount of borrowed token2 on protocol 1.
/// @return amount of borrowed token to pay back in wad standard /// @return amount of borrowed token to pay back in wad standard
function _wborrowedTokenToPayback( function wborrowedTokenToPayback(
uint256 _p1LiqRatio, uint256 _p1LiqRatio,
uint256 _p2LiqRatio, uint256 _p2LiqRatio,
uint256 _col, uint256 _col,
uint256 _bor uint256 _bor
) internal pure returns (uint256) { ) public pure returns (uint256) {
return return
_sub( _sub(
_bor, _bor,

View File

@ -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);
}
}

View File

@ -604,13 +604,15 @@ describe("Debt Bridge with External Provider", function () {
} }
); );
let makerVaultInitialBorrow = ethers.utils.parseUnits("1000", 18);
await dsa.cast( await dsa.cast(
[bre.network.config.ConnectMaker], [bre.network.config.ConnectMaker],
[ [
await bre.run("abi-encode-withselector", { await bre.run("abi-encode-withselector", {
abi: ConnectMaker.abi, abi: ConnectMaker.abi,
functionname: "borrow", functionname: "borrow",
inputs: [cdpId, ethers.utils.parseUnits("1000", 18), 0, 0], inputs: [cdpId, makerVaultInitialBorrow, 0, 0],
}), }),
], ],
userAddress userAddress
@ -844,13 +846,15 @@ describe("Debt Bridge with External Provider", function () {
let latestPrice = await oracleAggregator.getMakerTokenPrice(currencyPair); let latestPrice = await oracleAggregator.getMakerTokenPrice(currencyPair);
let fees = ethers.utils let fees = ethers.utils
.parseUnits("2000000", 0) .parseUnits(String(1933090 + 19331 * 2), 0)
.mul(await gelatoGasPriceOracle.latestAnswer()); .mul(await gelatoGasPriceOracle.latestAnswer());
let debt = await connectGelatoDebtBridge.getMakerVaultDebt(cdpId); let debt = await connectGelatoDebtBridge.getMakerVaultDebt(cdpId);
let collateral = wmul( let collateral = wmul(
await connectGelatoDebtBridge.getMakerVaultCollateralBalance(cdpId), (await connectGelatoDebtBridge.getMakerVaultCollateralBalance(cdpId)).sub(
fees
),
latestPrice latestPrice
).sub(fees); );
let expectedColWithdrawAmount = wcollateralToWithdraw( let expectedColWithdrawAmount = wcollateralToWithdraw(
wantedLiquidationRatioOnProtocol1, wantedLiquidationRatioOnProtocol1,
@ -867,6 +871,8 @@ describe("Debt Bridge with External Provider", function () {
debt debt
); );
//console.log(String(wdiv(collateral.sub(wmul(expectedColWithdrawAmount, latestPrice).add(fees)),debt.sub(expectedBorAmountToPayBack))));
//#endregion //#endregion
let providerBalanceBeforeExecution = await providerWallet.getBalance(); let providerBalanceBeforeExecution = await providerWallet.getBalance();
@ -898,30 +904,61 @@ describe("Debt Bridge with External Provider", function () {
.sub(await cEthToken.totalReserves()) .sub(await cEthToken.totalReserves())
.div(await cEthToken.totalSupply()); .div(await cEthToken.totalSupply());
expect( // Estimated amount to borrowed token should be equal to the actual one read on compound contracts
expectedBorAmountToPayBack.sub( expect(expectedBorAmountToPayBack).to.be.equal(
compoundPosition[0].borrowBalanceStoredUser compoundPosition[0].borrowBalanceStoredUser
) );
).to.be.gt(ethers.utils.parseUnits("1", 0));
// Estimated amount of collateral should be equal to the actual one read on compound contracts
expect( expect(
expectedColWithdrawAmount.sub( expectedColWithdrawAmount.sub(
compoundPosition[1].balanceOfUser.mul(exchangeRateCethToEth) 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( 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 compoundPosition[0].borrowBalanceStoredUser
) ).sub(wantedLiquidationRatioOnProtocol2)
).to.be.lt(ethers.utils.parseUnits("1", 16)); ).to.be.lt(ethers.utils.parseUnits("1", 12));
expect( expect(
expectedColWithdrawAmount.sub( wdiv(
compoundPosition[1].balanceOfUser.mul(exchangeRateCethToEth) wmul(
) collateral,
).to.be.lt(ethers.utils.parseUnits("1", 14)); await oracleAggregator.getMakerTokenPrice(currencyPair)
),
debt
).sub(wantedLiquidationRatioOnProtocol1)
).to.be.lt(ethers.utils.parseUnits("1", 1));
// DSA contain 1000 DAI // DSA contain 1000 DAI
expect(await daiToken.balanceOf(dsa.address)).to.be.equal( expect(await daiToken.balanceOf(dsa.address)).to.be.equal(
ethers.utils.parseUnits("1000", 18) makerVaultInitialBorrow
); );
//#endregion //#endregion

View File

@ -26,7 +26,7 @@ describe("Gelato Debt Bridge Connector unit test", function () {
let connectGelatoDebtBridge; let connectGelatoDebtBridge;
before(async function () { before(async function () {
const ConnectGelatoDebtBridge = await ethers.getContractFactory( const ConnectGelatoDebtBridge = await ethers.getContractFactory(
"ConnectGelatoDebtBridgeMock" "ConnectGelatoDebtBridge"
); );
connectGelatoDebtBridge = await ConnectGelatoDebtBridge.deploy( connectGelatoDebtBridge = await ConnectGelatoDebtBridge.deploy(
0, 0,