Skip to content

Instantly share code, notes, and snippets.

@hav-noms
Created February 20, 2020 04:56
Show Gist options
  • Save hav-noms/71b6cc4471518bb8eb3588c7fac9502f to your computer and use it in GitHub Desktop.
Save hav-noms/71b6cc4471518bb8eb3588c7fac9502f to your computer and use it in GitHub Desktop.
DappHelper utility contract
/**
*Submitted for verification at Etherscan.io on 2020-02-14
*/
pragma solidity 0.4.25;
contract ISynth {
bytes32 public currencyKey;
function balanceOf(address owner) external view returns (uint);
}
contract ISynthetix {
ISynth[] public availableSynths;
function availableSynthCount() public view returns (uint);
function effectiveValue(bytes32 sourceCurrencyKey, uint sourceAmount, bytes32 destinationCurrencyKey)
public
view
returns (uint);
function availableCurrencyKeys()
public
view
returns (bytes32[]);
}
contract IExchangeRates {
function rateIsFrozen(bytes32 currencyKey) external view returns (bool);
function ratesForCurrencies(bytes32[] currencyKeys) external view returns (uint[] memory);
}
contract SynthSummaryUtil {
ISynthetix public synthetix;
IExchangeRates public exchangeRates;
constructor(address _synthetix, address _exchangeRates) public {
synthetix = ISynthetix(_synthetix);
exchangeRates = IExchangeRates(_exchangeRates);
}
function totalSynthsInKey(address account, bytes32 currencyKey) external view returns (uint total) {
uint numSynths = synthetix.availableSynthCount();
for (uint i = 0; i < numSynths; i++) {
ISynth synth = synthetix.availableSynths(i);
total += synthetix.effectiveValue(synth.currencyKey(), synth.balanceOf(account), currencyKey);
}
return total;
}
function synthsBalances(address account) external view returns (bytes32[], uint[], uint[]) {
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] = synth.balanceOf(account);
sUSDBalances[i] = synthetix.effectiveValue(synth.currencyKey(), synth.balanceOf(account), 'sUSD');
}
return (currencyKeys, balances, sUSDBalances);
}
function frozenSynths() external view returns (bytes32[]) {
uint numSynths = synthetix.availableSynthCount();
bytes32[] memory frozenSynthsKeys = new bytes32[](numSynths);
for (uint i = 0; i < numSynths; i++) {
ISynth synth = synthetix.availableSynths(i);
if (exchangeRates.rateIsFrozen(synth.currencyKey())) {
frozenSynthsKeys[i] = synth.currencyKey();
}
}
return frozenSynthsKeys;
}
function synthsRates() external view returns (bytes32[], uint[]) {
bytes32[] memory currencyKeys = synthetix.availableCurrencyKeys();
return (currencyKeys, exchangeRates.ratesForCurrencies(currencyKeys));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment