Skip to content

Instantly share code, notes, and snippets.

@schmidsi
Last active September 28, 2021 19:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schmidsi/8485cd55c6a1c098d1250d657d292e4c to your computer and use it in GitHub Desktop.
Save schmidsi/8485cd55c6a1c098d1250d657d292e4c to your computer and use it in GitHub Desktop.
Proxy Pattern
import { Address, log } from "@graphprotocol/graph-ts";
import {
ImplementationAdded,
} from "../generated/CallProxy/CallProxy";
import {
ProxyEvent,
} from "../generated/schema";
export function handleImplementationAdded(event: ImplementationAdded): void {
let id =
event.transaction.hash.toHexString() + ":" + event.logIndex.toHexString();
log.info("handleImplementationAdded - implementation: {}", [
event.params.implementation.toHexString(),
]);
let entity = new ProxyEvent(id);
entity.timestamp = event.block.timestamp;
entity.blockNumber = event.block.number;
entity.blockHash = event.block.hash;
entity.transactionHash = event.transaction.hash;
entity.implementation = event.params.implementation;
entity.initializer = event.params.initializer;
entity.finalize = event.params.finalize;
entity.type = "ADDED";
entity.save();
let contractAddress = "0x" + event.params.initializer.toHexString().slice(26);
Implementation.create(
Address.fromHexString(contractAddress) as Address
);
}
specVersion: 0.0.2
schema:
file: ./schema.graphql
dataSources:
- kind: ethereum/contract
name: Proxy
network: mainnet
source:
address: "0xdeadbeef"
abi: Proxy
startBlock: 0
mapping:
kind: ethereum/events
apiVersion: 0.0.5
language: wasm/assemblyscript
entities:
- ProxyEvent
abis:
- name: Proxy
file: ./abis/Proxy.json
eventHandlers:
- event: ImplementationAdded(indexed address,bytes,bool)
handler: handleImplementationAdded
- event: Upgraded(indexed address)
handler: handleUpgraded
file: ./src/mapping.ts
templates:
- kind: ethereum/contract
name: Implementation
network: mainnet
source:
abi: Implementation
mapping:
kind: ethereum/events
apiVersion: 0.0.5
language: wasm/assemblyscript
entities:
- YourEntity
abis:
- name: Implementation
file: ./abis/Implementation.json
eventHandlers:
- event: YourEvent(bytes32,bytes32[])
handler: handleYourEvent
file: ./src/mapping.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment