Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial Collateralization & Liquidation #382

Merged
merged 29 commits into from
May 7, 2021
Merged

Conversation

haythemsellami
Copy link
Member

@haythemsellami haythemsellami commented Apr 12, 2021

Gamma Partial Collateralization

High Level Description

Code

  • Unit test 100% coverage
  • Does your code follow the naming and code documentation guidelines?

Documentation

  • Is your code up to date with the spec?
  • Have you added your tests to the testing doc?

haythem96 and others added 6 commits March 11, 2021 19:08
* update OpenVaultArgs; set vault type; add vaultLatestUpdate

* fix build

* fix tests

* fix integration tests

* add vault type to event

* increase tests coverage

* update comment

Co-authored-by: aparnakr <aparnalocked@gmail.com>

Co-authored-by: aparnakr <aparnalocked@gmail.com>
* update chainlink interface to V3

* add decimals() scaling
* update OpenVaultArgs; set vault type; add vaultLatestUpdate

* fix build

* fix tests

* fix integration tests

* add vault type to event

* increase tests coverage

* update chainlink interface to V3

* add historical price function

* fix integration tests

* add tests

* add more tests

* typo

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>

* add decimals() scaling

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>
* update OpenVaultArgs; set vault type; add vaultLatestUpdate

* fix build

* fix tests

* fix integration tests

* add vault type to event

* increase tests coverage

* update chainlink interface to V3

* add historical price function

* fix integration tests

* add tests

* add more tests

* add sync function

* just checking that vault is not under water at settling, and boom encountered stack tooo deep :p

* change variables scope

* add vault type to getVault()

* fix tests to work with the getVault() modification

* update getVault() to return latestUpdateTimestamp; add test for sync()

* add owner functions

* fix typo

* set oracle deviation value

* fix build

* add vaultType to getExcessCollateral

* check margin if naked vault and not expired yet

* fix tests

* add _findUpperBoundValue()

* add getter functions

* add test coverage

* improve coverage

* typo

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>

* add decimals() scaling

* re-order functions; increase coverage

* _getNakedMarginRequired()

* _getNakedMarginRequired() test

* re-run CI

* update package-lock.json

* lint contracts

* lint test files

* improve coverage

* more margin required tests

* more margin required tests

* fix bug

* call tests

* improve comment

* add view function to get required margin for specific product

* add backward compatibility for openVault action

* more tests

* refactor _getProductHash

* refactor

* test settling undercollateralized vault

* tests for removing collateral from expired positions

* updated harness

* remove console.log()

* clean

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>
Co-authored-by: Shelly Grossman <shellygr151@gmail.com>
* update OpenVaultArgs; set vault type; add vaultLatestUpdate

* fix build

* fix tests

* fix integration tests

* add vault type to event

* increase tests coverage

* update chainlink interface to V3

* add historical price function

* fix integration tests

* add tests

* add more tests

* add sync function

* just checking that vault is not under water at settling, and boom encountered stack tooo deep :p

* change variables scope

* add vault type to getVault()

* fix tests to work with the getVault() modification

* update getVault() to return latestUpdateTimestamp; add test for sync()

* add owner functions

* fix typo

* set oracle deviation value

* fix build

* add vaultType to getExcessCollateral

* check margin if naked vault and not expired yet

* fix tests

* add _findUpperBoundValue()

* add getter functions

* add test coverage

* improve coverage

* typo

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>

* add decimals() scaling

* re-order functions; increase coverage

* _getNakedMarginRequired()

* _getNakedMarginRequired() test

* re-run CI

* update package-lock.json

* lint contracts

* lint test files

* improve coverage

* more margin required tests

* more margin required tests

* fix bug

* call tests

* improve comment

* add view function to get required margin for specific product

* add backward compatibility for openVault action

* more tests

* refactor _getProductHash

* refactor

* test settling undercollateralized vault

* add liquidate action args

* liquidate action args tests

* get liquidation price; check if vault is liquidatable

* add comments

* init tests

* liquidate

* fix tests

* more tests

* add comments

* liquidation price testing

* more tests

* change AUCTION_TIME scaling

* tests

* tests

* fix calculation; more tests

* fix collateral to sell calculation

* full liquidation tests

* partially liquidation test

* add function description

* clear

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>

* clear

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>

* clear

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>

* clear

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>

* clear

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>

* clear

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>

* rename var; refactor util function

* natspec comments

* clean tests

* require dust amount is greater than zero

* set bignumber config

* fix coverage

* update circleci config

* fix tests

* clean code

* clean

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>

* clean

* fix build

* add requirement for spot shock value

* fix typo

* cap liquidation price

* fix capping

* fix tests

* add tests for call position

* add natspec comment

* fix timestamp check

* fix tests

* fix tests

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>
@haythemsellami haythemsellami self-assigned this Apr 12, 2021
@haythemsellami haythemsellami marked this pull request as draft April 12, 2021 06:51
* @param _owner vault owner address
* @param _vaultId vault id
*/
function sync(address _owner, uint256 _vaultId) external nonReentrant notFullyPaused {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it useful to have a canSync public function or just do via f/e calls or require devs monitoring via bots to do it themselves?

Copy link
Member Author

Choose a reason for hiding this comment

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

what canSync() for ? to check if it is able to sync? if so it will be basically the same as sync() that will check the margin and return bool. We can keep it in the frontend as it can be done by checking the margin state.

Copy link
Contributor

Choose a reason for hiding this comment

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

@aleone, it looks like the equivalent of canSync would be calling calculator.getExcessCollateral:

(, bool isValidVault) = calculator.getExcessCollateral(vault, typeVault);

I think a separate canSync function could be useful later on, but don't think it's necessary right now (for example, a FE could poll the vault state for a UI).

/// @dev FixedPoint 0
FPI.FixedPointInt internal ZERO = FPI.fromScaledUint(0, BASE);
/// @notice auction length
uint256 public constant AUCTION_TIME = 3600;
Copy link
Contributor

@aleone aleone Apr 14, 2021

Choose a reason for hiding this comment

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

I lean towards this being adjustable. If we start having failed liquidations we may need to adjust this quickly.

I guess we could always deploy a new controller, set all the values and then migrate it in the address book. That would require a bunch of tx, but only one tx to migrate, so we don't have a broken/controller with non finalized values.

Curious other folks thoughts.

Copy link
Contributor

Choose a reason for hiding this comment

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

If we do allow it to be set, we could set a min value - admin can prb steal a lot of margined funds by atomically:

-flashloan bunch of eth
-changing margin requirements
-setting auction_time to 0
-liquidating a bunch of folks (they might need a recursive operate function to do this have to mint+liquidate+burn)
-setting margin req back to normal + auction time back to normal

Copy link
Member Author

Choose a reason for hiding this comment

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

To update this, we need to redeploy MarginCalculator not the Controller, and yeah it will require more txs but I really prefer keeping this constant, as it is more gas efficient, and still if we have failed liquidation w can still batch deployment and params config in one transaction.

* @dev can only be called by owner
* @param _deviation deviation value
*/
function setOracleDeviation(uint256 _deviation) external onlyOwner {
Copy link
Contributor

@aleone aleone Apr 14, 2021

Choose a reason for hiding this comment

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

Might make sense that this should be a mapping per product as chainlink deviations are different between products. COMPUSD is 1% ETHUSD 0.5%, LINKUSD 1%, etc.

We could also just be conservative and use 2% or something.

Copy link
Member Author

@haythemsellami haythemsellami Apr 15, 2021

Choose a reason for hiding this comment

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

What do u think is better ? if keeping it conservative will work, I prefer that rather than changing code now.

contracts/Controller.sol Outdated Show resolved Hide resolved
@@ -150,7 +166,18 @@ library Actions {
require(_args.actionType == ActionType.OpenVault, "Actions: can only parse arguments for open vault actions");
require(_args.owner != address(0), "Actions: cannot open vault for an invalid account");

return OpenVaultArgs({owner: _args.owner, vaultId: _args.vaultId});
// if not _args.data included, vault type will be 0 by default
uint256 vaultType;
Copy link
Contributor

Choose a reason for hiding this comment

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

Like this for non breaking changes!

* update OpenVaultArgs; set vault type; add vaultLatestUpdate

* fix build

* fix tests

* fix integration tests

* add vault type to event

* increase tests coverage

* update chainlink interface to V3

* add historical price function

* fix integration tests

* add tests

* add more tests

* add sync function

* just checking that vault is not under water at settling, and boom encountered stack tooo deep :p

* change variables scope

* add vault type to getVault()

* fix tests to work with the getVault() modification

* update getVault() to return latestUpdateTimestamp; add test for sync()

* add owner functions

* fix typo

* set oracle deviation value

* fix build

* add vaultType to getExcessCollateral

* check margin if naked vault and not expired yet

* fix tests

* add _findUpperBoundValue()

* add getter functions

* add test coverage

* improve coverage

* typo

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>

* add decimals() scaling

* re-order functions; increase coverage

* _getNakedMarginRequired()

* _getNakedMarginRequired() test

* re-run CI

* update package-lock.json

* lint contracts

* lint test files

* improve coverage

* more margin required tests

* more margin required tests

* fix bug

* call tests

* improve comment

* add view function to get required margin for specific product

* add backward compatibility for openVault action

* more tests

* refactor _getProductHash

* refactor

* test settling undercollateralized vault

* add liquidate action args

* liquidate action args tests

* get liquidation price; check if vault is liquidatable

* add comments

* init tests

* liquidate

* fix tests

* more tests

* add comments

* liquidation price testing

* more tests

* change AUCTION_TIME scaling

* tests

* tests

* fix calculation; more tests

* fix collateral to sell calculation

* full liquidation tests

* partially liquidation test

* add function description

* clear

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>

* clear

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>

* clear

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>

* clear

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>

* clear

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>

* clear

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>

* rename var; refactor util function

* natspec comments

* clean tests

* require dust amount is greater than zero

* set bignumber config

* fix coverage

* update circleci config

* fix tests

* clean code

* clean

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>

* clean

* fix build

* add requirement for spot shock value

* fix typo

* cap liquidation price

* fix capping

* fix tests

* add tests for call position

* clean

* add natspec comment

* add natspec comment

* init integration tests

* more tests

* fix timestamp check

* more tests

* fix tests

* fix tests

* more tests

* init call integration tests

* more tests

* fix tests

* more tests

* add more tests for put and call naked margin positions

* chore: undo vault type changes

* fix tests

* fix unit tests

* fix integration tests for call

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>
@haythemsellami haythemsellami marked this pull request as ready for review April 22, 2021 11:15
antoncoding
antoncoding previously approved these changes Apr 22, 2021
@haythemsellami haythemsellami added the gamma-v2.0.0 Opyn partial collateralization label Apr 22, 2021
@haythemsellami haythemsellami changed the title Gamma Partial Collateralization Partial Collateralization & Liquidation Apr 22, 2021
…BoundValues() (#385)

* add setUpperBoundValues()

* update tests

* fix unit tests

* fix integration tests

* integration-tests:fix collateral check after liquidation

* assert liquidation price is greater than zero when testing liquidation scenarios

* clean

* unit tests:setUpperBoundValues()

* improve margin calculator coverage

* fix integration tests

* updateUpperBoundValue()

* improve updateUpperBoundValue() coverage
* improve comments

* Improve comment in Controller

* fix build failing

* remove unused code
haythem96 and others added 9 commits April 28, 2021 13:30
* improve comments

* Improve comment in Controller

* fix build failing

* remove LiquidationStatus struct

* revert package.json change

* Fix wrong sign

* improve comments

* Improve comment in Controller

* fix build failing

* remove unused code

* remove LiquidationStatus struct

* revert package.json change

* Fix wrong sign

Co-authored-by: Haythem Sellami <sellamihaythem96@gmail.com>
* fix tests

* update error delta
* refactor marginVault to reduce # of SLOAD

* Apply change to addCollateral and removeCollateral

* Simplify remove logic

* keep the old if block
* add donate()

* add tests

* fix tests

* add function comment
* undo _withdrawCollateral() changes and update revert messages

* update error messages

* update tests:

* Update error code

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>
haythem96 and others added 3 commits May 6, 2021 12:36
)
{
(, bool isUnderCollat, uint256 price, uint256 dust) = _isLiquidatable(_owner, _vaultId, _roundId);
return (isUnderCollat, price, dust);
Copy link
Member Author

Choose a reason for hiding this comment

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

Here I used isUnderCollat instead of isLiquidatable to avoid the compiler warning re shadowing vars

haythemsellami and others added 3 commits May 6, 2021 16:12
* fix yearn pricer to work with the new OpynPricerInterface

* update revert message

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>
CruzMolina and others added 3 commits May 6, 2021 19:42
* refactor: productToTimeToExpiry -> timesToExpiryForProduct

* refactor: ProductTimeToExpiryAdded -> TimeToExpiryAdded

* refactor: getProductTimeToExpiry -> getTimesToExpiry

* refactor: timeToExpiryToMaxPrice -> maxPriceAtTimeToExpiry

* refactor: TimeToExpiryValueAdded -> MaxPriceAdded

* refactor: getTimeToExpiryValue -> getMaxPrice

* docs: misc spotShock mapping comment update
* fix: contracts path

* fix harness contracts path
@haythemsellami haythemsellami merged commit 9a75da2 into master May 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gamma-v2.0.0 Opyn partial collateralization
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants