Skip to content

Commit

Permalink
Use single block context (#2773)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouzo authored Jan 9, 2024
1 parent 70e9e7a commit 2bf6899
Show file tree
Hide file tree
Showing 22 changed files with 102 additions and 218 deletions.
2 changes: 1 addition & 1 deletion lib/ain-ocean/src/api/loan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ async fn list_vault_auction_history(
Ok(Json(ApiPagedResponse::of(
auctions,
query.size,
|auction| auction.clone().sort,
|auction| auction.sort.to_string(),
)))
}

Expand Down
9 changes: 2 additions & 7 deletions lib/ain-ocean/src/indexer/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use log::debug;
use super::BlockContext;
use crate::{
indexer::{Index, Result},
model::{VaultAuctionBatchHistory, VaultAuctionBatchHistoryBlock},
model::VaultAuctionBatchHistory,
repository::RepositoryOps,
SERVICES,
};
Expand All @@ -22,12 +22,7 @@ impl Index for PlaceAuctionBid {
from: self.from.clone(),
amount: self.token_amount.amount,
token_id: self.token_amount.token.0,
block: VaultAuctionBatchHistoryBlock {
hash: ctx.hash,
height: ctx.height,
time: ctx.time,
median_time: ctx.median_time,
},
block: ctx.clone(),
};
debug!("auction : {:?}", auction);

Expand Down
9 changes: 2 additions & 7 deletions lib/ain-ocean/src/indexer/masternode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use log::debug;
use super::BlockContext;
use crate::{
indexer::{Index, Result},
model::{HistoryItem, Masternode, MasternodeBlock},
model::{HistoryItem, Masternode},
repository::RepositoryOps,
SERVICES,
};
Expand Down Expand Up @@ -35,12 +35,7 @@ impl Index for CreateMasternode {
resign_tx: None,
minted_blocks: 0,
timelock: self.timelock.0.unwrap_or_default(),
block: MasternodeBlock {
hash: ctx.hash,
height: ctx.height,
time: ctx.time,
median_time: ctx.median_time,
},
block: ctx.clone(),
collateral: tx.output[1].value.to_string(),
history: Vec::new(),
};
Expand Down
10 changes: 1 addition & 9 deletions lib/ain-ocean/src/indexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,10 @@ pub(crate) trait Index {
fn invalidate(&self, context: &BlockContext, tx: Transaction, idx: usize) -> Result<()>;
}

use bitcoin::BlockHash;
use dftx_rs::{deserialize, Block, DfTx};
use log::debug;

use crate::Result;

pub(crate) struct BlockContext {
height: u32,
hash: BlockHash,
time: u64,
median_time: u64,
}
use crate::{model::BlockContext, Result};

pub fn index_block(block: String, block_height: u32) -> Result<()> {
debug!("[index_block] Indexing block...");
Expand Down
10 changes: 10 additions & 0 deletions lib/ain-ocean/src/model/block.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bitcoin::BlockHash;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)]
Expand All @@ -22,3 +23,12 @@ pub struct Block {
pub size_stripped: i32,
pub weight: i32,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct BlockContext {
pub hash: BlockHash,
pub height: u32,
pub time: u64,
pub median_time: u64,
}
15 changes: 4 additions & 11 deletions lib/ain-ocean/src/model/masternode.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use bitcoin::{BlockHash, ScriptBuf, Txid};
use bitcoin::{ScriptBuf, Txid};
use serde::{Deserialize, Serialize};

use super::BlockContext;

#[derive(Debug, Serialize, Deserialize)]
pub struct Masternode {
pub id: Txid, // Keep for backward compatibility
Expand All @@ -13,7 +15,7 @@ pub struct Masternode {
pub minted_blocks: i32,
pub timelock: u16,
pub collateral: String,
pub block: MasternodeBlock,
pub block: BlockContext,
pub history: Vec<HistoryItem>,
}

Expand All @@ -23,12 +25,3 @@ pub struct HistoryItem {
pub owner_address: ScriptBuf,
pub operator_address: ScriptBuf,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct MasternodeBlock {
pub hash: BlockHash,
pub height: u32,
pub time: u64,
pub median_time: u64,
}
15 changes: 4 additions & 11 deletions lib/ain-ocean/src/model/masternode_stats.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Default)]
use super::BlockContext;

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct MasternodeStats {
pub id: String,
pub block: MasternodeStatsBlock,
pub block: BlockContext,
pub stats: MasternodeStatsStats,
}

Expand All @@ -16,15 +18,6 @@ pub struct TimelockStats {
pub count: i32,
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct MasternodeStatsBlock {
pub hash: String,
pub height: i32,
pub time: i32,
pub median_time: i32,
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct MasternodeStatsStats {
Expand Down
17 changes: 5 additions & 12 deletions lib/ain-ocean/src/model/oracle.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)]
use super::BlockContext;

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Oracle {
pub id: String,
pub owner_address: String,
pub weightage: i32,
pub price_feeds: Vec<PriceFeedsItem>,
pub block: OracleBlock,
pub block: BlockContext,
}

#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)]
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct PriceFeedsItem {
pub token: String,
pub currency: String,
}

#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct OracleBlock {
pub hash: String,
pub height: i32,
pub time: i32,
pub median_time: i32,
}
15 changes: 4 additions & 11 deletions lib/ain-ocean/src/model/oracle_history.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Default)]
use super::BlockContext;

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct OracleHistory {
pub id: String,
Expand All @@ -9,7 +11,7 @@ pub struct OracleHistory {
pub owner_address: String,
pub weightage: i32,
pub price_feeds: Vec<PriceFeedsItem>,
pub block: OracleHistoryBlock,
pub block: BlockContext,
}

#[derive(Serialize, Deserialize, Debug, Default)]
Expand All @@ -18,12 +20,3 @@ pub struct PriceFeedsItem {
pub token: String,
pub currency: String,
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct OracleHistoryBlock {
pub hash: String,
pub height: i32,
pub time: i32,
pub median_time: i32,
}
23 changes: 8 additions & 15 deletions lib/ain-ocean/src/model/oracle_price_active.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Default)]
use super::BlockContext;

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct OraclePriceActive {
pub id: String,
Expand All @@ -9,42 +11,33 @@ pub struct OraclePriceActive {
pub active: OraclePriceActiveActive,
pub next: OraclePriceActiveNext,
pub is_live: bool,
pub block: OraclePriceActiveBlock,
pub block: BlockContext,
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct OraclePriceActiveActive {
pub amount: String,
pub weightage: i32,
pub oracles: OraclePriceActiveActiveOracles,
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct OraclePriceActiveNext {
pub amount: String,
pub weightage: i32,
pub oracles: OraclePriceActiveNextOracles,
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct OraclePriceActiveBlock {
pub hash: String,
pub height: i32,
pub time: i32,
pub median_time: i32,
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct OraclePriceActiveActiveOracles {
pub active: i32,
pub total: i32,
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct OraclePriceActiveNextOracles {
pub active: i32,
Expand Down
19 changes: 6 additions & 13 deletions lib/ain-ocean/src/model/oracle_price_aggregated.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Default)]
use super::BlockContext;

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct OraclePriceAggregated {
pub id: String,
Expand All @@ -9,27 +11,18 @@ pub struct OraclePriceAggregated {
pub token: String,
pub currency: String,
pub aggregated: OraclePriceAggregatedAggregated,
pub block: OraclePriceAggregatedBlock,
pub block: BlockContext,
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct OraclePriceAggregatedAggregated {
pub amount: String,
pub weightage: i32,
pub oracles: OraclePriceAggregatedAggregatedOracles,
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct OraclePriceAggregatedBlock {
pub hash: String,
pub height: i32,
pub time: i32,
pub median_time: i32,
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct OraclePriceAggregatedAggregatedOracles {
pub active: i32,
Expand Down
19 changes: 6 additions & 13 deletions lib/ain-ocean/src/model/oracle_price_aggregated_interval.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Default)]
use super::BlockContext;

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct OraclePriceAggregatedInterval {
pub id: String,
Expand All @@ -9,10 +11,10 @@ pub struct OraclePriceAggregatedInterval {
pub token: String,
pub currency: String,
pub aggregated: OraclePriceAggregatedIntervalAggregated,
pub block: OraclePriceAggregatedIntervalBlock,
pub block: BlockContext,
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct OraclePriceAggregatedIntervalAggregated {
pub amount: String,
Expand All @@ -21,16 +23,7 @@ pub struct OraclePriceAggregatedIntervalAggregated {
pub oracles: OraclePriceAggregatedIntervalAggregatedOracles,
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct OraclePriceAggregatedIntervalBlock {
pub hash: String,
pub height: i32,
pub time: i32,
pub median_time: i32,
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct OraclePriceAggregatedIntervalAggregatedOracles {
pub active: i32,
Expand Down
15 changes: 4 additions & 11 deletions lib/ain-ocean/src/model/oracle_price_feed.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Default)]
use super::BlockContext;

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct OraclePriceFeed {
pub id: String,
Expand All @@ -12,14 +14,5 @@ pub struct OraclePriceFeed {
pub txid: String,
pub time: i32,
pub amount: String,
pub block: OraclePriceFeedBlock,
}

#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct OraclePriceFeedBlock {
pub hash: String,
pub height: i32,
pub time: i32,
pub median_time: i32,
pub block: BlockContext,
}
Loading

0 comments on commit 2bf6899

Please sign in to comment.