Skip to content

Latest commit

 

History

History
235 lines (194 loc) · 6.89 KB

PriceFeed.md

File metadata and controls

235 lines (194 loc) · 6.89 KB

The system price feed adapter (PriceFeed.sol)

View Source: contracts/PriceFeed.sol

↗ Extends: PriceFeedStorage, IPriceFeed

PriceFeed

The PriceFeed relies upon a main oracle and a secondary as a fallback in case of error

Events

event LastGoodPriceUpdated(uint256  _lastGoodPrice);
event PriceFeedBroken(uint8  index, address  priceFeedAddress);
event PriceFeedUpdated(uint8  index, address  newPriceFeedAddress);

Functions


setAddresses

function setAddresses(address _mainPriceFeed, address _backupPriceFeed) external nonpayable onlyOwner 

Arguments

Name Type Description
_mainPriceFeed address
_backupPriceFeed address
Source Code
function setAddresses(address _mainPriceFeed, address _backupPriceFeed) external onlyOwner {
        uint256 latestPrice = setAddress(0, _mainPriceFeed);
        setAddress(1, _backupPriceFeed);

        _storePrice(latestPrice);
    }

fetchPrice

Returns the latest price obtained from the Oracle. Called by Zero functions that require a current price. It uses the main price feed and fallback to the backup one in case of an error. If both fail return the last good price seen.

function fetchPrice() external nonpayable
returns(uint256)
Source Code
function fetchPrice() external override returns (uint256) {
        for (uint8 index = 0; index < 2; index++) {
            (uint256 price, bool success) = priceFeeds[index].latestAnswer();
            if (success) {
                _storePrice(price);
                return price;
            } else {
                emit PriceFeedBroken(index, address(priceFeeds[index]));
            }
        }
        return lastGoodPrice;
    }

setAddress

Allows users to setup the main and the backup price feeds

function setAddress(uint8 _index, address _newPriceFeed) public nonpayable onlyOwner 
returns(uint256)

Arguments

Name Type Description
_index uint8 the oracle to be configured
_newPriceFeed address address where an IExternalPriceFeed implementation is located

Returns

price the latest price of the inserted price feed

Source Code
function setAddress(uint8 _index, address _newPriceFeed) public onlyOwner returns (uint256) {
        require(_index < priceFeeds.length, "Out of bounds when setting the price feed");
        checkContract(_newPriceFeed);
        priceFeeds[_index] = IExternalPriceFeed(_newPriceFeed);
        (uint256 price, bool success) = priceFeeds[_index].latestAnswer();
        require(success, "PriceFeed: Price feed must be working");
        emit PriceFeedUpdated(_index, _newPriceFeed);
        return price;
    }

_storePrice

function _storePrice(uint256 _currentPrice) internal nonpayable

Arguments

Name Type Description
_currentPrice uint256
Source Code
function _storePrice(uint256 _currentPrice) internal {
        lastGoodPrice = _currentPrice;
        emit LastGoodPriceUpdated(_currentPrice);
    }

Contracts