-
Notifications
You must be signed in to change notification settings - Fork 0
/
transferSingleEvent.ts
58 lines (53 loc) · 1.72 KB
/
transferSingleEvent.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
import { isAddress } from "viem";
import { getBlockTimestamp } from "@/utils/getBlockTimestamp.js";
import { z } from "zod";
import { messages } from "@/utils/validation.js";
import { ParserMethod } from "@/indexer/processLogs.js";
const TransferSingleEventSchema = z.object({
address: z.string().refine(isAddress),
args: z.object({
operator: z.string().refine(isAddress),
from: z.string().refine(isAddress),
to: z.string().refine(isAddress),
id: z.bigint(),
value: z.bigint(),
}),
blockNumber: z.bigint(),
});
export const ParsedTransferSingle = z.object({
contract_address: z
.string()
.refine(isAddress, { message: messages.INVALID_ADDRESS }),
token_id: z.bigint(),
block_number: z.bigint(),
block_timestamp: z.bigint(),
value: z.bigint(),
to_owner_address: z
.string()
.refine(isAddress, { message: messages.INVALID_ADDRESS }),
from_owner_address: z
.string()
.refine(isAddress, { message: messages.INVALID_ADDRESS }),
contracts_id: z.string().optional(),
});
export type ParsedTransferSingle = z.infer<typeof ParsedTransferSingle>;
/*
* Helper method to get the sender, recipient, tokenID and value from the event. Will throw when the event is
* missing any of the required fields.
*
* @param event - The event object.
* */
export const parseTransferSingle: ParserMethod<ParsedTransferSingle> = async ({
log,
}) => {
const { args, blockNumber, address } = TransferSingleEventSchema.parse(log);
return ParsedTransferSingle.parse({
contract_address: address,
token_id: args.id,
block_number: blockNumber,
block_timestamp: await getBlockTimestamp(blockNumber),
value: args.value,
to_owner_address: args.to,
from_owner_address: args.from,
});
};