solidity_template.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. use crate::VERSION;
  2. use crate::{config::ProgramWorkspace, create_files};
  3. use anchor_syn::idl::types::Idl;
  4. use anyhow::Result;
  5. use heck::{ToLowerCamelCase, ToSnakeCase, ToUpperCamelCase};
  6. use solana_sdk::pubkey::Pubkey;
  7. use std::fmt::Write;
  8. use std::path::Path;
  9. /// Create a solidity program.
  10. pub fn create_program(name: &str) -> Result<()> {
  11. let files = vec![(
  12. Path::new("solidity").join(name).with_extension("sol"),
  13. solidity(name),
  14. )];
  15. create_files(&files)
  16. }
  17. pub fn default_program_id() -> Pubkey {
  18. "F1ipperKF9EfD821ZbbYjS319LXYiBmjhzkkf5a26rC"
  19. .parse()
  20. .unwrap()
  21. }
  22. pub fn idl_ts(idl: &Idl) -> Result<String> {
  23. let mut idl = idl.clone();
  24. for acc in idl.accounts.iter_mut() {
  25. acc.name = acc.name.to_lower_camel_case();
  26. }
  27. let idl_json = serde_json::to_string_pretty(&idl)?;
  28. Ok(format!(
  29. r#"export type {} = {};
  30. export const IDL: {} = {};
  31. "#,
  32. idl.name.to_upper_camel_case(),
  33. idl_json,
  34. idl.name.to_upper_camel_case(),
  35. idl_json
  36. ))
  37. }
  38. pub fn deploy_js_script_host(cluster_url: &str, script_path: &str) -> String {
  39. format!(
  40. r#"
  41. const anchor = require('@coral-xyz/anchor');
  42. // Deploy script defined by the user.
  43. const userScript = require("{script_path}");
  44. async function main() {{
  45. const url = "{cluster_url}";
  46. const preflightCommitment = 'recent';
  47. const connection = new anchor.web3.Connection(url, preflightCommitment);
  48. const wallet = anchor.Wallet.local();
  49. const provider = new anchor.AnchorProvider(connection, wallet, {{
  50. preflightCommitment,
  51. commitment: 'recent',
  52. }});
  53. // Run the user's deploy script.
  54. userScript(provider);
  55. }}
  56. main();
  57. "#
  58. )
  59. }
  60. pub fn deploy_ts_script_host(cluster_url: &str, script_path: &str) -> String {
  61. format!(
  62. r#"import * as anchor from '@coral-xyz/anchor';
  63. // Deploy script defined by the user.
  64. const userScript = require("{script_path}");
  65. async function main() {{
  66. const url = "{cluster_url}";
  67. const preflightCommitment = 'recent';
  68. const connection = new anchor.web3.Connection(url, preflightCommitment);
  69. const wallet = anchor.Wallet.local();
  70. const provider = new anchor.AnchorProvider(connection, wallet, {{
  71. preflightCommitment,
  72. commitment: 'recent',
  73. }});
  74. // Run the user's deploy script.
  75. userScript(provider);
  76. }}
  77. main();
  78. "#
  79. )
  80. }
  81. pub fn deploy_script() -> &'static str {
  82. r#"// Migrations are an early feature. Currently, they're nothing more than this
  83. // single deploy script that's invoked from the CLI, injecting a provider
  84. // configured from the workspace's Anchor.toml.
  85. const anchor = require("@coral-xyz/anchor");
  86. module.exports = async function (provider) {
  87. // Configure client to use the provider.
  88. anchor.setProvider(provider);
  89. // Add your deploy script here.
  90. };
  91. "#
  92. }
  93. pub fn ts_deploy_script() -> &'static str {
  94. r#"// Migrations are an early feature. Currently, they're nothing more than this
  95. // single deploy script that's invoked from the CLI, injecting a provider
  96. // configured from the workspace's Anchor.toml.
  97. const anchor = require("@coral-xyz/anchor");
  98. module.exports = async function (provider) {
  99. // Configure client to use the provider.
  100. anchor.setProvider(provider);
  101. // Add your deploy script here.
  102. };
  103. "#
  104. }
  105. pub fn solidity(name: &str) -> String {
  106. format!(
  107. r#"
  108. @program_id("{}")
  109. contract {} {{
  110. bool private value = true;
  111. @payer(payer)
  112. constructor(address payer) {{
  113. print("Hello, World!");
  114. }}
  115. /// A message that can be called on instantiated contracts.
  116. /// This one flips the value of the stored `bool` from `true`
  117. /// to `false` and vice versa.
  118. function flip() public {{
  119. value = !value;
  120. }}
  121. /// Simply returns the current value of our `bool`.
  122. function get() public view returns (bool) {{
  123. return value;
  124. }}
  125. }}
  126. "#,
  127. default_program_id(),
  128. name.to_snake_case(),
  129. )
  130. }
  131. pub fn mocha(name: &str) -> String {
  132. format!(
  133. r#"const anchor = require("@coral-xyz/anchor");
  134. describe("{}", () => {{
  135. // Configure the client to use the local cluster.
  136. anchor.setProvider(anchor.AnchorProvider.env());
  137. it("Is initialized!", async () => {{
  138. // Add your test here.
  139. const program = anchor.workspace.{};
  140. const tx = await program.methods.initialize().rpc();
  141. console.log("Your transaction signature", tx);
  142. const val1 = await program.methods.get()
  143. .accounts({{ dataAccount: dataAccount.publicKey }})
  144. .view();
  145. console.log("state", val1);
  146. await program.methods.flip()
  147. .accounts({{ dataAccount: dataAccount.publicKey }})
  148. .rpc();
  149. const val2 = await program.methods.get()
  150. .accounts({{ dataAccount: dataAccount.publicKey }})
  151. .view();
  152. console.log("state", val2);
  153. }});
  154. }});
  155. "#,
  156. name,
  157. name.to_upper_camel_case(),
  158. )
  159. }
  160. pub fn jest(name: &str) -> String {
  161. format!(
  162. r#"const anchor = require("@coral-xyz/anchor");
  163. describe("{}", () => {{
  164. // Configure the client to use the local cluster.
  165. anchor.setProvider(anchor.AnchorProvider.env());
  166. it("Is initialized!", async () => {{
  167. // Add your test here.
  168. const program = anchor.workspace.{};
  169. const tx = await program.methods.initialize().rpc();
  170. console.log("Your transaction signature", tx);
  171. }});
  172. }});
  173. "#,
  174. name,
  175. name.to_upper_camel_case(),
  176. )
  177. }
  178. pub fn package_json(jest: bool) -> String {
  179. if jest {
  180. format!(
  181. r#"{{
  182. "scripts": {{
  183. "lint:fix": "prettier */*.js \"*/**/*{{.js,.ts}}\" -w",
  184. "lint": "prettier */*.js \"*/**/*{{.js,.ts}}\" --check"
  185. }},
  186. "dependencies": {{
  187. "@coral-xyz/anchor": "^{VERSION}"
  188. }},
  189. "devDependencies": {{
  190. "jest": "^29.0.3",
  191. "prettier": "^2.6.2"
  192. }}
  193. }}
  194. "#
  195. )
  196. } else {
  197. format!(
  198. r#"{{
  199. "scripts": {{
  200. "lint:fix": "prettier */*.js \"*/**/*{{.js,.ts}}\" -w",
  201. "lint": "prettier */*.js \"*/**/*{{.js,.ts}}\" --check"
  202. }},
  203. "dependencies": {{
  204. "@coral-xyz/anchor": "^{VERSION}"
  205. }},
  206. "devDependencies": {{
  207. "chai": "^4.3.4",
  208. "mocha": "^9.0.3",
  209. "prettier": "^2.6.2"
  210. }}
  211. }}
  212. "#
  213. )
  214. }
  215. }
  216. pub fn ts_package_json(jest: bool) -> String {
  217. if jest {
  218. format!(
  219. r#"{{
  220. "scripts": {{
  221. "lint:fix": "prettier */*.js \"*/**/*{{.js,.ts}}\" -w",
  222. "lint": "prettier */*.js \"*/**/*{{.js,.ts}}\" --check"
  223. }},
  224. "dependencies": {{
  225. "@coral-xyz/anchor": "^{VERSION}"
  226. }},
  227. "devDependencies": {{
  228. "@types/bn.js": "^5.1.0",
  229. "@types/jest": "^29.0.3",
  230. "jest": "^29.0.3",
  231. "prettier": "^2.6.2",
  232. "ts-jest": "^29.0.2",
  233. "typescript": "^4.3.5"
  234. }}
  235. }}
  236. "#
  237. )
  238. } else {
  239. format!(
  240. r#"{{
  241. "scripts": {{
  242. "lint:fix": "prettier */*.js \"*/**/*{{.js,.ts}}\" -w",
  243. "lint": "prettier */*.js \"*/**/*{{.js,.ts}}\" --check"
  244. }},
  245. "dependencies": {{
  246. "@coral-xyz/anchor": "^{VERSION}"
  247. }},
  248. "devDependencies": {{
  249. "chai": "^4.3.4",
  250. "mocha": "^9.0.3",
  251. "ts-mocha": "^10.0.0",
  252. "@types/bn.js": "^5.1.0",
  253. "@types/chai": "^4.3.0",
  254. "@types/mocha": "^9.0.0",
  255. "typescript": "^4.3.5",
  256. "prettier": "^2.6.2"
  257. }}
  258. }}
  259. "#
  260. )
  261. }
  262. }
  263. pub fn ts_mocha(name: &str) -> String {
  264. format!(
  265. r#"import * as anchor from "@coral-xyz/anchor";
  266. import {{ Program }} from "@coral-xyz/anchor";
  267. import {{ {} }} from "../target/types/{}";
  268. describe("{}", () => {{
  269. // Configure the client to use the local cluster.
  270. const provider = anchor.AnchorProvider.env();
  271. anchor.setProvider(provider);
  272. const dataAccount = anchor.web3.Keypair.generate();
  273. const wallet = provider.wallet;
  274. const program = anchor.workspace.{} as Program<{}>;
  275. it("Is initialized!", async () => {{
  276. // Add your test here.
  277. const tx = await program.methods.new(wallet.publicKey)
  278. .accounts({{ dataAccount: dataAccount.publicKey }})
  279. .signers([dataAccount]).rpc();
  280. console.log("Your transaction signature", tx);
  281. const val1 = await program.methods.get()
  282. .accounts({{ dataAccount: dataAccount.publicKey }})
  283. .view();
  284. console.log("state", val1);
  285. await program.methods.flip()
  286. .accounts({{ dataAccount: dataAccount.publicKey }})
  287. .rpc();
  288. const val2 = await program.methods.get()
  289. .accounts({{ dataAccount: dataAccount.publicKey }})
  290. .view();
  291. console.log("state", val2); }});
  292. }});
  293. "#,
  294. name.to_upper_camel_case(),
  295. name.to_snake_case(),
  296. name,
  297. name.to_upper_camel_case(),
  298. name.to_upper_camel_case(),
  299. )
  300. }
  301. pub fn ts_jest(name: &str) -> String {
  302. format!(
  303. r#"import * as anchor from "@coral-xyz/anchor";
  304. import {{ Program }} from "@coral-xyz/anchor";
  305. import {{ {} }} from "../target/types/{}";
  306. describe("{}", () => {{
  307. // Configure the client to use the local cluster.
  308. const provider = anchor.AnchorProvider.env();
  309. anchor.setProvider(provider);
  310. const dataAccount = anchor.web3.Keypair.generate();
  311. const wallet = provider.wallet;
  312. const program = anchor.workspace.{} as Program<{}>;
  313. it("Is initialized!", async () => {{
  314. // Add your test here.
  315. const tx = await program.methods.new(wallet.publicKey)
  316. .accounts({{ dataAccount: dataAccount.publicKey }})
  317. .signers([dataAccount]).rpc();
  318. console.log("Your transaction signature", tx);
  319. }});
  320. }});
  321. "#,
  322. name.to_upper_camel_case(),
  323. name.to_snake_case(),
  324. name,
  325. name.to_upper_camel_case(),
  326. name.to_upper_camel_case(),
  327. )
  328. }
  329. pub fn ts_config(jest: bool) -> &'static str {
  330. if jest {
  331. r#"{
  332. "compilerOptions": {
  333. "types": ["jest"],
  334. "typeRoots": ["./node_modules/@types"],
  335. "lib": ["es2015"],
  336. "module": "commonjs",
  337. "target": "es6",
  338. "esModuleInterop": true
  339. }
  340. }
  341. "#
  342. } else {
  343. r#"{
  344. "compilerOptions": {
  345. "types": ["mocha", "chai"],
  346. "typeRoots": ["./node_modules/@types"],
  347. "lib": ["es2015"],
  348. "module": "commonjs",
  349. "target": "es6",
  350. "esModuleInterop": true
  351. }
  352. }
  353. "#
  354. }
  355. }
  356. pub fn git_ignore() -> &'static str {
  357. r#"
  358. .anchor
  359. .DS_Store
  360. target
  361. **/*.rs.bk
  362. node_modules
  363. test-ledger
  364. "#
  365. }
  366. pub fn prettier_ignore() -> &'static str {
  367. r#"
  368. .anchor
  369. .DS_Store
  370. target
  371. node_modules
  372. dist
  373. build
  374. test-ledger
  375. "#
  376. }
  377. pub fn node_shell(
  378. cluster_url: &str,
  379. wallet_path: &str,
  380. programs: Vec<ProgramWorkspace>,
  381. ) -> Result<String> {
  382. let mut eval_string = format!(
  383. r#"
  384. const anchor = require('@coral-xyz/anchor');
  385. const web3 = anchor.web3;
  386. const PublicKey = anchor.web3.PublicKey;
  387. const Keypair = anchor.web3.Keypair;
  388. const __wallet = new anchor.Wallet(
  389. Keypair.fromSecretKey(
  390. Buffer.from(
  391. JSON.parse(
  392. require('fs').readFileSync(
  393. "{wallet_path}",
  394. {{
  395. encoding: "utf-8",
  396. }},
  397. ),
  398. ),
  399. ),
  400. ),
  401. );
  402. const __connection = new web3.Connection("{cluster_url}", "processed");
  403. const provider = new anchor.AnchorProvider(__connection, __wallet, {{
  404. commitment: "processed",
  405. preflightcommitment: "processed",
  406. }});
  407. anchor.setProvider(provider);
  408. "#
  409. );
  410. for program in programs {
  411. write!(
  412. &mut eval_string,
  413. r#"
  414. anchor.workspace.{} = new anchor.Program({}, new PublicKey("{}"), provider);
  415. "#,
  416. program.name.to_upper_camel_case(),
  417. serde_json::to_string(&program.idl)?,
  418. program.program_id
  419. )?;
  420. }
  421. Ok(eval_string)
  422. }