Skip to main content

odra_casper_types/
args.rs

1use core::ops::{Deref, DerefMut};
2
3use alloc::{
4    string::{String, ToString},
5    vec::Vec
6};
7use casper_types::{
8    bytesrepr::{FromBytes, ToBytes},
9    CLType, CLTyped
10};
11
12/// Represents a collection of arguments passed to a smart contract entrypoint call.
13///
14/// Wraps casper's [RuntimeArgs](casper_types::RuntimeArgs).
15#[derive(Default, Debug, Clone)]
16pub struct CallArgs(casper_types::RuntimeArgs);
17
18impl CallArgs {
19    /// Creates a new no-args instance.
20    pub fn new() -> Self {
21        Self(casper_types::RuntimeArgs::default())
22    }
23
24    /// Inserts a new empty arg into the collection.
25    pub fn insert<K, V>(&mut self, key: K, value: V)
26    where
27        K: Into<String>,
28        V: CLTyped + ToBytes
29    {
30        self.0.insert(key, value).unwrap();
31    }
32
33    /// Retrieves a vector of argument names.
34    pub fn arg_names(&self) -> Vec<String> {
35        self.0
36            .named_args()
37            .map(|arg| arg.name().to_string())
38            .collect()
39    }
40
41    /// Return Casper's RuntimeArgs.
42    pub fn as_casper_runtime_args(&self) -> &casper_types::RuntimeArgs {
43        &self.0
44    }
45}
46
47impl ToBytes for CallArgs {
48    fn to_bytes(&self) -> Result<Vec<u8>, casper_types::bytesrepr::Error> {
49        self.0.to_bytes()
50    }
51
52    fn serialized_length(&self) -> usize {
53        self.0.serialized_length()
54    }
55}
56
57impl FromBytes for CallArgs {
58    fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), casper_types::bytesrepr::Error> {
59        casper_types::RuntimeArgs::from_bytes(bytes)
60            .map(|(args, leftovers)| (CallArgs(args), leftovers))
61    }
62}
63
64impl CLTyped for CallArgs {
65    fn cl_type() -> CLType {
66        CLType::Any
67    }
68}
69
70impl Deref for CallArgs {
71    type Target = casper_types::RuntimeArgs;
72
73    fn deref(&self) -> &Self::Target {
74        &self.0
75    }
76}
77
78impl DerefMut for CallArgs {
79    fn deref_mut(&mut self) -> &mut Self::Target {
80        &mut self.0
81    }
82}