Skip to content

Commit

Permalink
Ocean: fix active price (#3126)
Browse files Browse the repository at this point in the history
* fix get_feed_active api resp

* fix OraclePriceAggregatedId & OraclePriceFeedId sort key

* fix index_block_start and index_block_end

* clippy

* fmt

* add missing pagination

* [oracle] convert u32,i64 to be bytes

* ScriptAggregationId height u32 -> bytes

* storage: endianness - mn, poolswap

* storage: endianness - OracleHistory

* ScriptUnspentKey uzize to be

* fmt_rs

* ApiRpcResponse

* optimize prices indexer & api

* missing: script activity key usize to BE

* fmt_rs

* use filter_map to perform set
  • Loading branch information
canonbrother authored Nov 26, 2024
1 parent fcc4595 commit 997a654
Show file tree
Hide file tree
Showing 21 changed files with 465 additions and 255 deletions.
23 changes: 11 additions & 12 deletions lib/ain-ocean/src/api/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn get_latest_aggregation(
.services
.script_aggregation
.by_id
.list(Some((hid, u32::MAX)), SortOrder::Descending)?
.list(Some((hid, [0xffu8; 4])), SortOrder::Descending)?
.take(1)
.take_while(|item| match item {
Ok(((v, _), _)) => v == &hid,
Expand Down Expand Up @@ -342,8 +342,8 @@ async fn list_transactions(
_ => ScriptActivityTypeHex::Vout,
};
let txid = Txid::from_str(txid)?;
let n = n.parse::<usize>()?;
Ok::<([u8; 4], ScriptActivityTypeHex, Txid, usize), Error>((
let n = n.parse::<usize>()?.to_be_bytes();
Ok::<([u8; 4], ScriptActivityTypeHex, Txid, [u8; 8]), Error>((
height,
vin_vout_type,
txid,
Expand All @@ -352,10 +352,10 @@ async fn list_transactions(
})
.transpose()?
.unwrap_or((
[u8::MAX, u8::MAX, u8::MAX, u8::MAX],
[0xffu8; 4],
ScriptActivityTypeHex::Vout,
Txid::from_byte_array([0xffu8; 32]),
usize::MAX,
[0xffu8; 8],
));

let res = ctx
Expand Down Expand Up @@ -458,15 +458,14 @@ async fn list_transaction_unspent(
msg: format!("Invalid height: {}", height),
})?;
let txid = Txid::from_str(txid)?;
let n = n.parse::<usize>()?;
Ok::<([u8; 4], Txid, usize), Error>((height, txid, n))
let decoded_n = hex::decode(n)?;
let n = decoded_n.try_into().map_err(|_| Error::Other {
msg: format!("Invalid txno: {}", n),
})?;
Ok::<([u8; 4], Txid, [u8; 8]), Error>((height, txid, n))
})
.transpose()?
.unwrap_or((
[0u8, 0u8, 0u8, 0u8],
Txid::from_byte_array([0x00u8; 32]),
usize::default(),
));
.unwrap_or(([0u8; 4], Txid::from_byte_array([0x00u8; 32]), [0u8; 8]));

let res = ctx
.services
Expand Down
15 changes: 11 additions & 4 deletions lib/ain-ocean/src/api/loan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,14 @@ fn get_active_price(
.services
.oracle_price_active
.by_id
.list(Some((token, currency, u32::MAX)), SortOrder::Descending)?
.list(
Some((token.clone(), currency.clone(), [0xffu8; 4])),
SortOrder::Descending,
)?
.take_while(|item| match item {
Ok((k, _)) => k.0 == token && k.1 == currency,
_ => true,
})
.next()
.map(|item| {
let (_, v) = item?;
Expand Down Expand Up @@ -264,7 +271,7 @@ async fn list_loan_token(
.services
.oracle_price_active
.by_id
.list(Some((token, currency, u32::MAX)), SortOrder::Descending)?
.list(Some((token, currency, [0xffu8; 4])), SortOrder::Descending)?
.next()
.map(|item| {
let (_, v) = item?;
Expand Down Expand Up @@ -671,7 +678,7 @@ async fn map_liquidation_batches(
let id = (
Txid::from_str(vault_id)?,
batch.index.to_be_bytes(),
[0xffu8, 0xffu8, 0xffu8, 0xffu8],
[0xffu8; 4],
Txid::from_byte_array([0xffu8; 32]),
);
let bids = repo
Expand Down Expand Up @@ -733,7 +740,7 @@ async fn map_token_amounts(
.oracle_price_active
.by_id
.list(
Some((token_info.symbol.clone(), "USD".to_string(), u32::MAX)),
Some((token_info.symbol.clone(), "USD".to_string(), [0xffu8; 4])),
SortOrder::Descending,
)?
.take_while(|item| match item {
Expand Down
6 changes: 3 additions & 3 deletions lib/ain-ocean/src/api/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ async fn get_feed(
.list(None, SortOrder::Descending)?
.paginate(&query)
.flatten()
.filter(|((token, currency, oracle_id, _), _)| {
.filter(|((token, currency, oracle_id, _, _), _)| {
key.0.eq(token) && key.1.eq(currency) && key.2.eq(oracle_id)
})
.map(|((token, currency, oracle_id, txid), feed)| {
.map(|((token, currency, oracle_id, height, txid), feed)| {
let amount = Decimal::from(feed.amount) / Decimal::from(COIN);
OraclePriceFeedResponse {
id: format!("{}-{}-{}-{}", token, currency, oracle_id, txid),
key: format!("{}-{}-{}", token, currency, oracle_id),
sort: hex::encode(feed.block.height.to_string() + &txid.to_string()),
sort: hex::encode(u32::from_be_bytes(height).to_string() + &txid.to_string()),
token,
currency,
oracle_id,
Expand Down
Loading

0 comments on commit 997a654

Please sign in to comment.