anchor_cli/
solidity_template.rs1use crate::create_files;
2use anyhow::Result;
3use heck::{ToSnakeCase, ToUpperCamelCase};
4use std::path::Path;
5
6pub fn create_program(name: &str) -> Result<()> {
8 let files = vec![(
9 Path::new("solidity").join(name).with_extension("sol"),
10 solidity(name),
11 )];
12 create_files(&files)
13}
14
15fn solidity(name: &str) -> String {
16 format!(
17 r#"
18contract {} {{
19 bool private value = true;
20
21 @payer(payer)
22 constructor() {{
23 print("Hello, World!");
24 }}
25
26 /// A message that can be called on instantiated contracts.
27 /// This one flips the value of the stored `bool` from `true`
28 /// to `false` and vice versa.
29 function flip() public {{
30 value = !value;
31 }}
32
33 /// Simply returns the current value of our `bool`.
34 function get() public view returns (bool) {{
35 return value;
36 }}
37}}
38"#,
39 name.to_snake_case(),
40 )
41}
42
43pub fn mocha(name: &str) -> String {
44 format!(
45 r#"const anchor = require("@coral-xyz/anchor");
46
47describe("{}", () => {{
48 // Configure the client to use the local cluster.
49 anchor.setProvider(anchor.AnchorProvider.env());
50
51 it("Is initialized!", async () => {{
52 // Add your test here.
53 const program = anchor.workspace.{};
54 const dataAccount = anchor.web3.Keypair.generate();
55
56 const tx = await program.methods
57 .new()
58 .accounts({{ dataAccount: dataAccount.publicKey }})
59 .signers([dataAccount])
60 .rpc();
61
62 console.log("Your transaction signature", tx);
63
64 const val1 = await program.methods
65 .get()
66 .accounts({{ dataAccount: dataAccount.publicKey }})
67 .view();
68
69 console.log("state", val1);
70
71 await program.methods
72 .flip()
73 .accounts({{ dataAccount: dataAccount.publicKey }})
74 .rpc();
75
76 const val2 = await program.methods
77 .get()
78 .accounts({{ dataAccount: dataAccount.publicKey }})
79 .view();
80
81 console.log("state", val2);
82 }});
83}});
84"#,
85 name,
86 name.to_upper_camel_case(),
87 )
88}
89
90pub fn jest(name: &str) -> String {
91 format!(
92 r#"const anchor = require("@coral-xyz/anchor");
93
94describe("{}", () => {{
95 // Configure the client to use the local cluster.
96 anchor.setProvider(anchor.AnchorProvider.env());
97
98 it("Is initialized!", async () => {{
99 // Add your test here.
100 const program = anchor.workspace.{};
101 const tx = await program.methods.initialize().rpc();
102 console.log("Your transaction signature", tx);
103 }});
104}});
105"#,
106 name,
107 name.to_upper_camel_case(),
108 )
109}
110
111pub fn ts_mocha(name: &str) -> String {
112 format!(
113 r#"import * as anchor from "@coral-xyz/anchor";
114import {{ Program }} from "@coral-xyz/anchor";
115import {{ {} }} from "../target/types/{}";
116
117describe("{}", () => {{
118 // Configure the client to use the local cluster.
119 const provider = anchor.AnchorProvider.env();
120 anchor.setProvider(provider);
121
122 const dataAccount = anchor.web3.Keypair.generate();
123
124 const program = anchor.workspace.{} as Program<{}>;
125
126 it("Is initialized!", async () => {{
127 // Add your test here.
128 const tx = await program.methods
129 .new()
130 .accounts({{ dataAccount: dataAccount.publicKey }})
131 .signers([dataAccount])
132 .rpc();
133 console.log("Your transaction signature", tx);
134
135 const val1 = await program.methods
136 .get()
137 .accounts({{ dataAccount: dataAccount.publicKey }})
138 .view();
139
140 console.log("state", val1);
141
142 await program.methods
143 .flip()
144 .accounts({{ dataAccount: dataAccount.publicKey }})
145 .rpc();
146
147 const val2 = await program.methods
148 .get()
149 .accounts({{ dataAccount: dataAccount.publicKey }})
150 .view();
151
152 console.log("state", val2); }});
153}});
154"#,
155 name.to_upper_camel_case(),
156 name.to_snake_case(),
157 name,
158 name.to_upper_camel_case(),
159 name.to_upper_camel_case(),
160 )
161}