odra_core/
entry_point_callback.rs

1//! A module that contains structs representing entry points and entry point callers.
2
3use casper_types::CLType;
4
5use crate::args::EntrypointArgument;
6use crate::call_def::CallDef;
7use crate::casper_types::bytesrepr::Bytes;
8use crate::{host::HostEnv, prelude::*, ContractEnv};
9
10/// A struct representing an entry point caller.
11///
12/// The caller is used by the host environment to call entry points of a contract.
13///
14/// This struct is responsible for calling the entry points of a contract.
15/// It holds the host environment, a list of entry points, and a function pointer
16/// that takes a contract environment and a call definition as arguments and returns
17/// a result in the form of bytes.
18#[derive(Clone)]
19pub struct EntryPointsCaller {
20    f: fn(contract_env: ContractEnv, call_def: CallDef) -> OdraResult<Bytes>,
21    host_env: HostEnv,
22    entry_points: Vec<EntryPoint>
23}
24
25impl EntryPointsCaller {
26    /// Creates a new instance of `EntryPointsCaller`.
27    ///
28    /// # Arguments
29    ///
30    /// * `host_env` - The host environment.
31    /// * `entry_points` - A collection of available entry points.
32    /// * `f` - A function pointer that performs a call using a given contract environment and a call definition
33    ///   and returns a result in the form of bytes.
34    ///
35    /// # Returns
36    ///
37    /// A new instance of `EntryPointsCaller`.
38    pub fn new(
39        host_env: HostEnv,
40        entry_points: Vec<EntryPoint>,
41        f: fn(contract_env: ContractEnv, call_def: CallDef) -> OdraResult<Bytes>
42    ) -> Self {
43        EntryPointsCaller {
44            f,
45            host_env,
46            entry_points
47        }
48    }
49
50    /// Calls the entry point with the given call definition.
51    /// Returns the result of the entry point call in the form of bytes.
52    pub fn call(&self, call_def: CallDef) -> OdraResult<Bytes> {
53        (self.f)(self.host_env.contract_env(), call_def)
54    }
55
56    /// Returns a reference to the list of entry points.
57    pub fn entry_points(&self) -> &[EntryPoint] {
58        self.entry_points.as_ref()
59    }
60
61    /// Removes an entry point by its name.
62    pub fn remove_entry_point(&mut self, name: &str) {
63        self.entry_points.retain(|ep| ep.name != name);
64    }
65}
66
67/// A struct representing an entry point.
68#[derive(Clone)]
69pub struct EntryPoint {
70    /// The name of the entry point.
71    pub name: String,
72    /// The collection of arguments to the entry point.
73    pub args: Vec<Argument>,
74    /// A flag indicating whether the entry point is payable.
75    pub is_payable: bool
76}
77
78impl EntryPoint {
79    /// Creates a new instance of `EntryPoint`.
80    pub fn new(name: String, args: Vec<Argument>) -> Self {
81        Self {
82            name,
83            args,
84            is_payable: false
85        }
86    }
87
88    /// Creates a new instance of payable `EntryPoint`.
89    pub fn new_payable(name: String, args: Vec<Argument>) -> Self {
90        Self {
91            name,
92            args,
93            is_payable: true
94        }
95    }
96}
97
98/// A struct representing an argument to entry point.
99#[derive(Clone)]
100pub struct Argument {
101    /// The name of the argument.
102    pub name: String,
103    /// The type of the argument.
104    pub ty: CLType
105}
106
107impl Argument {
108    /// Creates a new instance of `Argument`.
109    pub fn new<T: EntrypointArgument>(name: String) -> Self {
110        Self {
111            name,
112            ty: T::cl_type()
113        }
114    }
115}