Skip to content

WIP: add Liquity custom integration#9

Open
ajb wants to merge 4 commits intobabylon-finance:mainfrom
ajb:custom-integration/liquidity
Open

WIP: add Liquity custom integration#9
ajb wants to merge 4 commits intobabylon-finance:mainfrom
ajb:custom-integration/liquidity

Conversation

@ajb
Copy link
Contributor

@ajb ajb commented Jun 2, 2022

This one is a little different.

I previously asked if lend / borrow should be in separate steps like AAVE, but it actually turns out that you can't do that with Liquity. When you open a trove, you must specify a borrow amount greater than the minimum (which is currently 1,800 LUSD.)

I will add some inline code comments, but also, the tests are currently failing and I'm not sure why:

Screen Shot 2022-06-01 at 7 48 49 PM

Something in the finalization step is failing, but I'm not sure how to debug this within this repo;.

(Also, it will probably be helpful to your sanity to comment-out the Idle Finance test suite before running npm test. You could use OPTIMIZER=false npx hardhat test --grep "Liquity", but as I've mentioned before, the test suite seems to share some state between the individual specs.)

* @return address Address of the resulting lp token
*/
function _getResultToken(address) internal view override returns (address) {
return liquityBorrowerOperations.lusdToken();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little weird to have LUSD as the result token, since it doesn't reflect the amount of ETH that we deposited to our trove. Is this OK?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this is fine but when we return the price of the result token we need to account for the value of the collateral. See below

address[] memory outputTokens = new address[](1);
// outputTokens[0] is address(0) (ether)
uint256[] memory outputAmounts = new uint256[](1);
outputAmounts[0] = _getCollateral(_strategy); // TODO: deal with fees
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually - I think the fees are taken in LUSD, so we don't need to do anything here. Will update....

Copy link
Contributor

@rrecuero rrecuero Jun 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the output amount should be the LUSD amount right and not the value of the collateral?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output token is Ether, so this should be the value of our Ether collateral, no?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct, naming is a bit confusing xD

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is simple, just return the current collateral amount.

address /* _tokenDenominator */
) external pure override returns (uint256) {
(, uint amountLUSDToBorrow) = abi.decode(_data, (address, uint));
return amountLUSDToBorrow; // we receive the exact number of LUSD tokens that we requested
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See note above about using LUSD as the result token. Is this ok?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to do something special here. Basically we want to return in total the value of the collateral minus the value of the LUSD taken as debt.

So the price per token should be : (Collateral in LUSD - Debt in LUSD)/ debt in LUSD

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe this function knows how much collateralInLUSD there is, however?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you would need the strategy address to fetch it right?

// when creating this custom strategy, don't allow borrowing up
// to the maximum collateral ratio. ensure that users have at least
// a 5% buffer.
babylonLiquidationBuffer = 50000000000000000;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as we discussed @rrecuero


return pricePerTokenUsd.preciseMul(
// convert from USD -> tokenDenominator
// TODO: use USD here, instead of LUSD? But how would we do that?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see note here

// transfer additional LUSD to strategy
// borrowed 9853.51 LUSD
// need additional 0.5% = 49.267551980000000000
// TODO: for some reason, this fails if the number is not exact?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests are failing here: help please? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants