Skip to content

Commit

Permalink
re-organize and clean-up code before further implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Sep 19, 2024
1 parent fe8e244 commit 0f61aca
Show file tree
Hide file tree
Showing 9 changed files with 1,278 additions and 1,224 deletions.
1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ bech32 = "0.11.0"
blockfrost = "1.0.1"
blockfrost-openapi = "0.0.3"
clap = "4.5.17"
color-print = "0.3.6"
hex = "0.4.3"
indoc = "2.0.5"
pallas-addresses = "0.30.2"
Expand Down
53 changes: 29 additions & 24 deletions cli/src/cardano.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::{collections::BTreeMap, env};

pub struct Cardano {
api: BlockfrostAPI,
client: reqwest::Client,
network: Network,
network_prefix: String,
project_id: String,
Expand Down Expand Up @@ -43,6 +44,7 @@ impl Cardano {
let api = BlockfrostAPI::new(project_id.as_str(), Default::default());
Cardano {
api,
client: reqwest::Client::new(),
network: if project_id.starts_with(MAINNET_PREFIX) {
Network::Mainnet
} else {
Expand Down Expand Up @@ -140,36 +142,39 @@ impl Cardano {
})
.collect::<Vec<_>>();

let client = reqwest::Client::new();

let mut txs: Vec<Tx> = vec![];
for tx_hash in history {
// NOTE: Not part of the Rust SDK somehow...
let response = client
.get(&format!(
"https://cardano-{}.blockfrost.io/api/v0/txs/{}/cbor",
self.network_prefix, tx_hash
))
.header("Accept", "application/json")
.header("project_id", self.project_id.as_str())
.send()
.await
.unwrap();
match response.status() {
reqwest::StatusCode::OK => {
let TxByHash { cbor } = response.json::<TxByHash>().await.unwrap();
let tx = cbor::decode(&hex::decode(cbor).unwrap()).unwrap();
txs.push(tx);
}
status => {
panic!("unexpected response status from Blockfrost: {}", status);
}
};
if let Some(tx) = self.transaction_by_hash(&tx_hash).await {
txs.push(tx)
}
}

txs
}

pub async fn transaction_by_hash(&self, tx_hash: &str) -> Option<Tx> {
// NOTE: Not part of the Rust SDK somehow...
let response = self
.client
.get(&format!(
"https://cardano-{}.blockfrost.io/api/v0/txs/{}/cbor",
self.network_prefix, tx_hash
))
.header("Accept", "application/json")
.header("project_id", self.project_id.as_str())
.send()
.await
.unwrap();

match response.status() {
reqwest::StatusCode::OK => {
let TxByHash { cbor } = response.json::<TxByHash>().await.unwrap();
let tx = cbor::decode(&hex::decode(cbor).unwrap()).unwrap();
Some(tx)
}
_ => None,
}
}

pub async fn resolve(&self, input: &TransactionInput) -> Option<PostAlonzoTransactionOutput> {
let utxo = self
.api
Expand Down
Loading

0 comments on commit 0f61aca

Please sign in to comment.