Skip to content

Commit cc35fd7

Browse files
authored
Merge pull request #271 from hypercerts-org/feature/index-operator-as-creator
feat(parsing): enhance parseClaimStoredEvent to extract operator from…
2 parents 7dbccea + 2e445aa commit cc35fd7

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

src/parsing/parseClaimStoredEvent.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { isAddress, isHex } from "viem";
1+
import { isAddress, isHex, parseEventLogs } from "viem";
22
import { z } from "zod";
33
import { Claim, ClaimSchema } from "@/storage/storeClaimStored.js";
44
import { ParserMethod } from "@/indexer/LogParser.js";
55
import { ZERO_ADDRESS } from "@/utils/constants.js";
66
import { getEvmClient } from "@/clients/evmClient.js";
7+
import { HypercertMinterAbi } from "@hypercerts-org/contracts";
78

89
export const ClaimStoredEventSchema = z.object({
910
address: z.string().refine(isAddress, {
@@ -32,18 +33,33 @@ export const parseClaimStoredEvent: ParserMethod<Claim> = async ({
3233
context: { chain_id, contracts_id },
3334
}) => {
3435
const { params, transactionHash } = ClaimStoredEventSchema.parse(event);
35-
const client = getEvmClient(chain_id);
36+
const client = getEvmClient(Number(chain_id));
3637

3738
try {
38-
const transaction = await client.getTransaction({
39+
// get the operator from the transferSingle event, this is necessary for safe support
40+
// otherwise transaction.from is the address of the last signer in safe
41+
const receipt = await client.getTransactionReceipt({
3942
hash: transactionHash,
4043
});
44+
const parsedLogs = parseEventLogs({
45+
abi: HypercertMinterAbi,
46+
logs: receipt.logs,
47+
});
48+
const transferSingleEvent = parsedLogs.find(
49+
// @ts-expect-error eventName is missing in the type
50+
(log) => log.eventName === "TransferSingle",
51+
);
52+
if (!transferSingleEvent) {
53+
throw new Error("TransferSingle event not found");
54+
}
55+
// @ts-expect-error args is missing in the type
56+
const operator = transferSingleEvent.args.operator;
4157

4258
return [
4359
ClaimSchema.parse({
4460
contracts_id: contracts_id,
4561
owner_address: ZERO_ADDRESS,
46-
creator_address: transaction.from,
62+
creator_address: operator,
4763
token_id: params.claimID,
4864
uri: params.uri,
4965
units: params.totalUnits,

test/parsing/claimStoredEvent.test.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,34 @@ vi.mock("../../src/clients/evmClient.js", () => ({
88
Promise.resolve({
99
from: "0x1234567890123456789012345678901234567890",
1010
}),
11+
getTransactionReceipt: () =>
12+
Promise.resolve({
13+
logs: [],
14+
}),
1115
}),
1216
}));
1317

14-
import { faker } from "@faker-js/faker";
18+
vi.mock("viem", async (importOriginal) => {
19+
const original = await importOriginal();
20+
return {
21+
// @ts-expect-error - spread types may only be created from object types
22+
...original,
23+
parseEventLogs: vi.fn(),
24+
// parseEventLogs: vi.fn().mockReturnValue([
25+
// {
26+
// eventName: "TransferSingle",
27+
// args: {
28+
// operator: "0x1234567890123456789012345678901234567891",
29+
// },
30+
// },
31+
// ]),
32+
};
33+
});
34+
35+
import { fa, faker } from "@faker-js/faker";
1536
import { Block } from "@hypercerts-org/chainsauce";
1637
import { getAddress } from "viem";
38+
import * as viem from "viem";
1739
import { parseClaimStoredEvent } from "../../src/parsing/parseClaimStoredEvent.js";
1840
import { generateClaimStoredEvent } from "../helpers/factories.js";
1941

@@ -36,6 +58,16 @@ describe("claimStoredEvent", {}, () => {
3658
};
3759

3860
it("parses a claim stored event", {}, async () => {
61+
const operator = getAddress(faker.finance.ethereumAddress());
62+
vi.spyOn(viem, "parseEventLogs").mockImplementationOnce(() => [
63+
// @ts-expect-error - mock implementation
64+
{
65+
eventName: "TransferSingle",
66+
args: {
67+
operator,
68+
},
69+
},
70+
]);
3971
const from = "0x1234567890123456789012345678901234567890";
4072
const event = {
4173
event: "ClaimStored",
@@ -53,7 +85,7 @@ describe("claimStoredEvent", {}, () => {
5385

5486
expect(claim).toEqual({
5587
contracts_id: context.contracts_id,
56-
creator_address: from,
88+
creator_address: operator,
5789
owner_address: "0x0000000000000000000000000000000000000000",
5890
uri: event.params.uri,
5991
units: event.params.totalUnits,

0 commit comments

Comments
 (0)