Skip to content

Commit 0013f78

Browse files
committed
refactor: cleaner code
1 parent 3c98053 commit 0013f78

File tree

12 files changed

+61
-123
lines changed

12 files changed

+61
-123
lines changed

README.md

+5-5
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_metadata): Promise<EventType> => {
44+
async (log_event, tx, chain_name, covalent_client): Promise<EventType> => {
4545
<!-- decoding logic -->
4646
}
4747
);
@@ -52,10 +52,10 @@ This repository contains the logic for decoding a `raw_log_event` of a transacti
5252
1. **Event Id**: A case-sensitive string concatenation of the `protocol name` with the `event name` by a `:`.
5353
2. **Networks**: An array of all the networks the defined decoding function will run for
5454
3. **Decoding Function**: The actual decoding function, it has 3 arguments passed to it:
55-
1. `log`: The raw log event that is being decoded.
56-
2. `chain_name`: Network to which the log belongs to.
57-
3. `covalent_client`: The covalent client created with your covalent API key.
58-
4. `tx_metadata`: The transaction object that generated this log.
55+
1. `log_event`: The raw log event that is being decoded.
56+
2. `tx`: The transaction object that generated this log.
57+
3. `chain_name`: Network to which the log belongs to.
58+
4. `covalent_client`: The covalent client created with your covalent API key.
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

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

1717
export const txRouter = Router();
@@ -26,15 +26,22 @@ const handleDecode = async (
2626
"x-covalent-api-key"
2727
];
2828
const { network, tx_hash } = req.body as DecodeTXRequest;
29-
const { log_events, metadata } = await fetchDataFromTx(
29+
const tx = await fetchTxFromHash(
3030
network as Chain,
3131
tx_hash,
3232
covalentApiKey
3333
);
34+
const {
35+
log_events,
36+
dex_details,
37+
nft_sale_details,
38+
lending_details,
39+
safe_details,
40+
...metadata
41+
} = tx;
3442
const events = await fetchEventsFromLogs(
3543
network as Chain,
36-
log_events,
37-
metadata,
44+
tx,
3845
covalentApiKey
3946
);
4047
const parsedMetadata = JSON.parse(

microservices/tx/tx.service.ts

+6-26
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@ import {
33
CovalentClient,
44
type Chain,
55
type LogEvent,
6+
type Transaction,
67
} from "@covalenthq/client-sdk";
7-
import { type TransactionMetadata } from "../../services/decoder/decoder.types";
88

9-
export const fetchDataFromTx = async (
9+
export const fetchTxFromHash = async (
1010
network: Chain,
1111
tx_hash: string,
1212
covalentApiKey: string
13-
): Promise<{
14-
log_events: LogEvent[];
15-
metadata: TransactionMetadata;
16-
}> => {
13+
): Promise<Transaction> => {
1714
const covalentClient = new CovalentClient(covalentApiKey);
1815
const { data, error_code, error_message } =
1916
await covalentClient.TransactionService.getTransaction(
@@ -30,18 +27,7 @@ export const fetchDataFromTx = async (
3027
);
3128
const tx = data?.items?.[0];
3229
if (tx) {
33-
const {
34-
log_events,
35-
dex_details,
36-
nft_sale_details,
37-
lending_details,
38-
safe_details,
39-
...metadata
40-
} = tx;
41-
return {
42-
log_events: log_events,
43-
metadata: metadata,
44-
};
30+
return tx;
4531
} else {
4632
throw {
4733
errorCode: error_code,
@@ -52,15 +38,9 @@ export const fetchDataFromTx = async (
5238

5339
export const fetchEventsFromLogs = async (
5440
network: Chain,
55-
logs: LogEvent[],
56-
metadata: TransactionMetadata,
41+
tx: Transaction,
5742
covalentApiKey: string
5843
) => {
59-
const events = await GoldRushDecoder.decode(
60-
network,
61-
logs.reverse(),
62-
metadata,
63-
covalentApiKey
64-
);
44+
const events = await GoldRushDecoder.decode(network, tx, covalentApiKey);
6545
return events;
6646
};

services/decoder/decoder.ts

+5-6
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 LogEvent,
6+
type Transaction,
77
} from "@covalenthq/client-sdk";
88
import {
99
type Configs,
@@ -12,7 +12,6 @@ import {
1212
type DecodingFunctions,
1313
type EventType,
1414
type DecoderConfig,
15-
type TransactionMetadata,
1615
} from "./decoder.types";
1716
import { encodeEventTopics, type Abi } from "viem";
1817

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

111110
public static decode = async (
112111
network: Chain,
113-
logs: LogEvent[],
114-
metadata: TransactionMetadata,
112+
tx: Transaction,
115113
covalent_api_key: string
116114
) => {
117115
try {
118116
const covalent_client = new CovalentClient(covalent_api_key);
119117
const events: EventType[] = [];
118+
const logs = tx.log_events.reverse();
120119
for (const log of logs) {
121120
const {
122121
raw_log_topics: [topic0_hash],
@@ -131,9 +130,9 @@ export class GoldRushDecoder {
131130
if (function_index !== undefined) {
132131
const event = await this.decoding_functions[function_index](
133132
log,
133+
tx,
134134
network,
135-
covalent_client,
136-
metadata
135+
covalent_client
137136
);
138137
events.push(event);
139138
}

services/decoder/decoder.types.ts

+3-12
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,11 @@ 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-
6960
export type DecodingFunction = (
70-
log: LogEvent,
61+
log_event: LogEvent,
62+
tx: Transaction,
7163
chain_name: Chain,
72-
covalent_client: CovalentClient,
73-
tx: TransactionMetadata
64+
covalent_client: CovalentClient
7465
) => Promise<EventType>;
7566

7667
export type DecoderConfig =

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

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

2218
const { args: decoded } = decodeEventLog({
2319
abi: ABI,
@@ -42,7 +38,7 @@ GoldRushDecoder.on(
4238
category: DECODED_EVENT_CATEGORY.OTHERS,
4339
name: "User Operation Event",
4440
protocol: {
45-
logo: log.sender_logo_url as string,
41+
logo: log_event.sender_logo_url as string,
4642
name: "4337 Entry Point",
4743
},
4844
details: [

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

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

2217
const { args: decoded } = decodeEventLog({
2318
abi: TransparentUpgradeableProxyABI,
@@ -41,7 +36,7 @@ GoldRushDecoder.on(
4136
category: DECODED_EVENT_CATEGORY.OTHERS,
4237
name: "Block Specimen Production Proof Submitted",
4338
protocol: {
44-
logo: log.sender_logo_url as string,
39+
logo: log_event.sender_logo_url as string,
4540
name: "Covalent Network",
4641
},
4742
details: [

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

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

2217
const { args: decoded } = decodeEventLog({
2318
abi: ABI,
@@ -38,8 +33,8 @@ GoldRushDecoder.on(
3833
category: DECODED_EVENT_CATEGORY.DEX,
3934
name: "Transfer",
4035
protocol: {
41-
logo: log.sender_logo_url as string,
42-
name: log.sender_name as string,
36+
logo: log_event.sender_logo_url as string,
37+
name: log_event.sender_name as string,
4338
},
4439
details: [
4540
{
@@ -55,11 +50,11 @@ GoldRushDecoder.on(
5550
],
5651
tokens: [
5752
{
58-
decimals: log.sender_contract_decimals,
53+
decimals: log_event.sender_contract_decimals,
5954
heading: "Token Amount",
6055
pretty_quote: "",
61-
ticker_logo: log.sender_logo_url,
62-
ticker_symbol: log.sender_contract_ticker_symbol,
56+
ticker_logo: log_event.sender_logo_url,
57+
ticker_symbol: log_event.sender_contract_ticker_symbol,
6358
value: decoded.value.toString(),
6459
},
6560
],

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

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

2924
enum ITEM_TYPE {
3025
"NATIVE" = 0,
@@ -188,7 +183,7 @@ GoldRushDecoder.on(
188183
category: DECODED_EVENT_CATEGORY.DEX,
189184
name: "Basic Order Fulfilled",
190185
protocol: {
191-
logo: log.sender_logo_url as string,
186+
logo: log_event.sender_logo_url as string,
192187
name: "Opensea",
193188
},
194189
details: details,

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

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

3025
const { args: decoded } = decodeEventLog({
3126
abi: SimpleSwapABI,
@@ -119,7 +114,7 @@ GoldRushDecoder.on(
119114
category: DECODED_EVENT_CATEGORY.DEX,
120115
name: "Swap V3",
121116
protocol: {
122-
logo: log.sender_logo_url as string,
117+
logo: log_event.sender_logo_url as string,
123118
name: "Paraswap V5",
124119
},
125120
details: details,

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

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

2217
const { args: decoded } = decodeEventLog({
2318
abi: SourceZorbABI,
@@ -45,7 +40,7 @@ GoldRushDecoder.on(
4540
category: DECODED_EVENT_CATEGORY.DEX,
4641
name: "Transfer",
4742
protocol: {
48-
logo: log.sender_logo_url as string,
43+
logo: log_event.sender_logo_url as string,
4944
name: "SOURCE ZORB",
5045
},
5146
nfts: [

0 commit comments

Comments
 (0)