Skip to content

Commit 2f55125

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

File tree

12 files changed

+40
-41
lines changed

12 files changed

+40
-41
lines changed

README.md

+2-1
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): Promise<EventType> => {
44+
async (log, chain_name, covalent_client, tx): Promise<EventType> => {
4545
<!-- decoding logic -->
4646
}
4747
);
@@ -55,6 +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.
5859

5960
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.
6061

microservices/tx/tx.routes.ts

+13-5
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 { fetchEventsFromTx, fetchTxDataFromHash } from "./tx.service";
1515
import { type Chain } from "@covalenthq/client-sdk";
1616

1717
export const txRouter = Router();
@@ -26,24 +26,32 @@ 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 fetchTxDataFromHash(
3030
network as Chain,
3131
tx_hash,
3232
covalentApiKey
3333
);
34-
const data = await fetchEventsFromLogs(
34+
const events = await fetchEventsFromTx(
3535
network as Chain,
36-
log_events,
36+
tx,
3737
covalentApiKey
3838
);
39+
const {
40+
log_events,
41+
dex_details,
42+
nft_sale_details,
43+
lending_details,
44+
safe_details,
45+
...metadata
46+
} = tx;
3947
const parsedMetadata = JSON.parse(
4048
JSON.stringify(metadata, (_key, value) => {
4149
return typeof value === "bigint" ? value.toString() : value;
4250
})
4351
);
4452
res.json({
4553
success: true,
46-
events: data,
54+
events: events,
4755
metadata: parsedMetadata,
4856
});
4957
} catch (error) {

microservices/tx/tx.service.ts

+8-22
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { GoldRushDecoder } from "../../services";
22
import {
33
CovalentClient,
44
type Chain,
5-
type LogEvent,
5+
type Transaction,
66
} from "@covalenthq/client-sdk";
77

8-
export const fetchDataFromTx = async (
8+
export const fetchTxDataFromHash = async (
99
network: Chain,
1010
tx_hash: string,
1111
covalentApiKey: string
@@ -24,19 +24,9 @@ export const fetchDataFromTx = async (
2424
withSafe: false,
2525
}
2626
);
27-
if (data) {
28-
const {
29-
log_events,
30-
dex_details,
31-
nft_sale_details,
32-
lending_details,
33-
safe_details,
34-
...metadata
35-
} = data.items[0];
36-
return {
37-
log_events: log_events,
38-
metadata: metadata,
39-
};
27+
const tx = data?.items?.[0];
28+
if (tx) {
29+
return tx;
4030
} else {
4131
throw {
4232
errorCode: error_code,
@@ -45,15 +35,11 @@ export const fetchDataFromTx = async (
4535
}
4636
};
4737

48-
export const fetchEventsFromLogs = async (
38+
export const fetchEventsFromTx = async (
4939
network: Chain,
50-
logs: LogEvent[],
40+
tx: Transaction,
5141
covalentApiKey: string
5242
) => {
53-
const events = await GoldRushDecoder.decode(
54-
network,
55-
logs.reverse(),
56-
covalentApiKey
57-
);
43+
const events = await GoldRushDecoder.decode(network, tx, covalentApiKey);
5844
return events;
5945
};

services/decoder/decoder.ts

+6-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 LogEvent,
6+
type Transaction,
77
} from "@covalenthq/client-sdk";
88
import {
99
type Configs,
@@ -109,12 +109,13 @@ export class GoldRushDecoder {
109109

110110
public static decode = async (
111111
network: Chain,
112-
logs: LogEvent[],
112+
tx: Transaction,
113113
covalent_api_key: string
114114
) => {
115115
try {
116-
const covalentClient = new CovalentClient(covalent_api_key);
116+
const covalent_client = new CovalentClient(covalent_api_key);
117117
const events: EventType[] = [];
118+
const logs = tx.log_events.reverse();
118119
for (const log of logs) {
119120
const {
120121
raw_log_topics: [topic0_hash],
@@ -130,7 +131,8 @@ export class GoldRushDecoder {
130131
const event = await this.decoding_functions[function_index](
131132
log,
132133
network,
133-
covalentClient
134+
covalent_client,
135+
tx
134136
);
135137
events.push(event);
136138
}

services/decoder/decoder.types.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
type CovalentClient,
33
type Chain,
44
type LogEvent,
5+
type Transaction,
56
} from "@covalenthq/client-sdk";
67
import {
78
type DECODED_ACTION,
@@ -59,7 +60,8 @@ export interface EventType {
5960
export type DecodingFunction = (
6061
log: LogEvent,
6162
chain_name: Chain,
62-
covalent_client: CovalentClient
63+
covalent_client: CovalentClient,
64+
tx: Transaction
6365
) => Promise<EventType>;
6466

6567
export type DecoderConfig =

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

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

1717
const { args: decoded } = decodeEventLog({

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

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

1717
const { args: decoded } = decodeEventLog({

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

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

1717
const { args: decoded } = decodeEventLog({

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

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

2424
enum ITEM_TYPE {

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

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

2525
const { args: decoded } = decodeEventLog({

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

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

1717
const { args: decoded } = decodeEventLog({

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ GoldRushDecoder.on(
1313
"uniswap-v2:Swap",
1414
["eth-mainnet"],
1515
PairABI as Abi,
16-
async (log, chain_name, covalent_client): Promise<EventType> => {
16+
async (log, chain_name, covalent_client, tx): Promise<EventType> => {
1717
const {
1818
sender_address: exchange_contract,
1919
block_signed_at,
@@ -143,7 +143,7 @@ GoldRushDecoder.on(
143143
"uniswap-v2:Mint",
144144
["eth-mainnet"],
145145
PairABI as Abi,
146-
async (log, chain_name, covalent_client): Promise<EventType> => {
146+
async (log, chain_name, covalent_client, tx): Promise<EventType> => {
147147
const {
148148
sender_address: exchange_contract,
149149
raw_log_data,

0 commit comments

Comments
 (0)