|
| 1 | +use crate::transactions::common::UtilsError; |
| 2 | + |
| 3 | +use super::common::CommonParams; |
| 4 | +use algokit_utils::transactions::{ |
| 5 | + AssetClawbackParams as RustAssetClawbackParams, AssetOptInParams as RustAssetOptInParams, |
| 6 | + AssetOptOutParams as RustAssetOptOutParams, AssetTransferParams as RustAssetTransferParams, |
| 7 | +}; |
| 8 | + |
| 9 | +#[derive(uniffi::Record)] |
| 10 | +pub struct AssetTransferParams { |
| 11 | + /// Common transaction parameters. |
| 12 | + pub common_params: CommonParams, |
| 13 | + |
| 14 | + /// The ID of the asset being transferred. |
| 15 | + pub asset_id: u64, |
| 16 | + |
| 17 | + /// The amount of the asset to transfer. |
| 18 | + pub amount: u64, |
| 19 | + |
| 20 | + /// The address that will receive the asset. |
| 21 | + pub receiver: String, |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(uniffi::Record)] |
| 25 | +pub struct AssetOptInParams { |
| 26 | + /// Common transaction parameters. |
| 27 | + pub common_params: CommonParams, |
| 28 | + |
| 29 | + /// The ID of the asset to opt into. |
| 30 | + pub asset_id: u64, |
| 31 | +} |
| 32 | + |
| 33 | +#[derive(uniffi::Record)] |
| 34 | +pub struct AssetOptOutParams { |
| 35 | + /// Common transaction parameters. |
| 36 | + pub common_params: CommonParams, |
| 37 | + |
| 38 | + /// The ID of the asset to opt out of. |
| 39 | + pub asset_id: u64, |
| 40 | + |
| 41 | + /// The address to close the remainder to. If None, defaults to the asset creator. |
| 42 | + pub close_remainder_to: Option<String>, |
| 43 | +} |
| 44 | + |
| 45 | +#[derive(uniffi::Record)] |
| 46 | +pub struct AssetClawbackParams { |
| 47 | + /// Common transaction parameters. |
| 48 | + pub common_params: CommonParams, |
| 49 | + |
| 50 | + /// The ID of the asset being clawed back. |
| 51 | + pub asset_id: u64, |
| 52 | + |
| 53 | + /// The amount of the asset to clawback. |
| 54 | + pub amount: u64, |
| 55 | + |
| 56 | + /// The address that will receive the clawed back asset. |
| 57 | + pub receiver: String, |
| 58 | + |
| 59 | + /// The address from which assets are taken. |
| 60 | + pub clawback_target: String, |
| 61 | +} |
| 62 | + |
| 63 | +impl TryFrom<AssetTransferParams> for RustAssetTransferParams { |
| 64 | + type Error = UtilsError; |
| 65 | + |
| 66 | + fn try_from(params: AssetTransferParams) -> Result<Self, Self::Error> { |
| 67 | + let common_params = params.common_params.try_into()?; |
| 68 | + Ok(RustAssetTransferParams { |
| 69 | + common_params, |
| 70 | + asset_id: params.asset_id, |
| 71 | + amount: params.amount, |
| 72 | + receiver: params |
| 73 | + .receiver |
| 74 | + .parse() |
| 75 | + .map_err(|e| UtilsError::UtilsError { |
| 76 | + message: format!("Failed to parse receiver address: {}", e), |
| 77 | + })?, |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl TryFrom<AssetOptInParams> for RustAssetOptInParams { |
| 83 | + type Error = UtilsError; |
| 84 | + |
| 85 | + fn try_from(params: AssetOptInParams) -> Result<Self, Self::Error> { |
| 86 | + let common_params = params.common_params.try_into()?; |
| 87 | + Ok(RustAssetOptInParams { |
| 88 | + common_params, |
| 89 | + asset_id: params.asset_id, |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl TryFrom<AssetOptOutParams> for RustAssetOptOutParams { |
| 95 | + type Error = UtilsError; |
| 96 | + |
| 97 | + fn try_from(params: AssetOptOutParams) -> Result<Self, Self::Error> { |
| 98 | + let common_params = params.common_params.try_into()?; |
| 99 | + let close_remainder_to = match params.close_remainder_to { |
| 100 | + Some(addr) => Some(addr.parse().map_err(|e| UtilsError::UtilsError { |
| 101 | + message: format!("Failed to parse close_remainder_to address: {}", e), |
| 102 | + })?), |
| 103 | + None => None, |
| 104 | + }; |
| 105 | + Ok(RustAssetOptOutParams { |
| 106 | + common_params, |
| 107 | + asset_id: params.asset_id, |
| 108 | + close_remainder_to, |
| 109 | + }) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +impl TryFrom<AssetClawbackParams> for RustAssetClawbackParams { |
| 114 | + type Error = UtilsError; |
| 115 | + |
| 116 | + fn try_from(params: AssetClawbackParams) -> Result<Self, Self::Error> { |
| 117 | + let common_params = params.common_params.try_into()?; |
| 118 | + Ok(RustAssetClawbackParams { |
| 119 | + common_params, |
| 120 | + asset_id: params.asset_id, |
| 121 | + amount: params.amount, |
| 122 | + receiver: params |
| 123 | + .receiver |
| 124 | + .parse() |
| 125 | + .map_err(|e| UtilsError::UtilsError { |
| 126 | + message: format!("Failed to parse receiver address: {}", e), |
| 127 | + })?, |
| 128 | + clawback_target: params.clawback_target.parse().map_err(|e| { |
| 129 | + UtilsError::UtilsError { |
| 130 | + message: format!("Failed to parse clawback_target address: {}", e), |
| 131 | + } |
| 132 | + })?, |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
0 commit comments