Skip to content

Commit e4c488c

Browse files
committed
!fix: terminology
1 parent dda0c66 commit e4c488c

22 files changed

+137
-134
lines changed

README.md

+12-11
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ This repository contains the logic for decoding a `raw_log_event` of a transacti
2020

2121
## Knowledge Primer
2222

23-
1. **Config**: A `config` is a mapping of a contract address, network, and protocol name to create a unique configuration for every protocol on all the networks across all the chains for all the contracts. A protocol can have a collection of configs in an array. It looks like
23+
1. **Config**: A `config` is a mapping of a contract address, chain name, and protocol name to create a unique configuration for every protocol across all the chains for all the contracts. A protocol can have a collection of configs in an array. It looks like
2424

2525
```ts
2626
export type Configs = {
2727
protocol_name: string;
28-
network: Chain;
28+
chain_name: Chain;
2929
address: string;
3030
is_factory: boolean;
3131
}[];
@@ -34,12 +34,12 @@ This repository contains the logic for decoding a `raw_log_event` of a transacti
3434
2. **GoldRushDecoder**: The `GoldRushDecoder` class has different methods that enable the decoding logic to run. The various methods are
3535

3636
1. `initDecoder`: Scans the `./services/decoder/protocols` directory for all the protocols, extracts the `configs` from them and creates a mapping to the respective decoding function. It is run when the server starts.
37-
2. `on`: Creates a decoding function for the specified protocol name on the specified networks. Its declaration is:
37+
2. `on`: Creates a decoding function for the specified protocol name on the specified chains. Its declaration is:
3838

3939
```ts
4040
GoldRushDecoder.on(
4141
"<protocol-name>:<EventName>",
42-
["<network_1>", "<network_2>"],
42+
["<chain_name_1>", "<chain_name_2>"],
4343
ABI as Abi,
4444
async (log_event, tx, chain_name, covalent_client): Promise<EventType> => {
4545
<!-- decoding logic -->
@@ -50,12 +50,12 @@ This repository contains the logic for decoding a `raw_log_event` of a transacti
5050
The method has 4 arguments:
5151

5252
1. **Event Id**: A case-sensitive string concatenation of the `protocol name` with the `event name` by a `:`.
53-
2. **Networks**: An array of all the networks the defined decoding function will run for.
53+
2. **Chain Names**: An array of all the chains the defined decoding function will run for.
5454
3. **ABI**: The ABI of the contract on which the event exists.
5555
4. **Decoding Function**: The actual decoding function, it has 3 arguments passed to it:
5656
1. `log_event`: The raw log event that is being decoded.
5757
2. `tx`: The transaction object that generated this log.
58-
3. `chain_name`: Network to which the log belongs to.
58+
3. `chain_name`: Name of the chain to which the log belongs to.
5959
4. `covalent_client`: The covalent client created with your covalent API key.
6060

6161
3. `fallback`: Creates a fallback function for the specified event name. This function is not linked to any chain or contract. Its declaration is:
@@ -77,7 +77,7 @@ This repository contains the logic for decoding a `raw_log_event` of a transacti
7777
3. **Decoding Function**: The actual decoding function, it has 3 arguments passed to it:
7878
1. `log_event`: The raw log event that is being decoded.
7979
2. `tx`: The transaction object that generated this log.
80-
3. `chain_name`: Network to which the log belongs to.
80+
3. `chain_name`: Name of the chain to which the log belongs to.
8181
4. `covalent_client`: The covalent client created with your covalent API key.
8282

8383
4. `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.
@@ -106,19 +106,19 @@ Follow the following steps to start the development server of the **GoldRush Dec
106106

107107
1. `/api/v1`: The default endpoint for the v1 of the server. A header of the key `x-covalent-api-key` with the value as the [Covalent API key](https://www.covalenthq.com/platform/apikey/) is **mandatory** for the Decoder to work.
108108

109-
1. `/tx/decode`: Decodes a transaction of a network.
109+
1. `/tx/decode`: Decodes a transaction of a chain.
110110

111111
Expects the JSON body:
112112

113-
1. `network`: The chain name of the transaction
113+
1. `chain_name`: The chain name of the transaction
114114
2. `tx_hash`: Hash of the transaction to be decoded.
115115

116116
```bash
117117
curl --location 'http://localhost:<PORT>/api/v1/tx/decode' \
118118
--header 'x-covalent-api-key: <COVALENT_API_KEY>' \
119119
--header 'Content-Type: application/json' \
120120
--data '{
121-
"network": "<CHAIN_NAME>",
121+
"chain_name": "<CHAIN_NAME>",
122122
"tx_hash": "<TX_HASH>"
123123
}'
124124
```
@@ -136,7 +136,7 @@ Follow the following steps to add a Decoding logic for an event from a contract
136136

137137
- `address`: This is the contract address. It can either be a standalone contract or a factory contract.
138138
- `is_factory`: If the input address is a factory contract or not.
139-
- `network`: The network or chain the added config is for.
139+
- `chain_name`: The chain for which the config is added.
140140

141141
This will modify the configs added to the [Protocols](services/protocols) folder. A config will be added to `${protocol_name}.configs.ts`. A sample decoder with a dummy event name (`<EVENT NAME>`) will be added to `${protocol_name}.decoders.ts`. Along with this, a test file `${protocol_name}.test.ts` will also be created which needs to be fixed so that the test passes.
142142

@@ -174,6 +174,7 @@ Follow the following steps to add a Decoding logic for an event from a contract
174174
details?: {
175175
title: string;
176176
value: string;
177+
type: "address" | "text";
177178
}[];
178179
}
179180
```

microservices/tx/tx.routes.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ const handleDecode = async (
2525
const covalentApiKey = (req.headers as DecodeTXHeaders)[
2626
"x-covalent-api-key"
2727
];
28-
const { network, tx_hash } = req.body as DecodeTXRequest;
28+
const { chain_name, tx_hash } = req.body as DecodeTXRequest;
2929
const tx = await fetchTxFromHash(
30-
network as Chain,
30+
chain_name as Chain,
3131
tx_hash,
3232
covalentApiKey
3333
);
@@ -40,7 +40,7 @@ const handleDecode = async (
4040
...metadata
4141
} = tx;
4242
const events = await decodeLogsfromTx(
43-
network as Chain,
43+
chain_name as Chain,
4444
tx,
4545
covalentApiKey
4646
);

microservices/tx/tx.schema.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { Chains } from "@covalenthq/client-sdk";
22
import * as yup from "yup";
33

44
export const decodeTXRequestSchema = yup.object({
5-
network: yup
5+
chain_name: yup
66
.mixed()
7-
.oneOf(Object.values(Chains))
8-
.required("network is required"),
7+
.oneOf(Object.values(Chains), "chain_name is incorrect")
8+
.required("chain_name is required"),
99
tx_hash: yup.string().trim().required("tx_hash is required"),
1010
});
1111

microservices/tx/tx.service.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import {
66
} from "@covalenthq/client-sdk";
77

88
export const fetchTxFromHash = async (
9-
network: Chain,
9+
chain_name: Chain,
1010
tx_hash: string,
1111
covalentApiKey: string
1212
): Promise<Transaction> => {
1313
const covalentClient = new CovalentClient(covalentApiKey);
1414
const { data, error_code, error_message } =
1515
await covalentClient.TransactionService.getTransaction(
16-
network,
16+
chain_name,
1717
tx_hash,
1818
{
1919
noLogs: false,
@@ -36,10 +36,10 @@ export const fetchTxFromHash = async (
3636
};
3737

3838
export const decodeLogsfromTx = async (
39-
network: Chain,
39+
chain_name: Chain,
4040
tx: Transaction,
4141
covalentApiKey: string
4242
) => {
43-
const events = await GoldRushDecoder.decode(network, tx, covalentApiKey);
43+
const events = await GoldRushDecoder.decode(chain_name, tx, covalentApiKey);
4444
return events;
4545
};

scripts/add-config.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ const customLog = (message: string, type: "success" | "info") => {
3939
const nameSchema = yup.string().trim().required("name is required");
4040
const addressSchema = yup.string().trim().required("address is required");
4141
const isFactorySchema = yup.boolean().required("is_factory is required");
42-
const networkSchema = yup
42+
const chainNameSchema = yup
4343
.mixed()
44-
.oneOf(Object.values(Chains), "incorrect network name")
45-
.required("network is required");
44+
.oneOf(Object.values(Chains), "chain_name is incorrect")
45+
.required("chain_name is required");
4646
(async () => {
4747
const { protocol_name } = (await prompt({
4848
type: "input",
@@ -86,7 +86,7 @@ const networkSchema = yup
8686
);
8787
}
8888

89-
const { address, is_factory, network } = (await prompt([
89+
const { address, is_factory, chain_name } = (await prompt([
9090
{
9191
type: "input",
9292
name: "address",
@@ -123,13 +123,13 @@ const networkSchema = yup
123123
},
124124
{
125125
type: "input",
126-
name: "network",
127-
message: "What is the Network or the Chain for this contract?",
126+
name: "chain_name",
127+
message: "What is the Chain on which this contract is deployed?",
128128
format: (value) => slugify(value),
129129
result: (value) => slugify(value),
130130
validate: async (value) => {
131131
try {
132-
await networkSchema.validate(value, {
132+
await chainNameSchema.validate(value, {
133133
abortEarly: false,
134134
});
135135
return true;
@@ -141,15 +141,15 @@ const networkSchema = yup
141141
])) as {
142142
address: string;
143143
is_factory: boolean;
144-
network: Chain;
144+
chain_name: Chain;
145145
};
146146

147147
if (!exists) {
148148
const eventName: string = "<EVENT NAME>";
149149
const abiContent: string = `[]`;
150-
const configsContent: string = `import{type Configs}from"../../decoder.types";\n\nconst configs:Configs=[{address:"${address}",is_factory:${is_factory},protocol_name:"${protocol_name}",network:"${network}"}];\n\nexport default configs;`;
151-
const decodersContent: string = `import{GoldRushDecoder}from"../../decoder";import{type EventType}from"../../decoder.types";import{DECODED_ACTION,DECODED_EVENT_CATEGORY}from"../../decoder.constants";import{decodeEventLog,type Abi}from"viem";import ABI from "./abis/${protocol_name}.abi.json";\n\nGoldRushDecoder.on("${protocol_name}:${eventName}",["${network}"],ABI as Abi,async(log_event,tx,chain_name,covalent_client):Promise<EventType> =>{const{raw_log_data,raw_log_topics}=log_event;\n\nconst{args:decoded}=decodeEventLog({abi:ABI,topics:raw_log_topics as[],data:raw_log_data as \`0x\${string}\`,eventName:"${eventName}"})as{eventName:"${eventName}";args:{}};\n\nreturn{action:DECODED_ACTION.SWAPPED,category:DECODED_EVENT_CATEGORY.DEX,name:"${eventName}",protocol:{logo:log_event.sender_logo_url as string,name:log_event.sender_name as string}};});`;
152-
const testContent: string = `import request from"supertest";import app from"../../../../api";import{type EventType}from"../../decoder.types";\n\ndescribe("${protocol_name}",()=>{test("${network}:${eventName}",async()=>{const res=await request(app).post("/api/v1/tx/decode").set({"x-covalent-api-key":process.env.TEST_COVALENT_API_KEY}).send({network:"${network}",tx_hash:"<ENTER TX HASH FOR TESTING>"});const{events}=res.body as{events:EventType[]};const event=events.find(({name})=>name==="${eventName}");if(!event){throw Error("Event not found")}const testAdded:boolean=false;expect(testAdded).toEqual(true)})});`;
150+
const configsContent: string = `import{type Configs}from"../../decoder.types";\n\nconst configs:Configs=[{address:"${address}",is_factory:${is_factory},protocol_name:"${protocol_name}",chain_name:"${chain_name}"}];\n\nexport default configs;`;
151+
const decodersContent: string = `import{GoldRushDecoder}from"../../decoder";import{type EventType}from"../../decoder.types";import{DECODED_ACTION,DECODED_EVENT_CATEGORY}from"../../decoder.constants";import{decodeEventLog,type Abi}from"viem";import ABI from "./abis/${protocol_name}.abi.json";\n\nGoldRushDecoder.on("${protocol_name}:${eventName}",["${chain_name}"],ABI as Abi,async(log_event,tx,chain_name,covalent_client):Promise<EventType> =>{const{raw_log_data,raw_log_topics}=log_event;\n\nconst{args:decoded}=decodeEventLog({abi:ABI,topics:raw_log_topics as[],data:raw_log_data as \`0x\${string}\`,eventName:"${eventName}"})as{eventName:"${eventName}";args:{}};\n\nreturn{action:DECODED_ACTION.SWAPPED,category:DECODED_EVENT_CATEGORY.DEX,name:"${eventName}",protocol:{logo:log_event.sender_logo_url as string,name:log_event.sender_name as string}};});`;
152+
const testContent: string = `import request from"supertest";import app from"../../../../api";import{type EventType}from"../../decoder.types";\n\ndescribe("${protocol_name}",()=>{test("${chain_name}:${eventName}",async()=>{const res=await request(app).post("/api/v1/tx/decode").set({"x-covalent-api-key":process.env.TEST_COVALENT_API_KEY}).send({chain_name:"${chain_name}",tx_hash:"<ENTER TX HASH FOR TESTING>"});const{events}=res.body as{events:EventType[]};const event=events.find(({name})=>name==="${eventName}");if(!event){throw Error("Event not found")}const testAdded:boolean=false;expect(testAdded).toEqual(true)})});`;
153153
await writeInFile(
154154
protocolDir,
155155
`${protocol_name}.decoders.ts`,
@@ -182,7 +182,7 @@ const networkSchema = yup
182182
address: address,
183183
is_factory: is_factory,
184184
protocol_name: protocol_name,
185-
network: network,
185+
chain_name: chain_name,
186186
});
187187
const configsContent: string = `import{type Configs}from"../../decoder.types";\n\nconst configs:Configs=${JSON.stringify(
188188
configs

services/decoder/decoder.ts

+22-20
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ export class GoldRushDecoder {
4848
const configs = require(join(protocolPath, configFile))
4949
.default as Configs;
5050
configs.forEach(
51-
({ address, is_factory, network, protocol_name }) => {
52-
this.configs[network] ??= {};
53-
this.configs[network][protocol_name] ??= {};
54-
this.configs[network][protocol_name][address] = {
51+
({ address, is_factory, chain_name, protocol_name }) => {
52+
this.configs[chain_name] ??= {};
53+
this.configs[chain_name][protocol_name] ??= {};
54+
this.configs[chain_name][protocol_name][address] = {
5555
is_factory: is_factory,
5656
};
5757
}
@@ -82,10 +82,10 @@ export class GoldRushDecoder {
8282

8383
const decodersCount = Object.keys(this.decoding_functions).length;
8484
const configsCount = Object.values(this.configs).reduce(
85-
(networkCount, network) => {
85+
(chainCount, chain) => {
8686
return (
87-
networkCount +
88-
Object.values(network).reduce((addressCount, protocol) => {
87+
chainCount +
88+
Object.values(chain).reduce((addressCount, protocol) => {
8989
return addressCount + Object.keys(protocol).length;
9090
}, 0)
9191
);
@@ -101,7 +101,7 @@ export class GoldRushDecoder {
101101

102102
public static on = (
103103
event_id: string,
104-
networks: Chain[],
104+
chain_names: Chain[],
105105
abi: Abi,
106106
decoding_function: DecodingFunction
107107
) => {
@@ -113,21 +113,23 @@ export class GoldRushDecoder {
113113
this.decoding_functions.push(decoding_function);
114114
const decoding_function_index: number =
115115
this.decoding_functions.length - 1;
116-
networks.forEach((network) => {
117-
const configExists = this.configs[network]?.[protocol]
116+
chain_names.forEach((chain_name) => {
117+
const configExists = this.configs[chain_name]?.[protocol]
118118
? true
119119
: false;
120120
if (!configExists) {
121121
throw Error(
122-
`config for ${protocol} does not exist on the network ${network}`
122+
`config for ${protocol} does not exist on ${chain_name}`
123123
);
124124
}
125-
Object.keys(this.configs[network][protocol]).forEach((address) => {
126-
this.decoders[network] ??= {};
127-
this.decoders[network][address] ??= {};
128-
this.decoders[network][address][topic0_hash] =
129-
decoding_function_index;
130-
});
125+
Object.keys(this.configs[chain_name][protocol]).forEach(
126+
(address) => {
127+
this.decoders[chain_name] ??= {};
128+
this.decoders[chain_name][address] ??= {};
129+
this.decoders[chain_name][address][topic0_hash] =
130+
decoding_function_index;
131+
}
132+
);
131133
});
132134
};
133135

@@ -147,7 +149,7 @@ export class GoldRushDecoder {
147149
};
148150

149151
public static decode = async (
150-
network: Chain,
152+
chain_name: Chain,
151153
tx: Transaction,
152154
covalent_api_key: string
153155
) => {
@@ -164,12 +166,12 @@ export class GoldRushDecoder {
164166
const decoding_index =
165167
// !ERROR: add factory_contract_address in the log_event(s)
166168
// factory_contract_address ||
167-
this.decoders[network][contract_address]?.[topic0_hash];
169+
this.decoders[chain_name][contract_address]?.[topic0_hash];
168170
const fallback_index = this.fallbacks[topic0_hash];
169171
if (decoding_index !== undefined || fallback_index !== undefined) {
170172
const event = await this.decoding_functions[
171173
decoding_index ?? fallback_index
172-
](log, tx, network, covalent_client);
174+
](log, tx, chain_name, covalent_client);
173175
events.push(event);
174176
}
175177
}

services/decoder/decoder.types.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111

1212
export type Configs = {
1313
protocol_name: string;
14-
network: Chain;
14+
chain_name: Chain;
1515
address: string;
1616
is_factory: boolean;
1717
}[];
@@ -66,7 +66,7 @@ export type DecodingFunction = (
6666

6767
export type DecoderConfig =
6868
| {
69-
[network in Chain]: {
69+
[chain_name in Chain]: {
7070
[protocol_name: string]: {
7171
[address: string]: {
7272
is_factory: boolean;
@@ -78,7 +78,7 @@ export type DecoderConfig =
7878

7979
export type Decoders =
8080
| {
81-
[network in Chain]: {
81+
[chain_name in Chain]: {
8282
[address: string]: {
8383
[topic0_hash: string]: number;
8484
};

services/decoder/fallbacks/transfer/transfer.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe("fallback", () => {
88
.post("/api/v1/tx/decode")
99
.set({ "x-covalent-api-key": process.env.TEST_COVALENT_API_KEY })
1010
.send({
11-
network: "eth-mainnet",
11+
chain_name: "eth-mainnet",
1212
tx_hash:
1313
"0xe7b894fdac8c037fa69bbabe168fe7984033226e1b1871bd9f70c861b6f6a35d",
1414
});

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ const configs: Configs = [
55
protocol_name: "4337-entry-point",
66
address: "0x5ff137d4b0fdcd49dca30c7cf57e578a026d2789",
77
is_factory: false,
8-
network: "matic-mainnet",
8+
chain_name: "matic-mainnet",
99
},
1010
{
1111
protocol_name: "4337-entry-point",
1212
address: "0x5ff137d4b0fdcd49dca30c7cf57e578a026d2789",
1313
is_factory: false,
14-
network: "avalanche-mainnet",
14+
chain_name: "avalanche-mainnet",
1515
},
1616
];
1717

0 commit comments

Comments
 (0)