-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathcontract.rs
More file actions
141 lines (126 loc) · 3.91 KB
/
contract.rs
File metadata and controls
141 lines (126 loc) · 3.91 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use super::changes::{Changes, FileCreation, TOMLEdition};
use crate::types::ContractConfig;
use std::{collections::HashMap, path::PathBuf};
pub struct GetChangesForNewContract {
manifest_path: PathBuf,
contract_name: String,
source: Option<String>,
changes: Vec<Changes>,
}
impl GetChangesForNewContract {
pub fn new(manifest_path: PathBuf, contract_name: String, source: Option<String>) -> Self {
Self {
manifest_path,
contract_name,
source,
changes: vec![],
}
}
pub fn run(&mut self, include_test: bool, deps: Vec<String>) -> Vec<Changes> {
self.create_template_contract();
if include_test {
self.create_template_test();
}
self.index_contract_in_clarinet_toml(deps);
self.changes.clone()
}
fn create_template_contract(&mut self) {
let content = if let Some(ref source) = self.source {
source.to_string()
} else {
format!(
r#"
;; {}
;; <add a description here>
;; constants
;;
;; data maps and vars
;;
;; private functions
;;
;; public functions
;;
"#,
self.contract_name
)
};
let name = format!("{}.clar", self.contract_name);
let project_path = {
let mut p = self.manifest_path.clone();
p.pop();
p
};
let path = format!("{}/contracts/{}", project_path.to_string_lossy(), name);
let change = FileCreation {
comment: format!("{} contracts/{}", green!("Created file"), name),
name,
content,
path,
};
self.changes.push(Changes::AddFile(change));
}
fn create_template_test(&mut self) {
let content = format!(
r#"
import {{ Clarinet, Tx, Chain, Account, types }} from 'https://deno.land/x/clarinet@v0.28.0/index.ts';
import {{ assertEquals }} from 'https://deno.land/std@0.90.0/testing/asserts.ts';
Clarinet.test({{
name: "Ensure that <...>",
async fn(chain: Chain, accounts: Map<string, Account>) {{
let block = chain.mineBlock([
/*
* Add transactions with:
* Tx.contractCall(...)
*/
]);
assertEquals(block.receipts.length, 0);
assertEquals(block.height, 2);
block = chain.mineBlock([
/*
* Add transactions with:
* Tx.contractCall(...)
*/
]);
assertEquals(block.receipts.length, 0);
assertEquals(block.height, 3);
}},
}});
"#
);
let project_path = {
let mut p = self.manifest_path.clone();
p.pop();
p
};
let name = format!("{}_test.ts", self.contract_name);
let path = format!("{}/tests/{}", project_path.to_string_lossy(), name);
let change = FileCreation {
comment: format!("{} tests/{}", green!("Created file"), name),
name,
content,
path,
};
self.changes.push(Changes::AddFile(change));
}
fn index_contract_in_clarinet_toml(&mut self, deps: Vec<String>) {
let contract_file_name = format!("{}.clar", self.contract_name);
let manifest_path = self.manifest_path.clone();
let contract_config = ContractConfig {
path: format!("contracts/{}", contract_file_name),
deployer: None,
};
let mut contracts_to_add = HashMap::new();
contracts_to_add.insert(self.contract_name.clone(), contract_config);
let change = TOMLEdition {
comment: format!(
"{} with contract {}",
yellow!("Updated Clarinet.toml"),
self.contract_name
),
manifest_path,
contracts_to_add,
requirements_to_add: vec![],
};
self.changes.push(Changes::EditTOML(change));
}
}