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
62/// A struct representing an entry point.
63#[derive(Clone)]
64pub struct EntryPoint {
65    /// The name of the entry point.
66    pub name: String,
67    /// The collection of arguments to the entry point.
68    pub args: Vec<Argument>,
69    /// A flag indicating whether the entry point is payable.
70    pub is_payable: bool
71}
72
73impl EntryPoint {
74    /// Creates a new instance of `EntryPoint`.
75    pub fn new(name: String, args: Vec<Argument>) -> Self {
76        Self {
77            name,
78            args,
79            is_payable: false
80        }
81    }
82
83    /// Creates a new instance of payable `EntryPoint`.
84    pub fn new_payable(name: String, args: Vec<Argument>) -> Self {
85        Self {
86            name,
87            args,
88            is_payable: true
89        }
90    }
91}
92
93/// A struct representing an argument to entry point.
94#[derive(Clone)]
95pub struct Argument {
96    /// The name of the argument.
97    pub name: String,
98    /// The type of the argument.
99    pub ty: CLType
100}
101
102impl Argument {
103    /// Creates a new instance of `Argument`.
104    pub fn new<T: EntrypointArgument>(name: String) -> Self {
105        Self {
106            name,
107            ty: T::cl_type()
108        }
109    }
110}