Skip to content

Commit

Permalink
Exchange oracles
Browse files Browse the repository at this point in the history
  • Loading branch information
pahor167 committed Jan 17, 2024
1 parent f05448f commit 7568b20
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/protocol/contracts-0.8/common/UsingRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "@openzeppelin/contracts8/access/Ownable.sol";
import "@openzeppelin/contracts8/token/ERC20/IERC20.sol";

import "../../contracts/common/interfaces/IRegistry.sol";
import "../../contracts/stability/interfaces/ISortedOracles.sol";

contract UsingRegistry is Ownable {
event RegistrySet(address indexed registryAddress);
Expand Down Expand Up @@ -63,4 +64,8 @@ contract UsingRegistry is Ownable {
function getGoldToken() internal view returns (IERC20) {
return IERC20(registry.getAddressForOrDie(GOLD_TOKEN_REGISTRY_ID));
}

function getSortedOracles() internal view returns (ISortedOracles) {
return ISortedOracles(registry.getAddressForOrDie(SORTED_ORACLES_REGISTRY_ID));
}
}
93 changes: 93 additions & 0 deletions packages/protocol/contracts-0.8/stability/ExchangeOracles.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity >=0.8.0 <0.8.20;

// Note: This is not an exact copy of UsingRegistry or UsingRegistryV2 in the contract's folder
// because Mento's interfaces still don't support Solidity 0.8

import "@openzeppelin/contracts8/access/Ownable.sol";
import "@openzeppelin/contracts8/token/ERC20/IERC20.sol";

import "../../contracts/common/interfaces/IRegistry.sol";
import "../../contracts/common/Initializable.sol";
import "../../contracts/common/FixidityLib.sol";
import "../../contracts/stability/interfaces/ISortedOracles.sol";
import "../common/UsingRegistry.sol";

contract ExchangeOracles is Ownable, Initializable, UsingRegistry {
using FixidityLib for FixidityLib.Fraction;

uint256 private constant FIXED1_UINT = 1000000000000000000000000;

mapping(address => address) public defaultTokenExchangePair;

/**
* @notice Sets initialized == true on implementation contracts
* @param test Set to true to skip implementation initialization
*/
constructor(bool test) public Initializable(test) {}

/**
* @notice Used in place of the constructor to allow the contract to be upgradable via proxy.
*/
function initialize(address _registryAddress) external initializer {
_transferOwnership(msg.sender);
setRegistry(_registryAddress);
}

function getExchangeRate(address token1, address token2) public view returns (uint256) {
require(token1 != address(0), "token1 is 0");
require(token2 != address(0), "token2 is 0");
require(token1 != token2, "token1 is token2");

ISortedOracles sortedOracles = getSortedOracles();

address goldTokenAddress = address(getGoldToken());

uint256 rate1;
uint256 rate2;

if (token1 == goldTokenAddress) {
(rate2, ) = sortedOracles.medianRate(token2);
return rate2;
}

if (token2 == goldTokenAddress) {
(rate1, ) = sortedOracles.medianRate(token1);
return rate1;
}

(rate1, ) = sortedOracles.medianRate(token1);
(rate2, ) = sortedOracles.medianRate(token2);

if (rate1 == 0 || rate2 == 0) {
return 0;
}

return FixidityLib.newFixed(rate2).divide(FixidityLib.newFixed(rate1)).fromFixed();
}

function medianRate(address token) external view returns (uint256, uint256) {
address token2 = defaultTokenExchangePair[token];
require(token2 != address(0), "no default token pair");

return (getExchangeRate(token, token2), FIXED1_UINT);
}

function setTokenPair(address token1, address token2) external onlyOwner {
require(token1 != address(0), "token1 is 0");
require(token2 != address(0), "token2 is 0");

defaultTokenExchangePair[token1] = token2;
}

/**
* @notice Returns the storage, major, minor, and patch version of the contract.
* @return Storage version of the contract.
* @return Major version of the contract.
* @return Minor version of the contract.
* @return Patch version of the contract.
*/
function getVersionNumber() external pure returns (uint256, uint256, uint256, uint256) {
return (1, 1, 0, 0);
}
}

0 comments on commit 7568b20

Please sign in to comment.