ink_env/call/
common.rs

1// Copyright (C) Use Ink (UK) Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Utilities, types and abstractions common to call and instantiation routines.
16
17use alloy_sol_types::SolValue;
18use core::marker::PhantomData;
19use ink_primitives::{
20    reflect::{
21        AbiDecodeWith,
22        ScaleEncoding,
23        SolEncoding,
24    },
25    MessageResult,
26};
27use scale::{
28    Decode,
29    DecodeAll,
30};
31
32/// Represents a return type.
33///
34/// Used as a marker type to define the return type of an ink! message in call builders.
35#[derive(Debug)]
36pub struct ReturnType<T>(PhantomData<fn() -> T>);
37
38impl<T> Clone for ReturnType<T> {
39    #[inline]
40    fn clone(&self) -> Self {
41        *self
42    }
43}
44
45impl<T> Copy for ReturnType<T> {}
46
47impl<T> Default for ReturnType<T> {
48    #[inline]
49    fn default() -> Self {
50        Self(Default::default())
51    }
52}
53
54/// A parameter that has been set to some value.
55#[derive(Debug, Copy, Clone)]
56pub struct Set<T>(pub T);
57
58impl<T> Set<T> {
59    /// Returns the set value.
60    #[inline]
61    pub fn value(self) -> T {
62        self.0
63    }
64}
65
66/// A parameter that has not been set, yet.
67#[derive(Debug)]
68pub struct Unset<T>(PhantomData<fn() -> T>);
69
70impl<T> Clone for Unset<T> {
71    #[inline]
72    fn clone(&self) -> Self {
73        *self
74    }
75}
76
77impl<T> Copy for Unset<T> {}
78
79impl<T> Default for Unset<T> {
80    #[inline]
81    fn default() -> Self {
82        Self(Default::default())
83    }
84}
85
86/// Implemented by [`Set`] and [`Unset`] in order to unwrap their value.
87///
88/// This is useful in case the use-site does not know if it is working with
89/// a set or an unset value generically unwrap it using a closure for fallback.
90pub trait Unwrap {
91    /// The output type of the `unwrap_or_else` operation.
92    type Output;
93
94    /// Returns the set value or evaluates the given closure.
95    fn unwrap_or_else<F>(self, f: F) -> Self::Output
96    where
97        F: FnOnce() -> Self::Output;
98}
99
100impl<T> Unwrap for Unset<T> {
101    type Output = T;
102
103    #[inline]
104    fn unwrap_or_else<F>(self, f: F) -> Self::Output
105    where
106        F: FnOnce() -> Self::Output,
107    {
108        f()
109    }
110}
111
112impl<T> Unwrap for Set<T> {
113    type Output = T;
114
115    #[inline]
116    fn unwrap_or_else<F>(self, _: F) -> Self::Output
117    where
118        F: FnOnce() -> Self::Output,
119    {
120        self.value()
121    }
122}
123
124/// A trait for decoding the output of a message based on different ABIs.
125/// This is necessary as contracts with different ABIs have different return types.
126/// For example, Solidity contracts return the output directly without `MessageResult`.
127pub trait DecodeMessageResult<Abi>: Sized {
128    /// Decodes the output of a message call, requiring the output
129    /// to be wrapped with `MessageResult` (if not included in the output).
130    fn decode_output(buffer: &[u8]) -> crate::Result<MessageResult<Self>>;
131}
132
133impl<R> DecodeMessageResult<ScaleEncoding> for R
134where
135    R: Decode,
136    MessageResult<R>: Decode,
137{
138    fn decode_output(mut buffer: &[u8]) -> crate::Result<MessageResult<Self>> {
139        let decoded = MessageResult::<R>::decode_all(&mut buffer)?;
140        Ok(decoded)
141    }
142}
143
144impl<R> DecodeMessageResult<SolEncoding> for R
145where
146    R: SolValue + From<<<R as SolValue>::SolType as alloy_sol_types::SolType>::RustType>,
147{
148    fn decode_output(buffer: &[u8]) -> crate::Result<MessageResult<Self>> {
149        // Solidity ABI Encoded contracts return the data without
150        // `MessageResult`.
151        let decoded = R::decode_with(buffer)?;
152        Ok(Ok(decoded))
153    }
154}