Skip to content

Commit 56e8801

Browse files
committed
refactor: use UTxOREST::new() constructor instead of inline struct building
Signed-off-by: William Hankins <[email protected]>
1 parent 74502fc commit 56e8801

File tree

3 files changed

+61
-78
lines changed

3 files changed

+61
-78
lines changed

modules/rest_blockfrost/src/handlers/accounts.rs

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ use acropolis_common::queries::utils::query_state;
1717
use acropolis_common::queries::utxos::{UTxOStateQuery, UTxOStateQueryResponse};
1818
use acropolis_common::rest_error::RESTError;
1919
use acropolis_common::serialization::{Bech32Conversion, Bech32WithHrp};
20-
use acropolis_common::{DRepChoice, Datum, ReferenceScript, StakeAddress};
21-
use blake2::{Blake2b512, Digest};
20+
use acropolis_common::{DRepChoice, StakeAddress};
2221
use caryatid_sdk::Context;
2322

2423
#[derive(serde::Serialize)]
@@ -822,42 +821,13 @@ pub async fn handle_account_utxos_blockfrost(
822821

823822
let mut rest_response = Vec::with_capacity(entries.len());
824823
for (i, entry) in entries.into_iter().enumerate() {
825-
let tx_hash = hashes.tx_hashes.get(i).map(hex::encode).unwrap_or_default();
826-
let block_hash = hashes.block_hashes.get(i).map(hex::encode).unwrap_or_default();
827-
let tx_index = utxo_identifiers[i].tx_index();
828-
let output_index = utxo_identifiers.get(i).map(|id| id.output_index()).unwrap_or(0);
829-
let (data_hash, inline_datum) = match &entry.datum {
830-
Some(Datum::Hash(h)) => (Some(hex::encode(h)), None),
831-
Some(Datum::Inline(bytes)) => (None, Some(hex::encode(bytes))),
832-
None => (None, None),
833-
};
834-
let reference_script_hash = match &entry.reference_script {
835-
Some(script) => {
836-
let bytes = match script {
837-
ReferenceScript::Native(b)
838-
| ReferenceScript::PlutusV1(b)
839-
| ReferenceScript::PlutusV2(b)
840-
| ReferenceScript::PlutusV3(b) => b,
841-
};
842-
let mut hasher = Blake2b512::new();
843-
hasher.update(bytes);
844-
let result = hasher.finalize();
845-
Some(hex::encode(&result[..32]))
846-
}
847-
None => None,
848-
};
849-
850-
rest_response.push(UTxOREST {
851-
address: entry.address.to_string()?,
852-
tx_hash,
853-
tx_index,
854-
output_index,
855-
amount: entry.value.into(),
856-
block: block_hash,
857-
data_hash,
858-
inline_datum,
859-
reference_script_hash,
860-
})
824+
rest_response.push(UTxOREST::new(
825+
entry.address.to_string()?,
826+
&utxo_identifiers[i],
827+
&entry,
828+
hashes.tx_hashes[i].as_ref(),
829+
hashes.block_hashes[i].as_ref(),
830+
));
861831
}
862832

863833
let json = serde_json::to_string_pretty(&rest_response)?;

modules/rest_blockfrost/src/handlers/addresses.rs

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ use acropolis_common::{
1414
},
1515
Address, Value,
1616
};
17-
use acropolis_common::{Datum, ReferenceScript};
18-
use blake2::{Blake2b512, Digest};
1917
use caryatid_sdk::Context;
2018

2119
/// Handle `/addresses/{address}` Blockfrost-compatible endpoint
@@ -248,42 +246,13 @@ pub async fn handle_address_utxos_blockfrost(
248246

249247
let mut rest_response = Vec::with_capacity(entries.len());
250248
for (i, entry) in entries.into_iter().enumerate() {
251-
let tx_hash = hex::encode(hashes.tx_hashes[i]);
252-
let block_hash = hex::encode(hashes.block_hashes[i]);
253-
let tx_index = utxo_identifiers[i].tx_index();
254-
let output_index = utxo_identifiers[i].output_index();
255-
let (data_hash, inline_datum) = match &entry.datum {
256-
Some(Datum::Hash(h)) => (Some(hex::encode(h)), None),
257-
Some(Datum::Inline(bytes)) => (None, Some(hex::encode(bytes))),
258-
None => (None, None),
259-
};
260-
let reference_script_hash = match &entry.reference_script {
261-
Some(script) => {
262-
let bytes = match script {
263-
ReferenceScript::Native(b)
264-
| ReferenceScript::PlutusV1(b)
265-
| ReferenceScript::PlutusV2(b)
266-
| ReferenceScript::PlutusV3(b) => b,
267-
};
268-
let mut hasher = Blake2b512::new();
269-
hasher.update(bytes);
270-
let result = hasher.finalize();
271-
Some(hex::encode(&result[..32]))
272-
}
273-
None => None,
274-
};
275-
276-
rest_response.push(UTxOREST {
277-
address: address_str.clone(),
278-
tx_hash,
279-
tx_index,
280-
output_index,
281-
amount: entry.value.into(),
282-
block: block_hash,
283-
data_hash,
284-
inline_datum,
285-
reference_script_hash,
286-
})
249+
rest_response.push(UTxOREST::new(
250+
address_str.clone(),
251+
&utxo_identifiers[i],
252+
&entry,
253+
hashes.tx_hashes[i].as_ref(),
254+
hashes.block_hashes[i].as_ref(),
255+
))
287256
}
288257

289258
let json = serde_json::to_string_pretty(&rest_response)?;

modules/rest_blockfrost/src/types.rs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ use acropolis_common::{
55
queries::{accounts::AccountReward, blocks::BlockInfo, governance::DRepActionUpdate},
66
rest_helper::ToCheckedF64,
77
serialization::{Bech32WithHrp, DisplayFromBech32, PoolPrefix},
8-
AssetAddressEntry, AssetMetadataStandard, AssetMintRecord, KeyHash, PolicyAsset,
9-
PoolEpochState, PoolId, PoolUpdateAction, Relay, TxHash, ValueMap, Vote, VrfKeyHash,
8+
AssetAddressEntry, AssetMetadataStandard, AssetMintRecord, Datum, KeyHash, PolicyAsset,
9+
PoolEpochState, PoolId, PoolUpdateAction, ReferenceScript, Relay, TxHash, UTXOValue,
10+
UTxOIdentifier, ValueMap, Vote, VrfKeyHash,
1011
};
1112
use anyhow::Result;
13+
use blake2::{Blake2b512, Digest};
1214
use num_traits::ToPrimitive;
1315
use rust_decimal::Decimal;
1416
use serde::Serialize;
@@ -939,6 +941,48 @@ pub struct UTxOREST {
939941
pub reference_script_hash: Option<String>,
940942
}
941943

944+
impl UTxOREST {
945+
pub fn new(
946+
address: String,
947+
utxo_id: &UTxOIdentifier,
948+
entry: &UTXOValue,
949+
tx_hash: &[u8],
950+
block_hash: &[u8],
951+
) -> Self {
952+
let (data_hash, inline_datum) = match &entry.datum {
953+
Some(Datum::Hash(h)) => (Some(hex::encode(h)), None),
954+
Some(Datum::Inline(bytes)) => (None, Some(hex::encode(bytes))),
955+
None => (None, None),
956+
};
957+
958+
let reference_script_hash = entry.reference_script.as_ref().map(|script| {
959+
let bytes = match script {
960+
ReferenceScript::Native(b)
961+
| ReferenceScript::PlutusV1(b)
962+
| ReferenceScript::PlutusV2(b)
963+
| ReferenceScript::PlutusV3(b) => b,
964+
};
965+
966+
let mut hasher = Blake2b512::new();
967+
hasher.update(bytes);
968+
let result = hasher.finalize();
969+
hex::encode(&result[..32])
970+
});
971+
972+
Self {
973+
address,
974+
tx_hash: hex::encode(tx_hash),
975+
tx_index: utxo_id.tx_index(),
976+
output_index: utxo_id.output_index(),
977+
amount: entry.value.clone().into(),
978+
block: hex::encode(block_hash),
979+
data_hash,
980+
inline_datum,
981+
reference_script_hash,
982+
}
983+
}
984+
}
985+
942986
#[derive(serde::Serialize)]
943987
pub struct AccountTotalsREST {
944988
pub stake_address: String,

0 commit comments

Comments
 (0)