-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathclarity_contract.rs
More file actions
90 lines (76 loc) · 2.39 KB
/
clarity_contract.rs
File metadata and controls
90 lines (76 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use clarity::types::StacksEpochId;
use clarity::vm::ClarityVersion;
use indoc::indoc;
use crate::repl::{
ClarityCodeSource, ClarityContract, ContractDeployer, Epoch, DEFAULT_CLARITY_VERSION,
DEFAULT_EPOCH,
};
impl ClarityContract {
pub fn fixture() -> Self {
#[rustfmt::skip]
let snippet = indoc!("
(define-data-var x uint u0)
(define-read-only (get-x)
(var-get x))
(define-public (incr)
(let ((new-x (+ (var-get x) u1)))
(var-set x new-x)
(ok new-x)))
");
Self {
code_source: ClarityCodeSource::ContractInMemory(snippet.to_owned()),
name: "contract".into(),
deployer: ContractDeployer::DefaultDeployer,
clarity_version: DEFAULT_CLARITY_VERSION,
epoch: Epoch::Specific(DEFAULT_EPOCH),
is_requirement: false,
}
}
}
pub struct ClarityContractBuilder {
contract: ClarityContract,
}
impl Default for ClarityContractBuilder {
fn default() -> Self {
Self::new()
}
}
impl ClarityContractBuilder {
pub fn new() -> Self {
Self {
contract: ClarityContract::fixture(),
}
}
pub fn code_source(mut self, code_source: String) -> Self {
self.contract.code_source = ClarityCodeSource::ContractInMemory(code_source);
self
}
pub fn name(mut self, name: &str) -> Self {
name.clone_into(&mut self.contract.name);
self
}
pub fn deployer(mut self, address: &str) -> Self {
self.contract.deployer = ContractDeployer::Address(address.to_owned());
self
}
pub fn clarity_version(mut self, clarity_version: ClarityVersion) -> Self {
self.contract.clarity_version = clarity_version;
self
}
pub fn epoch(mut self, epoch: StacksEpochId) -> Self {
self.contract.epoch = Epoch::Specific(epoch);
self
}
pub fn build(self) -> ClarityContract {
let default_version = ClarityVersion::default_for_epoch(self.contract.epoch.resolve());
let clarity_version = self.contract.clarity_version;
if clarity_version > default_version {
panic!(
"invalid clarity version {} for epoch {}",
clarity_version,
self.contract.epoch.resolve()
);
}
self.contract
}
}