Skip to content

Commit 2cd7b36

Browse files
fix doc. (#3)
1 parent 55ca54b commit 2cd7b36

File tree

3 files changed

+147
-33
lines changed

3 files changed

+147
-33
lines changed

doc/api.md

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* [3.1 统计delegate,undelegate,claim总量](#3.1)
1515

1616
<h3 id="1.1">1.1 获取Validator集合</h3>
17-
* `GET /api/valiators`
18-
* 参数
17+
* `GET /api/valiators`
18+
* 参数
1919

2020
| 参数 | 类型 | 必传 | 说明 |
2121
|-----------|--------|----|----------|
@@ -24,8 +24,8 @@
2424
| page | number | N | 页码,默认1 |
2525
| page_size | number | N | 页大小,默认10 |
2626

27-
* Request: `http://localhost/api/validators?online=true&page=1&page_size=5`
28-
* Response: 返回结果按power(质押总量)降序排列
27+
* Request: `http://localhost/api/validators?online=true&page=1&page_size=5`
28+
* Response: 返回结果按power(质押总量)降序排列
2929
```json
3030
{
3131
"total": 31,
@@ -146,15 +146,15 @@
146146
```
147147

148148
<h3 id="1.2">1.2 获取validator最近20笔质押变化</h3>
149-
* `GET /api/diff/latest`
150-
* 参数
149+
* `GET /api/diff/latest`
150+
* 参数
151151

152152
| 参数 | 类型 | 必传 | 说明 |
153153
|-----------|--------|----|-------------|
154154
| validator | string | Y | validator地址 |
155155

156-
* Request: `http://localhost/api/diff/latest?validator=0xc8d2d4ff0b882243f82c1fb20574c81e4c866e72`
157-
* Response:
156+
* Request: `http://localhost/api/diff/latest?validator=0xc8d2d4ff0b882243f82c1fb20574c81e4c866e72`
157+
* Response:
158158
* 按高度降序排列
159159
* 返回值中`amount`,非零正数表示delegate的数量,非零负数表示undelegate的数量
160160
* 如果`amount`是0,则用`op`区分,`op`为零表示delegate,非零表示undelegate
@@ -247,17 +247,17 @@
247247
]
248248
```
249249
<h3 id="1.3">1.3 获取validator的delegate记录</h3>
250-
* `GET /api/records/delegate`
251-
* 参数
250+
* `GET /api/records/delegate`
251+
* 参数
252252

253253
| 参数 | 类型 | 必传 | 说明 |
254254
|-----------|--------|----|--------------------------|
255255
| validator | string | N | validator地址,不传则返回所有的质押记录 |
256256
| page | number | N | 页码,默认1 |
257257
| page_size | number | N | 页大小,默认10 |
258258

259-
* Request: `http://localhost/api/records/delegate?validator=0xc8d2d4ff0b882243f82c1fb20574c81e4c866e72&page=1&page_size=5`
260-
* Response:
259+
* Request: `http://localhost/api/records/delegate?validator=0xc8d2d4ff0b882243f82c1fb20574c81e4c866e72&page=1&page_size=5`
260+
* Response:
261261
*`timestamp`降序排列
262262
```json
263263
{
@@ -304,17 +304,17 @@
304304
```
305305

306306
<h3 id="1.4">1.4 获取validator的undelegate记录</h3>
307-
* `GET /api/records/undelegate`
308-
* 参数
307+
* `GET /api/records/undelegate`
308+
* 参数
309309

310310
| 参数 | 类型 | 必传 | 说明 |
311311
|-----------|--------|----|---------------------------|
312312
| validator | string | N | validator地址,不传则返回所有的解质押记录 |
313313
| page | number | N | 页码,默认1 |
314314
| page_size | number | N | 页大小,默认10 |
315315

316-
* Request: `http://localhost/api/records/undelegate?validator=0x6e20c920f1bdb817f0e19cd05dae01c6affa5228&page=1&page_size=10`
317-
* Response:
316+
* Request: `http://localhost/api/records/undelegate?validator=0x6e20c920f1bdb817f0e19cd05dae01c6affa5228&page=1&page_size=10`
317+
* Response:
318318
*`timestamp`降序排列
319319
```json
320320
{
@@ -342,16 +342,16 @@
342342

343343

344344
<h3 id="2.1">2.1 获取bound数量</h3>
345-
* `GET /api/bound`
346-
* 参数
345+
* `GET /api/bound`
346+
* 参数
347347

348348
| 参数 | 类型 | 必传 | 说明 |
349349
|-----------|--------|----|-------------|
350350
| validator | string | Y | validator地址 |
351351
| delegator | string | Y | delegator地址 |
352352

353-
* Request: `http://localhost/api/bound?validator=0x09ef1db6b67d1cbf7eba6bd9b204611848993df7&delegator=0x2d15d52cc138ffb322b732239cd3630735abac88`
354-
* Response:
353+
* Request: `http://localhost/api/bound?validator=0x09ef1db6b67d1cbf7eba6bd9b204611848993df7&delegator=0x2d15d52cc138ffb322b732239cd3630735abac88`
354+
* Response:
355355
```json
356356
{
357357
"bound_amount": "110000001800000063120",
@@ -360,50 +360,49 @@
360360
```
361361

362362

363-
364363
<h3 id="2.2">2.2 获取reward数量</h3>
365-
* `GET /api/reward`
366-
* 参数
364+
* `GET /api/reward`
365+
* 参数
367366

368367
| 参数 | 类型 | 必传 | 说明 |
369368
|---------|--------|----|-------------|
370369
| address | string | Y | delegator地址 |
371370

372-
* Request: `http://localhost/api/reward?address=0x2d15d52cc138ffb322b732239cd3630735abac88`
373-
* Response:
371+
* Request: `http://localhost/api/reward?address=0x2d15d52cc138ffb322b732239cd3630735abac88`
372+
* Response:
374373
```json
375374
{
376375
"reward": "16742332457649244907"
377376
}
378377
```
379378

380379
<h3 id="2.3">2.3 获取debt数量</h3>
381-
* `GET /api/debt`
382-
* 参数
380+
* `GET /api/debt`
381+
* 参数
383382

384383
| 参数 | 类型 | 必传 | 说明 |
385384
|-----------|--------|----|-------------|
386385
| validator | string | Y | validator地址 |
387386
| delegator | string | Y | delegator地址 |
388387

389-
* Request: `http://localhost/api/debt?validator=0xd518c4f95a3f39ed853a2614566897c4ad5a008f&delegator=0x2d15d52cc138ffb322b732239cd3630735abac88`
390-
* Response:
388+
* Request: `http://localhost/api/debt?validator=0xd518c4f95a3f39ed853a2614566897c4ad5a008f&delegator=0x2d15d52cc138ffb322b732239cd3630735abac88`
389+
* Response:
391390
```json
392391
{
393392
"debt": "96558069283467635"
394393
}
395394
```
396395

397396
<h3 id="3.1">3.1 获取debt数量</h3>
398-
* `GET /api/sum`
399-
* 参数
397+
* `GET /api/sum`
398+
* 参数
400399

401400
| 参数 | 类型 | 必传 | 说明 |
402401
|---------|--------|----|----|
403402
| address | string | Y | 地址 |
404403

405-
* Request: `http://localhost/api/sum?address=0xeb2b96369e83e1466bb56f2bf9d97cbda130e741`
406-
* Response:
404+
* Request: `http://localhost/api/sum?address=0xeb2b96369e83e1466bb56f2bf9d97cbda130e741`
405+
* Response:
407406
```json
408407
{
409408
"delegate": "32254951206000000000000",

updater/Cargo.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[package]
2+
name = "updater"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
base64 = "0.22.0"
10+
clap = { version = "4.5.1", features = ["derive"] }
11+
crossbeam = "0.8.4"
12+
env_logger = "0.11.2"
13+
ethers = { version = "2.0.13", features = ["abigen","legacy"] }
14+
log = "0.4.20"
15+
num_cpus = "1.16.0"
16+
reqwest = { version = "0.12.0", features = ["json"] }
17+
rustc-hex = "2.1.0"
18+
serde = "1.0.197"
19+
serde_json = "1.0.114"
20+
sha2 = "0.10.8"
21+
sqlx = { version = "0.7.3", features = ["bigdecimal", "runtime-tokio", "postgres", "chrono", "json"]}
22+
tokio = { version = "1.36.0", features = ["full"]}
23+
toml = "0.8.12"
24+
url = "2.5.0"

updater/src/main.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
mod db;
2+
mod error;
3+
mod updater;
4+
5+
use crate::db::Storage;
6+
use crate::error::Result;
7+
use crate::updater::Updater;
8+
use clap::Parser;
9+
use ethers::contract::abigen;
10+
use ethers::prelude::{Http, Provider};
11+
use ethers::types::Address;
12+
use log::info;
13+
use serde::{Deserialize, Serialize};
14+
use sqlx::pool::PoolOptions;
15+
use sqlx::{Pool, Postgres};
16+
use std::fs::File;
17+
use std::io::Read;
18+
use std::sync::Arc;
19+
use std::time::Duration;
20+
21+
const DEFAULT_RPC_RETRIES: usize = 3;
22+
const DEFAULT_INTERVAL: u64 = 15; // 15s
23+
24+
abigen!(RewardContract, "../abi/Reward.json");
25+
abigen!(StakingContract, "../abi/Staking.json");
26+
27+
#[derive(Serialize, Deserialize)]
28+
struct UpdaterConfig {
29+
pub evm_rpc: String,
30+
pub staking: String,
31+
pub reward: String,
32+
pub db_url: String,
33+
}
34+
35+
impl UpdaterConfig {
36+
pub fn new(file_path: &str) -> Result<Self> {
37+
let mut f = File::open(file_path)?;
38+
let mut s = String::new();
39+
f.read_to_string(&mut s)?;
40+
let c: UpdaterConfig = toml::from_str(&s)?;
41+
Ok(c)
42+
}
43+
}
44+
45+
#[derive(Parser, Debug)]
46+
struct Args {
47+
/// Node RPC
48+
#[arg(long)]
49+
pub node: String,
50+
/// Block height to start scanning
51+
#[arg(long)]
52+
pub start: Option<u64>,
53+
/// Interval of scanning in seconds
54+
#[arg(long)]
55+
pub interval: Option<u64>,
56+
}
57+
58+
#[tokio::main]
59+
async fn main() -> Result<()> {
60+
env_logger::init();
61+
62+
let config = UpdaterConfig::new("./config.toml")?;
63+
info!("EVM RPC: {}", config.evm_rpc);
64+
info!("Staking contract: {}", config.staking);
65+
info!("Reward contract: {}", config.reward);
66+
67+
let pool: Pool<Postgres> = PoolOptions::new()
68+
.connect(&config.db_url)
69+
.await
70+
.expect("can't connect to database");
71+
info!("Connecting db...ok");
72+
73+
let storage = Storage::new(pool);
74+
let args = Args::parse();
75+
let interval = if let Some(interval) = args.interval {
76+
Duration::from_secs(interval)
77+
} else {
78+
Duration::from_secs(DEFAULT_INTERVAL)
79+
};
80+
81+
let provider = Provider::<Http>::try_from(config.evm_rpc)?;
82+
let staking_addr: Address = config.staking.parse()?;
83+
let staking = StakingContract::new(staking_addr, Arc::new(provider.clone()));
84+
let reward_addr: Address = config.reward.parse()?;
85+
let reward = RewardContract::new(reward_addr, Arc::new(provider.clone()));
86+
info!("Updating interval: {}s", interval.as_secs());
87+
let updater = Updater::new(DEFAULT_RPC_RETRIES, provider, staking, reward, storage);
88+
let _ = updater.run().await?;
89+
90+
Ok(())
91+
}

0 commit comments

Comments
 (0)