oasis_contract_sdk/contract.rs
1//! The contract trait.
2use crate::{context::Context, error, types};
3
4/// Trait that needs to be implemented by contract implementations.
5pub trait Contract {
6 /// Type of all requests.
7 type Request: cbor::Decode;
8 /// Type of all responses.
9 type Response: cbor::Encode;
10 /// Type of all errors.
11 type Error: error::Error;
12
13 /// Instantiate the contract.
14 fn instantiate<C: Context>(_ctx: &mut C, _request: Self::Request) -> Result<(), Self::Error> {
15 // Default implementation doesn't do anything.
16 Ok(())
17 }
18
19 /// Call the contract.
20 fn call<C: Context>(ctx: &mut C, request: Self::Request)
21 -> Result<Self::Response, Self::Error>;
22
23 /// Query the contract.
24 fn query<C: Context>(
25 _ctx: &mut C,
26 _request: Self::Request,
27 ) -> Result<Self::Response, Self::Error>;
28
29 /// Handle replies from sent messages.
30 fn handle_reply<C: Context>(
31 _ctx: &mut C,
32 _reply: types::message::Reply,
33 ) -> Result<Option<Self::Response>, Self::Error> {
34 // Default implementation does not perform any processing.
35 Ok(None)
36 }
37
38 /// Perform any pre-upgrade tasks. This method is called on the old contract code.
39 ///
40 /// If this method reports an error the upgrade will be aborted.
41 fn pre_upgrade<C: Context>(_ctx: &mut C, _request: Self::Request) -> Result<(), Self::Error> {
42 // Default implementation accepts all upgrades.
43 Ok(())
44 }
45
46 /// Perform any post-upgrade tasks. This method is called on the new contract code.
47 ///
48 /// If this method reports an error the upgrade will be aborted.
49 fn post_upgrade<C: Context>(_ctx: &mut C, _request: Self::Request) -> Result<(), Self::Error> {
50 // Default implementation accepts all upgrades.
51 Ok(())
52 }
53}