pragma solidity ^0.5.16; // Inheritance import "./interfaces/ISynth.sol"; import "./interfaces/IOikos.sol"; import "./interfaces/IExchangeRates.sol"; import "./interfaces/IAddressResolver.sol"; import "./interfaces/IERC20.sol"; // https://docs.oikos.cash/contracts/source/contracts/synthutil contract SynthUtil { IAddressResolver public addressResolverProxy; bytes32 internal constant CONTRACT_SYNTHETIX = "Oikos"; bytes32 internal constant CONTRACT_EXRATES = "ExchangeRates"; bytes32 internal constant SUSD = "oUSD"; constructor(address resolver) public { addressResolverProxy = IAddressResolver(resolver); } function _oikos() internal view returns (IOikos) { return IOikos(addressResolverProxy.requireAndGetAddress(CONTRACT_SYNTHETIX, "Missing Oikos address")); } function _exchangeRates() internal view returns (IExchangeRates) { return IExchangeRates(addressResolverProxy.requireAndGetAddress(CONTRACT_EXRATES, "Missing ExchangeRates address")); } function totalSynthsInKey(address account, bytes32 currencyKey) external view returns (uint total) { IOikos oikos = _oikos(); IExchangeRates exchangeRates = _exchangeRates(); uint numSynths = oikos.availableSynthCount(); for (uint i = 0; i < numSynths; i++) { ISynth synth = oikos.availableSynths(i); total += exchangeRates.effectiveValue( synth.currencyKey(), IERC20(address(synth)).balanceOf(account), currencyKey ); } return total; } function synthsBalances(address account) external view returns ( bytes32[] memory, uint[] memory, uint[] memory ) { IOikos oikos = _oikos(); IExchangeRates exchangeRates = _exchangeRates(); uint numSynths = oikos.availableSynthCount(); bytes32[] memory currencyKeys = new bytes32[](numSynths); uint[] memory balances = new uint[](numSynths); uint[] memory oUSDBalances = new uint[](numSynths); for (uint i = 0; i < numSynths; i++) { ISynth synth = oikos.availableSynths(i); currencyKeys[i] = synth.currencyKey(); balances[i] = IERC20(address(synth)).balanceOf(account); oUSDBalances[i] = exchangeRates.effectiveValue(currencyKeys[i], balances[i], SUSD); } return (currencyKeys, balances, oUSDBalances); } function synthsRates() external view returns (bytes32[] memory, uint[] memory) { bytes32[] memory currencyKeys = _oikos().availableCurrencyKeys(); return (currencyKeys, _exchangeRates().ratesForCurrencies(currencyKeys)); } function synthsTotalSupplies() external view returns ( bytes32[] memory, uint256[] memory, uint256[] memory ) { IOikos oikos = _oikos(); IExchangeRates exchangeRates = _exchangeRates(); uint256 numSynths = oikos.availableSynthCount(); bytes32[] memory currencyKeys = new bytes32[](numSynths); uint256[] memory balances = new uint256[](numSynths); uint256[] memory oUSDBalances = new uint256[](numSynths); for (uint256 i = 0; i < numSynths; i++) { ISynth synth = oikos.availableSynths(i); currencyKeys[i] = synth.currencyKey(); balances[i] = IERC20(address(synth)).totalSupply(); oUSDBalances[i] = exchangeRates.effectiveValue(currencyKeys[i], balances[i], SUSD); } return (currencyKeys, balances, oUSDBalances); } }