Skip to content

Commit 591e8ae

Browse files
committed
feat(decoders): add tx to decoding function
1 parent 2f55125 commit 591e8ae

File tree

12 files changed

+84
-26
lines changed

12 files changed

+84
-26
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ This repository contains the logic for decoding a `raw_log_event` of a transacti
4141
"<protocol-name>:<EventName>",
4242
["<network_1>", "<network_2>"],
4343
ABI as Abi,
44-
async (log, chain_name, covalent_client, tx): Promise<EventType> => {
44+
async (log, chain_name, covalent_client, tx_metadata): Promise<EventType> => {
4545
<!-- decoding logic -->
4646
}
4747
);
@@ -55,7 +55,7 @@ This repository contains the logic for decoding a `raw_log_event` of a transacti
5555
1. `log`: The raw log event that is being decoded.
5656
2. `chain_name`: Network to which the log belongs to.
5757
3. `covalent_client`: The covalent client created with your covalent API key.
58-
4. `tx`: The transaction object that generated this log.
58+
4. `tx_metadata`: The transaction object that generated this log.
5959

6060
3. `decode`: The function that chooses which decoding function needs to be called for which log event. It collects all the decoded events for a transaction and returns them in an array of structured data. It is run when the API server receives a request.
6161

microservices/tx/tx.routes.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
decodeTXHeadersSchema,
1212
type DecodeTXHeaders,
1313
} from "./tx.schema";
14-
import { fetchEventsFromTx, fetchTxDataFromHash } from "./tx.service";
14+
import { fetchEventsFromLogs, fetchTxDataFromHash } from "./tx.service";
1515
import { type Chain } from "@covalenthq/client-sdk";
1616

1717
export const txRouter = Router();
@@ -31,19 +31,20 @@ const handleDecode = async (
3131
tx_hash,
3232
covalentApiKey
3333
);
34-
const events = await fetchEventsFromTx(
35-
network as Chain,
36-
tx,
37-
covalentApiKey
38-
);
3934
const {
40-
log_events,
4135
dex_details,
4236
nft_sale_details,
4337
lending_details,
4438
safe_details,
39+
log_events,
4540
...metadata
4641
} = tx;
42+
const events = await fetchEventsFromLogs(
43+
network as Chain,
44+
log_events,
45+
metadata,
46+
covalentApiKey
47+
);
4748
const parsedMetadata = JSON.parse(
4849
JSON.stringify(metadata, (_key, value) => {
4950
return typeof value === "bigint" ? value.toString() : value;

microservices/tx/tx.service.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { GoldRushDecoder } from "../../services";
22
import {
33
CovalentClient,
44
type Chain,
5-
type Transaction,
5+
type LogEvent,
66
} from "@covalenthq/client-sdk";
7+
import { type TransactionMetadata } from "../../services/decoder/decoder.types";
78

89
export const fetchTxDataFromHash = async (
910
network: Chain,
@@ -35,11 +36,17 @@ export const fetchTxDataFromHash = async (
3536
}
3637
};
3738

38-
export const fetchEventsFromTx = async (
39+
export const fetchEventsFromLogs = async (
3940
network: Chain,
40-
tx: Transaction,
41+
logs: LogEvent[],
42+
metadata: TransactionMetadata,
4143
covalentApiKey: string
4244
) => {
43-
const events = await GoldRushDecoder.decode(network, tx, covalentApiKey);
45+
const events = await GoldRushDecoder.decode(
46+
network,
47+
logs.reverse(),
48+
metadata,
49+
covalentApiKey
50+
);
4451
return events;
4552
};

services/decoder/decoder.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { join } from "path";
33
import {
44
CovalentClient,
55
type Chain,
6-
type Transaction,
6+
type LogEvent,
77
} from "@covalenthq/client-sdk";
88
import {
99
type Configs,
@@ -12,6 +12,7 @@ import {
1212
type DecodingFunctions,
1313
type EventType,
1414
type DecoderConfig,
15+
type TransactionMetadata,
1516
} from "./decoder.types";
1617
import { encodeEventTopics, type Abi } from "viem";
1718

@@ -109,13 +110,13 @@ export class GoldRushDecoder {
109110

110111
public static decode = async (
111112
network: Chain,
112-
tx: Transaction,
113+
logs: LogEvent[],
114+
metadata: TransactionMetadata,
113115
covalent_api_key: string
114116
) => {
115117
try {
116118
const covalent_client = new CovalentClient(covalent_api_key);
117119
const events: EventType[] = [];
118-
const logs = tx.log_events.reverse();
119120
for (const log of logs) {
120121
const {
121122
raw_log_topics: [topic0_hash],
@@ -132,7 +133,7 @@ export class GoldRushDecoder {
132133
log,
133134
network,
134135
covalent_client,
135-
tx
136+
metadata
136137
);
137138
events.push(event);
138139
}

services/decoder/decoder.types.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,20 @@ export interface EventType {
5757
details?: EventDetails;
5858
}
5959

60+
export type TransactionMetadata = Omit<
61+
Transaction,
62+
| "log_events"
63+
| "dex_details"
64+
| "nft_sale_details"
65+
| "lending_details"
66+
| "safe_details"
67+
>;
68+
6069
export type DecodingFunction = (
6170
log: LogEvent,
6271
chain_name: Chain,
6372
covalent_client: CovalentClient,
64-
tx: Transaction
73+
tx: TransactionMetadata
6574
) => Promise<EventType>;
6675

6776
export type DecoderConfig =

services/decoder/protocols/4337-entry-point/4337-entry-point.decoders.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ GoldRushDecoder.on(
1111
"4337-entry-point:UserOperationEvent",
1212
["matic-mainnet"],
1313
ABI as Abi,
14-
async (log, chain_name, covalent_client, tx): Promise<EventType> => {
14+
async (
15+
log,
16+
chain_name,
17+
covalent_client,
18+
tx_metadata
19+
): Promise<EventType> => {
1520
const { raw_log_data, raw_log_topics, sender_contract_decimals } = log;
1621

1722
const { args: decoded } = decodeEventLog({

services/decoder/protocols/covalent-network/covalent-network.decoders.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ GoldRushDecoder.on(
1111
"covalent-network:BlockSpecimenProductionProofSubmitted",
1212
["moonbeam-mainnet"],
1313
TransparentUpgradeableProxyABI as Abi,
14-
async (log, chain_name, covalent_client, tx): Promise<EventType> => {
14+
async (
15+
log,
16+
chain_name,
17+
covalent_client,
18+
tx_metadata
19+
): Promise<EventType> => {
1520
const { raw_log_data, raw_log_topics } = log;
1621

1722
const { args: decoded } = decodeEventLog({

services/decoder/protocols/grindery-one/grindery-one.decoders.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ GoldRushDecoder.on(
1111
"grindery-one:Transfer",
1212
["matic-mainnet"],
1313
ABI as Abi,
14-
async (log, chain_name, covalent_client, tx): Promise<EventType> => {
14+
async (
15+
log,
16+
chain_name,
17+
covalent_client,
18+
tx_metadata
19+
): Promise<EventType> => {
1520
const { raw_log_data, raw_log_topics } = log;
1621

1722
const { args: decoded } = decodeEventLog({

services/decoder/protocols/opensea/opensea.decoders.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ GoldRushDecoder.on(
1818
"opensea:OrderFulfilled",
1919
["eth-mainnet", "matic-mainnet"],
2020
Seaport as Abi,
21-
async (log, chain_name, covalent_client, tx): Promise<EventType> => {
21+
async (
22+
log,
23+
chain_name,
24+
covalent_client,
25+
tx_metadata
26+
): Promise<EventType> => {
2227
const { block_signed_at, raw_log_data, raw_log_topics } = log;
2328

2429
enum ITEM_TYPE {

services/decoder/protocols/paraswap-v5/paraswap-v5.decoders.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ GoldRushDecoder.on(
1919
"paraswap-v5:SwappedV3",
2020
["eth-mainnet", "matic-mainnet"],
2121
SimpleSwapABI as Abi,
22-
async (log, chain_name, covalent_client, tx): Promise<EventType> => {
22+
async (
23+
log,
24+
chain_name,
25+
covalent_client,
26+
tx_metadata
27+
): Promise<EventType> => {
2328
const { raw_log_data, raw_log_topics } = log;
2429

2530
const { args: decoded } = decodeEventLog({

services/decoder/protocols/source-zorb/source-zorb.decoders.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ GoldRushDecoder.on(
1111
"source-zorb:Transfer",
1212
["zora-mainnet"],
1313
SourceZorbABI as Abi,
14-
async (log, chain_name, covalent_client, tx): Promise<EventType> => {
14+
async (
15+
log,
16+
chain_name,
17+
covalent_client,
18+
tx_metadata
19+
): Promise<EventType> => {
1520
const { raw_log_data, raw_log_topics, sender_address } = log;
1621

1722
const { args: decoded } = decodeEventLog({

services/decoder/protocols/uniswap-v2/uniswap-v2.decoders.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ GoldRushDecoder.on(
1313
"uniswap-v2:Swap",
1414
["eth-mainnet"],
1515
PairABI as Abi,
16-
async (log, chain_name, covalent_client, tx): Promise<EventType> => {
16+
async (
17+
log,
18+
chain_name,
19+
covalent_client,
20+
tx_metadata
21+
): Promise<EventType> => {
1722
const {
1823
sender_address: exchange_contract,
1924
block_signed_at,
@@ -143,7 +148,12 @@ GoldRushDecoder.on(
143148
"uniswap-v2:Mint",
144149
["eth-mainnet"],
145150
PairABI as Abi,
146-
async (log, chain_name, covalent_client, tx): Promise<EventType> => {
151+
async (
152+
log,
153+
chain_name,
154+
covalent_client,
155+
tx_metadata
156+
): Promise<EventType> => {
147157
const {
148158
sender_address: exchange_contract,
149159
raw_log_data,

0 commit comments

Comments
 (0)