-
Notifications
You must be signed in to change notification settings - Fork 12
/
mapping.ts
87 lines (73 loc) · 2.63 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { log, BigInt } from '@graphprotocol/graph-ts';
import { ERC721, Transfer as TransferEvent } from '../generated/ERC721/ERC721';
import { Token, Owner, Contract, Transfer } from '../generated/schema';
export function handleTransfer(event: TransferEvent): void {
log.debug('Transfer detected. From: {} | To: {} | TokenID: {}', [
event.params.from.toHexString(),
event.params.to.toHexString(),
event.params.tokenId.toHexString(),
]);
let previousOwner = Owner.load(event.params.from.toHexString());
let newOwner = Owner.load(event.params.to.toHexString());
let token = Token.load(event.params.tokenId.toHexString());
let transferId = event.transaction.hash
.toHexString()
.concat(':'.concat(event.transactionLogIndex.toHexString()));
let transfer = Transfer.load(transferId);
let contract = Contract.load(event.address.toHexString());
let instance = ERC721.bind(event.address);
if (previousOwner == null) {
previousOwner = new Owner(event.params.from.toHexString());
previousOwner.balance = BigInt.fromI32(0);
} else {
let prevBalance = previousOwner.balance;
if (prevBalance > BigInt.fromI32(0)) {
previousOwner.balance = prevBalance - BigInt.fromI32(1);
}
}
if (newOwner == null) {
newOwner = new Owner(event.params.to.toHexString());
newOwner.balance = BigInt.fromI32(1);
} else {
let prevBalance = newOwner.balance;
newOwner.balance = prevBalance + BigInt.fromI32(1);
}
if (token == null) {
token = new Token(event.params.tokenId.toHexString());
token.contract = event.address.toHexString();
let uri = instance.try_tokenURI(event.params.tokenId);
if (!uri.reverted) {
token.uri = uri.value;
}
}
token.owner = event.params.to.toHexString();
if (transfer == null) {
transfer = new Transfer(transferId);
transfer.token = event.params.tokenId.toHexString();
transfer.from = event.params.from.toHexString();
transfer.to = event.params.to.toHexString();
transfer.timestamp = event.block.timestamp;
transfer.block = event.block.number;
transfer.transactionHash = event.transaction.hash.toHexString();
}
if (contract == null) {
contract = new Contract(event.address.toHexString());
}
let name = instance.try_name();
if (!name.reverted) {
contract.name = name.value;
}
let symbol = instance.try_symbol();
if (!symbol.reverted) {
contract.symbol = symbol.value;
}
let totalSupply = instance.try_totalSupply();
if (!totalSupply.reverted) {
contract.totalSupply = totalSupply.value;
}
previousOwner.save();
newOwner.save();
token.save();
contract.save();
transfer.save();
}