Contract Diff Checker

Contract Name:
PriceFeeds

Contract Source Code:

// SPDX-License-Identifier: GPL-3.0-only

pragma solidity 0.8.26;

import "contracts/farm/interfaces/IStork.sol";
import "contracts/farm/interfaces/IPriceFeeds.sol";
import "contracts/proxies/Upgradeable2Step.sol";

contract PriceFeeds is IPriceFeeds, Upgradeable2Step {

    mapping(address => StorkData) public storkData;

    IStork public immutable stork;

    constructor(address stork_) {
        if (stork_ == address(0)) revert ZeroAddress();
        stork = IStork(stork_);
    }

    function getPrice(address poolToken_) external view returns (uint256) {
        StorkData storage token0Data = storkData[poolToken_];
        if (token0Data.feedType == FeedType.Stork) {
            // handle stork feed
            return getStorkPrice(token0Data.feedHash, token0Data.staleSeconds);
        } else {
            revert InvalidType();
        }
    }

    function getStorkPrice(bytes32 feedHash_, uint256 staleSeconds_) public view returns (uint256) {
        if (feedHash_ == 0) {
            // price feed not set
            return 0;
        }

        IStork.TemporalNumericValue memory storkValue = stork.getTemporalNumericValueUnsafeV1(feedHash_);

        if (staleSeconds_ != 0 && block.timestamp > (storkValue.timestampNs / 1000000000) + staleSeconds_) {
            // stale price
            return 0;
        }

        if (storkValue.quantizedValue <= 0) {
            // invalid price
            return 0;
        }
        
        return uint256(uint192(storkValue.quantizedValue));
    }

    // zero feedHash allowed, which would block updates to the pool
    function setStorkFeedsData(address farmContract, address[] memory poolTokens_, StorkData[] memory poolTokenDatas_) external onlyOwner {
        if (farmContract == address(0)) {
            revert ZeroAddress();
        }
        if (poolTokens_.length != poolTokenDatas_.length) {
            revert CountMismatch();
        }

        (bool success, ) = farmContract.call(abi.encodeWithSignature("massUpdatePools()"));
        if (!success) {
            revert InvalidCall();
        }

        for (uint256 i; i < poolTokens_.length; i++) {
            if (poolTokenDatas_[i].feedType != FeedType.Stork) {
                revert InvalidType();
            }
            if (poolTokenDatas_[i].staleSeconds == 0) {
                revert InvalidStaleSeconds();
            }

            StorkData storage tokenData = storkData[poolTokens_[i]];

            FeedType currentType = tokenData.feedType;
            if (currentType == FeedType.Undefined) {
                tokenData.feedType = poolTokenDatas_[i].feedType;
            } else {
                if (poolTokenDatas_[i].feedType != currentType) {
                    // we can't change the FeedType once it is set
                    revert TypeMismatch();
                }
            }

            tokenData.feedHash = poolTokenDatas_[i].feedHash;
            tokenData.staleSeconds = poolTokenDatas_[i].staleSeconds;

            emit SetPriceFeedData(poolTokenDatas_[i].feedType, poolTokenDatas_[i].feedHash, poolTokenDatas_[i].staleSeconds);
        }
    }
}

// SPDX-License-Identifier: GPL-3.0-only

pragma solidity 0.8.26;

interface IStork {
    struct TemporalNumericValue {
        // slot 1
        // nanosecond level precision timestamp of latest publisher update in batch
        uint64 timestampNs; // 8 bytes
        // should be able to hold all necessary numbers (up to 6277101735386680763835789423207666416102355444464034512895)
        int192 quantizedValue; // 8 bytes
    }

    function getTemporalNumericValueV1(bytes32 id) external view returns (TemporalNumericValue memory value);
    function getTemporalNumericValueUnsafeV1(bytes32 id) external view returns (TemporalNumericValue memory value);
}

// SPDX-License-Identifier: GPL-3.0-only

pragma solidity 0.8.26;

interface IPriceFeeds {

    event SetPriceFeedData(FeedType feedType, bytes32 feedHash, uint256 staleSeconds);

    error ZeroAddress();
    error CountMismatch();
    error InvalidCall();
    error InvalidType();
    error TypeMismatch();
    error InvalidStaleSeconds();

    enum FeedType {
        Undefined,
        Stork
    }

    struct StorkData {
        bytes32 feedHash;
        uint256 staleSeconds;
        FeedType feedType;
    }

    function getPrice(address poolToken_) external view returns (uint256);

    function getStorkPrice(bytes32 feedHash_, uint256 staleSeconds_) external view returns (uint256);

    function setStorkFeedsData(address farmContract, address[] memory poolTokens_, StorkData[] memory poolTokenDatas_) external;
}

// SPDX-License-Identifier: GPL-3.0-only
pragma solidity 0.8.26;

import "contracts/access/Ownable2Step.sol";

event ReplaceImplementationStarted(address indexed previousImplementation, address indexed newImplementation);
event ReplaceImplementation(address indexed previousImplementation, address indexed newImplementation);
error Unauthorized();

contract Upgradeable2Step is Ownable2Step {
    address public pendingImplementation;
    address public implementation;

    constructor() Ownable(msg.sender) {}

    // called on an inheriting proxy contract
    function replaceImplementation(address impl_) public onlyOwner {
        pendingImplementation = impl_;
        emit ReplaceImplementationStarted(implementation, impl_);
    }

    // called from an inheriting implementation contract
    function acceptImplementation() public {
        if (msg.sender != pendingImplementation) {
            revert OwnableUnauthorizedAccount(msg.sender);
        }
        emit ReplaceImplementation(implementation, msg.sender);
        delete pendingImplementation;
        implementation = msg.sender;
    }

    // called on an inheriting implementation contract
    function becomeImplementation(Upgradeable2Step proxy) public {
        if (msg.sender != proxy.owner()) {
            revert Unauthorized();
        }
        proxy.acceptImplementation();
    }
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable2Step.sol)

pragma solidity ^0.8.20;

import {Ownable} from "contracts/access/Ownable.sol";

/**
 * @dev Contract module which provides access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * The initial owner is specified at deployment time in the constructor for `Ownable`. This
 * can later be changed with {transferOwnership} and {acceptOwnership}.
 *
 * This module is used through inheritance. It will make available all functions
 * from parent (Ownable).
 */
abstract contract Ownable2Step is Ownable {
    address private _pendingOwner;

    event OwnershipTransferStarted(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Returns the address of the pending owner.
     */
    function pendingOwner() public view virtual returns (address) {
        return _pendingOwner;
    }

    /**
     * @dev Starts the ownership transfer of the contract to a new account. Replaces the pending transfer if there is one.
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual override onlyOwner {
        _pendingOwner = newOwner;
        emit OwnershipTransferStarted(owner(), newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`) and deletes any pending owner.
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual override {
        delete _pendingOwner;
        super._transferOwnership(newOwner);
    }

    /**
     * @dev The new owner accepts the ownership transfer.
     */
    function acceptOwnership() public virtual {
        address sender = _msgSender();
        if (pendingOwner() != sender) {
            revert OwnableUnauthorizedAccount(sender);
        }
        _transferOwnership(sender);
    }
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)

pragma solidity ^0.8.20;

import {Context} from "contracts/utils/Context.sol";

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * The initial owner is set to the address provided by the deployer. This can
 * later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    /**
     * @dev The caller account is not authorized to perform an operation.
     */
    error OwnableUnauthorizedAccount(address account);

    /**
     * @dev The owner is not a valid owner account. (eg. `address(0)`)
     */
    error OwnableInvalidOwner(address owner);

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the address provided by the deployer as the initial owner.
     */
    constructor(address initialOwner) {
        if (initialOwner == address(0)) {
            revert OwnableInvalidOwner(address(0));
        }
        _transferOwnership(initialOwner);
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        if (owner() != _msgSender()) {
            revert OwnableUnauthorizedAccount(_msgSender());
        }
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby disabling any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        if (newOwner == address(0)) {
            revert OwnableInvalidOwner(address(0));
        }
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)

pragma solidity ^0.8.20;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }

    function _contextSuffixLength() internal view virtual returns (uint256) {
        return 0;
    }
}

Please enter a contract address above to load the contract details and source code.

Context size (optional):