Skip to content

Commit

Permalink
type and interface updates
Browse files Browse the repository at this point in the history
  • Loading branch information
aalavandhan committed Mar 22, 2024
1 parent 160d594 commit 723afc3
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 54 deletions.
56 changes: 51 additions & 5 deletions spot-contracts/contracts/_interfaces/CommonTypes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,51 @@ pragma solidity ^0.8.0;

import { IERC20Upgradeable } from "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";

//-----------------------------------------------------------------------------
// Generic

struct TokenAmount {
/// @notice The asset token redeemed.
IERC20Upgradeable token;
/// @notice The amount redeemed.
uint256 amount;
}

struct Range {
uint256 lower;
uint256 upper;
}

struct SigmoidParams {
int256 lower;
int256 upper;
int256 growth;
}

struct LinearFn {
uint256 x1;
uint256 y1;
uint256 x2;
uint256 y2;
}

//-----------------------------------------------------------------------------

struct PairAmounts {
// NOTE `perpAmt` and `noteAmt` have different base denominations.
// @notice Amount of perp tokens.
uint256 perpAmt;
// @notice Amount of vault notes.
uint256 noteAmt;
}

struct RolloverData {
/// @notice The amount of tokens rolled out.
uint256 tokenOutAmt;
/// @notice The amount of trancheIn tokens rolled in.
uint256 trancheInAmt;
}

/// @notice The system subscription parameters.
struct SubscriptionParams {
/// @notice The current TVL of perp denominated in the underlying.
Expand All @@ -20,9 +58,17 @@ struct SubscriptionParams {
uint256 seniorTR;
}

struct RolloverData {
/// @notice The amount of tokens rolled out.
uint256 tokenOutAmt;
/// @notice The amount of trancheIn tokens rolled in.
uint256 trancheInAmt;
struct SystemFees {
// perp fees
uint256 perpMintFeePerc;
uint256 perpBurnFeePerc;
// vault fees
uint256 vaultMintFeePerc;
uint256 vaultBurnFeePerc;
// rollover fee
SigmoidParams rolloverFee;
// swap fee
Range underlyingToPerpSwapFeePercs;
Range perpToUnderlyingSwapFeePercs;
uint256 protocolSwapSharePerc;
}
6 changes: 6 additions & 0 deletions spot-contracts/contracts/_interfaces/IOwnable.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.0;

interface IOwnable {
function owner() external view returns (address);
}
47 changes: 27 additions & 20 deletions spot-contracts/contracts/_interfaces/IPerpetualTranche.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity ^0.8.0;
import { IERC20Upgradeable } from "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";

import { IBondIssuer } from "./IBondIssuer.sol";
import { IFeePolicy } from "./IFeePolicy.sol";
import { IBalancer } from "./IBalancer.sol";
import { IBondController } from "./buttonwood/IBondController.sol";
import { ITranche } from "./buttonwood/ITranche.sol";
import { IRolloverVault } from "./IRolloverVault.sol";
Expand All @@ -26,6 +26,16 @@ interface IPerpetualTranche is IERC20Upgradeable {
//--------------------------------------------------------------------------
// Methods

/// @notice Updates time dependent storage state.
function updateState() external;

/// @notice Updates the deposit bond, if enough time has passed.
/// @return The system's current deposit bond after the update.
function updateDepositBond() external returns (IBondController);

/// @notice Redeems mature tranche tokens held by perp for the underlying.
function recover() external;

/// @notice Deposits tranche tokens into the system and mint perp tokens.
/// @param trancheIn The address of the tranche token to be deposited.
/// @param trancheInAmt The amount of tranche tokens deposited.
Expand Down Expand Up @@ -66,63 +76,63 @@ interface IPerpetualTranche is IERC20Upgradeable {

/// @notice The parent bond whose tranches are currently accepted to mint perp tokens.
/// @return Address of the deposit bond.
function getDepositBond() external returns (IBondController);
function depositBond() external view returns (IBondController);

/// @notice The tranche token contract currently accepted to mint perp tokens.
/// @return Address of the deposit tranche ERC-20 token.
function getDepositTranche() external returns (ITranche);
function depositTranche() external view returns (ITranche);

/// @return The tranche ratio of the current deposit tranche.
function getDepositTrancheRatio() external returns (uint256);
function depositTrancheRatio() external view returns (uint256);

/// @notice The policy contract with the fee computation logic for the perp and vault systems.
/// @return Address of the policy contract.
function feePolicy() external view returns (IFeePolicy);
/// @notice The balancer contract which controls fees and orchestrates external actions with perp and vault systems.
/// @return Address of the balancer contract.
function balancer() external view returns (IBalancer);

/// @notice Total count of tokens held in the reserve.
/// @return The reserve token count.
function getReserveCount() external returns (uint256);
function reserveCount() external view returns (uint256);

/// @notice The token address from the reserve list by index.
/// @param index The index of a token.
/// @return The reserve token address.
function getReserveAt(uint256 index) external returns (IERC20Upgradeable);
function reserveAt(uint256 index) external view returns (IERC20Upgradeable);

/// @notice Checks if the given token is part of the reserve.
/// @param token The address of a token to check.
/// @return If the token is part of the reserve.
function inReserve(IERC20Upgradeable token) external returns (bool);
function inReserve(IERC20Upgradeable token) external view returns (bool);

/// @notice Fetches the reserve's token balance.
/// @param token The address of the tranche token held by the reserve.
/// @return The ERC-20 balance of the reserve token.
function getReserveTokenBalance(IERC20Upgradeable token) external returns (uint256);
function reserveTokenBalance(IERC20Upgradeable token) external view returns (uint256);

/// @notice Calculates the reserve's token value,
/// in a standard denomination as defined by the implementation.
/// @param token The address of the tranche token held by the reserve.
/// @return The value of the reserve token balance held by the reserve, in a standard denomination.
function getReserveTokenValue(IERC20Upgradeable token) external returns (uint256);
function reserveTokenValue(IERC20Upgradeable token) external view returns (uint256);

/// @notice Computes the total value of assets currently held in the reserve.
/// @return The total value of the perp system, in a standard denomination.
function getTVL() external returns (uint256);
function getTVL() external view returns (uint256);

/// @notice Fetches the list of reserve tokens which are up for rollover.
/// @return The list of reserve tokens up for rollover.
function getReserveTokensUpForRollover() external returns (IERC20Upgradeable[] memory);
function reserveTokensUpForRollover() external view returns (IERC20Upgradeable[] memory);

/// @notice Computes the amount of perp tokens minted when `trancheInAmt` `trancheIn` tokens
/// are deposited into the system.
/// @param trancheIn The tranche token deposited.
/// @param trancheInAmt The amount of tranche tokens deposited.
/// @return The amount of perp tokens to be minted.
function computeMintAmt(ITranche trancheIn, uint256 trancheInAmt) external returns (uint256);
function computeMintAmt(ITranche trancheIn, uint256 trancheInAmt) external view returns (uint256);

/// @notice Computes the amount reserve tokens redeemed when burning given number of perp tokens.
/// @param perpAmtBurnt The amount of perp tokens to be burnt.
/// @return tokensOut The list of reserve tokens and amounts redeemed.
function computeRedemptionAmts(uint256 perpAmtBurnt) external returns (TokenAmount[] memory tokensOut);
function computeRedemptionAmts(uint256 perpAmtBurnt) external view returns (TokenAmount[] memory tokensOut);

/// @notice Computes the amount reserve tokens that are rolled out for the given number
/// of `trancheIn` tokens rolled in.
Expand All @@ -134,8 +144,5 @@ interface IPerpetualTranche is IERC20Upgradeable {
ITranche trancheIn,
IERC20Upgradeable tokenOut,
uint256 trancheInAmtAvailable
) external returns (RolloverData memory r);

/// @notice Updates time dependent storage state.
function updateState() external;
) external view returns (RolloverData memory r);
}
30 changes: 19 additions & 11 deletions spot-contracts/contracts/_interfaces/IRolloverVault.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.0;

import { IBalancer } from "./IBalancer.sol";
import { IVault } from "./IVault.sol";
import { SubscriptionParams } from "./CommonTypes.sol";
import { ITranche } from "./buttonwood/ITranche.sol";

interface IRolloverVault is IVault {
/// @notice Deposits the tranche tokens from {msg.sender} into the vault and mints notes.
/// @param trancheAmt The amount tranche tokens to be deposited into the vault.
/// @return The amount of notes minted.
function deposit(ITranche tranche, uint256 trancheAmt) external returns (uint256);

/// @notice Computes the amount of notes minted when given amount of tranche tokens
/// are deposited into the system.
/// @param trancheAmt The amount tranche tokens to be deposited into the vault.
/// @return The amount of notes to be minted.
function computeMintAmt(ITranche tranche, uint256 trancheAmt) external returns (uint256);

/// @notice Allows users to swap their underlying tokens for perps held by the vault.
/// @param underlyingAmtIn The amount of underlying tokens swapped in.
/// @return The amount of perp tokens swapped out.
Expand All @@ -18,18 +30,14 @@ interface IRolloverVault is IVault {
/// @notice Computes the amount of perp tokens that are returned when user swaps a given number of underlying tokens.
/// @param underlyingAmtIn The number of underlying tokens the user swaps in.
/// @return perpAmtOut The number of perp tokens returned to the user.
/// @return perpFeeAmtToBurn The amount of perp tokens to be paid to the perp contract as mint fees.
/// @return s The pre-swap perp and vault subscription state.
function computeUnderlyingToPerpSwapAmt(
uint256 underlyingAmtIn
) external returns (uint256, uint256, SubscriptionParams memory);
function computeUnderlyingToPerpSwapAmt(uint256 underlyingAmtIn) external view returns (uint256);

/// @notice Computes the amount of underlying tokens that are returned when user swaps a given number of perp tokens.
/// @param perpAmtIn The number of perp tokens the user swaps in.
/// @return underlyingAmtOut The number of underlying tokens returned to the user.
/// @return perpFeeAmtToBurn The amount of perp tokens to be paid to the perp contract as burn fees.
/// @return s The pre-swap perp and vault subscription state.
function computePerpToUnderlyingSwapAmt(
uint256 perpAmtIn
) external returns (uint256, uint256, SubscriptionParams memory);
function computePerpToUnderlyingSwapAmt(uint256 perpAmtIn) external view returns (uint256);

/// @notice The balancer contract which controls fees and orchestrates external actions with perp and vault systems.
/// @return Address of the balancer contract.
function balancer() external view returns (IBalancer);
}
7 changes: 1 addition & 6 deletions spot-contracts/contracts/_interfaces/IVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,12 @@ interface IVault is IERC20Upgradeable {
/// @return The list of asset tokens and amounts redeemed.
function redeem(uint256 notes) external returns (TokenAmount[] memory);

/// @notice Batches the recover and redeem functions.
/// @param notes The amount of notes to be burnt.
/// @return The list of asset tokens and amounts redeemed.
function recoverAndRedeem(uint256 notes) external returns (TokenAmount[] memory);

/// @return The total value of assets currently held by the vault, denominated in a standard unit of account.
function getTVL() external view returns (uint256);

/// @param token The address of the asset ERC-20 token held by the vault.
/// @return The vault's asset token value, denominated in a standard unit of account.
function getVaultAssetValue(IERC20Upgradeable token) external view returns (uint256);
function vaultAssetValue(IERC20Upgradeable token) external view returns (uint256);

/// @notice The ERC20 token that can be deposited into this vault.
function underlying() external view returns (IERC20Upgradeable);
Expand Down
11 changes: 7 additions & 4 deletions spot-contracts/contracts/_interfaces/ProtocolErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ error ExceededMaxMintPerTranche();
//-------------------------------------------------------------------------
// Vault

/// @notice Expected more underlying token liquidity to perform operation.
error InsufficientLiquidity();

/// @notice Expected to swap non-zero assets.
error UnacceptableSwap();

Expand All @@ -64,8 +61,11 @@ error DeployedCountOverLimit();
/// @notice Expected parent bond to have only 2 children tranches.
error UnacceptableTrancheLength();

/// @notice Expected more underlying token liquidity to perform operation.
error InsufficientLiquidity();

//-------------------------------------------------------------------------
// Fee Policy
// Balancer

/// @notice Expected perc value to be at most (1 * 10**DECIMALS), i.e) 1.0 or 100%.
error InvalidPerc();
Expand All @@ -78,3 +78,6 @@ error InvalidDRBounds();

/// @notice Expected sigmoid asymptotes to be within defined bounds.
error InvalidSigmoidAsymptotes();

/// @notice Expected DR to be within defined bounds.
error DROutsideBound();
4 changes: 2 additions & 2 deletions spot-contracts/contracts/_utils/BondHelpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ library BondHelpers {
/// @notice Given a bond, returns the address of the most senior tranche.
/// @param b The address of the bond contract.
/// @return t The senior tranche address.
function getSeniorTranche(IBondController b) internal view returns (ITranche t) {
function seniorTranche(IBondController b) internal view returns (ITranche t) {
(t, ) = b.tranches(0);
}

/// @notice Given a bond, returns the tranche ratio of the most senior tranche.
/// @param b The address of the bond contract.
/// @return r The tranche ratio of the senior most tranche.
function getSeniorTrancheRatio(IBondController b) internal view returns (uint256 r) {
function seniorTrancheRatio(IBondController b) internal view returns (uint256 r) {
(, r) = b.tranches(0);
}

Expand Down
23 changes: 17 additions & 6 deletions spot-contracts/contracts/_utils/TrancheHelpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,38 @@ library TrancheHelpers {
ITranche tranche,
IERC20Upgradeable collateralToken
) internal view returns (uint256, uint256) {
IBondController bond = IBondController(tranche.bond());
return getTrancheCollateralization(tranche, IBondController(tranche.bond()), collateralToken);
}

/// @notice Given a tranche, calculates the claimable collateral balance backing the tranche supply.
/// @param tranche Address of the tranche token.
/// @param parentBond Address of the tranche's parent bond.
/// @param collateralToken Address of the tranche's underlying collateral token.
/// @return The collateral balance and the tranche token supply.
function getTrancheCollateralization(
ITranche tranche,
IBondController parentBond,
IERC20Upgradeable collateralToken
) internal view returns (uint256, uint256) {
uint256 trancheSupply = tranche.totalSupply();
uint256 trancheClaim = 0;

// When the tranche's parent bond is mature
if (bond.isMature()) {
if (parentBond.isMature()) {
trancheClaim = collateralToken.balanceOf(address(tranche));
return (trancheClaim, trancheSupply);
}

// NOTE: This implementation assumes the bond has only two tranches.
if (bond.trancheCount() != 2) {
if (parentBond.trancheCount() != 2) {
revert UnacceptableTrancheLength();
}

uint256 bondCollateralBalance = collateralToken.balanceOf(address(bond));
uint256 bondCollateralBalance = collateralToken.balanceOf(address(parentBond));

// For junior tranche
if (bond.trancheAt(1) == tranche) {
uint256 seniorSupply = bond.totalDebt() - trancheSupply;
if (parentBond.trancheAt(1) == tranche) {
uint256 seniorSupply = parentBond.totalDebt() - trancheSupply;
uint256 seniorClaim = MathUpgradeable.min(seniorSupply, bondCollateralBalance);
trancheClaim = bondCollateralBalance - seniorClaim;
}
Expand Down

0 comments on commit 723afc3

Please sign in to comment.