-
Notifications
You must be signed in to change notification settings - Fork 22
/
mapping.ts
45 lines (39 loc) · 1.02 KB
/
mapping.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { BigInt,log,box } from "@graphprotocol/graph-ts"
import {
ERC20Token, // The contract itself
Transfer
} from "../generated/ERC20Token/ERC20Token"
import {
User,
} from "../generated/schema"
export function handleTransfer(event: Transfer): void {
let contract = ERC20Token.bind(event.address)
let erc20Symbol = contract.symbol()
log.info("In handleTransfer of "+erc20Symbol,[]);
changeUser(event.params.from.toHex(), -event.params.value);
changeUser(event.params.to.toHex(), event.params.value);
}
function changeUser(address: string,delta: BigInt):void { // note delta can be neg.
if (address == "0x0000000000000000000000000000000000000000") return // skip 0 address
let user = User.load(address)
if (!user)
user = newUser(address)
user.balance += delta
user.save()
}
function newUser(address: string): User {
let user = new User(address)
user.address = address
user.balance = BigInt.fromI32(0)
return user
}
/*
{
query {
users {
id
address
balance
}
}
*/