This repository contains the logic for decoding a raw_log_event
of a transaction to meaningful, human-readable, structured data.
-
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 likeexport type Configs = { protocol_name: string; chain_name: Chain; address: string; is_factory: boolean; }[];
-
GoldRushDecoder: The
GoldRushDecoder
class has different methods that enable the decoding logic to run. The various methods are-
initDecoder
: Scans the./services/decoder/protocols
directory for all the protocols, extracts theconfigs
from them and creates a mapping to the respective decoding function. It is run when the server starts. -
on
: Creates a decoding function for the specified protocol name on the specified chains. Its declaration is:GoldRushDecoder.on( "<protocol-name>:<EventName>", ["<chain_name_1>", "<chain_name_2>", ...], ABI as Abi, async (log_event, tx, chain_name, goldrush_client, options): Promise<EventType> => { <!-- decoding logic --> } );
The method has 4 arguments:
- Event Id: A case-sensitive string concatenation of the
protocol name
with theevent name
by a:
. - Chain Names: An array of all the chains the defined decoding function will run for.
- ABI: The ABI of the contract on which the event exists.
- Decoding Function: The actual decoding function, it has 5 arguments passed to it:
log_event
: The raw log event that is being decoded.tx
: The transaction object that generated this log.chain_name
: Name of the chain to which the log belongs to.goldrush_client
: The covalent client created with your covalent API key.options
: Query parameters attached to the request for refining the response. These are of the following typesraw_logs
: Aboolean
that attaches the raw log of the event along with the decoded responsemin_usd
: A minimum number value for a transaction to have to be decoded
- Event Id: A case-sensitive string concatenation of the
-
fallback
: Creates a fallback function for the specified event name. This function is not linked to any chain or contract. Its declaration is:GoldRushDecoder.fallback( "EventName", ABI as Abi, async (log_event, tx, chain_name, goldrush_client, options): Promise<EventType> => { <!-- decoding logic --> } );
The fallback method has all the same arguments as the
on
arguments excluding theChain Names
array -
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.
-
Follow the following steps to start the development server of the GoldRush Decoder.
-
Install the dependencies
yarn install
-
Setup the environmental variables. Refer to .env.example for the list of environmental variables and store them in
.env
at the root level of the repository. -
Start the server
yarn dev
The development server will start on the URL -
http://localhost:8080
(port number may change based on the.env
, 8080 is default).
-
/api/v1
: The default endpoint for the v1 of the server. A header of the keyx-goldrush-api-key
with the value as the GoldRush API key is mandatory for the Decoder to work.-
/tx/decode
: Decodes a transaction of a chain.Expects the JSON body:
chain_name
: The chain name of the transactiontx_hash
: Hash of the transaction to be decoded.
curl --location 'http://localhost:<PORT>/api/v1/tx/decode' \ --header 'x-goldrush-api-key: <GOLDRUSH_API_KEY>' \ --header 'Content-Type: application/json' \ --data '{ "chain_name": "<CHAIN_NAME>", "tx_hash": "<TX_HASH>" }'
-
Follow the following steps to add a Decoding logic for an event from a contract of a chain.
-
Run this on your terminal
yarn add-config
-
Add a Protocol Name for which you want to add an config. If the protocol does not exist, a new protocol will be created. However, if it does exist, another config will be added for that protocol.
-
Input data as per the prompts. The data required after the
protocol_name
isaddress
: This is the contract address. It can either be a standalone contract or a factory contract.is_factory
: If the input address is a factory contract or not.chain_name
: The chain for which the config is added.
This will modify the configs added to the 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. -
In
${protocol_name}.decoders.ts
, a decoding logic declaration (Decoder.on(...)
) will be exposed wherein the decoding logic needs to be implemented. The return type of the decoding function expects the return typeEventType
:
Contributions, issues and feature requests are welcome! Feel free to check issues page.
Give a ⭐️ if this project helped you!
This project is MIT licensed.