-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
schema: implement price oracle handlers
- Loading branch information
1 parent
2bb8db2
commit 7785352
Showing
8 changed files
with
457 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Address, log } from '@graphprotocol/graph-ts' | ||
|
||
import { ERC20 as ERC20Entity } from '../types/schema' | ||
import { ERC20 as ERC20Contract } from '../types/templates/PriceOracle/ERC20' | ||
import { getNativeTokenName, getNativeTokenSymbol } from './Tokens' | ||
|
||
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' | ||
} |
Oops, something went wrong.