Skip to content

Commit 3877e0c

Browse files
committed
Rename to WTP and clean it up
1 parent c8fdd84 commit 3877e0c

File tree

6 files changed

+120
-108
lines changed

6 files changed

+120
-108
lines changed

Cargo.lock

Lines changed: 25 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
members = [".", "migration"]
33

44
[package]
5-
name = "cardano_price_feed"
5+
name = "wtp"
66
version = "0.1.0"
77
edition = "2021"
88

README.md

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
1-
# Fast Cardano swap price feed
1+
# What the price
2+
Tool for cardano to get Dex SWAP operation, store it to the Database and broadcast it throught the websocket.
23

3-
## Setting up
4+
## DEXes
5+
Current state is, that we have implemeneted 3 Dexes
6+
* WingRiders
7+
* MinSwap
8+
* SundaeSwap
49

5-
Run a Postgres instance and create an empty databas for this project:
10+
Dex can have more versions and more addresses per version.
611

7-
```bash
8-
psql -U postgres -c 'CREATE DATABASE cardano_price_feed;'
9-
```
12+
## Interface
13+
* `/health` - Health check endpoint
14+
* `/assets` - List of assets presents in the database. This is place, where pair asset_id with name and policy
15+
* `/exchange_rates` - Calculate exchange rate. There is no information about decimal numbers
16+
* `/mean_history/TOKEN1_ID/TOKEN2_ID?count=<number>` - Return mean swap price for tokens. Mean is not AVG, but ration on the pool address
17+
* `/asset_swap/TOKEN1_ID/TOKEN2_ID?count=<number>` - Return last swap price for tokens.
18+
* `/socket/` - WebSocket enpoint for Live informatino about swap.
1019

11-
## Running
20+
21+
## Setting up
22+
23+
Run a Postgres instance and create an empty databas for this project:
1224

1325
```bash
14-
export DATABASE_URL='postgres://postgres:postgres@localhost:5432/cardano_price_feed'
26+
export DATABASE_URL='postgres://postgres:postgres@localhost:5432/wtp'
27+
psql -U postgres -c 'CREATE DATABASE wtp;'
1528
cargo migrate up
1629
cargo run -- -s 'relays-new.cardano-mainnet.iohk.io:3001' -d $DATABASE_URL
1730
```
@@ -34,6 +47,8 @@ cargo migrate up
3447
sea-orm-cli generate entity -o src/entity
3548
```
3649

50+
## Run
51+
3752
```bash
3853
# Ideal run parametres for WR
3954
cargo run -- --socket localhost:3001 --database $DATABASE_URL --persistent --start 57270168:17a26b5607a6f61fe89bf73a7a242ff4fa6dd6c667f3b2d6fc56bbcad644e90b

src/main.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ struct Args {
3333
#[arg(short, long)]
3434
database: String,
3535

36-
/// Persistency option
36+
// Persistency option
3737
#[arg(short, long)]
3838
persistent: bool,
3939

40+
// Bind address
41+
#[arg(short, long, default_value_t = String::from("0.0.0.0:3000"))]
42+
bind: String,
43+
4044
/// Config file
4145
#[arg(short, long, default_value_t = String::from("example.toml"))]
4246
config: String,
@@ -49,8 +53,8 @@ async fn main() -> anyhow::Result<()> {
4953
let fmt_layer = tracing_subscriber::fmt::layer();
5054
let filter = tracing_subscriber::filter::Targets::new()
5155
.with_target("oura", tracing::Level::WARN)
52-
//.with_target("sqlx", tracing::Level::DEBUG)
53-
.with_target("cardano_price_feed", tracing::Level::TRACE);
56+
.with_target("sqlx", tracing::Level::DEBUG)
57+
.with_target("wtp", tracing::Level::TRACE);
5458

5559
tracing_subscriber::registry()
5660
.with(fmt_layer)
@@ -63,7 +67,7 @@ async fn main() -> anyhow::Result<()> {
6367
}
6468

6569
let db_path = args.database.clone();
66-
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
70+
let addr: SocketAddr = args.bind.parse().unwrap();
6771
let make_service = make_service_fn(move |_conn| {
6872
let db_path = db_path.clone();
6973
let service = service_fn(move |req| server::route(req, db_path.clone()));

src/queries.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use sea_orm::{
1919
Statement,
2020
};
2121

22-
#[allow(dead_code)]
22+
2323
pub async fn insert_block(block: &BlockRecord, db: &DatabaseConnection) -> anyhow::Result<i64> {
2424
let previous_hash = hex::decode(block.previous_hash.clone())?;
2525
let previous_block_model = block::Entity::find()
@@ -40,7 +40,7 @@ pub async fn insert_block(block: &BlockRecord, db: &DatabaseConnection) -> anyho
4040
Ok(block_model.id)
4141
}
4242

43-
#[allow(dead_code)]
43+
4444
pub async fn rollback_to_slot(slot: &u64, db: &DatabaseConnection) -> anyhow::Result<()> {
4545
// We remove all blocks that are after the given slot. Removing based on the rollback event's
4646
// block_hash might not work because it's affected by the --start option and thus the
@@ -52,7 +52,7 @@ pub async fn rollback_to_slot(slot: &u64, db: &DatabaseConnection) -> anyhow::Re
5252
Ok(())
5353
}
5454

55-
#[allow(dead_code)]
55+
5656
pub async fn insert_transaction(
5757
transaction: &TransactionRecord,
5858
block_id: i64,
@@ -102,7 +102,6 @@ pub async fn insert_transaction(
102102
Ok(transaction_model.id)
103103
}
104104

105-
#[allow(dead_code)]
106105
async fn insert_missing_addresses(
107106
addresses: HashSet<String>,
108107
db: &DatabaseConnection,
@@ -139,7 +138,6 @@ async fn insert_missing_addresses(
139138
.collect())
140139
}
141140

142-
#[allow(dead_code)]
143141
async fn insert_missing_tokens(
144142
tokens: HashSet<(Vec<u8>, Vec<u8>)>,
145143
db: &DatabaseConnection,
@@ -189,7 +187,6 @@ async fn insert_missing_tokens(
189187
.collect())
190188
}
191189

192-
#[allow(dead_code)]
193190
async fn insert_output(
194191
output: &TxOutputRecord,
195192
transaction_model: &transaction::Model,
@@ -241,7 +238,6 @@ async fn insert_output(
241238
Ok(())
242239
}
243240

244-
#[allow(dead_code)]
245241
pub async fn insert_price_update(
246242
tx_id: i64,
247243
script_hash: &[u8],
@@ -264,7 +260,6 @@ pub async fn insert_price_update(
264260
Ok(())
265261
}
266262

267-
#[allow(dead_code)]
268263
pub async fn insert_swap(
269264
tx_id: i64,
270265
script_hash: &[u8],
@@ -298,7 +293,6 @@ pub async fn get_token_id(asset: &Asset, db: &DatabaseConnection) -> anyhow::Res
298293
.id)
299294
}
300295

301-
#[allow(dead_code)]
302296
pub async fn get_latest_prices(db: &DatabaseConnection) -> anyhow::Result<Vec<ExchangeRate>> {
303297
// The raw SQL query here is rather unlucky, but we need to join the token table twice,
304298
// and the sea-orm version usde by us (dcSpark's fork which implements
@@ -312,7 +306,6 @@ pub async fn get_latest_prices(db: &DatabaseConnection) -> anyhow::Result<Vec<Ex
312306
t2_id: i64,
313307
amount1: i64,
314308
amount2: i64,
315-
tx_id: i64,
316309
}
317310

318311
let raw_exchange_rates: Vec<RawExchangeRate> =
@@ -324,8 +317,7 @@ pub async fn get_latest_prices(db: &DatabaseConnection) -> anyhow::Result<Vec<Ex
324317
t1.id AS t1_id,
325318
t2.id AS t2_id,
326319
amount1,
327-
amount2,
328-
tx_id
320+
amount2
329321
330322
FROM price_update
331323
JOIN token AS t1 ON t1.id = price_update.token1_id
@@ -423,7 +415,6 @@ pub async fn get_swap_history(
423415
.collect())
424416
}
425417

426-
#[allow(dead_code)]
427418
pub async fn get_utxo_input(
428419
inputs: &[TxInputRecord],
429420
db: &DatabaseConnection,

src/sink/sundaeswap_v1.rs

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -140,67 +140,69 @@ impl common::Dex for SundaeSwapV1 {
140140
== 0
141141
{
142142
let address = get_address_from_plutus(&plutus.plutus_data);
143-
let utxo_pos = free_utxo.iter().position(|o| o.address == address).unwrap();
144-
let utxo = free_utxo[utxo_pos];
145-
// Remove this UTxO as used
146-
free_utxo.remove(utxo_pos);
147-
148-
let (amount1, amount2, direction) = match plutus.plutus_data["fields"][3]
149-
["fields"][0]["constructor"]
150-
.as_i64()
151-
.unwrap()
152-
== 0
143+
if let Some(utxo_pos) = free_utxo.iter().position(|o| o.address == address)
153144
{
154-
true => (
155-
common::get_amount(input, &asset1.policy_id, &asset1.name)
156-
- common::reduce_ada_amount(
157-
&asset1.policy_id,
158-
&asset1.name,
159-
SS1_ADA_SWAP_IN,
160-
),
161-
common::get_amount(utxo, &asset2.policy_id, &asset2.name)
162-
- common::reduce_ada_amount(
163-
&asset2.policy_id,
164-
&asset2.name,
165-
SS1_ADA_SWAP_OUT,
166-
),
167-
false,
168-
),
169-
false => (
170-
common::get_amount(utxo, &asset1.policy_id, &asset1.name)
171-
- common::reduce_ada_amount(
172-
&asset1.policy_id,
173-
&asset1.name,
174-
SS1_ADA_SWAP_OUT,
175-
),
176-
common::get_amount(input, &asset2.policy_id, &asset2.name)
177-
- common::reduce_ada_amount(
178-
&asset2.policy_id,
179-
&asset2.name,
180-
SS1_ADA_SWAP_IN,
181-
),
182-
true,
183-
),
184-
};
145+
let utxo = free_utxo[utxo_pos];
146+
// Remove this UTxO as used
147+
free_utxo.remove(utxo_pos);
148+
149+
let (amount1, amount2, direction) = match plutus.plutus_data["fields"]
150+
[3]["fields"][0]["constructor"]
151+
.as_i64()
152+
.unwrap()
153+
== 0
154+
{
155+
true => (
156+
common::get_amount(input, &asset1.policy_id, &asset1.name)
157+
- common::reduce_ada_amount(
158+
&asset1.policy_id,
159+
&asset1.name,
160+
SS1_ADA_SWAP_IN,
161+
),
162+
common::get_amount(utxo, &asset2.policy_id, &asset2.name)
163+
- common::reduce_ada_amount(
164+
&asset2.policy_id,
165+
&asset2.name,
166+
SS1_ADA_SWAP_OUT,
167+
),
168+
false,
169+
),
170+
false => (
171+
common::get_amount(utxo, &asset1.policy_id, &asset1.name)
172+
- common::reduce_ada_amount(
173+
&asset1.policy_id,
174+
&asset1.name,
175+
SS1_ADA_SWAP_OUT,
176+
),
177+
common::get_amount(input, &asset2.policy_id, &asset2.name)
178+
- common::reduce_ada_amount(
179+
&asset2.policy_id,
180+
&asset2.name,
181+
SS1_ADA_SWAP_IN,
182+
),
183+
true,
184+
),
185+
};
185186

186-
// Add swap to the result
187-
swaps.push(Swap {
188-
first: AssetAmount {
189-
asset: Asset {
190-
policy_id: asset1.policy_id.clone(),
191-
name: asset1.name.clone(),
187+
// Add swap to the result
188+
swaps.push(Swap {
189+
first: AssetAmount {
190+
asset: Asset {
191+
policy_id: asset1.policy_id.clone(),
192+
name: asset1.name.clone(),
193+
},
194+
amount: amount1 as u64,
192195
},
193-
amount: amount1 as u64,
194-
},
195-
second: AssetAmount {
196-
asset: Asset {
197-
policy_id: asset2.policy_id.clone(),
198-
name: asset2.name.clone(),
196+
second: AssetAmount {
197+
asset: Asset {
198+
policy_id: asset2.policy_id.clone(),
199+
name: asset2.name.clone(),
200+
},
201+
amount: amount2 as u64,
199202
},
200-
amount: amount2 as u64,
201-
},
202-
direction,
203-
});
203+
direction,
204+
});
205+
}
204206
}
205207
}
206208
}

0 commit comments

Comments
 (0)