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

Add resistantBalanceAndFei to Curve deposit #169

Merged
merged 5 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
62 changes: 61 additions & 1 deletion contracts/pcv/curve/StableSwapOperatorV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ contract StableSwapOperatorV1 is PCVDeposit {
/// @notice The StableSwap pool to deposit in
address public pool;

/// @notice the min and max ratios for FEI-to-value in pool (these can be set by governance)
/// @notice this ratio is expressed as a percentile with 18 decimal precision, ie 0.1e18 = 10%
uint256 public minimumRatioThreshold;
uint256 public maximumRatioThreshold;

// ------------------ Private properties -----------------------------------

/// some fixed variables to interact with the pool
Expand All @@ -66,8 +71,14 @@ contract StableSwapOperatorV1 is PCVDeposit {
address _core,
address _pool,
address _curve3pool,
uint256 _depositMaxSlippageBasisPoints
uint256 _depositMaxSlippageBasisPoints,
uint256 _minimumRatioThreshold,
uint256 _maximumRatioThreshold
) CoreRef(_core) {
require(minimumRatioThreshold < maximumRatioThreshold, "Min ratio must be less than max ratio");
require(minimumRatioThreshold >= 0.01e18, "Min ratio must be at least 1%.");
kryptoklob marked this conversation as resolved.
Show resolved Hide resolved
require(maximumRatioThreshold <= 100e18, "Max ratio cannot be above 100x.");
kryptoklob marked this conversation as resolved.
Show resolved Hide resolved

// public variables
pool = _pool;
depositMaxSlippageBasisPoints = _depositMaxSlippageBasisPoints;
Expand All @@ -83,6 +94,27 @@ contract StableSwapOperatorV1 is PCVDeposit {
_usdt = IStableSwap3(_curve3pool).coins(2);
}


/// @notice set the minimum ratio threshold for a valid reading of restistant balances
function setMinRatio(uint256 _minimumRatioThreshold) public onlyGovernor {
require(_minimumRatioThreshold < maximumRatioThreshold, "Min ratio must be less than max ratio");
Copy link
Contributor

Choose a reason for hiding this comment

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

A good pattern for this is to have an internal method that gets called by both the governor setter and constructor

Copy link
Contributor

Choose a reason for hiding this comment

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

@Joeysantoro fixed

require(_minimumRatioThreshold >= 0.01e18, "Min ratio must be at least 1%.");
minimumRatioThreshold = _minimumRatioThreshold;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should call internal method here

Copy link
Contributor

Choose a reason for hiding this comment

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

Done

}

/// @notice set the maximum ratio threshold for a valid reading of resistant balances
function setMaxRatio(uint256 _maximumRatioThreshold) public onlyGovernor {
require(_maximumRatioThreshold > minimumRatioThreshold, "Max ratio must be greater than min ratio");
require(_maximumRatioThreshold <= 100e18, "Max ratio cannot be above 100x.");
maximumRatioThreshold = _maximumRatioThreshold;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should call internal method here

Copy link
Contributor

Choose a reason for hiding this comment

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

Done

}

/// @notice set both min & max ratios
function setRatios(uint256 _minimumRatioThreshold, uint256 _maximumRatioThreshold) public onlyGovernor {
setMinRatio(_minimumRatioThreshold);
setMaxRatio(_maximumRatioThreshold);
kryptoklob marked this conversation as resolved.
Show resolved Hide resolved
}

/// @notice deposit DAI, USDC, USDT, 3crv, and FEI into the pool.
/// Note: the FEI has to be minted & deposited on this contract in a previous
/// tx, as this contract does not use the Minter role.
Expand Down Expand Up @@ -285,7 +317,35 @@ contract StableSwapOperatorV1 is PCVDeposit {
return _daiOut;
}

/// @notice returns the token address in which this contract reports its
/// balance.
/// @return the DAI address
function balanceReportedIn() public view override returns(address) {
return _dai;
}

/// @notice gets the resistant token balance and protocol owned fei of this deposit
/// for balance, returns half of the theoretical USD value of the LP tokens, even though
/// there may be some slippage when withdrawing to DAI only. for fei, take the
/// same value (assumes there is no broken peg in the pool).
/// @return resistantBalance the resistant balance of DAI (theoretical USD value)
/// @return resistantFei the resistant balance of FEI (theoretical USD value)
function resistantBalanceAndFei() public view override returns (
kryptoklob marked this conversation as resolved.
Show resolved Hide resolved
uint256 resistantBalance,
uint256 resistantFei
) {
uint256 _lpBalance = IERC20(pool).balanceOf(address(this));
uint256 _lpVirtualPrice = IStableSwap2(pool).get_virtual_price();
uint256 _lpPriceUSD = _lpBalance * _lpVirtualPrice / 1e18;
resistantBalance = _lpPriceUSD / 2;
resistantFei = resistantBalance;

// revert if there is 10 times more FEI than 3crv or the other way around,
// which would mean there is a broken peg
uint256 _3crvVirtualPrice = IStableSwap3(_3pool).get_virtual_price();
uint256 _feiInPool = IStableSwap2(pool).balances(uint256(_feiIndex));
uint256 _3crvInPool = IStableSwap2(pool).balances(uint256(_3crvIndex));
uint256 _ratio = _feiInPool * 1e18 / (_3crvInPool * _3crvVirtualPrice / 1e18);
require(_ratio > minimumRatioThreshold && _ratio < maximumRatioThreshold, "StableSwapOperatorV1: broken peg");
}
}
10 changes: 5 additions & 5 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const runE2ETests = process.env.RUN_E2E_TESTS;
const mainnetAlchemyApiKey = process.env.MAINNET_ALCHEMY_API_KEY;

if (!rinkebyAlchemyApiKey || !testnetPrivateKey || !privateKey || !mainnetAlchemyApiKey) {
throw new Error('Please set your Ethereum keys in a .env')
console.warn("Not all Ethereum keys provided; some functionality will be unavailable.")
}

const config: HardhatUserConfig = {
Expand All @@ -28,21 +28,21 @@ const config: HardhatUserConfig = {
hardhat: {
gas: 12e6,
chainId: 5777, // Any network (default: none)
forking: {
forking: mainnetAlchemyApiKey ? {
url: `https://eth-mainnet.alchemyapi.io/v2/${mainnetAlchemyApiKey}`,
blockNumber: 13135475
}
} : undefined
},
localhost: {
url: 'http://127.0.0.1:8545'
},
rinkeby: {
url: `https://eth-rinkeby.alchemyapi.io/v2/${rinkebyAlchemyApiKey}`,
accounts: [testnetPrivateKey]
accounts: testnetPrivateKey ? [testnetPrivateKey] : []
},
mainnet: {
url: `https://eth-mainnet.alchemyapi.io/v2/${mainnetAlchemyApiKey}`,
accounts: [privateKey]
accounts: privateKey ? [privateKey] : []
},
},
solidity: {
Expand Down
27 changes: 27 additions & 0 deletions test/pcv/StableSwapOperatorV1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,31 @@ describe('StableSwapOperatorV1', function () {
expect(await this.deposit.balance()).to.be.bignumber.equal('4974253861920711893216');
});
});
describe('balanceReportedIn()', function() {
it('should return the DAI address', async function() {
expect(await this.deposit.balanceReportedIn()).to.be.equal(this.dai.address);
});
});
describe('resistantBalanceAndFei()', function() {
it('should return the current balance as half of the USD of LP tokens', async function() {
await this.dai.mint(this.deposit.address, `5000${e18}`);
await this.fei.mint(this.deposit.address, `6000${e18}`, {from: minterAddress});
await this.deposit.deposit({from: pcvControllerAddress});
expect((await this.deposit.resistantBalanceAndFei()).resistantBalance).to.be.bignumber.equal(`5000${e18}`);
});
it('should return the FEI as half of the USD value of LP tokens', async function() {
await this.dai.mint(this.deposit.address, `5000${e18}`);
await this.fei.mint(this.deposit.address, `6000${e18}`, {from: minterAddress});
await this.deposit.deposit({from: pcvControllerAddress});
expect((await this.deposit.resistantBalanceAndFei()).resistantFei).to.be.bignumber.equal(`5000${e18}`);
});
it('should revert if a peg is broken (amount in pool differs more than 50%)', async function() {
await this.dai.mint(this.deposit.address, `5000${e18}`);
await this.fei.mint(this.deposit.address, `6000${e18}`, {from: minterAddress});
await this.deposit.deposit({from: pcvControllerAddress});
expect((await this.deposit.resistantBalanceAndFei()).resistantFei).to.be.bignumber.equal(`5000${e18}`);
await this.fei.mint(this.mockMetapool.address, `10000000${e18}`, {from: minterAddress});
await expectRevert(this.deposit.resistantBalanceAndFei(), 'StableSwapOperatorV1: broken peg');
});
});
});