Skip to content

Commit 03dbeae

Browse files
fixed responses
1 parent 1a145ae commit 03dbeae

File tree

5 files changed

+144
-89
lines changed

5 files changed

+144
-89
lines changed

contracts/liquidity_book/lb_pair/lb_pair.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pub struct BinResponse {
3030
}
3131
```
3232

33+
`BinResponse` is used multiple times across the responses
34+
3335
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'.
3436

3537
### Queries for Fetching heights at which Bins updated
@@ -61,15 +63,13 @@ GetUpdatedBinAtMultipleHeights { heights: Vec<u64> },
6163
to receive:
6264

6365
```rust
64-
pub struct UpdatedBinsAtHeightResponse {
65-
pub height: u64,
66-
pub ids: Vec<u32>,
67-
}
68-
pub struct UpdatedBinsAtMultipleHeightResponse(pub Vec<UpdatedBinsAtHeightResponse>);
66+
pub struct UpdatedBinsAtHeightResponse(pub Vec<BinResponse>);
67+
68+
pub struct UpdatedBinsAtMultipleHeightResponse(pub Vec<BinResponse>);
6969

7070
```
7171

72-
Update `last_update_height` to the last index of `Vec<UpdatedBinsAtHeightResponse>`.
72+
Update `last_update_height` to the last index of `Vec<BinResponse>`.
7373
Then, query heights again, focusing only on `heights` > `last_updated_height`.
7474

7575

@@ -87,11 +87,10 @@ GetUpdatedBinAfterHeight {
8787
```
8888
to receive:
8989
```rust
90-
pub struct UpdatedBinsAtHeightResponse {
91-
pub height: u64,
92-
pub ids: Vec<u32>,
90+
pub struct UpdatedBinsAfterHeightResponse {
91+
pub bins: Vec<BinResponse>,
92+
pub current_block_height: u64,
9393
}
94-
pub struct UpdatedBinsAfterHeightResponse(pub Vec<UpdatedBinsAtHeightResponse>);
9594
```
9695

9796
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.

contracts/liquidity_book/lb_pair/src/contract.rs

Lines changed: 73 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,7 +1832,7 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<Binary> {
18321832
height,
18331833
page,
18341834
page_size,
1835-
} => query_updated_bins_after_height(deps, height, page, page_size),
1835+
} => query_updated_bins_after_height(deps, env, height, page, page_size),
18361836

18371837
QueryMsg::GetBinUpdatingHeights { page, page_size } => {
18381838
query_bins_updating_heights(deps, page, page_size)
@@ -2156,7 +2156,7 @@ fn query_all_bins_reserves(
21562156
let response = AllBinsResponse {
21572157
reserves: bin_responses,
21582158
last_id: id,
2159-
last_block_height: env.block.height,
2159+
current_block_height: env.block.height,
21602160
};
21612161
to_binary(&response).map_err(Error::CwErr)
21622162
}
@@ -2189,51 +2189,62 @@ fn query_bins_reserves(deps: Deps, ids: Vec<u32>) -> Result<Binary> {
21892189
fn query_updated_bins_at_height(deps: Deps, height: u64) -> Result<Binary> {
21902190
let ids = BIN_RESERVES_UPDATED.load(deps.storage, height)?;
21912191

2192-
let response: UpdatedBinsAtHeightResponse = UpdatedBinsAtHeightResponse { height, ids };
2192+
let mut bin_responses = Vec::new();
2193+
2194+
for id in ids {
2195+
let bin: Bytes32 = BIN_MAP.load(deps.storage, id).unwrap_or([0u8; 32]);
2196+
let (bin_reserve_x, bin_reserve_y) = bin.decode();
2197+
bin_responses.push(BinResponse {
2198+
bin_reserve_x,
2199+
bin_reserve_y,
2200+
bin_id: id,
2201+
});
2202+
}
2203+
2204+
let response: UpdatedBinsAtHeightResponse = UpdatedBinsAtHeightResponse(bin_responses);
21932205

21942206
to_binary(&response).map_err(Error::CwErr)
21952207
}
21962208

2209+
use std::collections::HashSet;
2210+
21972211
fn query_updated_bins_at_multiple_heights(deps: Deps, heights: Vec<u64>) -> Result<Binary> {
2198-
let mut updates = Vec::new();
2212+
let mut bin_responses = Vec::new();
2213+
let mut processed_ids = HashSet::new();
2214+
21992215
for height in heights {
22002216
let ids = BIN_RESERVES_UPDATED.load(deps.storage, height)?;
2201-
updates.push(UpdatedBinsAtHeightResponse { height, ids });
2217+
2218+
for id in ids {
2219+
// Check if the id has already been processed
2220+
if processed_ids.insert(id) {
2221+
let bin: Bytes32 = BIN_MAP.load(deps.storage, id).unwrap_or([0u8; 32]);
2222+
let (bin_reserve_x, bin_reserve_y) = bin.decode();
2223+
bin_responses.push(BinResponse {
2224+
bin_reserve_x,
2225+
bin_reserve_y,
2226+
bin_id: id,
2227+
});
2228+
}
2229+
}
22022230
}
22032231

22042232
let response: UpdatedBinsAtMultipleHeightResponse =
2205-
UpdatedBinsAtMultipleHeightResponse(updates);
2206-
2207-
to_binary(&response).map_err(Error::CwErr)
2208-
}
2209-
2210-
fn query_bins_updating_heights(
2211-
deps: Deps,
2212-
page: Option<u32>,
2213-
page_size: Option<u32>,
2214-
) -> Result<Binary> {
2215-
let page = page.unwrap_or(0);
2216-
let page_size = page_size.unwrap_or(10);
2217-
let txs: StdResult<Vec<u64>> = BIN_RESERVES_UPDATED_LOG
2218-
.iter(deps.storage)?
2219-
.rev()
2220-
.skip((page * page_size) as usize)
2221-
.take(page_size as usize)
2222-
.collect();
2223-
2224-
let response = BinUpdatingHeightsResponse(txs?);
2233+
UpdatedBinsAtMultipleHeightResponse(bin_responses);
22252234

22262235
to_binary(&response).map_err(Error::CwErr)
22272236
}
22282237

22292238
fn query_updated_bins_after_height(
22302239
deps: Deps,
2240+
env: Env,
22312241
height: u64,
22322242
page: Option<u32>,
22332243
page_size: Option<u32>,
22342244
) -> Result<Binary> {
22352245
let page = page.unwrap_or(0);
22362246
let page_size = page_size.unwrap_or(10);
2247+
let mut processed_ids = HashSet::new();
22372248

22382249
let heights: StdResult<Vec<u64>> = BIN_RESERVES_UPDATED_LOG
22392250
.iter(deps.storage)?
@@ -2252,13 +2263,47 @@ fn query_updated_bins_after_height(
22522263
.take(page_size as usize)
22532264
.collect();
22542265

2255-
let mut updates = Vec::new();
2266+
let mut bin_responses = Vec::new();
2267+
22562268
for height in heights? {
22572269
let ids = BIN_RESERVES_UPDATED.load(deps.storage, height)?;
2258-
updates.push(UpdatedBinsAtHeightResponse { height, ids });
2270+
2271+
for id in ids {
2272+
if processed_ids.insert(id) {
2273+
let bin: Bytes32 = BIN_MAP.load(deps.storage, id).unwrap_or([0u8; 32]);
2274+
let (bin_reserve_x, bin_reserve_y) = bin.decode();
2275+
bin_responses.push(BinResponse {
2276+
bin_reserve_x,
2277+
bin_reserve_y,
2278+
bin_id: id,
2279+
});
2280+
}
2281+
}
22592282
}
22602283

2261-
let response = UpdatedBinsAfterHeightResponse(updates);
2284+
let response = UpdatedBinsAfterHeightResponse {
2285+
bins: bin_responses,
2286+
current_block_height: env.block.height,
2287+
};
2288+
2289+
to_binary(&response).map_err(Error::CwErr)
2290+
}
2291+
2292+
fn query_bins_updating_heights(
2293+
deps: Deps,
2294+
page: Option<u32>,
2295+
page_size: Option<u32>,
2296+
) -> Result<Binary> {
2297+
let page = page.unwrap_or(0);
2298+
let page_size = page_size.unwrap_or(10);
2299+
let txs: StdResult<Vec<u64>> = BIN_RESERVES_UPDATED_LOG
2300+
.iter(deps.storage)?
2301+
.rev()
2302+
.skip((page * page_size) as usize)
2303+
.take(page_size as usize)
2304+
.collect();
2305+
2306+
let response = BinUpdatingHeightsResponse(txs?);
22622307

22632308
to_binary(&response).map_err(Error::CwErr)
22642309
}

contracts/liquidity_book/tests/src/multitests/lb_pair_queries.rs

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,7 @@ pub fn test_query_bins_reserves() -> Result<(), anyhow::Error> {
329329

330330
assert!(balance > Uint256::MIN, "test_sample_mint::9");
331331
}
332-
println!("ids: {:?}", ids);
333-
println!("reserves: {:?}", reserves);
332+
334333
Ok(())
335334
}
336335

@@ -352,14 +351,14 @@ pub fn test_query_all_bins_reserves() -> Result<(), anyhow::Error> {
352351
ids.push(id);
353352
}
354353

355-
let (multiple_reserves, last_id) =
354+
let (multiple_reserves, last_id, _) =
356355
lb_pair::query_all_bins_reserves(&app, &lb_pair.lb_pair.contract, None, None, Some(5))?;
357356

358357
//checking pagination
359358
assert_eq!(last_id, ids[4]);
360359
assert_eq!(multiple_reserves.len(), 5);
361360

362-
let (multiple_reserves, last_id) = lb_pair::query_all_bins_reserves(
361+
let (multiple_reserves, last_id, _) = lb_pair::query_all_bins_reserves(
363362
&app,
364363
&lb_pair.lb_pair.contract,
365364
Some(last_id),
@@ -371,7 +370,7 @@ pub fn test_query_all_bins_reserves() -> Result<(), anyhow::Error> {
371370
assert_eq!(last_id, ids[9]);
372371
assert_eq!(multiple_reserves.len(), 5);
373372

374-
let (multiple_reserves, last_id) = lb_pair::query_all_bins_reserves(
373+
let (multiple_reserves, last_id, _) = lb_pair::query_all_bins_reserves(
375374
&app,
376375
&lb_pair.lb_pair.contract,
377376
Some(last_id),
@@ -383,7 +382,7 @@ pub fn test_query_all_bins_reserves() -> Result<(), anyhow::Error> {
383382
assert_eq!(last_id, ids[14]);
384383
assert_eq!(multiple_reserves.len(), 5);
385384

386-
let (multiple_reserves, last_id) = lb_pair::query_all_bins_reserves(
385+
let (multiple_reserves, last_id, _) = lb_pair::query_all_bins_reserves(
387386
&app,
388387
&lb_pair.lb_pair.contract,
389388
Some(last_id),
@@ -395,7 +394,7 @@ pub fn test_query_all_bins_reserves() -> Result<(), anyhow::Error> {
395394
assert_eq!(last_id, 0);
396395
assert_eq!(multiple_reserves.len(), 4);
397396

398-
let (multiple_reserves, last_id) =
397+
let (multiple_reserves, last_id, _) =
399398
lb_pair::query_all_bins_reserves(&app, &lb_pair.lb_pair.contract, None, None, Some(50))?;
400399
assert_eq!(last_id, 0);
401400

@@ -646,12 +645,15 @@ pub fn test_query_update_at_height() -> Result<(), anyhow::Error> {
646645

647646
let height = heights[0];
648647

649-
let (query_height, mut query_ids) =
648+
let bin_responses =
650649
lb_pair::query_updated_bins_at_height(&app, &lb_pair.lb_pair.contract, height)?;
650+
let mut bin_ids: Vec<u32> = bin_responses
651+
.into_iter()
652+
.map(|response| response.bin_id)
653+
.collect();
654+
bin_ids.sort();
651655

652-
assert_eq!(query_height, height);
653-
654-
assert_eq!(query_ids.sort(), ids.sort());
656+
assert_eq!(bin_ids, ids);
655657

656658
Ok(())
657659
}
@@ -687,12 +689,13 @@ pub fn test_query_update_at_multiple_heights() -> Result<(), anyhow::Error> {
687689

688690
let height = heights[0];
689691

690-
let (query_height, mut query_ids) =
691-
lb_pair::query_updated_bins_at_height(&app, &lb_pair.lb_pair.contract, height)?;
692+
let mut query_ids: Vec<u32> =
693+
lb_pair::query_updated_bins_at_height(&app, &lb_pair.lb_pair.contract, height)?
694+
.into_iter()
695+
.map(|x| x.bin_id)
696+
.collect();
692697

693-
assert_eq!(query_height, height);
694-
695-
assert_eq!(query_ids.sort(), ids.sort());
698+
assert_eq!(query_ids, ids);
696699

697700
roll_blockchain(&mut app, None);
698701

@@ -717,16 +720,15 @@ pub fn test_query_update_at_multiple_heights() -> Result<(), anyhow::Error> {
717720
heights.sort();
718721
assert_eq!(heights.len(), 50);
719722
//user have all the heights now
720-
let updated_bins = lb_pair::query_updated_bins_at_multiple_heights(
723+
let updated_bins: Vec<u32> = lb_pair::query_updated_bins_at_multiple_heights(
721724
&app,
722725
&lb_pair.lb_pair.contract,
723726
heights.clone(),
724-
)?;
725-
726-
for (updated_bin, h) in updated_bins.iter().zip(heights) {
727-
assert_eq!(updated_bin.height, h);
728-
assert_eq!(updated_bin.ids, ids);
729-
}
727+
)?
728+
.into_iter()
729+
.map(|x| x.bin_id)
730+
.collect();
731+
assert_eq!(updated_bins, ids);
730732

731733
Ok(())
732734
}
@@ -762,12 +764,13 @@ pub fn test_query_update_after_height() -> Result<(), anyhow::Error> {
762764

763765
let height = heights[0];
764766

765-
let (query_height, mut query_ids) =
766-
lb_pair::query_updated_bins_at_height(&app, &lb_pair.lb_pair.contract, height)?;
767-
768-
assert_eq!(query_height, height);
767+
let query_ids: Vec<u32> =
768+
lb_pair::query_updated_bins_at_height(&app, &lb_pair.lb_pair.contract, height)?
769+
.into_iter()
770+
.map(|x| x.bin_id)
771+
.collect();
769772

770-
assert_eq!(query_ids.sort(), ids.sort());
773+
assert_eq!(query_ids, ids);
771774

772775
for _ in 0..49 {
773776
roll_blockchain(&mut app, None);
@@ -790,35 +793,35 @@ pub fn test_query_update_after_height() -> Result<(), anyhow::Error> {
790793
heights.sort();
791794
assert_eq!(heights.len(), 50);
792795
//user have all the heights now
793-
let updated_bins = lb_pair::query_updated_bins_after_multiple_heights(
796+
let (updated_bins, _) = lb_pair::query_updated_bins_after_multiple_heights(
794797
&app,
795798
&lb_pair.lb_pair.contract,
796799
heights[0],
797800
Some(0),
798801
Some(10),
799802
)?;
800803

801-
assert_eq!(updated_bins.len(), 10);
804+
assert_eq!(updated_bins.len(), 19); //All the bins changed
802805

803-
let updated_bins = lb_pair::query_updated_bins_after_multiple_heights(
806+
let (updated_bins, _) = lb_pair::query_updated_bins_after_multiple_heights(
804807
&app,
805808
&lb_pair.lb_pair.contract,
806809
heights[0],
807810
Some(0),
808811
Some(20),
809812
)?;
810813

811-
assert_eq!(updated_bins.len(), 20);
814+
assert_eq!(updated_bins.len(), 19);
812815

813-
let updated_bins = lb_pair::query_updated_bins_after_multiple_heights(
816+
let (updated_bins, _) = lb_pair::query_updated_bins_after_multiple_heights(
814817
&app,
815818
&lb_pair.lb_pair.contract,
816819
heights[0],
817820
Some(0),
818821
Some(100),
819822
)?;
820823

821-
assert_eq!(updated_bins.len(), 49);
824+
assert_eq!(updated_bins.len(), 19);
822825

823826
Ok(())
824827
}

0 commit comments

Comments
 (0)