forked from Synthetixio/synthetix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSynthUtil.sol
98 lines (87 loc) · 3.67 KB
/
SynthUtil.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
pragma solidity ^0.5.16;
// Inheritance
import "./interfaces/ISynth.sol";
import "./interfaces/ISynthetix.sol";
import "./interfaces/IExchangeRates.sol";
import "./interfaces/IAddressResolver.sol";
import "./interfaces/IERC20.sol";
// https://docs.synthetix.io/contracts/source/contracts/synthutil
contract SynthUtil {
IAddressResolver public addressResolverProxy;
bytes32 internal constant CONTRACT_SYNTHETIX = "Synthetix";
bytes32 internal constant CONTRACT_EXRATES = "ExchangeRates";
bytes32 internal constant SUSD = "sUSD";
constructor(address resolver) public {
addressResolverProxy = IAddressResolver(resolver);
}
function _synthetix() internal view returns (ISynthetix) {
return ISynthetix(addressResolverProxy.requireAndGetAddress(CONTRACT_SYNTHETIX, "Missing Synthetix 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) {
ISynthetix synthetix = _synthetix();
IExchangeRates exchangeRates = _exchangeRates();
uint numSynths = synthetix.availableSynthCount();
for (uint i = 0; i < numSynths; i++) {
ISynth synth = synthetix.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
)
{
ISynthetix synthetix = _synthetix();
IExchangeRates exchangeRates = _exchangeRates();
uint numSynths = synthetix.availableSynthCount();
bytes32[] memory currencyKeys = new bytes32[](numSynths);
uint[] memory balances = new uint[](numSynths);
uint[] memory sUSDBalances = new uint[](numSynths);
for (uint i = 0; i < numSynths; i++) {
ISynth synth = synthetix.availableSynths(i);
currencyKeys[i] = synth.currencyKey();
balances[i] = IERC20(address(synth)).balanceOf(account);
sUSDBalances[i] = exchangeRates.effectiveValue(currencyKeys[i], balances[i], SUSD);
}
return (currencyKeys, balances, sUSDBalances);
}
function synthsRates() external view returns (bytes32[] memory, uint[] memory) {
bytes32[] memory currencyKeys = _synthetix().availableCurrencyKeys();
return (currencyKeys, _exchangeRates().ratesForCurrencies(currencyKeys));
}
function synthsTotalSupplies()
external
view
returns (
bytes32[] memory,
uint256[] memory,
uint256[] memory
)
{
ISynthetix synthetix = _synthetix();
IExchangeRates exchangeRates = _exchangeRates();
uint256 numSynths = synthetix.availableSynthCount();
bytes32[] memory currencyKeys = new bytes32[](numSynths);
uint256[] memory balances = new uint256[](numSynths);
uint256[] memory sUSDBalances = new uint256[](numSynths);
for (uint256 i = 0; i < numSynths; i++) {
ISynth synth = synthetix.availableSynths(i);
currencyKeys[i] = synth.currencyKey();
balances[i] = IERC20(address(synth)).totalSupply();
sUSDBalances[i] = exchangeRates.effectiveValue(currencyKeys[i], balances[i], SUSD);
}
return (currencyKeys, balances, sUSDBalances);
}
}