algokit_transact_ffi/transactions/app_call.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
use crate::*;
/// Represents an app call transaction that interacts with Algorand Smart Contracts.
///
/// App call transactions are used to create, update, delete, opt-in to,
/// close out of, or clear state from Algorand applications (smart contracts).
#[ffi_record]
pub struct AppCallTransactionFields {
/// ID of the app being called.
///
/// Set this to 0 to indicate an app creation call.
app_id: u64,
/// Defines what additional actions occur with the transaction.
on_complete: OnApplicationComplete,
/// Logic executed for every app call transaction, except when
/// on-completion is set to "clear".
///
/// Approval programs may reject the transaction.
/// Only required for app creation and update transactions.
approval_program: Option<ByteBuf>,
/// Logic executed for app call transactions with on-completion set to "clear".
///
/// Clear state programs cannot reject the transaction.
/// Only required for app creation and update transactions.
clear_state_program: Option<ByteBuf>,
/// Holds the maximum number of global state values.
///
/// Only required for app creation transactions.
/// This cannot be changed after creation.
global_state_schema: Option<StateSchema>,
/// Holds the maximum number of local state values.
///
/// Only required for app creation transactions.
/// This cannot be changed after creation.
local_state_schema: Option<StateSchema>,
/// Number of additional pages allocated to the app's approval
/// and clear state programs.
///
/// Each extra program page is 2048 bytes. The sum of approval program
/// and clear state program may not exceed 2048*(1+extra_program_pages) bytes.
/// Currently, the maximum value is 3.
/// This cannot be changed after creation.
extra_program_pages: Option<u64>,
/// Transaction specific arguments available in the app's
/// approval program and clear state program.
args: Option<Vec<ByteBuf>>,
/// List of accounts in addition to the sender that may be accessed
/// from the app's approval program and clear state program.
account_references: Option<Vec<String>>,
/// List of apps in addition to the current app that may be called
/// from the app's approval program and clear state program.
app_references: Option<Vec<u64>>,
/// Lists the assets whose parameters may be accessed by this app's
/// approval program and clear state program.
///
/// The access is read-only.
asset_references: Option<Vec<u64>>,
/// The boxes that should be made available for the runtime of the program.
box_references: Option<Vec<BoxReference>>,
}
impl From<algokit_transact::AppCallTransactionFields> for AppCallTransactionFields {
fn from(tx: algokit_transact::AppCallTransactionFields) -> Self {
Self {
app_id: tx.app_id,
on_complete: tx.on_complete.into(),
approval_program: tx.approval_program.map(Into::into),
clear_state_program: tx.clear_state_program.map(Into::into),
global_state_schema: tx.global_state_schema.map(Into::into),
local_state_schema: tx.local_state_schema.map(Into::into),
extra_program_pages: tx.extra_program_pages,
args: tx
.args
.map(|args| args.into_iter().map(Into::into).collect()),
account_references: tx
.account_references
.map(|addrs| addrs.into_iter().map(|addr| addr.as_str()).collect()),
app_references: tx.app_references,
asset_references: tx.asset_references,
box_references: tx
.box_references
.map(|boxes| boxes.into_iter().map(Into::into).collect()),
}
}
}
impl TryFrom<Transaction> for algokit_transact::AppCallTransactionFields {
type Error = AlgoKitTransactError;
fn try_from(tx: Transaction) -> Result<Self, Self::Error> {
if tx.transaction_type != TransactionType::AppCall || tx.app_call.is_none() {
return Err(Self::Error::DecodingError {
message: "AppCall call data missing".to_string(),
});
}
let data = tx.clone().app_call.unwrap();
let header: algokit_transact::TransactionHeader = tx.try_into()?;
let transaction_fields = Self {
header,
app_id: data.app_id,
on_complete: data.on_complete.into(),
approval_program: data.approval_program.map(ByteBuf::into_vec),
clear_state_program: data.clear_state_program.map(ByteBuf::into_vec),
global_state_schema: data.global_state_schema.map(Into::into),
local_state_schema: data.local_state_schema.map(Into::into),
extra_program_pages: data.extra_program_pages,
args: data
.args
.map(|args| args.into_iter().map(ByteBuf::into_vec).collect()),
account_references: data
.account_references
.map(|addrs| {
addrs
.into_iter()
.map(|addr| addr.parse())
.collect::<Result<Vec<_>, _>>()
})
.transpose()?,
app_references: data.app_references,
asset_references: data.asset_references,
box_references: data
.box_references
.map(|boxes| boxes.into_iter().map(Into::into).collect()),
};
transaction_fields
.validate()
.map_err(|errors| AlgoKitTransactError::DecodingError {
message: format!("App call validation failed: {}", errors.join("\n")),
})?;
Ok(transaction_fields)
}
}
/// Box reference for app call transactions.
///
/// References a specific box that should be made available for the runtime
/// of the program.
#[ffi_record]
pub struct BoxReference {
/// App ID that owns the box.
/// A value of 0 indicates the current app.
app_id: u64,
/// Name of the box.
name: ByteBuf,
}
impl From<algokit_transact::BoxReference> for BoxReference {
fn from(value: algokit_transact::BoxReference) -> Self {
Self {
app_id: value.app_id,
name: value.name.into(),
}
}
}
impl From<BoxReference> for algokit_transact::BoxReference {
fn from(val: BoxReference) -> Self {
algokit_transact::BoxReference {
app_id: val.app_id,
name: val.name.into_vec(),
}
}
}
/// On-completion actions for app transactions.
///
/// These values define what additional actions occur with the transaction.
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
#[cfg_attr(feature = "ffi_wasm", derive(Tsify))]
#[cfg_attr(feature = "ffi_wasm", tsify(into_wasm_abi, from_wasm_abi))]
#[cfg_attr(feature = "ffi_uniffi", derive(uniffi::Enum))]
pub enum OnApplicationComplete {
/// NoOp indicates that an app transaction will simply call its
/// approval program without any additional action.
NoOp = 0,
/// OptIn indicates that an app transaction will allocate some
/// local state for the app in the sender's account.
OptIn = 1,
/// CloseOut indicates that an app transaction will deallocate
/// some local state for the app from the user's account.
CloseOut = 2,
/// ClearState is similar to CloseOut, but may never fail. This
/// allows users to reclaim their minimum balance from an app
/// they no longer wish to opt in to.
ClearState = 3,
/// UpdateApplication indicates that an app transaction will
/// update the approval program and clear state program for the app.
UpdateApplication = 4,
/// DeleteApplication indicates that an app transaction will
/// delete the app parameters for the app from the creator's
/// balance record.
DeleteApplication = 5,
}
impl From<algokit_transact::OnApplicationComplete> for OnApplicationComplete {
fn from(value: algokit_transact::OnApplicationComplete) -> Self {
match value {
algokit_transact::OnApplicationComplete::NoOp => OnApplicationComplete::NoOp,
algokit_transact::OnApplicationComplete::OptIn => OnApplicationComplete::OptIn,
algokit_transact::OnApplicationComplete::CloseOut => OnApplicationComplete::CloseOut,
algokit_transact::OnApplicationComplete::ClearState => {
OnApplicationComplete::ClearState
}
algokit_transact::OnApplicationComplete::UpdateApplication => {
OnApplicationComplete::UpdateApplication
}
algokit_transact::OnApplicationComplete::DeleteApplication => {
OnApplicationComplete::DeleteApplication
}
}
}
}
impl From<OnApplicationComplete> for algokit_transact::OnApplicationComplete {
fn from(val: OnApplicationComplete) -> Self {
match val {
OnApplicationComplete::NoOp => algokit_transact::OnApplicationComplete::NoOp,
OnApplicationComplete::OptIn => algokit_transact::OnApplicationComplete::OptIn,
OnApplicationComplete::CloseOut => algokit_transact::OnApplicationComplete::CloseOut,
OnApplicationComplete::ClearState => {
algokit_transact::OnApplicationComplete::ClearState
}
OnApplicationComplete::UpdateApplication => {
algokit_transact::OnApplicationComplete::UpdateApplication
}
OnApplicationComplete::DeleteApplication => {
algokit_transact::OnApplicationComplete::DeleteApplication
}
}
}
}
/// Schema for app state storage.
///
/// Defines the maximum number of values that may be stored in app
/// key/value storage for both global and local state.
#[ffi_record]
pub struct StateSchema {
/// Maximum number of integer values that may be stored.
num_uints: u64,
/// Maximum number of byte slice values that may be stored.
num_byte_slices: u64,
}
impl From<algokit_transact::StateSchema> for StateSchema {
fn from(value: algokit_transact::StateSchema) -> Self {
Self {
num_uints: value.num_uints,
num_byte_slices: value.num_byte_slices,
}
}
}
impl From<StateSchema> for algokit_transact::StateSchema {
fn from(val: StateSchema) -> Self {
algokit_transact::StateSchema {
num_uints: val.num_uints,
num_byte_slices: val.num_byte_slices,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use algokit_transact::test_utils::TestDataMother;
#[test]
fn test_encode_transaction_validation_integration() {
// invalid
let mut tx: Transaction = TestDataMother::app_call().transaction.try_into().unwrap();
tx.app_call.as_mut().unwrap().app_id = 0;
let result = encode_transaction(tx);
assert!(result.is_err());
// valid
let result = encode_transaction(TestDataMother::app_call().transaction.try_into().unwrap());
assert!(result.is_ok());
}
}