odra_core/call_def.rs
1use crate::prelude::*;
2use casper_types::bytesrepr::FromBytes;
3use casper_types::{CLTyped, RuntimeArgs, U512};
4use serde::{Deserialize, Serialize};
5
6/// Represents a call definition, which includes the method name, runtime arguments, attached value, and mutability flag.
7#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
8pub struct CallDef {
9 entry_point: String,
10 args: RuntimeArgs,
11 amount: U512,
12 is_mut: bool
13}
14
15impl CallDef {
16 /// Creates a new `CallDef` instance with the given method name and runtime arguments.
17 ///
18 /// # Arguments
19 ///
20 /// * `method` - The method name.
21 /// * `is_mut` - Indicates if the call is mutable.
22 /// * `args` - The runtime arguments.
23 ///
24 /// # Example
25 ///
26 /// ```
27 /// # use odra_core::CallDef;
28 /// # use casper_types::RuntimeArgs;
29 /// let method = "my_method";
30 /// let args = RuntimeArgs::new();
31 /// let call_def = CallDef::new(method, false, args);
32 /// ```
33 pub fn new<T: ToString>(method: T, is_mut: bool, args: RuntimeArgs) -> Self {
34 CallDef {
35 entry_point: method.to_string(),
36 args,
37 amount: U512::zero(),
38 is_mut
39 }
40 }
41
42 /// Sets the attached value for the `CallDef` instance.
43 ///
44 /// # Arguments
45 ///
46 /// * `amount` - The attached value.
47 ///
48 /// # Returns
49 ///
50 /// The modified `CallDef` instance.
51 ///
52 /// # Example
53 ///
54 /// ```
55 /// # use odra_core::CallDef;
56 /// # use casper_types::RuntimeArgs;
57 /// # use casper_types::U512;
58 /// let call_def = CallDef::new("my_method", false, RuntimeArgs::new())
59 /// .with_amount(U512::from(100));
60 /// ```
61 pub fn with_amount(mut self, amount: U512) -> Self {
62 self.amount = amount;
63 self
64 }
65
66 /// Returns a reference to the entry point name of the `CallDef` instance.
67 pub fn entry_point(&self) -> &str {
68 &self.entry_point
69 }
70
71 /// Returns the attached value of the `CallDef` instance.
72 pub fn amount(&self) -> U512 {
73 self.amount
74 }
75
76 /// Returns a reference to the runtime arguments of the `CallDef` instance.
77 pub fn args(&self) -> &RuntimeArgs {
78 &self.args
79 }
80
81 /// Retrieves a value from the runtime arguments of the `CallDef` instance.
82 ///
83 /// # Arguments
84 ///
85 /// * `name` - The name of the value to retrieve.
86 ///
87 /// # Returns
88 ///
89 /// An `Option` containing the retrieved value, or `None` if the value does not exist or cannot be converted to the specified type.
90 ///
91 /// # Example
92 ///
93 /// ```
94 /// # use odra_core::CallDef;
95 /// # use casper_types::{CLTyped, bytesrepr::FromBytes, RuntimeArgs};
96 /// let call_def = CallDef::new("my_method", false, RuntimeArgs::new());
97 /// let value: Option<u32> = call_def.get("my_value");
98 /// ```
99 pub fn get<T: CLTyped + FromBytes>(&self, name: &str) -> Option<T> {
100 self.args.get(name).and_then(|v| v.clone().into_t().ok())
101 }
102
103 /// Returns whether the call mutates the state.
104 pub fn is_mut(&self) -> bool {
105 self.is_mut
106 }
107}
108
109#[cfg(test)]
110mod test {
111 use crate::CallDef;
112 use casper_types::{runtime_args, RuntimeArgs};
113
114 #[test]
115 fn test_get_arg() {
116 let args = runtime_args! {
117 "my_value" => 42u32
118 };
119 let call_def = CallDef::new("my_method", false, args);
120 let value: Option<u32> = call_def.get("my_value");
121 assert_eq!(value, Some(42u32));
122
123 let value: Option<u32> = call_def.get("your_value");
124 assert_eq!(value, None);
125 }
126
127 #[test]
128 fn test_is_mut() {
129 let call_def = CallDef::new("my_method", false, RuntimeArgs::new());
130 assert!(!call_def.is_mut());
131
132 let call_def = CallDef::new("my_method", true, RuntimeArgs::new());
133 assert!(call_def.is_mut());
134 }
135}