Skip to content

Commit 26cb24f

Browse files
authored
Yj-feat/decoder-tx-object (#11)
1 parent 0ca5521 commit 26cb24f

15 files changed

+81
-77
lines changed

README.md

+5-4
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_event, tx, chain_name, covalent_client): Promise<EventType> => {
4545
<!-- decoding logic -->
4646
}
4747
);
@@ -52,9 +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.
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.
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 { decodeLogsfromTx, fetchTxFromHash } from "./tx.service";
1515
import { type Chain } from "@covalenthq/client-sdk";
1616

1717
export const txRouter = Router();
@@ -26,14 +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 data = await fetchEventsFromLogs(
35-
network as Chain,
34+
const {
3635
log_events,
36+
dex_details,
37+
nft_sale_details,
38+
lending_details,
39+
safe_details,
40+
...metadata
41+
} = tx;
42+
const events = await decodeLogsfromTx(
43+
network as Chain,
44+
tx,
3745
covalentApiKey
3846
);
3947
const parsedMetadata = JSON.parse(
@@ -43,7 +51,7 @@ const handleDecode = async (
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

+9-22
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import {
33
CovalentClient,
44
type Chain,
55
type LogEvent,
6+
type Transaction,
67
} from "@covalenthq/client-sdk";
78

8-
export const fetchDataFromTx = async (
9+
export const fetchTxFromHash = async (
910
network: Chain,
1011
tx_hash: string,
1112
covalentApiKey: string
12-
) => {
13+
): Promise<Transaction> => {
1314
const covalentClient = new CovalentClient(covalentApiKey);
1415
const { data, error_code, error_message } =
1516
await covalentClient.TransactionService.getTransaction(
@@ -24,19 +25,9 @@ export const fetchDataFromTx = async (
2425
withSafe: false,
2526
}
2627
);
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-
};
28+
const tx = data?.items?.[0];
29+
if (tx) {
30+
return tx;
4031
} else {
4132
throw {
4233
errorCode: error_code,
@@ -45,15 +36,11 @@ export const fetchDataFromTx = async (
4536
}
4637
};
4738

48-
export const fetchEventsFromLogs = async (
39+
export const decodeLogsfromTx = async (
4940
network: Chain,
50-
logs: LogEvent[],
41+
tx: Transaction,
5142
covalentApiKey: string
5243
) => {
53-
const events = await GoldRushDecoder.decode(
54-
network,
55-
logs.reverse(),
56-
covalentApiKey
57-
);
44+
const events = await GoldRushDecoder.decode(network, tx, covalentApiKey);
5845
return events;
5946
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"author": "",
1515
"license": "ISC",
1616
"dependencies": {
17-
"@covalenthq/client-sdk": "^0.8.5",
17+
"@covalenthq/client-sdk": "^0.8.7",
1818
"cors": "^2.8.5",
1919
"dotenv": "^16.3.1",
2020
"enquirer": "^2.4.1",

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],
@@ -129,8 +130,9 @@ export class GoldRushDecoder {
129130
if (function_index !== undefined) {
130131
const event = await this.decoding_functions[function_index](
131132
log,
133+
tx,
132134
network,
133-
covalentClient
135+
covalent_client
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,
@@ -57,7 +58,8 @@ export interface EventType {
5758
}
5859

5960
export type DecodingFunction = (
60-
log: LogEvent,
61+
log_event: LogEvent,
62+
tx: Transaction,
6163
chain_name: Chain,
6264
covalent_client: CovalentClient
6365
) => Promise<EventType>;

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ GoldRushDecoder.on(
1111
"4337-entry-point:UserOperationEvent",
1212
["matic-mainnet"],
1313
ABI as Abi,
14-
async (log, chain_name, covalent_client): Promise<EventType> => {
15-
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;
1617

1718
const { args: decoded } = decodeEventLog({
1819
abi: ABI,
@@ -37,8 +38,8 @@ GoldRushDecoder.on(
3738
category: DECODED_EVENT_CATEGORY.OTHERS,
3839
name: "User Operation Event",
3940
protocol: {
40-
logo: log.sender_logo_url as string,
41-
name: log.sender_name as string,
41+
logo: log_event.sender_logo_url as string,
42+
name: "4337 Entry Point",
4243
},
4344
details: [
4445
{
@@ -70,7 +71,7 @@ GoldRushDecoder.on(
7071
{
7172
title: "User Operation Hash",
7273
value: decoded.userOpHash,
73-
type: "text",
74+
type: "address",
7475
},
7576
],
7677
};

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ GoldRushDecoder.on(
1111
"covalent-network:BlockSpecimenProductionProofSubmitted",
1212
["moonbeam-mainnet"],
1313
TransparentUpgradeableProxyABI as Abi,
14-
async (log, chain_name, covalent_client): Promise<EventType> => {
15-
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;
1616

1717
const { args: decoded } = decodeEventLog({
1818
abi: TransparentUpgradeableProxyABI,
@@ -36,7 +36,7 @@ GoldRushDecoder.on(
3636
category: DECODED_EVENT_CATEGORY.OTHERS,
3737
name: "Block Specimen Production Proof Submitted",
3838
protocol: {
39-
logo: log.sender_logo_url as string,
39+
logo: log_event.sender_logo_url as string,
4040
name: "Covalent Network",
4141
},
4242
details: [

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

+12-10
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ GoldRushDecoder.on(
1111
"grindery-one:Transfer",
1212
["matic-mainnet"],
1313
ABI as Abi,
14-
async (log, chain_name, covalent_client): Promise<EventType> => {
15-
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 } = log_event;
1616

1717
const { args: decoded } = decodeEventLog({
1818
abi: ABI,
@@ -33,8 +33,8 @@ GoldRushDecoder.on(
3333
category: DECODED_EVENT_CATEGORY.DEX,
3434
name: "Transfer",
3535
protocol: {
36-
logo: log.sender_logo_url as string,
37-
name: log.sender_name as string,
36+
logo: log_event.sender_logo_url as string,
37+
name: log_event.sender_name as string,
3838
},
3939
details: [
4040
{
@@ -47,13 +47,15 @@ GoldRushDecoder.on(
4747
value: decoded.to,
4848
type: "address",
4949
},
50+
],
51+
tokens: [
5052
{
51-
title: "Value",
52-
value: (
53-
decoded.value /
54-
BigInt(Math.pow(10, sender_contract_decimals))
55-
).toString(),
56-
type: "text",
53+
decimals: log_event.sender_contract_decimals,
54+
heading: "Token Amount",
55+
pretty_quote: "",
56+
ticker_logo: log_event.sender_logo_url,
57+
ticker_symbol: log_event.sender_contract_ticker_symbol,
58+
value: decoded.value.toString(),
5759
},
5860
],
5961
};

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe("grindery-one", () => {
1717
if (!event) {
1818
throw Error("Event not found");
1919
}
20-
expect(event.details?.length).toEqual(3);
20+
expect(event.details?.length).toEqual(2);
21+
expect(event.tokens?.length).toEqual(1);
2122
});
2223
});

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ GoldRushDecoder.on(
1818
"opensea:OrderFulfilled",
1919
["eth-mainnet", "matic-mainnet"],
2020
Seaport as Abi,
21-
async (log, chain_name, covalent_client): Promise<EventType> => {
22-
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;
2323

2424
enum ITEM_TYPE {
2525
"NATIVE" = 0,
@@ -183,7 +183,7 @@ GoldRushDecoder.on(
183183
category: DECODED_EVENT_CATEGORY.DEX,
184184
name: "Basic Order Fulfilled",
185185
protocol: {
186-
logo: log.sender_logo_url as string,
186+
logo: log_event.sender_logo_url as string,
187187
name: "Opensea",
188188
},
189189
details: details,

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ GoldRushDecoder.on(
1919
"paraswap-v5:SwappedV3",
2020
["eth-mainnet", "matic-mainnet"],
2121
SimpleSwapABI as Abi,
22-
async (log, chain_name, covalent_client): Promise<EventType> => {
23-
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;
2424

2525
const { args: decoded } = decodeEventLog({
2626
abi: SimpleSwapABI,
@@ -114,7 +114,7 @@ GoldRushDecoder.on(
114114
category: DECODED_EVENT_CATEGORY.DEX,
115115
name: "Swap V3",
116116
protocol: {
117-
logo: log.sender_logo_url as string,
117+
logo: log_event.sender_logo_url as string,
118118
name: "Paraswap V5",
119119
},
120120
details: details,

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ GoldRushDecoder.on(
1111
"source-zorb:Transfer",
1212
["zora-mainnet"],
1313
SourceZorbABI as Abi,
14-
async (log, chain_name, covalent_client): Promise<EventType> => {
15-
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;
1616

1717
const { args: decoded } = decodeEventLog({
1818
abi: SourceZorbABI,
@@ -40,7 +40,7 @@ GoldRushDecoder.on(
4040
category: DECODED_EVENT_CATEGORY.DEX,
4141
name: "Transfer",
4242
protocol: {
43-
logo: log.sender_logo_url as string,
43+
logo: log_event.sender_logo_url as string,
4444
name: "SOURCE ZORB",
4545
},
4646
nfts: [

0 commit comments

Comments
 (0)