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 1 commit
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
28 changes: 28 additions & 0 deletions contracts/pcv/curve/StableSwapOperatorV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,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 > 0.1e18 && _ratio < 1.9e18, "StableSwapOperatorV1: broken peg");
}
}
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');
});
});
});