Skip to content

Commit 3980660

Browse files
authored
Merge pull request #146 from lidofinance/feature/si-1491-erlang-and-json-rpc
Erlang-bridge and JSON RPC
2 parents bea4366 + 22b4ce2 commit 3980660

39 files changed

+2194
-6
lines changed

docs/docusaurus.config.ts

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ const config: Config = {
3232
sidebarPath: './sidebarsExamples.ts',
3333
},
3434
],
35+
[
36+
'@docusaurus/plugin-content-docs',
37+
{
38+
id: 'lidoPulse',
39+
path: 'lidoPulse',
40+
routeBasePath: 'lidoPulse',
41+
sidebarPath: './sidebarsLidoPulse.ts',
42+
},
43+
],
3544
],
3645
presets: [
3746
[
@@ -74,6 +83,7 @@ const config: Config = {
7483
label: 'Docs',
7584
},
7685
{ to: '/examples/intro', label: 'Examples', position: 'left' },
86+
{ to: '/lidoPulse/intro', label: 'LidoPulse', position: 'left' },
7787
{
7888
href: '/playground',
7989
label: 'Playground',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"label": "Erlang Bridge",
3+
"position": 3,
4+
"link": {
5+
"type": "generated-index"
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
sidebar_position: 2
3+
---
4+
5+
# Installation
6+
7+
## Installing Erlang
8+
9+
Download and install Erlang/OTP from the [official website](https://www.erlang.org/downloads).
10+
11+
## Installing Node.js (version >= 20)
12+
13+
Download and install Node.js and NPM from the [official website](https://nodejs.org/).
14+
15+
## Installing Node.js Dependencies
16+
17+
Navigate to the project directory and install the necessary dependencies:
18+
19+
```bash
20+
yarn install
21+
```

docs/examples/erlang-bridge/intro.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
sidebar_position: 1
3+
title: Introduction
4+
---
5+
6+
# Erlang and Lido Ethereum SDK Interaction
7+
8+
## Introduction
9+
10+
This example demonstrate the interaction between Erlang and Lido Ethereum SDK processes. The Erlang process launches a Node.js process and sends commands to retrieve a result from the Lido SDK methods.
11+
12+
- `main.erl`: Erlang module that manages launching and interacting with the Node.js process.
13+
- `sdk.js`: Lido SDK script that processes commands received from the Erlang process and returns results.
14+
15+
## Use Case
16+
17+
The primary use case for this project is to integrate blockchain reward retrieval functionalities into an Erlang-based application. By leveraging the Lido SDK in a Node.js process, this project provides a way to access blockchain data and utilities that may not be easily accessible within the Erlang ecosystem.

docs/examples/erlang-bridge/usage.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
sidebar_position: 3
3+
---
4+
5+
# Usage
6+
7+
## Starting the Erlang Process
8+
9+
1. Navigate to the project directory and install deps :
10+
11+
```bash
12+
cd examples/erlang-bridge/src
13+
14+
yarn install
15+
```
16+
17+
2. Replace RPC_URL in `sdk.js` with the actual RPC URL and set the `chain` parameter to which chain you want to connect:
18+
19+
```ts
20+
const rpcProvider = createPublicClient({
21+
chain: mainnet,
22+
transport: http('RPC_URL'),
23+
});
24+
```
25+
26+
3. Start the Erlang shell:
27+
28+
```bash
29+
rebar3 get-deps && rebar3 compile
30+
```
31+
32+
```bash
33+
erlc main.erl
34+
```
35+
36+
```bash
37+
erl -pa _build/default/lib/*/ebin
38+
```
39+
40+
4. In the Erlang shell, compile the `main` module:
41+
42+
```erlang
43+
c(main).
44+
```
45+
46+
5. Start the Node.js process from Erlang and get the port:
47+
48+
```erlang
49+
{ok, Port} = main:start().
50+
```
51+
52+
6. Define the parameters and call the `get_rewards_from_chain` function:
53+
54+
```erlang
55+
Params = [
56+
{<<"address">>, <<"0x">>},
57+
{<<"stepBlock">>, 10000},
58+
{<<"back">>, {<<"days">>, 1}}
59+
].
60+
61+
Result = main:get_rewards_from_chain(Port, Params).
62+
```
63+
64+
- address: Ethereum address for which to retrieve rewards.
65+
- stepBlock: Max blocks in 1 query - depend on the RPC capabilities and pricing plans
66+
- back.days: Number of days to step back.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"label": "Get Started",
3+
"position": 2,
4+
"link": {
5+
"type": "generated-index"
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
sidebar_position: 4
3+
---
4+
5+
# Batch Requests
6+
7+
The server also supports batch requests, allowing multiple JSON-RPC calls to be sent in a single HTTP request. Each request in the batch will be processed independently, and a single response containing the results of all requests will be returned. Note that the order of responses in the batch response may not match the order of the requests. You should match responses to requests using the `id` field.
8+
9+
## Example Batch JSON-RPC Request
10+
11+
```json
12+
[
13+
{
14+
"jsonrpc": "2.0",
15+
"method": "shares.balance",
16+
"params": ["0x2f0EA53F92252167d658963f334a91de0824e322"],
17+
"id": 1
18+
},
19+
{
20+
"jsonrpc": "2.0",
21+
"method": "unsteth.getNFTsByAccount",
22+
"params": ["0x2f0EA53F92252167d658963f334a91de0824e322"],
23+
"id": 2
24+
},
25+
{
26+
"jsonrpc": "2.0",
27+
"method": "events.stethEvents.getLastRebaseEvent",
28+
"params": [],
29+
"id": 3
30+
}
31+
]
32+
```
33+
34+
## Example Batch Response
35+
36+
```json
37+
[
38+
{
39+
"jsonrpc": "2.0",
40+
"result": [
41+
{
42+
"amountOfStETH": "10000000000000000",
43+
"amountOfShares": "9995810557743733",
44+
"owner": "0x",
45+
"timestamp": "16973450812",
46+
"isFinalized": true,
47+
"isClaimed": false,
48+
"id": "777"
49+
}
50+
],
51+
"id": 2
52+
},
53+
{
54+
"jsonrpc": "2.0",
55+
"result": "14066156191713469572",
56+
"id": 1
57+
},
58+
{
59+
"jsonrpc": "2.0",
60+
"result": {
61+
"eventName": "TokenRebased",
62+
"args": {
63+
"reportTimestamp": "1721072820",
64+
"timeElapsed": "4608",
65+
"preTotalShares": "1079179109989045486885777",
66+
"preTotalEther": "1093196098970760843229455",
67+
"postTotalShares": "1079179369664131812438396",
68+
"postTotalEther": "1093198738453702928397482",
69+
"sharesMintedAsFees": "260563376065765428"
70+
},
71+
"address": "0x3f1c547b21f65e10480de3ad8e19faac46c95034",
72+
"topics": [
73+
"0xff08c3ef606d198e316ef5b822193c489965899eb4e3c248cea1a4626c3eda50",
74+
"0x0000000000000000000000000000000000000000000000000000000066957cb4"
75+
],
76+
"data": "0x000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000e4866ab1ef0fa0ba1b9100000000000000000000000000000000000000000000e77e477e6cbd9ce5f90f00000000000000000000000000000000000000000000e4866e4c7c2d1e2e317c00000000000000000000000000000000000000000000e77e6c1fc07dee9674aa000000000000000000000000000000000000000000000000039db5028fe06034",
77+
"blockNumber": "1934791",
78+
"transactionHash": "0x826b3e20a499dfb655828829ba08cab037b81873237397502159ea41f9186246",
79+
"transactionIndex": 16,
80+
"blockHash": "0x61ea6638b24ef3b0215556e67bb847e44f52c97e9f24b898452b3b6c426ee82e",
81+
"logIndex": 43,
82+
"removed": false
83+
},
84+
"id": 3
85+
}
86+
]
87+
```
88+
89+
Batch requests are useful for optimizing network usage and reducing latency by sending multiple requests at once.

docs/lidoPulse/get-started/errors.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
sidebar_position: 5
3+
---
4+
5+
# Handling Errors
6+
7+
The server includes comprehensive error handling. Errors are returned in the standard JSON-RPC format, including an error code and message.
8+
9+
## Example Error Response
10+
11+
```json
12+
{
13+
"jsonrpc": "2.0",
14+
"error": {
15+
"code": -32600,
16+
"message": "Invalid Request"
17+
},
18+
"id": null
19+
}
20+
```
21+
22+
## Error Codes
23+
24+
The following error codes are used by the server:
25+
26+
- `-32600`: Invalid Request
27+
- `-32700`: Parse error
28+
- `-32601`: Method not found
29+
- `-32602`: Invalid params
30+
- `-32603`: Internal error
31+
- `-32000`: Server error
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
sidebar_position: 2
3+
---
4+
5+
# Installation
6+
7+
To get started, clone the repository and install the dependencies:
8+
9+
```bash
10+
git clone https://github.com/lidofinance/lido-ethereum-sdk.git
11+
cd packages/lido-pulse
12+
cp .env.example .env
13+
yarn install
14+
```
15+
16+
Fill in the `.env` file with the required environment variables.
17+
18+
## Running the Server
19+
20+
To start the Fastify server, run:
21+
22+
```bash
23+
yarn start
24+
```
25+
26+
You can also run the server in development mode with hot reloading:
27+
28+
```bash
29+
yarn dev
30+
```
31+
32+
The server will start on port 3000 by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
sidebar_position: 1
3+
---
4+
5+
# Security Disclaimer
6+
7+
:::info
8+
9+
**Important:** The API provided by LidoPulse is intended to be run on localhost and is not secure by itself. It is crucial to protect this API behind firewalls and other security measures to prevent unauthorized access and ensure the integrity of your data. Here are some recommendations:
10+
11+
:::
12+
13+
- Run the LidoPulse API on a secure internal network.
14+
- Use firewall rules to restrict access to the API.
15+
- Implement additional security layers, such as VPNs and access controls, to safeguard the API from external threats.

0 commit comments

Comments
 (0)