Skip to content

Commit

Permalink
schema: implement price oracle handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
facuspagnuolo committed Aug 14, 2023
1 parent 61f2a2b commit f1fee69
Show file tree
Hide file tree
Showing 7 changed files with 450 additions and 1 deletion.
224 changes: 224 additions & 0 deletions abis/IERC20.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
[
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "from",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "to",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
},
{
"inputs": [
{
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"internalType": "address",
"name": "spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "account",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "decimals",
"outputs": [
{
"internalType": "uint8",
"name": "",
"type": "uint8"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "name",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "symbol",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
}
]
23 changes: 23 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ type PriceOracle @entity {
name: String!
implementation: Implementation!
environment: Environment!
feeds: [PriceOracleFeed!] @derivedFrom(field: "priceOracle")
signers: [PriceOracleSigner!] @derivedFrom(field: "priceOracle")
}

type PriceOracleSigner @entity {
id: ID!
priceOracle: PriceOracle!
signer: String!
}

type PriceOracleFeed @entity {
id: ID!
priceOracle: PriceOracle!
base: ERC20!
quote: ERC20!
feed: String!
}

type Task @entity {
Expand All @@ -63,3 +79,10 @@ type Implementation @entity {
stateless: Boolean!
deprecated: Boolean!
}

type ERC20 @entity {
id: ID!
name: String!
symbol: String!
decimals: Int!
}
4 changes: 3 additions & 1 deletion src/Deployer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Address, Bytes, crypto, log } from '@graphprotocol/graph-ts'

import { Authorizer as AuthorizerTemplate } from '../types/templates'
import { Authorizer as AuthorizerTemplate, PriceOracle as PriceOracleTemplate } from '../types/templates'
import { Authorizer, Environment, PriceOracle, SmartVault, Task } from '../types/schema'
import { AuthorizerDeployed, PriceOracleDeployed, SmartVaultDeployed, TaskDeployed } from '../types/Deployer/Deployer'

Expand Down Expand Up @@ -34,6 +34,8 @@ export function handlePriceOracleDeployed(event: PriceOracleDeployed): void {
priceOracle.implementation = implementation.id
priceOracle.environment = environment.id
priceOracle.save()

PriceOracleTemplate.create(event.params.instance)
}

export function handleSmartVaultDeployed(event: SmartVaultDeployed): void {
Expand Down
96 changes: 96 additions & 0 deletions src/ERC20.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { Address, log } from '@graphprotocol/graph-ts'

import { ERC20 as ERC20Entity } from '../types/schema'
import { ERC20 as ERC20Contract } from '../types/templates/PriceOracle/ERC20'

import { isAvalanche, isBinance, isEthNetwork, isFantom, isGnosis, isMaticNetwork } from './Networks'

const NATIVE_TOKEN_ADDRESS = Address.fromString('0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee')

export function loadOrCreateNativeToken(): ERC20Entity {
let id = NATIVE_TOKEN_ADDRESS.toHexString()
let erc20 = ERC20Entity.load(id)

if (erc20 === null) {
erc20 = new ERC20Entity(id)
erc20.name = getNativeTokenName()
erc20.symbol = getNativeTokenSymbol()
erc20.decimals = 18
erc20.save()
}

return erc20
}

export function loadOrCreateERC20(address: Address): ERC20Entity {
if (address.equals(NATIVE_TOKEN_ADDRESS)) return loadOrCreateNativeToken()

let id = address.toHexString()
let erc20 = ERC20Entity.load(id)

if (erc20 === null) {
erc20 = new ERC20Entity(id)
erc20.name = getERC20Name(address)
erc20.symbol = getERC20Symbol(address)
erc20.decimals = getERC20Decimals(address)
erc20.save()
}

return erc20
}

export function getERC20Decimals(address: Address): i32 {
let erc20Contract = ERC20Contract.bind(address)
let decimalsCall = erc20Contract.try_decimals()

if (!decimalsCall.reverted) {
return decimalsCall.value
}

log.warning('decimals() call reverted for {}', [address.toHexString()])
return 0
}

export function getERC20Name(address: Address): string {
let erc20Contract = ERC20Contract.bind(address)
let nameCall = erc20Contract.try_name()

if (!nameCall.reverted) {
return nameCall.value
}

log.warning('name() call reverted for {}', [address.toHexString()])
return 'Unknown'
}

export function getERC20Symbol(address: Address): string {
let erc20Contract = ERC20Contract.bind(address)
let symbolCall = erc20Contract.try_symbol()

if (!symbolCall.reverted) {
return symbolCall.value
}

log.warning('symbol() call reverted for {}', [address.toHexString()])
return 'Unknown'
}

export function getNativeTokenSymbol(): string {
if (isEthNetwork()) return 'ETH'
if (isMaticNetwork()) return 'MATIC'
if (isAvalanche()) return 'AVAX'
if (isBinance()) return 'BNB'
if (isFantom()) return 'FTM'
if (isGnosis()) return 'DAI'
return 'Unknown'
}

export function getNativeTokenName(): string {
if (isEthNetwork()) return 'Ether'
if (isMaticNetwork()) return 'Matic'
if (isAvalanche()) return 'Avax'
if (isBinance()) return 'BNB'
if (isFantom()) return 'Fantom'
if (isGnosis()) return 'Dai'
return 'Unknown'
}
Loading

0 comments on commit f1fee69

Please sign in to comment.