@@ -13,7 +13,6 @@ use crate::{
13
13
use oura:: model:: {
14
14
BlockRecord , OutputAssetRecord , TransactionRecord , TxInputRecord , TxOutputRecord ,
15
15
} ;
16
- use sea_orm:: entity:: prelude:: * ;
17
16
use sea_orm:: {
18
17
ActiveModelTrait , ColumnTrait , Condition , DatabaseConnection , DbBackend , EntityTrait ,
19
18
FromQueryResult , JoinType , Order , QueryFilter , QueryOrder , QuerySelect , RelationTrait , Set ,
@@ -250,30 +249,14 @@ pub async fn insert_price_update(
250
249
asset2 : & AssetAmount ,
251
250
db : & DatabaseConnection ,
252
251
) -> 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 ?;
271
254
272
255
let price_update_model = price_update:: ActiveModel {
273
256
tx_id : Set ( tx_id) ,
274
257
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 ) ,
277
260
amount1 : Set ( asset1. amount as i64 ) ,
278
261
amount2 : Set ( asset2. amount as i64 ) ,
279
262
..Default :: default ( )
@@ -291,30 +274,14 @@ pub async fn insert_swap(
291
274
direction : bool ,
292
275
db : & DatabaseConnection ,
293
276
) -> 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 ?;
312
279
313
280
let swap_model = swap:: ActiveModel {
314
281
tx_id : Set ( tx_id) ,
315
282
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 ) ,
318
285
amount1 : Set ( asset1. amount as i64 ) ,
319
286
amount2 : Set ( asset2. amount as i64 ) ,
320
287
direction : Set ( direction) ,
@@ -324,6 +291,19 @@ pub async fn insert_swap(
324
291
Ok ( ( ) )
325
292
}
326
293
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
+
327
307
#[ allow( dead_code) ]
328
308
pub async fn get_latest_prices ( db : & DatabaseConnection ) -> anyhow:: Result < Vec < ExchangeRate > > {
329
309
// 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
334
314
#[ derive( Debug , FromQueryResult ) ]
335
315
struct RawExchangeRate {
336
316
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 ,
341
319
amount1 : i64 ,
342
320
amount2 : i64 ,
343
- timestamp : DateTime ,
321
+ tx_id : i64 ,
344
322
}
345
323
346
324
let raw_exchange_rates: Vec < RawExchangeRate > =
@@ -349,19 +327,17 @@ pub async fn get_latest_prices(db: &DatabaseConnection) -> anyhow::Result<Vec<Ex
349
327
r#"
350
328
SELECT
351
329
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,
356
332
amount1,
357
333
amount2,
358
- timestamp
334
+ tx_id
359
335
360
336
FROM price_update
361
337
JOIN token AS t1 ON t1.id = price_update.token1_id
362
338
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 )
365
341
FROM price_update
366
342
GROUP BY script_hash, token1_id, token2_id
367
343
)
@@ -376,20 +352,8 @@ pub async fn get_latest_prices(db: &DatabaseConnection) -> anyhow::Result<Vec<Ex
376
352
. iter ( )
377
353
. map ( |r| ExchangeRate {
378
354
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 ,
393
357
rate : r. amount1 as f64 / r. amount2 as f64 ,
394
358
} )
395
359
. collect ( ) )
@@ -420,7 +384,7 @@ pub async fn get_token_price_history(
420
384
let data = price_update:: Entity :: find ( )
421
385
. filter ( price_update:: Column :: Token1Id . eq ( asset_id1) )
422
386
. 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 )
424
388
. limit ( count)
425
389
. all ( db)
426
390
. await ?;
@@ -431,7 +395,7 @@ pub async fn get_token_price_history(
431
395
amount1 : p. amount1 ,
432
396
amount2 : p. amount2 ,
433
397
rate : p. amount1 as f64 / p. amount2 as f64 ,
434
- timestamp : p. timestamp ,
398
+ tx_id : p. tx_id ,
435
399
} )
436
400
. collect ( ) )
437
401
}
0 commit comments