-
Notifications
You must be signed in to change notification settings - Fork 25
/
txParser.ts
86 lines (77 loc) · 2.16 KB
/
txParser.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
import { Hash, PublicClient, decodeEventLog } from "viem";
import { HypercertMinterAbi } from "@hypercerts-org/contracts";
export type ClaimStoredEvent = {
claimId: bigint;
uri: string;
totalUnits: bigint;
};
export type ParserReturnType = {
success: boolean;
data?: ClaimStoredEvent;
errors?: Record<string, string | string[]>;
};
/**
*
* Utility method to parse a hypercert mint transaction (createAllowlist, mintClaim) and get the ID of the minted claim
*
* @notice This method is a wrapper around basic viem utilties to parse ClaimStored(uint256 indexed claimID, string uri, uint256 totalUnits).
*
* @param client public client provided by viem
* @param hash transaction hash returned from the transaction
* @returns {Promise<ParserReturnType>} returns a promise with the parsed data or errors
*/
export const getClaimStoredDataFromTxHash = async (client: PublicClient, hash: Hash): Promise<ParserReturnType> => {
const receipt = await client.getTransactionReceipt({
hash,
});
const events = receipt.logs.map((log) =>
decodeEventLog({
abi: HypercertMinterAbi,
data: log.data,
topics: log.topics,
}),
);
if (!events) {
return {
errors: {
noEvents: "No events found for this transaction",
},
success: false,
};
}
const claimEvent = events.find((e) => e.eventName === "ClaimStored");
if (!claimEvent) {
return {
errors: {
noClaimStoredEvent: "No ClaimStored event found",
},
success: false,
};
}
if (isClaimStoredLog(claimEvent.args)) {
return {
data: claimEvent.args,
success: true,
};
} else {
return {
errors: {
couldNotParseLog: "Log arguments could not be mapped to ClaimStoredEvent",
dataToParse: JSON.stringify(claimEvent.args),
},
success: false,
};
}
};
const isClaimStoredLog = (args: unknown): args is ClaimStoredEvent => {
return (
typeof args === "object" &&
args !== null &&
"claimId" in args &&
typeof args.claimId === "bigint" &&
"uri" in args &&
typeof args.uri === "string" &&
"totalUnits" in args &&
typeof args.totalUnits === "bigint"
);
};