From dc108bf05245a4fdb4f5b4e62d38e3c454c5feed Mon Sep 17 00:00:00 2001 From: Edward Mulraney Date: Fri, 11 Jun 2021 13:10:29 +0100 Subject: [PATCH] add system state getter to make it easier for instadapp to display required information --- contracts/protocols/mainnet/liquity.sol | 35 ++++++++++++++++++++++--- test/liquity.js | 14 ++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/contracts/protocols/mainnet/liquity.sol b/contracts/protocols/mainnet/liquity.sol index 3dfec37..fef0dc0 100644 --- a/contracts/protocols/mainnet/liquity.sol +++ b/contracts/protocols/mainnet/liquity.sol @@ -2,7 +2,10 @@ pragma solidity ^0.6.0; pragma experimental ABIEncoderV2; interface TroveManagerLike { + function getBorrowingRateWithDecay() external view returns (uint); + function getTCR(uint _price) external view returns (uint); function getCurrentICR(address _borrower, uint _price) external view returns (uint); + function checkRecoveryMode(uint _price) external view returns (bool); function getEntireDebtAndColl(address _borrower) external view returns ( uint debt, uint coll, @@ -27,7 +30,13 @@ abstract contract PriceFeedLike { uint public lastGoodPrice; } -contract Helpers { +contract DSMath { + function add(uint x, uint y) internal pure returns (uint z) { + require((z = x + y) >= x, "math-not-safe"); + } +} + +contract Helpers is DSMath { TroveManagerLike internal constant troveManager = TroveManagerLike(0xA39739EF8b0231DbFA0DcdA07d7e29faAbCf4bb2); @@ -40,6 +49,10 @@ contract Helpers { PriceFeedLike internal constant priceFeed = PriceFeedLike(0x4c517D4e2C851CA76d7eC94B805269Df0f2201De); + address constant activePoolAddress = 0xDf9Eb223bAFBE5c5271415C75aeCD68C21fE3D7F; + + address constant defaultPoolAddress = 0x896a3F03176f05CFbb4f006BfCd8723F2B0D741C; + struct Trove { uint collateral; uint debt; @@ -63,14 +76,21 @@ contract Helpers { StabilityDeposit stability; Stake stake; } + + struct System { + uint borrowFee; + uint ethTvl; + uint tcr; + bool isInRecoveryMode; + } } contract Resolver is Helpers { function getTrove(address owner) public view returns (Trove memory) { (uint debt, uint collateral, uint _, uint __) = troveManager.getEntireDebtAndColl(owner); - uint price = priceFeed.lastGoodPrice(); - uint icr = troveManager.getCurrentICR(owner, price); + uint ethPrice = priceFeed.lastGoodPrice(); + uint icr = troveManager.getCurrentICR(owner, ethPrice); return Trove(collateral, debt, icr); } @@ -94,6 +114,15 @@ contract Resolver is Helpers { Stake memory stake = getStake(owner); return Position(trove, stability, stake); } + + function getSystemState() external view returns (System memory) { + uint borrowFee = troveManager.getBorrowingRateWithDecay(); + uint ethTvl = add(activePoolAddress.balance, defaultPoolAddress.balance); + uint ethPrice = priceFeed.lastGoodPrice(); + uint tcr = troveManager.getTCR(ethPrice); + bool isInRecoveryMode = troveManager.checkRecoveryMode(ethPrice); + return System(borrowFee, ethTvl, tcr, isInRecoveryMode); + } } contract InstaLiquityResolver is Resolver { diff --git a/test/liquity.js b/test/liquity.js index 5f76158..f0f85f0 100644 --- a/test/liquity.js +++ b/test/liquity.js @@ -24,6 +24,13 @@ const expectedStakePosition = [ /* ethGain */ BigNumber.from("18910541408996344243"), /* lusdGain */ BigNumber.from("66201062534511228032281"), ]; + +const expectedSystemState = [ + /* borrowFee */ BigNumber.from("6900285109012952"), + /* ethTvl */ BigNumber.from("852500462432421494350957"), + /* tcr */ BigNumber.from("3232993993257432140"), + /* isInRecoveryMode */ false, +]; /* End: Mock test data */ describe("InstaLiquityResolver", () => { @@ -78,6 +85,13 @@ describe("InstaLiquityResolver", () => { expect(position).to.eql(expectedPosition); }); }); + + describe("getSystemState()", () => { + it("returns Liquity system state", async () => { + const systemState = await liquity.getSystemState(); + expect(systemState).to.eql(expectedSystemState); + }); + }); }); const resetHardhatBlockNumber = async (blockNumber) => {