template.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. use crate::config::ProgramWorkspace;
  2. use crate::VERSION;
  3. use anyhow::Result;
  4. use heck::{CamelCase, SnakeCase};
  5. pub fn virtual_manifest() -> &'static str {
  6. r#"[workspace]
  7. members = [
  8. "programs/*"
  9. ]
  10. "#
  11. }
  12. pub fn cargo_toml(name: &str) -> String {
  13. format!(
  14. r#"[package]
  15. name = "{0}"
  16. version = "0.1.0"
  17. description = "Created with Anchor"
  18. edition = "2018"
  19. [lib]
  20. crate-type = ["cdylib", "lib"]
  21. name = "{1}"
  22. [features]
  23. no-entrypoint = []
  24. no-idl = []
  25. cpi = ["no-entrypoint"]
  26. default = []
  27. [dependencies]
  28. anchor-lang = "{2}"
  29. "#,
  30. name,
  31. name.to_snake_case(),
  32. VERSION,
  33. )
  34. }
  35. pub fn deploy_script_host(cluster_url: &str, script_path: &str) -> String {
  36. format!(
  37. r#"
  38. const anchor = require('@project-serum/anchor');
  39. // Deploy script defined by the user.
  40. const userScript = require("{0}");
  41. async function main() {{
  42. const url = "{1}";
  43. const preflightCommitment = 'recent';
  44. const connection = new anchor.web3.Connection(url, preflightCommitment);
  45. const wallet = anchor.Wallet.local();
  46. const provider = new anchor.Provider(connection, wallet, {{
  47. preflightCommitment,
  48. commitment: 'recent',
  49. }});
  50. // Run the user's deploy script.
  51. userScript(provider);
  52. }}
  53. main();
  54. "#,
  55. script_path, cluster_url,
  56. )
  57. }
  58. pub fn deploy_script() -> &'static str {
  59. r#"
  60. // Migrations are an early feature. Currently, they're nothing more than this
  61. // single deploy script that's invoked from the CLI, injecting a provider
  62. // configured from the workspace's Anchor.toml.
  63. const anchor = require("@project-serum/anchor");
  64. module.exports = async function (provider) {
  65. // Configure client to use the provider.
  66. anchor.setProvider(provider);
  67. // Add your deploy script here.
  68. }
  69. "#
  70. }
  71. pub fn ts_deploy_script() -> &'static str {
  72. r#"
  73. // Migrations are an early feature. Currently, they're nothing more than this
  74. // single deploy script that's invoked from the CLI, injecting a provider
  75. // configured from the workspace's Anchor.toml.
  76. const anchor = require("@project-serum/anchor");
  77. module.exports = async function (provider) {
  78. // Configure client to use the provider.
  79. anchor.setProvider(provider);
  80. // Add your deploy script here.
  81. }
  82. "#
  83. }
  84. pub fn xargo_toml() -> &'static str {
  85. r#"[target.bpfel-unknown-unknown.dependencies.std]
  86. features = []"#
  87. }
  88. pub fn lib_rs(name: &str) -> String {
  89. format!(
  90. r#"use anchor_lang::prelude::*;
  91. #[program]
  92. pub mod {} {{
  93. use super::*;
  94. pub fn initialize(ctx: Context<Initialize>) -> ProgramResult {{
  95. Ok(())
  96. }}
  97. }}
  98. #[derive(Accounts)]
  99. pub struct Initialize {{}}"#,
  100. name.to_snake_case(),
  101. )
  102. }
  103. pub fn mocha(name: &str) -> String {
  104. format!(
  105. r#"const anchor = require('@project-serum/anchor');
  106. describe('{}', () => {{
  107. // Configure the client to use the local cluster.
  108. anchor.setProvider(anchor.Provider.env());
  109. it('Is initialized!', async () => {{
  110. // Add your test here.
  111. const program = anchor.workspace.{};
  112. const tx = await program.rpc.initialize();
  113. console.log("Your transaction signature", tx);
  114. }});
  115. }});
  116. "#,
  117. name,
  118. name.to_camel_case(),
  119. )
  120. }
  121. pub fn ts_mocha(name: &str) -> String {
  122. format!(
  123. r#"import * as anchor from '@project-serum/anchor';
  124. describe('{}', () => {{
  125. // Configure the client to use the local cluster.
  126. anchor.setProvider(anchor.Provider.env());
  127. it('Is initialized!', async () => {{
  128. // Add your test here.
  129. const program = anchor.workspace.{};
  130. const tx = await program.rpc.initialize();
  131. console.log("Your transaction signature", tx);
  132. }});
  133. }});
  134. "#,
  135. name,
  136. name.to_camel_case(),
  137. )
  138. }
  139. pub fn ts_config() -> &'static str {
  140. r#"{
  141. "compilerOptions": {
  142. "types": ["mocha", "chai"],
  143. "typeRoots": ["./node_modules/@types"],
  144. "lib": ["es2015"],
  145. "module": "commonjs",
  146. "target": "es6",
  147. "esModuleInterop": true
  148. }
  149. }
  150. "#
  151. }
  152. pub fn git_ignore() -> &'static str {
  153. r#"
  154. .anchor
  155. .DS_Store
  156. target
  157. **/*.rs.bk
  158. "#
  159. }
  160. pub fn node_shell(
  161. cluster_url: &str,
  162. wallet_path: &str,
  163. programs: Vec<ProgramWorkspace>,
  164. ) -> Result<String> {
  165. let mut eval_string = format!(
  166. r#"
  167. const anchor = require('@project-serum/anchor');
  168. const web3 = anchor.web3;
  169. const PublicKey = anchor.web3.PublicKey;
  170. const __wallet = new anchor.Wallet(
  171. Buffer.from(
  172. JSON.parse(
  173. require('fs').readFileSync(
  174. "{}",
  175. {{
  176. encoding: "utf-8",
  177. }},
  178. ),
  179. ),
  180. ),
  181. );
  182. const __connection = new web3.Connection("{}", "processed");
  183. const provider = new anchor.Provider(__connection, __wallet, {{
  184. commitment: "processed",
  185. preflightcommitment: "processed",
  186. }});
  187. anchor.setProvider(provider);
  188. "#,
  189. wallet_path, cluster_url,
  190. );
  191. for program in programs {
  192. eval_string.push_str(&format!(
  193. r#"
  194. anchor.workspace.{} = new anchor.Program({}, new PublicKey("{}"), provider);
  195. "#,
  196. program.name,
  197. serde_json::to_string(&program.idl)?,
  198. program.program_id.to_string()
  199. ));
  200. }
  201. Ok(eval_string)
  202. }