Skip to content

Commit 0ec7716

Browse files
committed
feat: add asset transfer transactions to utils FFI
Add AssetTransferParams, AssetOptInParams, AssetOptOutParams, and AssetClawbackParams with corresponding Composer methods. Includes Python bindings and integration test TODOs.
1 parent 751cce1 commit 0ec7716

File tree

7 files changed

+577
-2
lines changed

7 files changed

+577
-2
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
}

crates/algokit_utils_ffi/src/transactions/composer.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,54 @@ impl Composer {
109109
})
110110
}
111111

112+
pub fn add_asset_transfer(
113+
&self,
114+
params: super::asset_transfer::AssetTransferParams,
115+
) -> Result<(), UtilsError> {
116+
let mut composer = self.inner_composer.blocking_lock();
117+
composer
118+
.add_asset_transfer(params.try_into()?)
119+
.map_err(|e| UtilsError::UtilsError {
120+
message: e.to_string(),
121+
})
122+
}
123+
124+
pub fn add_asset_opt_in(
125+
&self,
126+
params: super::asset_transfer::AssetOptInParams,
127+
) -> Result<(), UtilsError> {
128+
let mut composer = self.inner_composer.blocking_lock();
129+
composer
130+
.add_asset_opt_in(params.try_into()?)
131+
.map_err(|e| UtilsError::UtilsError {
132+
message: e.to_string(),
133+
})
134+
}
135+
136+
pub fn add_asset_opt_out(
137+
&self,
138+
params: super::asset_transfer::AssetOptOutParams,
139+
) -> Result<(), UtilsError> {
140+
let mut composer = self.inner_composer.blocking_lock();
141+
composer
142+
.add_asset_opt_out(params.try_into()?)
143+
.map_err(|e| UtilsError::UtilsError {
144+
message: e.to_string(),
145+
})
146+
}
147+
148+
pub fn add_asset_clawback(
149+
&self,
150+
params: super::asset_transfer::AssetClawbackParams,
151+
) -> Result<(), UtilsError> {
152+
let mut composer = self.inner_composer.blocking_lock();
153+
composer
154+
.add_asset_clawback(params.try_into()?)
155+
.map_err(|e| UtilsError::UtilsError {
156+
message: e.to_string(),
157+
})
158+
}
159+
112160
pub async fn send(&self) -> Result<Vec<String>, UtilsError> {
113161
let mut composer = self.inner_composer.blocking_lock();
114162
let result = composer

crates/algokit_utils_ffi/src/transactions/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod asset_freeze;
2+
pub mod asset_transfer;
23
pub mod common;
34
pub mod composer;
45
pub mod key_registration;

0 commit comments

Comments
 (0)