Skip to content

Commit d7fb6d5

Browse files
authored
Carp 48 endpoint unification (#23)
Unifying endpoints Co-authored-by: MichalVido <[email protected]>
1 parent 7be90e8 commit d7fb6d5

File tree

6 files changed

+76
-76
lines changed

6 files changed

+76
-76
lines changed

migration/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod m20221013_194016_add_datum_hash;
1111
mod m20221014_125218_create_indices;
1212
mod m20221021_115605_add_indices_to_foreign_keys;
1313
mod m20221024_135934_create_swap_table;
14+
mod m20221026_160617_drop_timestamp_column;
1415

1516
pub struct Migrator;
1617

@@ -29,6 +30,7 @@ impl MigratorTrait for Migrator {
2930
Box::new(m20221014_125218_create_indices::Migration),
3031
Box::new(m20221021_115605_add_indices_to_foreign_keys::Migration),
3132
Box::new(m20221024_135934_create_swap_table::Migration),
33+
Box::new(m20221026_160617_drop_timestamp_column::Migration),
3234
]
3335
}
3436
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use sea_orm_migration::prelude::*;
2+
3+
use crate::m20221013_162928_create_price_update_table::PriceUpdate;
4+
5+
#[derive(DeriveMigrationName)]
6+
pub struct Migration;
7+
8+
#[async_trait::async_trait]
9+
impl MigrationTrait for Migration {
10+
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
11+
manager
12+
.alter_table(
13+
Table::alter()
14+
.table(PriceUpdate::Table)
15+
.drop_column(PriceUpdate::Timestamp)
16+
.to_owned(),
17+
)
18+
.await
19+
}
20+
21+
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
22+
manager
23+
.alter_table(
24+
Table::alter()
25+
.table(PriceUpdate::Table)
26+
.add_column(
27+
ColumnDef::new(PriceUpdate::Timestamp)
28+
.timestamp()
29+
.not_null()
30+
.extra("DEFAULT CURRENT_TIMESTAMP".to_owned()),
31+
)
32+
.to_owned(),
33+
)
34+
.await
35+
}
36+
}

src/entity/price_update.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub struct Model {
1313
pub token2_id: i64,
1414
pub amount1: i64,
1515
pub amount2: i64,
16-
pub timestamp: DateTime,
1716
}
1817

1918
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]

src/queries.rs

Lines changed: 33 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use crate::{
1313
use oura::model::{
1414
BlockRecord, OutputAssetRecord, TransactionRecord, TxInputRecord, TxOutputRecord,
1515
};
16-
use sea_orm::entity::prelude::*;
1716
use sea_orm::{
1817
ActiveModelTrait, ColumnTrait, Condition, DatabaseConnection, DbBackend, EntityTrait,
1918
FromQueryResult, JoinType, Order, QueryFilter, QueryOrder, QuerySelect, RelationTrait, Set,
@@ -250,30 +249,14 @@ pub async fn insert_price_update(
250249
asset2: &AssetAmount,
251250
db: &DatabaseConnection,
252251
) -> anyhow::Result<()> {
253-
let token1_model = token::Entity::find()
254-
.filter(
255-
token::Column::PolicyId
256-
.eq(hex::decode(&asset1.asset.policy_id)?)
257-
.and(token::Column::Name.eq(hex::decode(&asset1.asset.name)?)),
258-
)
259-
.one(db)
260-
.await?
261-
.ok_or_else(|| anyhow::anyhow!("Token1 not found"))?;
262-
let token2_model = token::Entity::find()
263-
.filter(
264-
token::Column::PolicyId
265-
.eq(hex::decode(&asset2.asset.policy_id)?)
266-
.and(token::Column::Name.eq(hex::decode(&asset2.asset.name)?)),
267-
)
268-
.one(db)
269-
.await?
270-
.ok_or_else(|| anyhow::anyhow!("Token2 not found"))?;
252+
let token1_id = get_token_id(&asset1.asset, db).await?;
253+
let token2_id = get_token_id(&asset2.asset, db).await?;
271254

272255
let price_update_model = price_update::ActiveModel {
273256
tx_id: Set(tx_id),
274257
script_hash: Set(script_hash.to_vec()),
275-
token1_id: Set(token1_model.id),
276-
token2_id: Set(token2_model.id),
258+
token1_id: Set(token1_id),
259+
token2_id: Set(token2_id),
277260
amount1: Set(asset1.amount as i64),
278261
amount2: Set(asset2.amount as i64),
279262
..Default::default()
@@ -291,30 +274,14 @@ pub async fn insert_swap(
291274
direction: bool,
292275
db: &DatabaseConnection,
293276
) -> anyhow::Result<()> {
294-
let token1_model = token::Entity::find()
295-
.filter(
296-
token::Column::PolicyId
297-
.eq(hex::decode(&asset1.asset.policy_id)?)
298-
.and(token::Column::Name.eq(hex::decode(&asset1.asset.name)?)),
299-
)
300-
.one(db)
301-
.await?
302-
.ok_or_else(|| anyhow::anyhow!("Token1 not found"))?;
303-
let token2_model = token::Entity::find()
304-
.filter(
305-
token::Column::PolicyId
306-
.eq(hex::decode(&asset2.asset.policy_id)?)
307-
.and(token::Column::Name.eq(hex::decode(&asset2.asset.name)?)),
308-
)
309-
.one(db)
310-
.await?
311-
.ok_or_else(|| anyhow::anyhow!("Token2 not found"))?;
277+
let token1_id = get_token_id(&asset1.asset, db).await?;
278+
let token2_id = get_token_id(&asset2.asset, db).await?;
312279

313280
let swap_model = swap::ActiveModel {
314281
tx_id: Set(tx_id),
315282
script_hash: Set(script_hash.to_vec()),
316-
token1_id: Set(token1_model.id),
317-
token2_id: Set(token2_model.id),
283+
token1_id: Set(token1_id),
284+
token2_id: Set(token2_id),
318285
amount1: Set(asset1.amount as i64),
319286
amount2: Set(asset2.amount as i64),
320287
direction: Set(direction),
@@ -324,6 +291,19 @@ pub async fn insert_swap(
324291
Ok(())
325292
}
326293

294+
pub async fn get_token_id(asset: &Asset, db: &DatabaseConnection) -> anyhow::Result<i64> {
295+
Ok(token::Entity::find()
296+
.filter(
297+
token::Column::PolicyId
298+
.eq(hex::decode(&asset.policy_id)?)
299+
.and(token::Column::Name.eq(hex::decode(&asset.name)?)),
300+
)
301+
.one(db)
302+
.await?
303+
.ok_or_else(|| anyhow::anyhow!("Token not found"))?
304+
.id)
305+
}
306+
327307
#[allow(dead_code)]
328308
pub async fn get_latest_prices(db: &DatabaseConnection) -> anyhow::Result<Vec<ExchangeRate>> {
329309
// The raw SQL query here is rather unlucky, but we need to join the token table twice,
@@ -334,13 +314,11 @@ pub async fn get_latest_prices(db: &DatabaseConnection) -> anyhow::Result<Vec<Ex
334314
#[derive(Debug, FromQueryResult)]
335315
struct RawExchangeRate {
336316
script_hash: Vec<u8>,
337-
policy_id1: Vec<u8>,
338-
name1: Vec<u8>,
339-
policy_id2: Vec<u8>,
340-
name2: Vec<u8>,
317+
t1_id: i64,
318+
t2_id: i64,
341319
amount1: i64,
342320
amount2: i64,
343-
timestamp: DateTime,
321+
tx_id: i64,
344322
}
345323

346324
let raw_exchange_rates: Vec<RawExchangeRate> =
@@ -349,19 +327,17 @@ pub async fn get_latest_prices(db: &DatabaseConnection) -> anyhow::Result<Vec<Ex
349327
r#"
350328
SELECT
351329
script_hash,
352-
t1.policy_id AS policy_id1,
353-
t1.name AS name1,
354-
t2.policy_id AS policy_id2,
355-
t2.name AS name2,
330+
t1.id AS t1_id,
331+
t2.id AS t2_id,
356332
amount1,
357333
amount2,
358-
timestamp
334+
tx_id
359335
360336
FROM price_update
361337
JOIN token AS t1 ON t1.id = price_update.token1_id
362338
JOIN token AS t2 ON t2.id = price_update.token2_id
363-
WHERE (script_hash, token1_id, token2_id, timestamp) IN (
364-
SELECT script_hash, token1_id, token2_id, MAX(timestamp)
339+
WHERE (script_hash, token1_id, token2_id, tx_id) IN (
340+
SELECT script_hash, token1_id, token2_id, MAX(tx_id)
365341
FROM price_update
366342
GROUP BY script_hash, token1_id, token2_id
367343
)
@@ -376,20 +352,8 @@ pub async fn get_latest_prices(db: &DatabaseConnection) -> anyhow::Result<Vec<Ex
376352
.iter()
377353
.map(|r| ExchangeRate {
378354
script_hash: hex::encode(r.script_hash.clone()),
379-
asset1: AssetAmount {
380-
asset: Asset {
381-
policy_id: hex::encode(r.policy_id1.clone()),
382-
name: hex::encode(r.name1.clone()),
383-
},
384-
amount: r.amount1 as u64,
385-
},
386-
asset2: AssetAmount {
387-
asset: Asset {
388-
policy_id: hex::encode(r.policy_id2.clone()),
389-
name: hex::encode(r.name2.clone()),
390-
},
391-
amount: r.amount2 as u64,
392-
},
355+
asset1: r.t1_id,
356+
asset2: r.t2_id,
393357
rate: r.amount1 as f64 / r.amount2 as f64,
394358
})
395359
.collect())
@@ -420,7 +384,7 @@ pub async fn get_token_price_history(
420384
let data = price_update::Entity::find()
421385
.filter(price_update::Column::Token1Id.eq(asset_id1))
422386
.filter(price_update::Column::Token2Id.eq(asset_id2))
423-
.order_by(price_update::Column::Timestamp, Order::Desc)
387+
.order_by(price_update::Column::TxId, Order::Desc)
424388
.limit(count)
425389
.all(db)
426390
.await?;
@@ -431,7 +395,7 @@ pub async fn get_token_price_history(
431395
amount1: p.amount1,
432396
amount2: p.amount2,
433397
rate: p.amount1 as f64 / p.amount2 as f64,
434-
timestamp: p.timestamp,
398+
tx_id: p.tx_id,
435399
})
436400
.collect())
437401
}

src/sink.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,8 @@ pub async fn start(
347347
wr_get_transaction(transaction_record, &script_hash)
348348
{
349349
let exchange_rate = ExchangeRate {
350-
asset1: asset1.clone(),
351-
asset2: asset2.clone(),
350+
asset1: queries::get_token_id(&asset1.asset, &db).await?,
351+
asset2: queries::get_token_id(&asset2.asset, &db).await?,
352352
script_hash: pool.script_hash.clone(),
353353
rate: asset1.amount as f64 / asset2.amount as f64,
354354
};

src/types.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use sea_orm::prelude::DateTime;
21
use serde::Serialize;
32

43
#[derive(Debug, Serialize, Clone)]
@@ -16,8 +15,8 @@ pub struct AssetAmount {
1615
#[derive(Debug, Serialize, Clone)]
1716
pub struct ExchangeRate {
1817
pub script_hash: String,
19-
pub asset1: AssetAmount,
20-
pub asset2: AssetAmount,
18+
pub asset1: i64,
19+
pub asset2: i64,
2120
pub rate: f64,
2221
}
2322

@@ -26,7 +25,7 @@ pub struct ExchangeHistory {
2625
pub amount1: i64,
2726
pub amount2: i64,
2827
pub rate: f64,
29-
pub timestamp: DateTime,
28+
pub tx_id: i64,
3029
}
3130

3231
#[derive(Debug, Serialize)]

0 commit comments

Comments
 (0)