Skip to content

Commit 0f61aca

Browse files
committed
re-organize and clean-up code before further implementation.
1 parent fe8e244 commit 0f61aca

File tree

9 files changed

+1278
-1224
lines changed

9 files changed

+1278
-1224
lines changed

cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ bech32 = "0.11.0"
88
blockfrost = "1.0.1"
99
blockfrost-openapi = "0.0.3"
1010
clap = "4.5.17"
11+
color-print = "0.3.6"
1112
hex = "0.4.3"
1213
indoc = "2.0.5"
1314
pallas-addresses = "0.30.2"

cli/src/cardano.rs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::{collections::BTreeMap, env};
1111

1212
pub struct Cardano {
1313
api: BlockfrostAPI,
14+
client: reqwest::Client,
1415
network: Network,
1516
network_prefix: String,
1617
project_id: String,
@@ -43,6 +44,7 @@ impl Cardano {
4344
let api = BlockfrostAPI::new(project_id.as_str(), Default::default());
4445
Cardano {
4546
api,
47+
client: reqwest::Client::new(),
4648
network: if project_id.starts_with(MAINNET_PREFIX) {
4749
Network::Mainnet
4850
} else {
@@ -140,36 +142,39 @@ impl Cardano {
140142
})
141143
.collect::<Vec<_>>();
142144

143-
let client = reqwest::Client::new();
144-
145145
let mut txs: Vec<Tx> = vec![];
146146
for tx_hash in history {
147-
// NOTE: Not part of the Rust SDK somehow...
148-
let response = client
149-
.get(&format!(
150-
"https://cardano-{}.blockfrost.io/api/v0/txs/{}/cbor",
151-
self.network_prefix, tx_hash
152-
))
153-
.header("Accept", "application/json")
154-
.header("project_id", self.project_id.as_str())
155-
.send()
156-
.await
157-
.unwrap();
158-
match response.status() {
159-
reqwest::StatusCode::OK => {
160-
let TxByHash { cbor } = response.json::<TxByHash>().await.unwrap();
161-
let tx = cbor::decode(&hex::decode(cbor).unwrap()).unwrap();
162-
txs.push(tx);
163-
}
164-
status => {
165-
panic!("unexpected response status from Blockfrost: {}", status);
166-
}
167-
};
147+
if let Some(tx) = self.transaction_by_hash(&tx_hash).await {
148+
txs.push(tx)
149+
}
168150
}
169-
170151
txs
171152
}
172153

154+
pub async fn transaction_by_hash(&self, tx_hash: &str) -> Option<Tx> {
155+
// NOTE: Not part of the Rust SDK somehow...
156+
let response = self
157+
.client
158+
.get(&format!(
159+
"https://cardano-{}.blockfrost.io/api/v0/txs/{}/cbor",
160+
self.network_prefix, tx_hash
161+
))
162+
.header("Accept", "application/json")
163+
.header("project_id", self.project_id.as_str())
164+
.send()
165+
.await
166+
.unwrap();
167+
168+
match response.status() {
169+
reqwest::StatusCode::OK => {
170+
let TxByHash { cbor } = response.json::<TxByHash>().await.unwrap();
171+
let tx = cbor::decode(&hex::decode(cbor).unwrap()).unwrap();
172+
Some(tx)
173+
}
174+
_ => None,
175+
}
176+
}
177+
173178
pub async fn resolve(&self, input: &TransactionInput) -> Option<PostAlonzoTransactionOutput> {
174179
let utxo = self
175180
.api

0 commit comments

Comments
 (0)