whisky_provider/maestro/utils/
address_utils.rs1use maestro_rust_sdk::models::addresses::{Asset as MAsset, Utxo};
2use whisky_common::models::{Asset, UTxO, UtxoInput, UtxoOutput};
3use whisky_csl::csl::{Address, BaseAddress, JsError, RewardAddress};
4
5pub fn maestro_asset_to_asset(asset: MAsset) -> Asset {
6 Asset::new(asset.unit.clone(), asset.amount.to_string())
7}
8
9pub fn maestro_utxo_to_utxo(utxo: Utxo) -> UTxO {
10 UTxO {
11 input: UtxoInput {
12 output_index: utxo.index as u32,
13 tx_hash: utxo.tx_hash,
14 },
15 output: UtxoOutput {
16 address: utxo.address,
17 amount: utxo
18 .assets
19 .iter()
20 .map(|asset| maestro_asset_to_asset(asset.clone()))
21 .collect(),
22 data_hash: utxo.datum.as_ref().and_then(|datum| {
23 datum
24 .get("hash")
25 .and_then(|hash| hash.as_str().map(|s| s.to_string()))
26 }),
27 plutus_data: utxo.datum.as_ref().and_then(|datum| {
28 datum
29 .get("bytes")
30 .and_then(|hash| hash.as_str().map(|s| s.to_string()))
31 }),
32 script_ref: utxo
33 .reference_script
34 .as_ref()
35 .map(|script| script.bytes.clone()),
36 script_hash: utxo
37 .reference_script
38 .as_ref()
39 .map(|script| script.hash.clone()),
40 },
41 }
42}
43
44pub fn resolve_reward_address(bech32: &str) -> Result<String, JsError> {
45 let address = Address::from_bech32(bech32)?;
46
47 if let Some(base_address) = BaseAddress::from_address(&address) {
48 let stake_credential = BaseAddress::stake_cred(&base_address);
49
50 let reward_address = RewardAddress::new(address.network_id()?, &stake_credential)
51 .to_address()
52 .to_bech32(None);
53 Ok(reward_address?)
54 } else {
55 Err(JsError::from_str(
56 "An error occurred during resolveRewardAddress",
57 ))
58 }
59}