Skip to content

Commit

Permalink
feat: use ChainLink price feed in LibUibquityPool
Browse files Browse the repository at this point in the history
  • Loading branch information
gitcoindev committed Dec 8, 2023
1 parent cb96b9c commit 8a1a471
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
18 changes: 18 additions & 0 deletions packages/contracts/src/dollar/facets/UbiquityPoolFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,24 @@ contract UbiquityPoolFacet is IUbiquityPool, Modifiers {
LibUbiquityPool.setCollateralPrice(collateralIndex, newPrice);
}

/// @inheritdoc IUbiquityPool
function setCollateralChainLinkPriceFeedAddress(
address collateralAddress,
address chainLinkPriceFeedAddress
) external onlyAdmin {
LibUbiquityPool.setCollateralChainLinkPriceFeedAddress(
collateralAddress,
chainLinkPriceFeedAddress
);
}

/// @inheritdoc IUbiquityPool
function updateChainLinkCollateralPrice(
uint256 collateralIndex
) external onlyAdmin {
LibUbiquityPool.updateChainLinkCollateralPrice(collateralIndex);
}

/// @inheritdoc IUbiquityPool
function setFees(
uint256 collateralIndex,
Expand Down
58 changes: 54 additions & 4 deletions packages/contracts/src/dollar/libraries/LibUbiquityPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {UBIQUITY_POOL_PRICE_PRECISION} from "./Constants.sol";
import {LibAppStorage} from "./LibAppStorage.sol";
import {LibTWAPOracle} from "./LibTWAPOracle.sol";
import {LibCollateralOracle} from "./LibCollateralOracle.sol";
import {AggregatorV3Interface} from "../../dollar/interfaces/AggregatorV3Interface.sol";

/**
* @notice Ubiquity pool library
Expand Down Expand Up @@ -43,7 +44,7 @@ library LibUbiquityPool {
address[] collateralAddresses;
// collateral address -> collateral index
mapping(address => uint256) collateralAddressToIndex;
// Stores price of the collateral, if price is paused. CONSIDER ORACLES EVENTUALLY!!!
// Stores price of the collateral
uint256[] collateralPrices;
// array collateral symbols
string[] collateralSymbols;
Expand Down Expand Up @@ -84,6 +85,8 @@ library LibUbiquityPool {
bool[] mintPaused;
// whether redeeming is paused for a particular collateral index
bool[] redeemPaused;
// Collateral price feed contract address
address[] collateralPriceFeedAddresses;
}

/// @notice Struct used for detailed collateral information
Expand Down Expand Up @@ -127,6 +130,8 @@ library LibUbiquityPool {
event AmoMinterRemoved(address amoMinterAddress);
/// @notice Emitted on setting a collateral price
event CollateralPriceSet(uint256 collateralIndex, uint256 newPrice);
/// @notice Emitted on setting a collateral price feed address
event CollateralPriceFeedSet(uint256 collateralIndex, uint256 newPrice);
/// @notice Emitted on enabling/disabling a particular collateral token
event CollateralToggled(uint256 collateralIndex, bool newState);
/// @notice Emitted when fees are updated
Expand Down Expand Up @@ -614,7 +619,7 @@ library LibUbiquityPool {
}

/**
* @notice Sets collateral token price in USD
* @notice Sets exact collateral token price in USD
* @param collateralIndex Collateral token index
* @param newPrice New USD price (precision 1e6)
*/
Expand All @@ -623,13 +628,58 @@ library LibUbiquityPool {
uint256 newPrice
) internal {
UbiquityPoolStorage storage poolStorage = ubiquityPoolStorage();

// Notice from Frax: CONSIDER ORACLES EVENTUALLY!!!
poolStorage.collateralPrices[collateralIndex] = newPrice;

emit CollateralPriceSet(collateralIndex, newPrice);
}

/**
* @notice Sets collateral ChainLink price feed address
* @param collateralIndex Collateral token index
* @param newPrice New USD price (precision 1e6)
*/
function setCollateralChainLinkPriceFeedAddress(
address collateralAddress,
address chainLinkPriceFeedAddress
) internal {
UbiquityPoolStorage storage poolStorage = ubiquityPoolStorage();

uint256 collateralIndex = poolStorage.collateralAddressToIndex[
collateralAddress
];
poolStorage.collateralPriceFeedAddresses[
collateralIndex
] = chainLinkPriceFeedAddress;

emit CollateralPriceFeedSet(collateralIndex, chainLinkPriceFeedAddress);
}

/**
* @notice Updates collateral token price in USD from ChainLink price feed
* @param collateralIndex Collateral token index
*/
function updateChainLinkCollateralPrice(uint256 collateralIndex) internal {
UbiquityPoolStorage storage poolStorage = ubiquityPoolStorage();

AggregatorV3Interface priceFeed = AggregatorV3Interface(
poolStorage.collateralPriceFeedAddresses[collateralIndex]
);
(
uint80 roundID,
int256 price,
,
uint256 updatedAt,
uint80 answeredInRound
) = priceFeed.latestRoundData();
require(
price >= 0 && updatedAt != 0 && answeredInRound >= roundID,
"Invalid ChainLink price"
);

poolStorage.collateralPrices[collateralIndex] = uint256(price);
emit CollateralPriceSet(collateralIndex, uint256(price));
}

/**
* @notice Sets mint and redeem fees, 1_000_000 = 100%
* @param collateralIndex Collateral token index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -796,4 +796,10 @@ contract UbiquityPoolFacetTest is DiamondTestSetup {
curve3PriceUSD;
console.log("dollarsOut:", dollarsOut);
}

function testCollateralPriceFeed() public {
vm.startPrank(admin);

vm.stopPrank();
}
}

0 comments on commit 8a1a471

Please sign in to comment.