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

Named Static Cached PCVDeposit #379

Merged
merged 46 commits into from
Dec 27, 2021
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
d298cf4
first cut of Named Static PCV Deposit Wrapper
ElliotFriedman Dec 14, 2021
de0d342
add unit tests
ElliotFriedman Dec 14, 2021
c1e490a
updated tests
ElliotFriedman Dec 14, 2021
746840c
update tests and create bulk add deposit function
ElliotFriedman Dec 15, 2021
945476c
Update contracts/pcv/utils/NamedStaticPCVDepositWrapper.sol
ElliotFriedman Dec 15, 2021
26e62a9
Merge branch 'develop' into feat/named-static-cached-pcvdeposit
ElliotFriedman Dec 15, 2021
efaf3ee
Merge branch 'develop' into feat/named-static-cached-pcvdeposit
ElliotFriedman Dec 15, 2021
4936255
Update test/unit/pcv/NamedStaticPCVDepositWrapper.test.ts
ElliotFriedman Dec 15, 2021
d1a756e
Update test/unit/pcv/NamedStaticPCVDepositWrapper.test.ts
ElliotFriedman Dec 15, 2021
a628eb0
Update test/unit/pcv/NamedStaticPCVDepositWrapper.test.ts
ElliotFriedman Dec 15, 2021
9522a0e
update tests and sc to delete correctly
ElliotFriedman Dec 16, 2021
6653a45
Merge branch 'feat/named-static-cached-pcvdeposit' of github.com:fei-…
ElliotFriedman Dec 16, 2021
165e4b8
merge
ElliotFriedman Dec 16, 2021
9488d11
remove unchecked math
ElliotFriedman Dec 16, 2021
3c7b19f
Merge branch 'develop' into feat/named-static-cached-pcvdeposit
ElliotFriedman Dec 16, 2021
5b50e87
add bounds check for editing and removing deposits, update tests
ElliotFriedman Dec 16, 2021
8ac6652
Merge branch 'feat/named-static-cached-pcvdeposit' of github.com:fei-…
ElliotFriedman Dec 16, 2021
cff5867
remove bulkRemove and removeAll deposits
ElliotFriedman Dec 16, 2021
63f14b6
Merge branch 'develop' into feat/named-static-cached-pcvdeposit
ElliotFriedman Dec 16, 2021
b05a289
rename pcv deposit that will be deleted
ElliotFriedman Dec 16, 2021
013e6dd
Merge branch 'feat/named-static-cached-pcvdeposit' of github.com:fei-…
ElliotFriedman Dec 16, 2021
5151e86
Merge branch 'develop' into feat/named-static-cached-pcvdeposit
ElliotFriedman Dec 16, 2021
0ce4a81
update tests to have more readable names
ElliotFriedman Dec 16, 2021
a5c23df
Merge branch 'feat/named-static-cached-pcvdeposit' of github.com:fei-…
ElliotFriedman Dec 16, 2021
dc45987
Merge branch 'develop' into feat/named-static-cached-pcvdeposit
ElliotFriedman Dec 17, 2021
603af7c
Merge branch 'develop' into feat/named-static-cached-pcvdeposit
ElliotFriedman Dec 20, 2021
8b9eaaf
add dao script & it
ElliotFriedman Dec 20, 2021
ae3597f
Merge branch 'feat/named-static-cached-pcvdeposit' into feat/named-st…
ElliotFriedman Dec 20, 2021
c57436b
allow guardian to remove deposits
ElliotFriedman Dec 20, 2021
45cebc7
update test to include guardian deposit removal
ElliotFriedman Dec 20, 2021
2e4cc64
edit deposit fixes and updated unit test
ElliotFriedman Dec 20, 2021
d01a8f5
use cached variables in _editDeposit helper function
ElliotFriedman Dec 20, 2021
7bc3475
remove dao script, simulate OA action in setup
ElliotFriedman Dec 21, 2021
cdd77f0
Merge pull request #397 from fei-protocol/feat/named-static-cached-pc…
ElliotFriedman Dec 21, 2021
77a8290
Merge branch 'develop' into feat/named-static-cached-pcvdeposit
ElliotFriedman Dec 21, 2021
57351b6
Merge branch 'develop' into feat/named-static-cached-pcvdeposit
ElliotFriedman Dec 22, 2021
34c5af3
Merge branch 'develop' into feat/named-static-cached-pcvdeposit
ElliotFriedman Dec 23, 2021
d6709a5
add gas optimizations to remove 2 unneeded SLOAD's
ElliotFriedman Dec 23, 2021
267f4b4
Merge branch 'feat/named-static-cached-pcvdeposit' of github.com:fei-…
ElliotFriedman Dec 23, 2021
8c74f52
add deployment, fix it & proposal config
ElliotFriedman Dec 23, 2021
e7867a5
Merge branch 'develop' into feat/named-static-cached-pcvdeposit
Joeysantoro Dec 27, 2021
e0f620a
Merge branch 'develop' into feat/named-static-cached-pcvdeposit
Joeysantoro Dec 27, 2021
3c90927
move cr tests
Dec 27, 2021
5e14be2
Merge branch 'feat/named-static-cached-pcvdeposit' of github.com:fei-…
Dec 27, 2021
8310953
overwrite chainlink
Dec 27, 2021
0b12fa6
remove fip-33
Dec 27, 2021
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
204 changes: 204 additions & 0 deletions contracts/pcv/utils/NamedStaticPCVDepositWrapper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
pragma solidity ^0.8.4;

import "../IPCVDepositBalances.sol";
import "../../Constants.sol";
import "../../refs/CoreRef.sol";
import "@openzeppelin/contracts/utils/math/SafeCast.sol";

/**
@notice a contract to report static PCV data to cover PCV not held with a reliable oracle or on-chain reading
@author Fei Protocol

Returns PCV in USD terms
*/
contract NamedStaticPCVDepositWrapper is IPCVDepositBalances, CoreRef {
using SafeCast for *;

// -------------- Events ---------------
/// @notice event to update fei and usd balance
event BalanceUpdate(uint256 oldBalance, uint256 newBalance, uint256 oldFEIBalance, uint256 newFEIBalance);

/// @notice event to remove a deposit
event DepositRemoved(uint256 index);
Joeysantoro marked this conversation as resolved.
Show resolved Hide resolved

/// @notice event to add a new deposit
event DepositAdded(uint256 index, string indexed depositName);

/// @notice event emitted when a deposit is edited
event DepositChanged(uint256 index, string indexed depositName);

/// @notice struct to store info on each PCV Deposit
struct DepositInfo {
string depositName;
uint256 usdAmount; /// USD equivalent in this deposit, not including FEI value
uint256 feiAmount; /// amount of FEI in this deposit
uint256 underlyingTokenAmount; /// amount of underlying token in this deposit
address underlyingToken; /// address of the underlying token this deposit is reporting
}

/// @notice a list of all pcv deposits
DepositInfo[] public pcvDeposits;

/// @notice the PCV balance
uint256 public override balance;

/// @notice the reported FEI balance to track protocol controlled FEI in these deposits
uint256 public feiReportBalance;

constructor(address _core, DepositInfo[] memory newPCVDeposits) CoreRef(_core) {

// Uses oracle admin to share admin with CR oracle where this contract is used
_setContractAdminRole(keccak256("ORACLE_ADMIN_ROLE"));

// add all pcv deposits
for (uint256 i = 0; i < newPCVDeposits.length; i++) {
_addDeposit(newPCVDeposits[i]);
}
}

// ----------- Helper methods to change state -----------

/// @notice helper method to add a PCV deposit
function _addDeposit(DepositInfo memory newPCVDeposit) internal {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we store a mapping of names -> DepositInfo for lookups? Can also be used to ensure that no duplicate names are added to @ditchfieldcaleb 's earlier concerns

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We shouldn't need to think adversarially about deposit names as only OA and the governor can update the list and guardian can remove deposits instantly.

If we were to do this feature, the data structure would be name -> DepositInfo Index so that we aren't storing each struct and it's data twice.

require(newPCVDeposit.feiAmount > 0 || newPCVDeposit.usdAmount > 0, "NamedStaticPCVDepositWrapper: must supply either fei or usd amount");
ElliotFriedman marked this conversation as resolved.
Show resolved Hide resolved

uint256 oldBalance = balance;
uint256 oldFEIBalance = feiReportBalance;

balance += newPCVDeposit.usdAmount;
feiReportBalance += newPCVDeposit.feiAmount;
pcvDeposits.push(newPCVDeposit);

emit DepositAdded(pcvDeposits.length - 1, newPCVDeposit.depositName);
emit BalanceUpdate(oldBalance, balance, oldFEIBalance, feiReportBalance);
}
Joeysantoro marked this conversation as resolved.
Show resolved Hide resolved

/// @notice helper method to edit a PCV deposit
function _editDeposit(
uint256 index,
string calldata depositName,
uint256 usdAmount,
uint256 feiAmount,
uint256 underlyingTokenAmount,
address underlyingToken
) internal {
require(index < pcvDeposits.length, "NamedStaticPCVDepositWrapper: cannot edit index out of bounds");

DepositInfo storage updatePCVDeposit = pcvDeposits[index];

uint256 oldBalance = balance;
uint256 oldFEIBalance = feiReportBalance;
uint256 newBalance = balance - updatePCVDeposit.usdAmount + usdAmount;
uint256 newFeiReportBalance = feiReportBalance - updatePCVDeposit.feiAmount + feiAmount;
Copy link
Contributor

Choose a reason for hiding this comment

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

Use oldBalance and oldFeiBalance to save another sload

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added


balance = newBalance;
feiReportBalance = newFeiReportBalance;

updatePCVDeposit.usdAmount = usdAmount;
updatePCVDeposit.feiAmount = feiAmount;
updatePCVDeposit.depositName = depositName;
updatePCVDeposit.underlyingTokenAmount = underlyingTokenAmount;
updatePCVDeposit.underlyingToken = underlyingToken;

emit DepositChanged(index, depositName);
emit BalanceUpdate(oldBalance, newBalance, oldFEIBalance, newFeiReportBalance);
}

/// @notice helper method to delete a PCV deposit
function _removeDeposit(uint256 index) internal {
require(index < pcvDeposits.length, "NamedStaticPCVDepositWrapper: cannot remove index out of bounds");

DepositInfo storage pcvDepositToRemove = pcvDeposits[index];

uint256 depositBalance = pcvDepositToRemove.usdAmount;
uint256 feiDepositBalance = pcvDepositToRemove.feiAmount;
uint256 oldBalance = balance;
uint256 oldFeiReportBalance = feiReportBalance;
uint256 lastIndex = pcvDeposits.length - 1;

if (lastIndex != index) {
DepositInfo storage lastvalue = pcvDeposits[lastIndex];

pcvDeposits[index] = lastvalue;
}

pcvDeposits.pop();
balance -= depositBalance;
feiReportBalance -= feiDepositBalance;

emit BalanceUpdate(oldBalance, balance, oldFeiReportBalance, feiReportBalance);
emit DepositRemoved(index);
}

// ----------- Governor only state changing api -----------

/// @notice function to add a deposit
function addDeposit(
DepositInfo calldata newPCVDeposit
) external onlyGovernorOrAdmin {
_addDeposit(newPCVDeposit);
}

/// @notice function to bulk add deposits
function bulkAddDeposits(
DepositInfo[] calldata newPCVDeposits
) external onlyGovernorOrAdmin {
for (uint256 i = 0; i < newPCVDeposits.length; i++) {
_addDeposit(newPCVDeposits[i]);
}
}
ElliotFriedman marked this conversation as resolved.
Show resolved Hide resolved


/// @notice function to remove a PCV Deposit
function removeDeposit(uint256 index) external isGovernorOrGuardianOrAdmin {
_removeDeposit(index);
}

/// @notice function to edit an existing deposit
function editDeposit(
uint256 index,
ElliotFriedman marked this conversation as resolved.
Show resolved Hide resolved
uint256 usdAmount,
uint256 feiAmount,
uint256 underlyingTokenAmount,
string calldata depositName,
address underlying
) external onlyGovernorOrAdmin {
_editDeposit(
index,
depositName,
usdAmount,
feiAmount,
underlyingTokenAmount,
underlying
);
}

// ----------- Getters -----------

/// @notice returns the current number of PCV deposits
function numDeposits() public view returns (uint256) {
return pcvDeposits.length;
ElliotFriedman marked this conversation as resolved.
Show resolved Hide resolved
}

/// @notice returns the resistant balance and FEI in the deposit
function resistantBalanceAndFei() public view override returns (uint256, uint256) {
return (balance, feiReportBalance);
}

/// @notice display the related token of the balance reported
function balanceReportedIn() public pure override returns (address) {
return Constants.USD;
}

/// @notice function to return all of the different tokens deposited into this contract
function getAllUnderlying() public view returns (address[] memory) {
ElliotFriedman marked this conversation as resolved.
Show resolved Hide resolved
uint256 totalDeposits = numDeposits();

address[] memory allUnderlyingTokens = new address[](totalDeposits);
for (uint256 i = 0; i < totalDeposits; i++) {
allUnderlyingTokens[i] = pcvDeposits[i].underlyingToken;
}

return allUnderlyingTokens;
}
}
Loading