-
Notifications
You must be signed in to change notification settings - Fork 0
/
transferSingleEvent.ts
54 lines (49 loc) · 1.53 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
import { isAddress } from "viem";
import { z } from "zod";
import { messages } from "@/utils/validation.js";
import { ParserMethod } from "@/indexer/LogParser.js";
const TransferSingleEventSchema = z.object({
address: z.string().refine(isAddress),
params: z.object({
operator: z.string().refine(isAddress),
from: z.string().refine(isAddress),
to: z.string().refine(isAddress),
id: z.coerce.bigint(),
value: z.coerce.bigint(),
}),
});
export const ParsedTransferSingle = z.object({
contract_address: z
.string()
.refine(isAddress, { message: messages.INVALID_ADDRESS }),
token_id: z.coerce.bigint(),
value: z.coerce.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 ({
data,
}) => {
const { params, address } = TransferSingleEventSchema.parse(data);
return [
ParsedTransferSingle.parse({
contract_address: address,
token_id: params.id,
value: params.value,
to_owner_address: params.to,
from_owner_address: params.from,
}),
];
};