S Price: $0.881736 (+2.83%)

Contract Diff Checker

Contract Name:
ChainlinkPriceFeedMock

Contract Source Code:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

/*
 * A Mocker contract that returns multiplied answer every X blocks as set by values
 */
contract ChainlinkPriceFeedMock is AggregatorV3Interface {
  // mocked values returned by contract:
  uint80 internal s_roundId;
  int256 internal s_answer;
  uint256 internal s_startedAt;
  uint256 internal s_updatedAt;
  uint80 internal s_answeredInRound;

  // stepChange - added to base value multiplied by ( blocks changed / stepBlocks) to set a pace to change.
  int256 internal s_stepChange;
  // stepBlocks - sets the pace of value change, the amounts of blocks between every change applied to value.
  uint256 internal s_stepBlocks;
  // block - the block at the last time the values were changed, where we start the count from.
  uint256 internal s_block;

  constructor(
    uint80 roundId,
    int256 answer,
    uint256 startedAt,
    uint256 updatedAt,
    uint80 answeredInRound,
    int256 stepChange,
    uint256 stepBlocks
  ) public {
    s_roundId = roundId;
    s_answer = answer;
    s_startedAt = startedAt;
    s_updatedAt = updatedAt;
    s_answeredInRound = answeredInRound;
    s_stepChange = stepChange;
    s_stepBlocks = stepBlocks;
    s_block = block.number;
  }

  // Used to modoify mocking values
  function setValues(
    uint80 roundId,
    int256 answer,
    uint256 startedAt,
    uint256 updatedAt,
    uint80 answeredInRound,
    int256 stepChange,
    uint256 stepBlocks
  ) public {
    s_roundId = roundId;
    s_answer = answer;
    s_startedAt = startedAt;
    s_updatedAt = updatedAt;
    s_answeredInRound = answeredInRound;
    s_stepChange = stepChange;
    s_stepBlocks = stepBlocks;
    s_block = block.number;
  }

  /*
     Implement AggregatorV3Interface:
    */
  function decimals() public pure override returns (uint8) {
    return 0;
  }

  function description() public pure override returns (string memory) {
    return "voltaile value aggregator mocker";
  }

  function version() public pure override returns (uint256) {
    return 0;
  }

  function getRoundData(
    uint80 _roundId
  ) public view override returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound) {
    int256 direction = 1;
    bool NegativeCycle = ((block.number - s_block) % (2 * s_stepBlocks)) > s_stepBlocks;
    if (NegativeCycle) {
      direction = -1;
    }

    int256 pace = 0;
    if (s_stepBlocks != 0) {
      pace = int256(((block.number - s_block) / s_stepBlocks));
    }
    int256 mocked_answer = s_answer + direction * s_stepChange * pace;
    return (_roundId, mocked_answer, s_startedAt, s_updatedAt, s_answeredInRound);
  }

  function latestRoundData()
    public
    view
    override
    returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound)
  {
    int256 mocked_answer = s_answer;
    if (s_stepBlocks != 0) {
      mocked_answer = s_answer + s_stepChange * int256(((block.number - s_block) / s_stepBlocks));
    }
    return (s_roundId, mocked_answer, s_startedAt, s_updatedAt, s_answeredInRound);
  }
}

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface AggregatorV3Interface {
  function decimals() external view returns (uint8);

  function description() external view returns (string memory);

  function version() external view returns (uint256);

  function getRoundData(uint80 _roundId)
    external
    view
    returns (
      uint80 roundId,
      int256 answer,
      uint256 startedAt,
      uint256 updatedAt,
      uint80 answeredInRound
    );

  function latestRoundData()
    external
    view
    returns (
      uint80 roundId,
      int256 answer,
      uint256 startedAt,
      uint256 updatedAt,
      uint80 answeredInRound
    );
}

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

Context size (optional):