template.rs 5.8 KB

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