Skip to content

Instantly share code, notes, and snippets.

@alexroan
Created June 4, 2021 14:20
Show Gist options
  • Save alexroan/a81bc4166fa555148aa9528a6b61deac to your computer and use it in GitHub Desktop.
Save alexroan/a81bc4166fa555148aa9528a6b61deac to your computer and use it in GitHub Desktop.
Compare UAVs
from web3 import Web3
import requests, datetime
# Replace INFURA_URL with mainnet url
# Run this file with:
# `python3 compare_oracles.py`
w3 = Web3(Web3.HTTPProvider('INFURA_URL'))
prod_oracle='0x4007B71e01424b2314c020fB0344b03A7C499E1A'
proposed_oracle='0x841616a5CBA946CF415Efe8a326A621A794D0f97'
cTokens={
'bat': ['basic-attention-token', '0x6c8c6b02e7b2be14d4fa6022dfd6d75921d90e4e'],
'comp': ['compound-governance-token', '0x70e36f6bf80a52b3b46b3af8e106cc0ed743e8e4'],
'dai': ['dai', '0x5d3a536e4d6dbd6114cc1ead35777bab948e3643'],
'eth': ['ethereum', '0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5'],
'link': ['chainlink', '0xface851a4921ce59e912d19329929ce6da6eb0c7'],
'rep': ['augur', '0x158079ee67fce2f58472a96584a73c7ab9ac95c1'],
'sai': ['sai', '0xf5dce57282a584d2746faf1593d3121fcac444dc'],
'tusd': ['true-usd', '0x12392f67bdf24fae0af363c24ac620a2f67dad86'],
'uni': ['uniswap', '0x35a18000230da775cac24873d00ff85bccded550'],
'usdc': ['usd-coin', '0x39aa39c021dfbae8fac545936693ac917d5e7563'],
'usdt': ['tether', '0xf650c3d88d12db855b8bf7d11be6c55a4e07dcc9'],
'btc': ['bitcoin', '0xc11b1268c1a384e55c48c2391d8d480264a3a7f4'],
'btc2': ['bitcoin', '0xccf4429db6322d5c611ee964527d42e5d685dd6a'],
'zrx': ['0x', '0xb3319f5d18bc0d84dd1b4825dcde5d5f7266d407']
}
abi = '[{"inputs": [{"internalType": "uint256","name": "anchorToleranceMantissa_","type": "uint256"},{"internalType": "uint256","name": "anchorPeriod_","type": "uint256"},{"components": [{"internalType": "address","name": "cToken","type": "address"},{"internalType": "address","name": "underlying","type": "address"},{"internalType": "bytes32","name": "symbolHash","type": "bytes32"},{"internalType": "uint256","name": "baseUnit","type": "uint256"},{"internalType": "enum UniswapConfig.PriceSource","name": "priceSource","type": "uint8"},{"internalType": "uint256","name": "fixedPrice","type": "uint256"},{"internalType": "address","name": "uniswapMarket","type": "address"},{"internalType": "address","name": "reporter","type": "address"},{"internalType": "uint256","name": "reporterMultiplier","type": "uint256"},{"internalType": "bool","name": "isUniswapReversed","type": "bool"}],"internalType": "struct UniswapConfig.TokenConfig[]","name": "configs","type": "tuple[]"}],"stateMutability": "nonpayable","type": "constructor"},{"anonymous": false,"inputs": [{"indexed": true,"internalType": "bytes32","name": "symbolHash","type": "bytes32"},{"indexed": false,"internalType": "uint256","name": "anchorPrice","type": "uint256"},{"indexed": false,"internalType": "uint256","name": "oldTimestamp","type": "uint256"},{"indexed": false,"internalType": "uint256","name": "newTimestamp","type": "uint256"}],"name": "AnchorPriceUpdated","type": "event"},{"anonymous": false,"inputs": [{"indexed": true,"internalType": "bytes32","name": "symbolHash","type": "bytes32"}],"name": "FailoverActivated","type": "event"},{"anonymous": false,"inputs": [{"indexed": true,"internalType": "bytes32","name": "symbolHash","type": "bytes32"}],"name": "FailoverDeactivated","type": "event"},{"anonymous": false,"inputs": [{"indexed": true,"internalType": "address","name": "from","type": "address"},{"indexed": true,"internalType": "address","name": "to","type": "address"}],"name": "OwnershipTransferRequested","type": "event"},{"anonymous": false,"inputs": [{"indexed": true,"internalType": "address","name": "from","type": "address"},{"indexed": true,"internalType": "address","name": "to","type": "address"}],"name": "OwnershipTransferred","type": "event"},{"anonymous": false,"inputs": [{"indexed": true,"internalType": "bytes32","name": "symbolHash","type": "bytes32"},{"indexed": false,"internalType": "uint256","name": "reporter","type": "uint256"},{"indexed": false,"internalType": "uint256","name": "anchor","type": "uint256"}],"name": "PriceGuarded","type": "event"},{"anonymous": false,"inputs": [{"indexed": true,"internalType": "bytes32","name": "symbolHash","type": "bytes32"},{"indexed": false,"internalType": "uint256","name": "price","type": "uint256"}],"name": "PriceUpdated","type": "event"},{"anonymous": false,"inputs": [{"indexed": true,"internalType": "bytes32","name": "symbolHash","type": "bytes32"},{"indexed": false,"internalType": "uint256","name": "oldTimestamp","type": "uint256"},{"indexed": false,"internalType": "uint256","name": "newTimestamp","type": "uint256"},{"indexed": false,"internalType": "uint256","name": "oldPrice","type": "uint256"},{"indexed": false,"internalType": "uint256","name": "newPrice","type": "uint256"}],"name": "UniswapWindowUpdated","type": "event"},{"inputs": [],"name": "acceptOwnership","outputs": [],"stateMutability": "nonpayable","type": "function"},{"inputs": [{"internalType": "bytes32","name": "symbolHash","type": "bytes32"}],"name": "activateFailover","outputs": [],"stateMutability": "nonpayable","type": "function"},{"inputs": [],"name": "anchorPeriod","outputs": [{"internalType": "uint256","name": "","type": "uint256"}],"stateMutability": "view","type": "function"},{"inputs": [{"internalType": "bytes32","name": "symbolHash","type": "bytes32"}],"name": "deactivateFailover","outputs": [],"stateMutability": "nonpayable","type": "function"},{"inputs": [],"name": "ethBaseUnit","outputs": [{"internalType": "uint256","name": "","type": "uint256"}],"stateMutability": "view","type": "function"},{"inputs": [],"name": "expScale","outputs": [{"internalType": "uint256","name": "","type": "uint256"}],"stateMutability": "view","type": "function"},{"inputs": [{"internalType": "uint256","name": "i","type": "uint256"}],"name": "getTokenConfig","outputs": [{"components": [{"internalType": "address","name": "cToken","type": "address"},{"internalType": "address","name": "underlying","type": "address"},{"internalType": "bytes32","name": "symbolHash","type": "bytes32"},{"internalType": "uint256","name": "baseUnit","type": "uint256"},{"internalType": "enum UniswapConfig.PriceSource","name": "priceSource","type": "uint8"},{"internalType": "uint256","name": "fixedPrice","type": "uint256"},{"internalType": "address","name": "uniswapMarket","type": "address"},{"internalType": "address","name": "reporter","type": "address"},{"internalType": "uint256","name": "reporterMultiplier","type": "uint256"},{"internalType": "bool","name": "isUniswapReversed","type": "bool"}],"internalType": "struct UniswapConfig.TokenConfig","name": "","type": "tuple"}],"stateMutability": "view","type": "function"},{"inputs": [{"internalType": "address","name": "cToken","type": "address"}],"name": "getTokenConfigByCToken","outputs": [{"components": [{"internalType": "address","name": "cToken","type": "address"},{"internalType": "address","name": "underlying","type": "address"},{"internalType": "bytes32","name": "symbolHash","type": "bytes32"},{"internalType": "uint256","name": "baseUnit","type": "uint256"},{"internalType": "enum UniswapConfig.PriceSource","name": "priceSource","type": "uint8"},{"internalType": "uint256","name": "fixedPrice","type": "uint256"},{"internalType": "address","name": "uniswapMarket","type": "address"},{"internalType": "address","name": "reporter","type": "address"},{"internalType": "uint256","name": "reporterMultiplier","type": "uint256"},{"internalType": "bool","name": "isUniswapReversed","type": "bool"}],"internalType": "struct UniswapConfig.TokenConfig","name": "","type": "tuple"}],"stateMutability": "view","type": "function"},{"inputs": [{"internalType": "address","name": "reporter","type": "address"}],"name": "getTokenConfigByReporter","outputs": [{"components": [{"internalType": "address","name": "cToken","type": "address"},{"internalType": "address","name": "underlying","type": "address"},{"internalType": "bytes32","name": "symbolHash","type": "bytes32"},{"internalType": "uint256","name": "baseUnit","type": "uint256"},{"internalType": "enum UniswapConfig.PriceSource","name": "priceSource","type": "uint8"},{"internalType": "uint256","name": "fixedPrice","type": "uint256"},{"internalType": "address","name": "uniswapMarket","type": "address"},{"internalType": "address","name": "reporter","type": "address"},{"internalType": "uint256","name": "reporterMultiplier","type": "uint256"},{"internalType": "bool","name": "isUniswapReversed","type": "bool"}],"internalType": "struct UniswapConfig.TokenConfig","name": "","type": "tuple"}],"stateMutability": "view","type": "function"},{"inputs": [{"internalType": "string","name": "symbol","type": "string"}],"name": "getTokenConfigBySymbol","outputs": [{"components": [{"internalType": "address","name": "cToken","type": "address"},{"internalType": "address","name": "underlying","type": "address"},{"internalType": "bytes32","name": "symbolHash","type": "bytes32"},{"internalType": "uint256","name": "baseUnit","type": "uint256"},{"internalType": "enum UniswapConfig.PriceSource","name": "priceSource","type": "uint8"},{"internalType": "uint256","name": "fixedPrice","type": "uint256"},{"internalType": "address","name": "uniswapMarket","type": "address"},{"internalType": "address","name": "reporter","type": "address"},{"internalType": "uint256","name": "reporterMultiplier","type": "uint256"},{"internalType": "bool","name": "isUniswapReversed","type": "bool"}],"internalType": "struct UniswapConfig.TokenConfig","name": "","type": "tuple"}],"stateMutability": "view","type": "function"},{"inputs": [{"internalType": "bytes32","name": "symbolHash","type": "bytes32"}],"name": "getTokenConfigBySymbolHash","outputs": [{"components": [{"internalType": "address","name": "cToken","type": "address"},{"internalType": "address","name": "underlying","type": "address"},{"internalType": "bytes32","name": "symbolHash","type": "bytes32"},{"internalType": "uint256","name": "baseUnit","type": "uint256"},{"internalType": "enum UniswapConfig.PriceSource","name": "priceSource","type": "uint8"},{"internalType": "uint256","name": "fixedPrice","type": "uint256"},{"internalType": "address","name": "uniswapMarket","type": "address"},{"internalType": "address","name": "reporter","type": "address"},{"internalType": "uint256","name": "reporterMultiplier","type": "uint256"},{"internalType": "bool","name": "isUniswapReversed","type": "bool"}],"internalType": "struct UniswapConfig.TokenConfig","name": "","type": "tuple"}],"stateMutability": "view","type": "function"},{"inputs": [{"internalType": "address","name": "underlying","type": "address"}],"name": "getTokenConfigByUnderlying","outputs": [{"components": [{"internalType": "address","name": "cToken","type": "address"},{"internalType": "address","name": "underlying","type": "address"},{"internalType": "bytes32","name": "symbolHash","type": "bytes32"},{"internalType": "uint256","name": "baseUnit","type": "uint256"},{"internalType": "enum UniswapConfig.PriceSource","name": "priceSource","type": "uint8"},{"internalType": "uint256","name": "fixedPrice","type": "uint256"},{"internalType": "address","name": "uniswapMarket","type": "address"},{"internalType": "address","name": "reporter","type": "address"},{"internalType": "uint256","name": "reporterMultiplier","type": "uint256"},{"internalType": "bool","name": "isUniswapReversed","type": "bool"}],"internalType": "struct UniswapConfig.TokenConfig","name": "","type": "tuple"}],"stateMutability": "view","type": "function"},{"inputs": [{"internalType": "address","name": "cToken","type": "address"}],"name": "getUnderlyingPrice","outputs": [{"internalType": "uint256","name": "","type": "uint256"}],"stateMutability": "view","type": "function"},{"inputs": [],"name": "lowerBoundAnchorRatio","outputs": [{"internalType": "uint256","name": "","type": "uint256"}],"stateMutability": "view","type": "function"},{"inputs": [],"name": "maxTokens","outputs": [{"internalType": "uint256","name": "","type": "uint256"}],"stateMutability": "view","type": "function"},{"inputs": [{"internalType": "bytes32","name": "","type": "bytes32"}],"name": "newObservations","outputs": [{"internalType": "uint256","name": "timestamp","type": "uint256"},{"internalType": "uint256","name": "acc","type": "uint256"}],"stateMutability": "view","type": "function"},{"inputs": [],"name": "numTokens","outputs": [{"internalType": "uint256","name": "","type": "uint256"}],"stateMutability": "view","type": "function"},{"inputs": [{"internalType": "bytes32","name": "","type": "bytes32"}],"name": "oldObservations","outputs": [{"internalType": "uint256","name": "timestamp","type": "uint256"},{"internalType": "uint256","name": "acc","type": "uint256"}],"stateMutability": "view","type": "function"},{"inputs": [],"name": "owner","outputs": [{"internalType": "address","name": "","type": "address"}],"stateMutability": "view","type": "function"},{"inputs": [{"internalType": "bytes32","name": "symbolHash","type": "bytes32"}],"name": "pokeFailedOverPrice","outputs": [],"stateMutability": "nonpayable","type": "function"},{"inputs": [{"internalType": "string","name": "symbol","type": "string"}],"name": "price","outputs": [{"internalType": "uint256","name": "","type": "uint256"}],"stateMutability": "view","type": "function"},{"inputs": [{"internalType": "bytes32","name": "","type": "bytes32"}],"name": "prices","outputs": [{"internalType": "uint248","name": "price","type": "uint248"},{"internalType": "bool","name": "failoverActive","type": "bool"}],"stateMutability": "view","type": "function"},{"inputs": [{"internalType": "address","name": "to","type": "address"}],"name": "transferOwnership","outputs": [],"stateMutability": "nonpayable","type": "function"},{"inputs": [],"name": "upperBoundAnchorRatio","outputs": [{"internalType": "uint256","name": "","type": "uint256"}],"stateMutability": "view","type": "function"},{"inputs": [{"internalType": "uint256","name": "","type": "uint256"},{"internalType": "int256","name": "","type": "int256"},{"internalType": "uint256","name": "","type": "uint256"},{"internalType": "int256","name": "currentAnswer","type": "int256"}],"name": "validate","outputs": [{"internalType": "bool","name": "valid","type": "bool"}],"stateMutability": "nonpayable","type": "function"}]'
prod_uav = w3.eth.contract(address=prod_oracle, abi=abi)
proposed_uav = w3.eth.contract(address=proposed_oracle, abi=abi)
print('Values retrieved at: {}'.format(datetime.datetime.now()))
for key in cTokens:
token = cTokens[key]
id = token[0]
url = 'https://api.coingecko.com/api/v3/simple/price?ids={}&vs_currencies=usd'.format(id)
r = requests.get(url)
data = r.json()
float_coingecko_price = float(data[id]['usd'])
transformed_coingecko_price = float_coingecko_price * 1e18
address = Web3.toChecksumAddress(token[1])
prod_price = prod_uav.functions.getUnderlyingPrice(address).call()
proposed_price = proposed_uav.functions.getUnderlyingPrice(address).call()
print(key)
print('production: {}'.format(prod_price))
print('proposed: {}'.format(proposed_price))
print('coingecko: {}'.format(transformed_coingecko_price))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment