Skip to content

Commit

Permalink
Merge pull request #173 from gammaswap/gammapool-metadata
Browse files Browse the repository at this point in the history
publish package
  • Loading branch information
0xDanr committed Jan 1, 2024
2 parents 80f39ff + b1df41d commit 493c001
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 116 deletions.
16 changes: 8 additions & 8 deletions contracts/GammaPoolFactory.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.21;

import "./interfaces/IGammaPool.sol";
import "./base/AbstractGammaPoolFactory.sol";
import "./rates/storage/AbstractRateParamsStore.sol";
import "./libraries/AddressCalculator.sol";
Expand Down Expand Up @@ -56,16 +55,17 @@ contract GammaPoolFactory is AbstractGammaPoolFactory, AbstractRateParamsStore,

/// @dev See {IGammaPoolFactory-addProtocol}
function addProtocol(address implementation) external virtual override onlyOwner {
if(IGammaPool(implementation).protocolId() == 0) revert ZeroProtocol();// implementation protocolId is zero
if(getProtocol[IGammaPool(implementation).protocolId()] != address(0)) revert ProtocolExists(); // protocolId already set
if(IProtocol(implementation).protocolId() == 0) revert ZeroProtocol();// implementation protocolId is zero
if(getProtocol[IProtocol(implementation).protocolId()] != address(0)) revert ProtocolExists(); // protocolId already set

getProtocol[IGammaPool(implementation).protocolId()] = implementation; // store implementation
getProtocol[IProtocol(implementation).protocolId()] = implementation; // store implementation
}

/// @dev See {IGammaPoolFactory-updateProtocol}
function updateProtocol(uint16 _protocolId, address _newImplementation) external virtual override onlyOwner {
isProtocolNotSet(_protocolId);
if(IGammaPool(_newImplementation).protocolId() == 0) revert ZeroProtocol();
if(IGammaPool(_newImplementation).protocolId() != _protocolId) revert ProtocolMismatch();
if(IProtocol(_newImplementation).protocolId() == 0) revert ZeroProtocol();
if(IProtocol(_newImplementation).protocolId() != _protocolId) revert ProtocolMismatch();
if(getProtocol[_protocolId] == _newImplementation) revert ProtocolExists(); // protocolId already set with same implementation

getProtocol[_protocolId] = _newImplementation;
Expand All @@ -90,7 +90,7 @@ contract GammaPoolFactory is AbstractGammaPoolFactory, AbstractRateParamsStore,
address implementation = getProtocol[_protocolId];

// check GammaPool can be created with this implementation
address[] memory _tokensOrdered = IGammaPool(implementation).validateCFMM(_tokens, _cfmm, _data);
address[] memory _tokensOrdered = IProtocol(implementation).validateCFMM(_tokens, _cfmm, _data);

// calculate unique identifier of GammaPool that will also be used as salt for instantiating the proxy contract address
bytes32 key = AddressCalculator.getGammaPoolKey(_cfmm, _protocolId);
Expand All @@ -106,7 +106,7 @@ contract GammaPoolFactory is AbstractGammaPoolFactory, AbstractRateParamsStore,

uint8[] memory _decimals = getDecimals(_tokensOrdered);
uint72 _minBorrow = uint72(10**((_decimals[0] + _decimals[1]) / 2));
IGammaPool(pool).initialize(_cfmm, _tokensOrdered, _decimals, _minBorrow, _data); // initialize GammaPool's state variables
IProtocol(pool).initialize(_cfmm, _tokensOrdered, _decimals, _minBorrow, _data); // initialize GammaPool's state variables

getPool[key] = pool; // map unique key to new instance of GammaPool
getKey[pool] = key; // map unique key to new instance of GammaPool
Expand Down
18 changes: 12 additions & 6 deletions contracts/base/AbstractGammaPoolFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity >=0.8.4;

import "../interfaces/IGammaPoolFactory.sol";
import "../interfaces/IGammaPool.sol";
import "../interfaces/IProtocol.sol";
import "../interfaces/IPausable.sol";
import "../utils/TwoStepOwnable.sol";
import "../libraries/AddressCalculator.sol";
Expand All @@ -21,6 +21,7 @@ abstract contract AbstractGammaPoolFactory is IGammaPoolFactory, TwoStepOwnable
error PoolExists();
error DeployFailed();
error ZeroAddress();
error ExecuteFailed();

/// @dev See {IGammaPoolFactory-getPool}
mapping(bytes32 => address) public override getPool; // all GS Pools addresses can be predetermined through key
Expand Down Expand Up @@ -74,11 +75,16 @@ abstract contract AbstractGammaPoolFactory is IGammaPoolFactory, TwoStepOwnable
return IPausable(_pool).unpause(_functionId);
}

/// @dev See {IGammaPoolFactory-setPoolParams}
function setPoolParams(address _pool, uint16 _origFee, uint8 _extSwapFee, uint8 _emaMultiplier, uint8 _minUtilRate1, uint8 _minUtilRate2, uint16 _feeDivisor, uint8 _liquidationFee, uint8 _ltvThreshold, uint72 _minBorrow) external virtual override {
isForbidden(feeToSetter); // only feeToSetter can update origination fee parameters
IGammaPool(_pool).setPoolParams(_origFee, _extSwapFee, _emaMultiplier, _minUtilRate1, _minUtilRate2, _feeDivisor, _liquidationFee, _ltvThreshold, _minBorrow);
emit PoolParamsUpdate(_pool, _origFee, _extSwapFee, _emaMultiplier, _minUtilRate1, _minUtilRate2, _feeDivisor, _liquidationFee, _ltvThreshold, _minBorrow);
/// @dev See {IGammaPoolFactory-execute}
function execute(address _pool, bytes calldata _data) external virtual override {
isForbidden(feeToSetter);
(bool success, bytes memory result) = _pool.call(_data);
if (!success) {
if (result.length == 0) revert ExecuteFailed();
assembly {
revert(add(32, result), mload(result))
}
}
}

/// @dev See {IGammaPoolFactory-setFee}
Expand Down
45 changes: 14 additions & 31 deletions contracts/interfaces/IGammaPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
pragma solidity >=0.8.0;

import "./IGammaPoolEvents.sol";
import "./IProtocol.sol";
import "./strategies/events/IGammaPoolERC20Events.sol";
import "./rates/IRateModel.sol";

/// @title Interface for GammaPool
/// @author Daniel D. Alcarraz (https://github.com/0xDanr)
/// @dev Interface used for GammaPool implementations
interface IGammaPool is IGammaPoolEvents, IGammaPoolERC20Events, IRateModel {
interface IGammaPool is IProtocol, IGammaPoolEvents, IGammaPoolERC20Events, IRateModel {
/// @dev Struct containing Loan data plus tokenId
struct LoanData {
/// @dev Loan counter, used to generate unique tokenId which indentifies the loan in the GammaPool
Expand Down Expand Up @@ -235,32 +236,9 @@ interface IGammaPool is IGammaPoolEvents, IGammaPoolERC20Events, IRateModel {
uint72 minBorrow;
}

/// @dev Function to initialize state variables GammaPool, called usually from GammaPoolFactory contract right after GammaPool instantiation
/// @param _cfmm - address of CFMM GammaPool is for
/// @param _tokens - ERC20 tokens of CFMM
/// @param _decimals - decimals of CFMM tokens, indices must match _tokens[] array
/// @param _data - custom struct containing additional information used to verify the `_cfmm`
/// @param _minBorrow - minimum amount of liquidity that can be borrowed or left unpaid in a loan
function initialize(address _cfmm, address[] calldata _tokens, uint8[] calldata _decimals, uint72 _minBorrow, bytes calldata _data) external;

/// @dev Set parameters to calculate origination fee, liquidation fee, and ltv threshold
/// @param origFee - loan opening origination fee in basis points
/// @param extSwapFee - external swap fee in basis points, max 255 basis points = 2.55%
/// @param emaMultiplier - multiplier used in EMA calculation of utilization rate
/// @param minUtilRate1 - minimum utilization rate to calculate dynamic origination fee in exponential model
/// @param minUtilRate2 - minimum utilization rate to calculate dynamic origination fee in linear model
/// @param feeDivisor - fee divisor for calculating origination fee, based on 2^(maxUtilRate - minUtilRate1)
/// @param liquidationFee - liquidation fee to charge during liquidations in basis points (1 - 255 => 0.01% to 2.55%)
/// @param ltvThreshold - ltv threshold (1 - 255 => 0.1% to 25.5%)
/// @param minBorrow - minimum liquidity amount that can be borrowed or left unpaid in a loan
function setPoolParams(uint16 origFee, uint8 extSwapFee, uint8 emaMultiplier, uint8 minUtilRate1, uint8 minUtilRate2, uint16 feeDivisor, uint8 liquidationFee, uint8 ltvThreshold, uint72 minBorrow) external;

/// @dev cfmm - address of CFMM this GammaPool is for
function cfmm() external view returns(address);

/// @dev Protocol id of the implementation contract for this GammaPool
function protocolId() external view returns(uint16);

/// @dev ERC20 tokens of CFMM
function tokens() external view returns(address[] memory);

Expand Down Expand Up @@ -288,6 +266,18 @@ interface IGammaPool is IGammaPoolEvents, IGammaPoolERC20Events, IRateModel {
/// @dev Batch Liquidations Strategy implementation contract for this GammaPool
function batchLiquidationStrategy() external view returns(address);

/// @dev Set parameters to calculate origination fee, liquidation fee, and ltv threshold
/// @param origFee - loan opening origination fee in basis points
/// @param extSwapFee - external swap fee in basis points, max 255 basis points = 2.55%
/// @param emaMultiplier - multiplier used in EMA calculation of utilization rate
/// @param minUtilRate1 - minimum utilization rate to calculate dynamic origination fee in exponential model
/// @param minUtilRate2 - minimum utilization rate to calculate dynamic origination fee in linear model
/// @param feeDivisor - fee divisor for calculating origination fee, based on 2^(maxUtilRate - minUtilRate1)
/// @param liquidationFee - liquidation fee to charge during liquidations in basis points (1 - 255 => 0.01% to 2.55%)
/// @param ltvThreshold - ltv threshold (1 - 255 => 0.1% to 25.5%)
/// @param minBorrow - minimum liquidity amount that can be borrowed or left unpaid in a loan
function setPoolParams(uint16 origFee, uint8 extSwapFee, uint8 emaMultiplier, uint8 minUtilRate1, uint8 minUtilRate2, uint16 feeDivisor, uint8 liquidationFee, uint8 ltvThreshold, uint72 minBorrow) external;

/// @dev Balances in the GammaPool of collateral tokens, CFMM LP tokens, and invariant amounts at last update
/// @return tokenBalances - balances of collateral tokens in GammaPool
/// @return lpTokenBalance - CFMM LP token balance of GammaPool
Expand All @@ -313,13 +303,6 @@ interface IGammaPool is IGammaPoolEvents, IGammaPoolERC20Events, IRateModel {
/// @return data - struct containing all relevant global state variables and descriptive information of GammaPool. Used to avoid making multiple calls
function getPoolData() external view returns(PoolData memory data);

/// @dev Check GammaPool for CFMM and tokens can be created with this implementation
/// @param _tokens - assumed tokens of CFMM, validate function should check CFMM is indeed for these tokens
/// @param _cfmm - address of CFMM GammaPool will be for
/// @param _data - custom struct containing additional information used to verify the `_cfmm`
/// @return _tokensOrdered - tokens ordered to match the same order as in CFMM
function validateCFMM(address[] calldata _tokens, address _cfmm, bytes calldata _data) external view returns(address[] memory _tokensOrdered);

// Short Gamma

/// @dev Deposit CFMM LP token and get GS LP token, without doing a transferFrom transaction. Must have sent CFMM LP token first
Expand Down
16 changes: 4 additions & 12 deletions contracts/interfaces/IGammaPoolFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,10 @@ interface IGammaPoolFactory {
/// @param _isSet - bool flag, true use fee information, false use GammaSwap default fees
function setPoolFee(address _pool, address _to, uint16 _protocolFee, uint16 _origFeeShare, bool _isSet) external;

/// @dev Set parameters to calculate origination fee for GammaPool and set liquidation fee and ltvThreshold
/// @param _pool - address of GammaPool whose origination fee parameters function will update
/// @param _origFee - loan opening origination fee in basis points
/// @param _extSwapFee - external swap fee in basis points, max 255 basis points = 2.55%
/// @param _emaMultiplier - multiplier used in EMA calculation of utilization rate
/// @param _minUtilRate1 - minimum utilization rate to calculate dynamic origination fee in exponential model
/// @param _minUtilRate2 - minimum utilization rate to calculate dynamic origination fee in linear model
/// @param _feeDivisor - fee divisor for calculating origination fee, based on 2^(maxUtilRate - minUtilRate1)
/// @param _liquidationFee - liquidation fee to charge during liquidations in basis points (1 - 255 => 0.01% to 2.55%)
/// @param _ltvThreshold - ltv threshold (1 - 255 => 0.1% to 25.5%)
/// @param _minBorrow - minimum liquidity amount that can be borrowed or left unpaid in a loan
function setPoolParams(address _pool, uint16 _origFee, uint8 _extSwapFee, uint8 _emaMultiplier, uint8 _minUtilRate1, uint8 _minUtilRate2, uint16 _feeDivisor, uint8 _liquidationFee, uint8 _ltvThreshold, uint72 _minBorrow) external;
/// @dev Call admin function in GammaPool contract
/// @param _pool - address of GammaPool whose admin function will be called
/// @param _data - custom struct containing information to execute in pool contract
function execute(address _pool, bytes calldata _data) external;

/// @dev Pause a GammaPool's function identified by a `_functionId`
/// @param _pool - address of GammaPool whose functions we will pause
Expand Down
25 changes: 25 additions & 0 deletions contracts/interfaces/IProtocol.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity >=0.8.0;

/// @title Interface for Protocol
/// @author Daniel D. Alcarraz (https://github.com/0xDanr)
/// @dev Interface used to add protocols and initialize them in GammaPoolFactory
interface IProtocol {
/// @dev Protocol id of the implementation contract for this GammaPool
function protocolId() external view returns(uint16);

/// @dev Check GammaPool for CFMM and tokens can be created with this implementation
/// @param _tokens - assumed tokens of CFMM, validate function should check CFMM is indeed for these tokens
/// @param _cfmm - address of CFMM GammaPool will be for
/// @param _data - custom struct containing additional information used to verify the `_cfmm`
/// @return _tokensOrdered - tokens ordered to match the same order as in CFMM
function validateCFMM(address[] calldata _tokens, address _cfmm, bytes calldata _data) external view returns(address[] memory _tokensOrdered);

/// @dev Function to initialize state variables GammaPool, called usually from GammaPoolFactory contract right after GammaPool instantiation
/// @param _cfmm - address of CFMM GammaPool is for
/// @param _tokens - ERC20 tokens of CFMM
/// @param _decimals - decimals of CFMM tokens, indices must match _tokens[] array
/// @param _data - custom struct containing additional information used to verify the `_cfmm`
/// @param _minBorrow - minimum amount of liquidity that can be borrowed or left unpaid in a loan
function initialize(address _cfmm, address[] calldata _tokens, uint8[] calldata _decimals, uint72 _minBorrow, bytes calldata _data) external;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gammaswap/v1-core",
"version": "1.1.17",
"version": "1.1.18",
"description": "Core smart contracts for the GammaSwap V1 protocol",
"homepage": "https://gammaswap.com",
"scripts": {
Expand Down

0 comments on commit 493c001

Please sign in to comment.