Skip to content

Commit

Permalink
Merge pull request #2 from aavegotchi/feat/atokens-optimize
Browse files Browse the repository at this point in the history
Feat/atokens optimize
  • Loading branch information
froid1911 authored Oct 30, 2023
2 parents a3337b8 + 1166df2 commit 508f57d
Show file tree
Hide file tree
Showing 4 changed files with 343 additions and 126 deletions.
46 changes: 2 additions & 44 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ type Account @entity {
id: Bytes!
asERC20: ERC20Contract
ERC20balances: [ERC20Balance!]! @derivedFrom(field: "account")
ERC20approvalsOwner: [ERC20Approval!]! @derivedFrom(field: "owner")
ERC20approvalsSpender: [ERC20Approval!]! @derivedFrom(field: "spender")
ERC20transferFromEvent: [ERC20Transfer!]! @derivedFrom(field: "from")
ERC20transferToEvent: [ERC20Transfer!]! @derivedFrom(field: "to")
events: [Event!]! @derivedFrom(field: "emitter")
}

type ERC20Contract @entity(immutable: true) {
id: Bytes!
asAccount: Account!
Expand All @@ -17,50 +13,12 @@ type ERC20Contract @entity(immutable: true) {
burned: ERC20Balance!
totalSupply: ERC20Balance!
balances: [ERC20Balance!]! @derivedFrom(field: "contract")
approvals: [ERC20Approval!]! @derivedFrom(field: "contract")
transfers: [ERC20Transfer!]! @derivedFrom(field: "contract")
}

type ERC20Balance @entity {
id: ID!
contract: ERC20Contract!
account: Account
value: BigDecimal!
valueExact: BigInt!
transferFromEvent: [ERC20Transfer!]! @derivedFrom(field: "fromBalance")
transferToEvent: [ERC20Transfer!]! @derivedFrom(field: "toBalance")
}
type ERC20Approval @entity {
id: ID!
contract: ERC20Contract!
owner: Account!
spender: Account!
value: BigDecimal!
valueExact: BigInt!
}
type ERC20Transfer implements Event @entity(immutable: true) {
id: ID!
emitter: Account!
transaction: Transaction!
timestamp: BigInt!
contract: ERC20Contract!
from: Account
fromBalance: ERC20Balance
to: Account
toBalance: ERC20Balance
value: BigDecimal!
valueExact: BigInt!
}

interface Event {
id: ID!
transaction: Transaction!
emitter: Account!
timestamp: BigInt!
}

type Transaction @entity(immutable: true) {
id: ID!
timestamp: BigInt!
blockNumber: BigInt!
events: [Event!]! @derivedFrom(field: "transaction")
}
43 changes: 0 additions & 43 deletions src/erc20.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
import { ERC20Transfer } from "../generated/schema";

import {
Transfer as TransferEvent,
Approval as ApprovalEvent,
} from "@openzeppelin/subgraphs/generated/erc20/IERC20";

import {
constants,
decimals,
events,
transactions,
} from "@amxx/graphprotocol-utils";

import { fetchAccount } from "./fetch/account";

import {
fetchERC20,
fetchERC20Balance,
fetchERC20Approval,
} from "./fetch/erc20";
import { ADDRESS_BURN } from "./constants";

export function handleTransfer(event: TransferEvent): void {
let contract = fetchERC20(event.address);
let ev = new ERC20Transfer(events.id(event));
ev.emitter = contract.id;
ev.transaction = transactions.log(event).id;
ev.timestamp = event.block.timestamp;
ev.contract = contract.id;
ev.value = decimals.toDecimals(event.params.value, contract.decimals);
ev.valueExact = event.params.value;

if (event.params.from == constants.ADDRESS_ZERO) {
let totalSupply = fetchERC20Balance(contract, null);
totalSupply.valueExact = totalSupply.valueExact.plus(
Expand All @@ -50,9 +36,6 @@ export function handleTransfer(event: TransferEvent): void {
contract.decimals
);
balance.save();

ev.from = from.id;
ev.fromBalance = balance.id;
}

if (
Expand Down Expand Up @@ -90,31 +73,5 @@ export function handleTransfer(event: TransferEvent): void {
contract.decimals
);
balance.save();

ev.to = to.id;
ev.toBalance = balance.id;
}
ev.save();
}

export function handleApproval(event: ApprovalEvent): void {
let contract = fetchERC20(event.address);

let owner = fetchAccount(event.params.owner);
let spender = fetchAccount(event.params.spender);
let approval = fetchERC20Approval(contract, owner, spender);
approval.valueExact = event.params.value;
approval.value = decimals.toDecimals(event.params.value, contract.decimals);
approval.save();

// let ev = new ERC20ApprovalEvent(events.id(event))
// ev.emitter = contract.id
// ev.transaction = transactions.log(event).id
// ev.timestamp = event.block.timestamp
// ev.token = token.id
// ev.owner = owner.id
// ev.spender = spender.id
// ev.approval = approval.id
// ev.value = value.value
// ev.save()
}
28 changes: 1 addition & 27 deletions src/fetch/erc20.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, BigDecimal, BigInt } from "@graphprotocol/graph-ts";
import { Address } from "@graphprotocol/graph-ts";

import { IERC20 } from "../../generated/kek/IERC20";

Expand All @@ -9,7 +9,6 @@ import {
Account,
ERC20Contract,
ERC20Balance,
ERC20Approval,
} from "../../generated/schema";
import { ADDRESS_BURN } from "../constants";
export function fetchERC20(address: Address): ERC20Contract {
Expand Down Expand Up @@ -68,28 +67,3 @@ export function fetchERC20Balance(

return balance as ERC20Balance;
}

export function fetchERC20Approval(
contract: ERC20Contract,
owner: Account,
spender: Account
): ERC20Approval {
let id = contract.id
.toHex()
.concat("/")
.concat(owner.id.toHex())
.concat("/")
.concat(spender.id.toHex());
let approval = ERC20Approval.load(id);

if (approval == null) {
approval = new ERC20Approval(id);
approval.contract = contract.id;
approval.owner = owner.id;
approval.spender = spender.id;
approval.value = constants.BIGDECIMAL_ZERO;
approval.valueExact = constants.BIGINT_ZERO;
}

return approval as ERC20Approval;
}
Loading

0 comments on commit 508f57d

Please sign in to comment.