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

feat: getVaultWithDetails() #432

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 30 additions & 24 deletions contracts/core/Controller.sol
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ contract Controller is Initializable, OwnableUpgradeSafe, ReentrancyGuardUpgrade
* @return amount of collateral that can be taken out
*/
function getProceed(address _owner, uint256 _vaultId) external view returns (uint256) {
(MarginVault.Vault memory vault, uint256 typeVault, ) = getVault(_owner, _vaultId);
(MarginVault.Vault memory vault, uint256 typeVault, ) = getVaultWithDetails(_owner, _vaultId);

(uint256 netValue, bool isExcess) = calculator.getExcessCollateral(vault, typeVault);

Expand Down Expand Up @@ -534,23 +534,22 @@ contract Controller is Initializable, OwnableUpgradeSafe, ReentrancyGuardUpgrade
}

/**
* @dev return if an expired oToken is ready to be settled, only true when price for underlying,
* strike and collateral assets at this specific expiry is available in our Oracle module
* @param _underlying oToken underlying asset
* @param _strike oToken strike asset
* @param _collateral oToken collateral asset
* @param _expiry otoken expiry timestamp
* @dev return if an expired oToken contract’s settlement price has been finalized
* @param _otoken address of the oToken
* @return True if the oToken has expired AND all oracle prices at the expiry timestamp have been finalized, False if not
*/
function isSettlementAllowed(
address _underlying,
address _strike,
address _collateral,
uint256 _expiry
) public view returns (bool) {
bool isUnderlyingFinalized = oracle.isDisputePeriodOver(_underlying, _expiry);
bool isStrikeFinalized = oracle.isDisputePeriodOver(_strike, _expiry);
bool isCollateralFinalized = oracle.isDisputePeriodOver(_collateral, _expiry);
function isSettlementAllowed(address _otoken) public view returns (bool) {
OtokenInterface otoken = OtokenInterface(_otoken);

address underlying = otoken.underlyingAsset();
address strike = otoken.strikeAsset();
address collateral = otoken.collateralAsset();

uint256 expiry = otoken.expiryTimestamp();

bool isUnderlyingFinalized = oracle.isDisputePeriodOver(underlying, expiry);
bool isStrikeFinalized = oracle.isDisputePeriodOver(strike, expiry);
bool isCollateralFinalized = oracle.isDisputePeriodOver(collateral, expiry);

return isUnderlyingFinalized && isStrikeFinalized && isCollateralFinalized;
}
Expand Down Expand Up @@ -581,7 +580,7 @@ contract Controller is Initializable, OwnableUpgradeSafe, ReentrancyGuardUpgrade
* @param _vaultId vault id of vault to return
* @return Vault struct that corresponds to the _vaultId of _owner, vault type and the latest timestamp when the vault was updated
*/
function getVault(address _owner, uint256 _vaultId)
function getVaultWithDetails(address _owner, uint256 _vaultId)
public
view
returns (
Expand All @@ -593,6 +592,10 @@ contract Controller is Initializable, OwnableUpgradeSafe, ReentrancyGuardUpgrade
return (vaults[_owner][_vaultId], vaultType[_owner][_vaultId], vaultLatestUpdate[_owner][_vaultId]);
}

function getVault(address _owner, uint256 _vaultId) public view returns (MarginVault.Vault memory) {
return (vaults[_owner][_vaultId]);
}

/**
* @notice execute a variety of actions
* @dev for each action in the action array, execute the corresponding action, only one vault can be modified
Expand Down Expand Up @@ -670,7 +673,7 @@ contract Controller is Initializable, OwnableUpgradeSafe, ReentrancyGuardUpgrade
* @param _vaultId vault id of the final vault
*/
function _verifyFinalState(address _owner, uint256 _vaultId) internal view {
(MarginVault.Vault memory vault, uint256 typeVault, ) = getVault(_owner, _vaultId);
(MarginVault.Vault memory vault, uint256 typeVault, ) = getVaultWithDetails(_owner, _vaultId);
(, bool isValidVault) = calculator.getExcessCollateral(vault, typeVault);

require(isValidVault, "CO14");
Expand Down Expand Up @@ -763,7 +766,7 @@ contract Controller is Initializable, OwnableUpgradeSafe, ReentrancyGuardUpgrade

require(whitelist.isWhitelistedCollateral(_args.asset), "CO21");

(, uint256 typeVault, ) = getVault(_args.owner, _args.vaultId);
(, uint256 typeVault, ) = getVaultWithDetails(_args.owner, _args.vaultId);

if (typeVault == 1) {
nakedPoolBalance[_args.asset] = nakedPoolBalance[_args.asset].add(_args.amount);
Expand All @@ -790,7 +793,7 @@ contract Controller is Initializable, OwnableUpgradeSafe, ReentrancyGuardUpgrade
{
require(_checkVaultId(_args.owner, _args.vaultId), "CO35");

(MarginVault.Vault memory vault, uint256 typeVault, ) = getVault(_args.owner, _args.vaultId);
(MarginVault.Vault memory vault, uint256 typeVault, ) = getVaultWithDetails(_args.owner, _args.vaultId);

if (_isNotEmpty(vault.shortOtokens)) {
OtokenInterface otoken = OtokenInterface(vault.shortOtokens[0]);
Expand Down Expand Up @@ -878,7 +881,7 @@ contract Controller is Initializable, OwnableUpgradeSafe, ReentrancyGuardUpgrade
// only allow redeeming expired otoken
require(now >= expiry, "CO28");

require(isSettlementAllowed(underlying, strike, collateral, expiry), "CO29");
require(isSettlementAllowed(address(otoken)), "CO29");

uint256 payout = getPayout(_args.otoken, _args.amount);

Expand All @@ -897,7 +900,7 @@ contract Controller is Initializable, OwnableUpgradeSafe, ReentrancyGuardUpgrade
function _settleVault(Actions.SettleVaultArgs memory _args) internal onlyAuthorized(msg.sender, _args.owner) {
require(_checkVaultId(_args.owner, _args.vaultId), "CO35");

(MarginVault.Vault memory vault, uint256 typeVault, ) = getVault(_args.owner, _args.vaultId);
(MarginVault.Vault memory vault, uint256 typeVault, ) = getVaultWithDetails(_args.owner, _args.vaultId);

OtokenInterface otoken;

Expand Down Expand Up @@ -925,7 +928,7 @@ contract Controller is Initializable, OwnableUpgradeSafe, ReentrancyGuardUpgrade

// do not allow settling vault with un-expired otoken
require(now >= expiry, "CO31");
require(isSettlementAllowed(underlying, strike, collateral, expiry), "CO29");
require(isSettlementAllowed(address(otoken)), "CO29");

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

Expand Down Expand Up @@ -1057,7 +1060,10 @@ contract Controller is Initializable, OwnableUpgradeSafe, ReentrancyGuardUpgrade
uint256
)
{
(MarginVault.Vault memory vault, uint256 typeVault, uint256 latestUpdateTimestamp) = getVault(_owner, _vaultId);
(MarginVault.Vault memory vault, uint256 typeVault, uint256 latestUpdateTimestamp) = getVaultWithDetails(
_owner,
_vaultId
);
(bool isUnderCollat, uint256 price, uint256 collateralDust) = calculator.isLiquidatable(
vault,
typeVault,
Expand Down
4 changes: 2 additions & 2 deletions docs/contracts-documentation/core/Controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Contract that controls the Gamma Protocol and the interaction of all sub contrac

- `hasExpired(address _otoken) (external)`

- `getVault(address _owner, uint256 _vaultId) (public)`
- `getVaultWithDetails(address _owner, uint256 _vaultId) (public)`

- `_runActions(struct Actions.ActionArgs[] _actions) (internal)`

Expand Down Expand Up @@ -420,7 +420,7 @@ check if an oToken has expired

- True if the otoken has expired, False if not

### Function `getVault(address _owner, uint256 _vaultId) → struct MarginVault.Vault, uint256, uint256 public`
### Function `getVaultWithDetails(address _owner, uint256 _vaultId) → struct MarginVault.Vault, uint256, uint256 public`

return a specific vault

Expand Down
4 changes: 2 additions & 2 deletions docs/contracts-documentation/tests/MarginVaultTester.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Functions:

- `getVault(uint256 _vaultIndex) (external)`
- `getVaultWithDetails(uint256 _vaultIndex) (external)`

- `testAddShort(uint256 _vaultIndex, address _shortOtoken, uint256 _amount, uint256 _index) (external)`

Expand All @@ -16,7 +16,7 @@

- `testRemoveCollateral(uint256 _vaultIndex, address _collateralAsset, uint256 _amount, uint256 _index) (external)`

### Function `getVault(uint256 _vaultIndex) → struct MarginVault.Vault external`
### Function `getVaultWithDetails(uint256 _vaultIndex) → struct MarginVault.Vault external`

### Function `testAddShort(uint256 _vaultIndex, address _shortOtoken, uint256 _amount, uint256 _index) external`

Expand Down
2 changes: 1 addition & 1 deletion docs/uml/GammaController.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/uml/GammaUML.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions test/integration-tests/longCallSpreadExpireItm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ contract('Long Call Spread Option expires Itm flow', ([accountOwner1, nakedBuyer
const longOtokenSupplyBefore = new BigNumber(await lowerStrikeCall.totalSupply())

// Check that we start at a valid state
const vaultBefore = await controllerProxy.getVault(accountOwner1, vaultCounter1)
const vaultBefore = await controllerProxy.getVaultWithDetails(accountOwner1, vaultCounter1)
const vaultStateBefore = await calculator.getExcessCollateral(vaultBefore[0], vaultBefore[1])
assert.equal(vaultStateBefore[0].toString(), '0')
assert.equal(vaultStateBefore[1], true)
Expand Down Expand Up @@ -336,7 +336,7 @@ contract('Long Call Spread Option expires Itm flow', ([accountOwner1, nakedBuyer
assert.equal(longOtokenSupplyBefore.minus(scaledOptionsAmount).toString(), longOtokenSupplyAfter.toString())

// Check that we end at a valid state
const vaultAfter = await controllerProxy.getVault(accountOwner1, vaultCounter1)
const vaultAfter = await controllerProxy.getVaultWithDetails(accountOwner1, vaultCounter1)
const vaultStateAfter = await calculator.getExcessCollateral(vaultAfter[0], vaultAfter[1])
assert.equal(vaultStateAfter[0].toString(), '0')
assert.equal(vaultStateAfter[1], true)
Expand Down Expand Up @@ -430,7 +430,7 @@ contract('Long Call Spread Option expires Itm flow', ([accountOwner1, nakedBuyer
const lowerStrikeCallSupplyBefore = new BigNumber(await lowerStrikeCall.totalSupply())

// Check that we start at a valid state
const vaultBefore = await controllerProxy.getVault(accountOwner2, vaultCounter2)
const vaultBefore = await controllerProxy.getVaultWithDetails(accountOwner2, vaultCounter2)
const vaultStateBefore = await calculator.getExcessCollateral(vaultBefore[0], vaultBefore[1])
assert.equal(vaultStateBefore[0].toString(), scaledCollateralAmount)
assert.equal(vaultStateBefore[1], true)
Expand Down Expand Up @@ -471,7 +471,7 @@ contract('Long Call Spread Option expires Itm flow', ([accountOwner1, nakedBuyer
assert.equal(lowerStrikeCallSupplyBefore.toString(), lowerStrikeCallSupplyAfter.toString())

// Check that we end at a valid state
const vaultAfter = await controllerProxy.getVault(accountOwner2, vaultCounter2)
const vaultAfter = await controllerProxy.getVaultWithDetails(accountOwner2, vaultCounter2)
const vaultStateAfter = await calculator.getExcessCollateral(vaultAfter[0], vaultAfter[1])
assert.equal(vaultStateAfter[0].toString(), '0')
assert.equal(vaultStateAfter[1], true)
Expand Down
8 changes: 4 additions & 4 deletions test/integration-tests/longCallSpreadExpireOtm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ contract('Long Call Spread Option expires Otm flow', ([accountOwner1, nakedBuyer
const lowerStrikeCallSupplyBefore = new BigNumber(await lowerStrikeCall.totalSupply())

// Check that we start at a valid state
const vaultBefore = await controllerProxy.getVault(accountOwner1, vaultCounter1)
const vaultBefore = await controllerProxy.getVaultWithDetails(accountOwner1, vaultCounter1)
const vaultStateBefore = await calculator.getExcessCollateral(vaultBefore[0], vaultBefore[1])
assert.equal(vaultStateBefore[0].toString(), '0')
assert.equal(vaultStateBefore[1], true)
Expand Down Expand Up @@ -322,7 +322,7 @@ contract('Long Call Spread Option expires Otm flow', ([accountOwner1, nakedBuyer
)

// Check that we end at a valid state
const vaultAfter = await controllerProxy.getVault(accountOwner1, vaultCounter1)
const vaultAfter = await controllerProxy.getVaultWithDetails(accountOwner1, vaultCounter1)
const vaultStateAfter = await calculator.getExcessCollateral(vaultAfter[0], vaultAfter[1])
assert.equal(vaultStateAfter[0].toString(), '0')
assert.equal(vaultStateAfter[1], true)
Expand Down Expand Up @@ -397,7 +397,7 @@ contract('Long Call Spread Option expires Otm flow', ([accountOwner1, nakedBuyer
const lowerStrikeCallSupplyBefore = new BigNumber(await lowerStrikeCall.totalSupply())

// Check that we start at a valid state
const vaultBefore = await controllerProxy.getVault(accountOwner2, vaultCounter2)
const vaultBefore = await controllerProxy.getVaultWithDetails(accountOwner2, vaultCounter2)
const vaultStateBefore = await calculator.getExcessCollateral(vaultBefore[0], vaultBefore[1])
assert.equal(vaultStateBefore[0].toString(), scaledCollateralAmount)
assert.equal(vaultStateBefore[1], true)
Expand Down Expand Up @@ -438,7 +438,7 @@ contract('Long Call Spread Option expires Otm flow', ([accountOwner1, nakedBuyer
assert.equal(lowerStrikeCallSupplyBefore.toString(), lowerStrikeCallSupplyAfter.toString())

// Check that we end at a valid state
const vaultAfter = await controllerProxy.getVault(accountOwner2, vaultCounter2)
const vaultAfter = await controllerProxy.getVaultWithDetails(accountOwner2, vaultCounter2)
const vaultStateAfter = await calculator.getExcessCollateral(vaultAfter[0], vaultAfter[1])
assert.equal(vaultStateAfter[0].toString(), '0')
assert.equal(vaultStateAfter[1], true)
Expand Down
12 changes: 6 additions & 6 deletions test/integration-tests/longCallSpreadPreExpiry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ contract('Long Call Spread Option closed before expiry flow', ([accountOwner1, n
const marginPoolLongOtokenBalanceBefore = new BigNumber(await lowerStrikeCall.balanceOf(marginPool.address))

// Check that we start at a valid state
const vaultBefore = await controllerProxy.getVault(accountOwner1, vaultCounter1)
const vaultBefore = await controllerProxy.getVaultWithDetails(accountOwner1, vaultCounter1)
const vaultStateBefore = await calculator.getExcessCollateral(vaultBefore[0], vaultBefore[1])
assert.equal(vaultStateBefore[0].toString(), '0')
assert.equal(vaultStateBefore[1], true)
Expand Down Expand Up @@ -324,7 +324,7 @@ contract('Long Call Spread Option closed before expiry flow', ([accountOwner1, n
)

// Check that we end at a valid state
const vaultAfter = await controllerProxy.getVault(accountOwner1, vaultCounter1)
const vaultAfter = await controllerProxy.getVaultWithDetails(accountOwner1, vaultCounter1)
const vaultStateAfter = await calculator.getExcessCollateral(vaultAfter[0], vaultAfter[1])
assert.equal(vaultStateAfter[0].toString(), '0')
assert.equal(vaultStateAfter[1], true)
Expand Down Expand Up @@ -370,7 +370,7 @@ contract('Long Call Spread Option closed before expiry flow', ([accountOwner1, n
const marginPoolLongOtokenBalanceBefore = new BigNumber(await lowerStrikeCall.balanceOf(marginPool.address))

// Check that we start at a valid state
const vaultBefore = await controllerProxy.getVault(accountOwner1, vaultCounter1)
const vaultBefore = await controllerProxy.getVaultWithDetails(accountOwner1, vaultCounter1)
const vaultStateBefore = await calculator.getExcessCollateral(vaultBefore[0], vaultBefore[1])
assert.equal(vaultStateBefore[0].toString(), '0')
assert.equal(vaultStateBefore[1], true)
Expand Down Expand Up @@ -434,7 +434,7 @@ contract('Long Call Spread Option closed before expiry flow', ([accountOwner1, n
)

// Check that we end at a valid state
const vaultAfter = await controllerProxy.getVault(accountOwner1, vaultCounter1)
const vaultAfter = await controllerProxy.getVaultWithDetails(accountOwner1, vaultCounter1)
const vaultStateAfter = await calculator.getExcessCollateral(vaultAfter[0], vaultAfter[1])
assert.equal(vaultStateAfter[0].toString(), '0')
assert.equal(vaultStateAfter[1], true)
Expand Down Expand Up @@ -472,7 +472,7 @@ contract('Long Call Spread Option closed before expiry flow', ([accountOwner1, n
const marginPoolLowerStrikeCallBalanceBefore = new BigNumber(await lowerStrikeCall.balanceOf(marginPool.address))

// Check that we start at a valid state
const vaultBefore = await controllerProxy.getVault(accountOwner2, vaultCounter2)
const vaultBefore = await controllerProxy.getVaultWithDetails(accountOwner2, vaultCounter2)
const vaultStateBefore = await calculator.getExcessCollateral(vaultBefore[0], vaultBefore[1])
assert.equal(vaultStateBefore[0].toString(), '0')
assert.equal(vaultStateBefore[1], true)
Expand Down Expand Up @@ -527,7 +527,7 @@ contract('Long Call Spread Option closed before expiry flow', ([accountOwner1, n
assert.equal(marginPoolLowerStrikeCallBalanceBefore.toString(), marginPoolLowerStrikeCallBalanceAfter.toString())

// Check that we end at a valid state
const vaultAfter = await controllerProxy.getVault(accountOwner2, vaultCounter2)
const vaultAfter = await controllerProxy.getVaultWithDetails(accountOwner2, vaultCounter2)
const vaultStateAfter = await calculator.getExcessCollateral(vaultAfter[0], vaultAfter[1])
assert.equal(vaultStateAfter[0].toString(), '0')
assert.equal(vaultStateAfter[1], true)
Expand Down
Loading