Skip to content

Commit 6a2bb38

Browse files
committed
feat(parsing): add log index to TakerBid and update parsing logic
- Introduced a new `log_index` field in the TakerBid schema to track event logs. - Updated the `parseTakerBidEvent` function to utilize the new `log_index` and simplified claim ID retrieval. - Refactored currency amount calculation to sum fee amounts directly from the bid parameters. - Added a migration to the sales table to accommodate the new `log_index` column with a unique constraint on transaction hash and log index.
1 parent 7087c90 commit 6a2bb38

File tree

3 files changed

+15
-39
lines changed

3 files changed

+15
-39
lines changed

src/parsing/parseTakerBidEvent.ts

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { ParserMethod } from "@/indexer/LogParser.js";
33
import { TakerBid } from "@/storage/storeTakerBid.js";
44
import { getDeployment } from "@/utils/getDeployment.js";
55
import { messages } from "@/utils/validation.js";
6-
import { HypercertExchangeAbi, HypercertMinterAbi, getHypercertTokenId } from "@hypercerts-org/sdk";
6+
import {
7+
HypercertExchangeAbi,
8+
HypercertMinterAbi,
9+
getHypercertTokenId,
10+
} from "@hypercerts-org/sdk";
711
import {
812
erc20Abi,
913
getAddress,
@@ -75,6 +79,7 @@ export const TakerBidEventSchema = z.object({
7579
}),
7680
blockNumber: z.coerce.bigint(),
7781
transactionHash: z.string(),
82+
logIndex: z.number().int(),
7883
});
7984

8085
export const parseTakerBidEvent: ParserMethod<TakerBid> = async ({
@@ -83,7 +88,6 @@ export const parseTakerBidEvent: ParserMethod<TakerBid> = async ({
8388
}) => {
8489
const { addresses } = getDeployment(Number(chain_id));
8590
const client = getEvmClient(Number(chain_id));
86-
8791
try {
8892
const bid = TakerBidEventSchema.parse(event);
8993

@@ -114,19 +118,7 @@ export const parseTakerBidEvent: ParserMethod<TakerBid> = async ({
114118
(log) => log.eventName === "TransferSingle",
115119
);
116120

117-
// Get the claim ID from either event type
118-
let claimId;
119-
// @ts-expect-error args is missing in the type
120-
if (batchValueTransferLog?.args?.claimIDs?.[0]) {
121-
// @ts-expect-error args is missing in the type
122-
claimId = batchValueTransferLog.args.claimIDs[0];
123-
// @ts-expect-error args is missing in the type
124-
} else if (transferSingleLog?.args?.id) {
125-
// In this case, the ID from the transferSingleLog is a fraction token ID
126-
// We need to get the claim ID from the fraction token ID
127-
// @ts-expect-error args is missing in the type
128-
claimId = getHypercertTokenId(transferSingleLog.args.id);
129-
}
121+
const claimId = getHypercertTokenId(bid.params.itemIds[0]);
130122

131123
if (!claimId) {
132124
throw new Error(
@@ -136,30 +128,9 @@ export const parseTakerBidEvent: ParserMethod<TakerBid> = async ({
136128

137129
const hypercertId = `${chain_id}-${getAddress(bid.params?.collection)}-${claimId}`;
138130

139-
let currencyAmount = 0n;
140-
const currency = getAddress(bid.params.currency);
141-
if (currency === zeroAddress) {
142-
// Get value of the transaction
143-
const transaction = await client.getTransaction({
144-
hash: bid.transactionHash as `0x${string}`,
145-
});
146-
currencyAmount = transaction.value;
147-
} else {
148-
const currencyLogs = transactionReceipt.logs.filter(
149-
(log) => log.address.toLowerCase() === currency.toLowerCase(),
150-
);
151-
const parsedCurrencyLogs = parseEventLogs({
152-
abi: erc20Abi,
153-
logs: currencyLogs,
154-
});
155-
const transferLogs = parsedCurrencyLogs.filter(
156-
(log) => log.eventName === "Transfer",
157-
);
158-
currencyAmount = transferLogs.reduce(
159-
(acc, transferLog) => acc + (transferLog?.args?.value ?? 0n),
160-
0n,
161-
);
162-
}
131+
const currencyAmount = bid.params.feeAmounts.reduce((acc, amount) => {
132+
return acc + amount;
133+
}, 0n);
163134

164135
const exchangeLogs = transactionReceipt.logs.filter(
165136
(log) =>
@@ -192,6 +163,7 @@ export const parseTakerBidEvent: ParserMethod<TakerBid> = async ({
192163
currency_amount: currencyAmount,
193164
fee_amounts: fee_amounts,
194165
fee_recipients: fee_recipients,
166+
log_index: bid.logIndex,
195167
}),
196168
];
197169
} catch (e) {

src/storage/storeTakerBid.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const TakerBid = z.object({
3030
fee_recipients: z.array(
3131
z.string().refine(isAddress, { message: "Invalid fee recipient address" }),
3232
),
33+
log_index: z.number().int(),
3334
});
3435

3536
export type TakerBid = z.infer<typeof TakerBid>;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
alter table sales add column log_index integer;
2+
3+
alter table sales add constraint sales_log_index_unique unique (transaction_hash, log_index);

0 commit comments

Comments
 (0)