From 9fb2c4aaac9c2074a70879ca3a6b669a91071a0f Mon Sep 17 00:00:00 2001 From: Shivva Date: Tue, 27 Oct 2020 19:11:44 +0100 Subject: [PATCH] fix one wei discrepency who appear during debt reading from insta maker resolver --- .../ConnectGelatoDebtBridgeFromMaker.sol | 92 ++++++++++++------- .../2_Full-Debt-Bridge-Maker-Compound.test.js | 6 +- test/3_Full-Debt-Bridge-ETHA-ETHB.test.js | 10 +- 3 files changed, 65 insertions(+), 43 deletions(-) diff --git a/contracts/connectors/ConnectGelatoDebtBridgeFromMaker.sol b/contracts/connectors/ConnectGelatoDebtBridgeFromMaker.sol index ec8472e..323476c 100644 --- a/contracts/connectors/ConnectGelatoDebtBridgeFromMaker.sol +++ b/contracts/connectors/ConnectGelatoDebtBridgeFromMaker.sol @@ -20,22 +20,29 @@ interface GelatoGasPriceOracle { function latestAnswer() external view returns (int256); } -interface IMakerResolver { - struct VaultData { - uint256 id; - address owner; - string colType; - uint256 collateral; - uint256 art; - uint256 debt; - uint256 liquidatedCol; - uint256 borrowRate; - uint256 colPrice; - uint256 liquidationRatio; - address vaultAddress; - } +interface ManagerLike { + function ilks(uint256) external view returns (bytes32); - function getVaultById(uint256 id) external view returns (VaultData memory); + function urns(uint256) external view returns (address); + + function vat() external view returns (address); +} + +interface VatLike { + function ilks(bytes32) + external + view + returns ( + uint256, + uint256, + uint256, + uint256, + uint256 + ); + + function dai(address) external view returns (uint256); + + function urns(bytes32, address) external view returns (uint256, uint256); } library GelatoBytes { @@ -152,6 +159,13 @@ abstract contract DSMath { abstract contract Helpers is ConnectorInterface, DSMath { uint256 internal __id; + /** + * @dev Return Maker MCD Manager Address. + */ + function getMcdManager() internal pure returns (address) { + return 0x5ef30b9986345249bc32d8928B7ee64DE9435E39; + } + /** * @dev Return ethereum address */ @@ -226,16 +240,36 @@ abstract contract GelatoHelpers is Helpers { } abstract contract MakerResolver is GelatoHelpers { - function getMakerVault(uint256 _vaultId) - public + function _getVaultData(ManagerLike cdpManager, uint256 vault) + internal view - returns (IMakerResolver.VaultData memory) + returns (bytes32 ilk, address urn) { - return IMakerResolver(_getMakerResolver()).getVaultById(_vaultId); + ilk = cdpManager.ilks(vault); + urn = cdpManager.urns(vault); } - function getMakerVaultDebt(uint256 _vaultId) public view returns (uint256) { - return getMakerVault(_vaultId).debt; + function _getManager() internal pure returns (ManagerLike) { + return ManagerLike(getMcdManager()); + } + + function getMakerVaultDebt(uint256 _vaultId) + public + view + returns (uint256 wad) + { + ManagerLike cdpManager = _getManager(); + + (bytes32 ilk, address urn) = _getVaultData(cdpManager, _vaultId); + VatLike vat = VatLike(cdpManager.vat()); + (, uint256 rate, , , ) = VatLike(vat).ilks(ilk); + (, uint256 art) = VatLike(vat).urns(ilk, urn); + uint256 dai = VatLike(vat).dai(urn); + + uint256 rad = _sub(_mul(art, rate), dai); + wad = rad / RAY; + + wad = _mul(wad, RAY) < rad ? wad + 1 : wad; } function getMakerVaultCollateralBalance(uint256 _vaultId) @@ -243,19 +277,13 @@ abstract contract MakerResolver is GelatoHelpers { view returns (uint256) { - return getMakerVault(_vaultId).collateral; - } + ManagerLike cdpManager = _getManager(); - function getMakerVaultCollateralType(uint256 _vaultId) - public - view - returns (string memory) - { - return getMakerVault(_vaultId).colType; - } + VatLike vat = VatLike(cdpManager.vat()); + (bytes32 ilk, address urn) = _getVaultData(cdpManager, _vaultId); + (uint256 ink, ) = vat.urns(ilk, urn); - function _getMakerResolver() internal pure returns (address) { - return 0x0A7008B38E7015F8C36A49eEbc32513ECA8801E5; + return ink; } } diff --git a/test/2_Full-Debt-Bridge-Maker-Compound.test.js b/test/2_Full-Debt-Bridge-Maker-Compound.test.js index 6185a75..cfab42f 100644 --- a/test/2_Full-Debt-Bridge-Maker-Compound.test.js +++ b/test/2_Full-Debt-Bridge-Maker-Compound.test.js @@ -263,10 +263,8 @@ describe("Full Debt Bridge refinancing loan from Maker to Compound", function () // DSA contain 1000 DAI expect( - (await contracts.daiToken.balanceOf(contracts.dsa.address)).sub( - constants.MAKER_INITIAL_DEBT - ) - ).to.be.lte(ethers.utils.parseUnits("1", 0)); + await contracts.daiToken.balanceOf(contracts.dsa.address) + ).to.be.equal(constants.MAKER_INITIAL_DEBT); //#endregion }); diff --git a/test/3_Full-Debt-Bridge-ETHA-ETHB.test.js b/test/3_Full-Debt-Bridge-ETHA-ETHB.test.js index 3aba675..e26fb84 100644 --- a/test/3_Full-Debt-Bridge-ETHA-ETHB.test.js +++ b/test/3_Full-Debt-Bridge-ETHA-ETHB.test.js @@ -241,9 +241,7 @@ describe("Full Debt Bridge refinancing loan from ETH-A to ETH-B", function () { ); // Estimated amount to borrowed token should be equal to the actual one read on compound contracts - expect(debtOnMakerBefore.sub(debtOnMakerVaultB)).to.be.lte( - ethers.utils.parseUnits("1", 0) - ); + expect(debtOnMakerBefore).to.be.equal(debtOnMakerVaultB); // Estimated amount of collateral should be equal to the actual one read on compound contracts expect(pricedCollateral).to.be.equal(pricedCollateralOnVaultB); @@ -261,10 +259,8 @@ describe("Full Debt Bridge refinancing loan from ETH-A to ETH-B", function () { // DSA has maximum 2 wei DAI in it due to maths inaccuracies expect( - (await contracts.daiToken.balanceOf(contracts.dsa.address)).sub( - constants.MAKER_INITIAL_DEBT - ) - ).to.be.lte(ethers.utils.parseUnits("1", 0)); + await contracts.daiToken.balanceOf(contracts.dsa.address) + ).to.be.equal(constants.MAKER_INITIAL_DEBT); //#endregion });