Skip to main content

thruput_sdk/call/
contract_client.rs

1pub trait ContractClient {
2    /// The id of the interface this client calls, matching that interface's
3    /// `INTERFACE_ID`.
4    const INTERFACE_ID: crate::InterfaceId;
5
6    fn new(address: &crate::types::ContractAddress) -> Self;
7}
8
9/// Classify a borrowed exception during a generated call.
10///
11/// Generated clients implement this trait. Use
12/// `client.map_exceptions(classify).try_method(...)` to return a compact
13/// exception value instead of an owned [`RawException`](crate::exn::RawException).
14/// The adapter preserves the client's address, gas limit and invocation flags,
15/// and is consumed by one call. Only a parsed exception invokes the mapper;
16/// success, faults, rejected calls and encode/decode errors keep their usual
17/// [`RawCallError`](super::RawCallError) representation.
18///
19/// The mapper borrows abort bytes already copied from the host into guest
20/// scratch; its return value cannot borrow them. This avoids an additional
21/// owned exception buffer, not the host/guest copies.
22/// Prefer a compact classification or verbatim rethrow. Taking an owned copy
23/// is allowed, but in RV32 contracts with `alloc` that allocation is not
24/// reclaimed during the invocation. Decoding types with owned fields can
25/// also allocate.
26///
27/// The call scratch stays checked out during the mapper. Do other SDK work
28/// or throw a new typed error after the mapped call returns, when its scratch
29/// checkout has ended. Without `alloc`, this does not guarantee that
30/// the compiler reuses its stack slot; test the release RV32 artifact.
31///
32/// Import this trait (included in `use thruput_sdk::*`) for method syntax.
33/// If a contract has its own `map_exceptions` method, use
34/// `MapExceptions::map_exceptions(&client, classify)` instead.
35pub trait MapExceptions {
36    /// A generated client's single-call adapter with mapper `F`.
37    type Mapped<F>;
38
39    /// Map a borrowed exception during the next generated `try_*` call.
40    fn map_exceptions<X, F>(&self, on_exn: F) -> Self::Mapped<F>
41    where
42        F: for<'s> FnOnce(crate::exn::RawExceptionRef<'s>) -> X;
43}