sonic_callreq/
builder.rs

1// SONIC: Standard library for formally-verifiable distributed contracts
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Designed in 2019-2025 by Dr Maxim Orlovsky <orlovsky@ubideco.org>
6// Written in 2024-2025 by Dr Maxim Orlovsky <orlovsky@ubideco.org>
7//
8// Copyright (C) 2019-2024 LNP/BP Standards Association, Switzerland.
9// Copyright (C) 2024-2025 Laboratories for Ubiquitous Deterministic Computing (UBIDECO),
10//                         Institute for Distributed and Cognitive Systems (InDCS), Switzerland.
11// Copyright (C) 2019-2025 Dr Maxim Orlovsky.
12// All rights under the above copyrights are reserved.
13//
14// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
15// in compliance with the License. You may obtain a copy of the License at
16//
17//        http://www.apache.org/licenses/LICENSE-2.0
18//
19// Unless required by applicable law or agreed to in writing, software distributed under the License
20// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
21// or implied. See the License for the specific language governing permissions and limitations under
22// the License.
23
24use amplify::confinement;
25use chrono::{DateTime, Utc};
26use strict_types::{StrictVal, TypeName};
27use ultrasonic::Consensus;
28
29use crate::{CallRequest, CallState, Endpoint, Layer1, MethodName, StateName};
30
31impl<T, A> CallRequest<T, A> {
32    pub fn bitcoin_mainnet(scope: T, auth: A, data: Option<StrictVal>) -> Self {
33        Self::new(scope, Consensus::Bitcoin, false, auth, data)
34    }
35
36    pub fn bitcoin_testnet(scope: T, auth: A, data: Option<StrictVal>) -> Self {
37        Self::new(scope, Consensus::Bitcoin, true, auth, data)
38    }
39
40    pub fn liquid_mainnet(scope: T, auth: A, data: Option<StrictVal>) -> Self {
41        Self::new(scope, Consensus::Liquid, false, auth, data)
42    }
43
44    pub fn liquid_testnet(scope: T, auth: A, data: Option<StrictVal>) -> Self {
45        Self::new(scope, Consensus::Liquid, true, auth, data)
46    }
47
48    pub fn new(scope: T, consensus: Consensus, testnet: bool, auth: A, data: Option<StrictVal>) -> Self {
49        Self {
50            scope,
51            layer1: Layer1::new(consensus, testnet),
52            api: None,
53            call: None,
54            auth,
55            data,
56            lock: None,
57            expiry: None,
58            endpoints: Default::default(),
59            unknown_query: Default::default(),
60        }
61    }
62
63    pub fn use_api(mut self, api: impl Into<TypeName>) -> Self {
64        self.api = Some(api.into());
65        self
66    }
67
68    pub fn use_method(mut self, method: MethodName) -> Self {
69        if let Some(call) = &mut self.call {
70            call.method = method;
71        } else {
72            self.call = Some(CallState::new(method));
73        }
74        self
75    }
76
77    pub fn use_state(mut self, state: StateName) -> Self {
78        let mut call = self
79            .call
80            .expect("use_method must be called before use_state");
81        call.owned = Some(state);
82        self.call = Some(call);
83        self
84    }
85
86    pub fn use_expiry(mut self, expiry: DateTime<Utc>) -> Self {
87        self.expiry = Some(expiry);
88        self
89    }
90
91    pub fn add_endpoint(mut self, endpoint: Endpoint) -> Result<Self, confinement::Error> {
92        self.endpoints.push(endpoint)?;
93        Ok(self)
94    }
95}