Skip to content

Commit

Permalink
deploying 🧀
Browse files Browse the repository at this point in the history
  • Loading branch information
dysbulic committed Jul 2, 2024
1 parent 5b73075 commit 2ad97c6
Show file tree
Hide file tree
Showing 8 changed files with 370 additions and 139 deletions.
222 changes: 222 additions & 0 deletions abis/ERC20.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
[
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_spender",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_from",
"type": "address"
},
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [
{
"name": "",
"type": "uint8"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"name": "balance",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
},
{
"name": "_spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"payable": true,
"stateMutability": "payable",
"type": "fallback"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "owner",
"type": "address"
},
{
"indexed": true,
"name": "spender",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "from",
"type": "address"
},
{
"indexed": true,
"name": "to",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
}
]
19 changes: 19 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,22 @@ type ProxyUpdated @entity(immutable: true) {
blockTimestamp: BigInt!
transactionHash: Bytes!
}

type Token @entity {
id: ID!
name: String!
symbol: String!
decimals: BigDecimal!
}

type Account @entity {
id: ID!
balances: [TokenBalance!]! @derivedFrom(field: "account")
}

type TokenBalance @entity {
id: ID!
token: Token!
account: Account!
amount: BigDecimal!
}
41 changes: 41 additions & 0 deletions src/erc-20.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Transfer } from "../generated/SEED-ERC20/ERC20"
import { TokenBalance } from "../generated/schema"
import {
fetchTokenDetails,
fetchAccount,
fetchBalance
} from "./utils"
import { BigDecimal } from "@graphprotocol/graph-ts"

export function handleTransfer(event: Transfer): void {
let token = fetchTokenDetails(event)
if(!token) return

let fromAddress = event.params.from.toHex()
let toAddress = event.params.to.toHex()

let fromAccount = fetchAccount(fromAddress)
let toAccount = fetchAccount(toAddress)

let fromTokenBalance = TokenBalance.load(token.id + "-" + fromAccount.id)
if (!fromTokenBalance) {
fromTokenBalance = new TokenBalance(token.id + "-" + fromAccount.id)
fromTokenBalance.token = token.id
fromTokenBalance.account = fromAccount.id
}
fromTokenBalance.amount = fetchBalance(event.address, event.params.from)
if(fromTokenBalance.amount != BigDecimal.zero()) {
fromTokenBalance.save()
}

let toTokenBalance = TokenBalance.load(token.id + "-" + toAccount.id)
if (!toTokenBalance) {
toTokenBalance = new TokenBalance(token.id + "-" + toAccount.id)
toTokenBalance.token = token.id
toTokenBalance.account = toAccount.id
}
toTokenBalance.amount = fetchBalance(event.address, event.params.to)
if (toTokenBalance.amount != BigDecimal.zero()) {
toTokenBalance.save()
}
}
33 changes: 0 additions & 33 deletions src/u-child-erc-20-proxy.ts

This file was deleted.

56 changes: 56 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { ERC20 } from "../generated/SEED-ERC20/ERC20"
import { Account, Token } from "../generated/schema"
import { BigDecimal, ethereum, Address } from "@graphprotocol/graph-ts"

export function fetchTokenDetails(event: ethereum.Event): Token | null {
let token = Token.load(event.address.toHex())
if (!token) {
token = new Token(event.address.toHex())

token.name = "Uɴᴋɴᴏᴡɴ"
token.symbol = "𝑈𝑛𝑘𝑛𝑜𝑤𝑛"
token.decimals = BigDecimal.zero()

let erc20 = ERC20.bind(event.address)

let tokenName = erc20.try_name()
if (!tokenName.reverted) {
token.name = tokenName.value
}

let tokenSymbol = erc20.try_symbol()
if (!tokenSymbol.reverted) {
token.symbol = tokenSymbol.value
}

let tokenDecimal = erc20.try_decimals()
if (!tokenDecimal.reverted) {
token.decimals = BigDecimal.fromString(tokenDecimal.value.toString())
}

token.save()
}
return token
}

export function fetchAccount(address: string): Account {
let account = Account.load(address)
if (!account) {
account = new Account(address)
account.save()
}
if(!account) throw new Error(`Failed to fetch account: "${address}".`)
return account
}

export function fetchBalance(
tokenAddress: Address,
accountAddress: Address
): BigDecimal {
let erc20 = ERC20.bind(tokenAddress)
let tokenBalance = erc20.try_balanceOf(accountAddress)
if(tokenBalance.reverted) {
throw new Error(`Failed to fetch balance: "${tokenBalance.reverted.toString()}".`)
}
return tokenBalance.value.toBigDecimal()
}
Loading

0 comments on commit 2ad97c6

Please sign in to comment.