Skip to content

Commit ed2eff9

Browse files
Bin queries + tests
1 parent e219da4 commit ed2eff9

File tree

13 files changed

+1345
-47
lines changed

13 files changed

+1345
-47
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Liquidity Book Query Algorithm/Pseudocode
2+
3+
## Querying All Reserves
4+
5+
To query all the reserves, we use `GetAllBinsReserves`. This function supports pagination, allowing you to query multiple times. Users can provide an `id`, and the function will return the reserves for the corresponding IDs.
6+
7+
### Request Structure
8+
9+
```rust
10+
GetAllBinsReserves {
11+
id: Option<u32>,
12+
page: Option<u32>,
13+
page_size: Option<u32>,
14+
},
15+
```
16+
### Response Structure
17+
18+
```rust
19+
20+
pub struct AllBinsResponse {
21+
pub reserves: Vec<BinResponse>,
22+
pub last_id: u32,
23+
}
24+
25+
pub struct BinResponse {
26+
pub bin_id: u32,
27+
pub bin_reserve_x: u128,
28+
pub bin_reserve_y: u128,
29+
}
30+
```
31+
32+
After obtaining the database of all the bins, it's necessary to maintain and check the changes made in the reserves. This allows for updating specific bins instead of all 'heights'.
33+
34+
### Queries for Fetching heights at which Bins updated
35+
36+
Admins can query:
37+
38+
```rust
39+
GetBinUpdatingHeights {
40+
page: Option<u32>,
41+
page_size: Option<u32>,
42+
},
43+
44+
```
45+
and receive:
46+
47+
```rust
48+
pub struct BinUpdatingHeightsResponse(pub Vec<u64>);
49+
```
50+
This response is a list of the heights at which changes were made. The admin can use the last updated height to query the reserve changes at those heights and then update to the latest height.
51+
52+
### Queries to Update Only Bins that changed
53+
54+
#### Method 1:
55+
Use the 'heights' and send them to:
56+
```rust
57+
GetUpdatedBinAtMultipleHeights { heights: Vec<u64> },
58+
59+
```
60+
to receive:
61+
62+
```rust
63+
pub struct UpdatedBinsAtHeightResponse {
64+
pub height: u64,
65+
pub ids: Vec<u32>,
66+
}
67+
pub struct UpdatedBinsAtMultipleHeightResponse(pub Vec<UpdatedBinsAtHeightResponse>);
68+
69+
```
70+
71+
Update `last_update_height` to the last index of `Vec<UpdatedBinsAtHeightResponse>`.
72+
Then, query heights again, focusing only on `heights` > `last_updated_height`.
73+
74+
75+
#### Method 2
76+
77+
Simplt query:
78+
79+
```rust
80+
GetUpdatedBinAfterHeight {
81+
height: u64,
82+
page: Option<u32>,
83+
page_size: Option<u32>,
84+
},
85+
86+
```
87+
to receive:
88+
```rust
89+
pub struct UpdatedBinsAtHeightResponse {
90+
pub height: u64,
91+
pub ids: Vec<u32>,
92+
}
93+
pub struct UpdatedBinsAfterHeightResponse(pub Vec<UpdatedBinsAtHeightResponse>);
94+
```
95+
96+
Using the response, the admin updates all the bin reserves and stores the `height` in the vector as `last_updated_height`. Then, send that height to `GetUpdatedBinAfterHeight` to get `GetUpdatedBinAfterHeight`, thus continuing the loop.

0 commit comments

Comments
 (0)