template.rs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. use crate::config::ProgramWorkspace;
  2. use crate::VERSION;
  3. use anchor_syn::idl::Idl;
  4. use anyhow::Result;
  5. use heck::{CamelCase, MixedCase, SnakeCase};
  6. use solana_sdk::pubkey::Pubkey;
  7. pub fn default_program_id() -> Pubkey {
  8. "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"
  9. .parse()
  10. .unwrap()
  11. }
  12. pub fn virtual_manifest() -> &'static str {
  13. r#"[workspace]
  14. members = [
  15. "programs/*"
  16. ]
  17. "#
  18. }
  19. pub fn credentials(token: &str) -> String {
  20. format!(
  21. r#"[registry]
  22. token = "{}"
  23. "#,
  24. token
  25. )
  26. }
  27. pub fn idl_ts(idl: &Idl) -> Result<String> {
  28. let mut idl = idl.clone();
  29. for acc in idl.accounts.iter_mut() {
  30. acc.name = acc.name.to_mixed_case();
  31. }
  32. let idl_json = serde_json::to_string_pretty(&idl)?;
  33. Ok(format!(
  34. r#"export type {} = {};
  35. export const IDL: {} = {};
  36. "#,
  37. idl.name.to_camel_case(),
  38. idl_json,
  39. idl.name.to_camel_case(),
  40. idl_json
  41. ))
  42. }
  43. pub fn cargo_toml(name: &str) -> String {
  44. format!(
  45. r#"[package]
  46. name = "{0}"
  47. version = "0.1.0"
  48. description = "Created with Anchor"
  49. edition = "2018"
  50. [lib]
  51. crate-type = ["cdylib", "lib"]
  52. name = "{1}"
  53. [features]
  54. no-entrypoint = []
  55. no-idl = []
  56. cpi = ["no-entrypoint"]
  57. default = []
  58. [dependencies]
  59. anchor-lang = "{2}"
  60. "#,
  61. name,
  62. name.to_snake_case(),
  63. VERSION,
  64. )
  65. }
  66. pub fn deploy_js_script_host(cluster_url: &str, script_path: &str) -> String {
  67. format!(
  68. r#"
  69. const anchor = require('@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_ts_script_host(cluster_url: &str, script_path: &str) -> String {
  90. format!(
  91. r#"import * as anchor from '@project-serum/anchor';
  92. // Deploy script defined by the user.
  93. const userScript = require("{0}");
  94. async function main() {{
  95. const url = "{1}";
  96. const preflightCommitment = 'recent';
  97. const connection = new anchor.web3.Connection(url, preflightCommitment);
  98. const wallet = anchor.Wallet.local();
  99. const provider = new anchor.Provider(connection, wallet, {{
  100. preflightCommitment,
  101. commitment: 'recent',
  102. }});
  103. // Run the user's deploy script.
  104. userScript(provider);
  105. }}
  106. main();
  107. "#,
  108. script_path, cluster_url,
  109. )
  110. }
  111. pub fn deploy_script() -> &'static str {
  112. r#"// Migrations are an early feature. Currently, they're nothing more than this
  113. // single deploy script that's invoked from the CLI, injecting a provider
  114. // configured from the workspace's Anchor.toml.
  115. const anchor = require("@project-serum/anchor");
  116. module.exports = async function (provider) {
  117. // Configure client to use the provider.
  118. anchor.setProvider(provider);
  119. // Add your deploy script here.
  120. }
  121. "#
  122. }
  123. pub fn ts_deploy_script() -> &'static str {
  124. r#"// Migrations are an early feature. Currently, they're nothing more than this
  125. // single deploy script that's invoked from the CLI, injecting a provider
  126. // configured from the workspace's Anchor.toml.
  127. const anchor = require("@project-serum/anchor");
  128. module.exports = async function (provider) {
  129. // Configure client to use the provider.
  130. anchor.setProvider(provider);
  131. // Add your deploy script here.
  132. }
  133. "#
  134. }
  135. pub fn xargo_toml() -> &'static str {
  136. r#"[target.bpfel-unknown-unknown.dependencies.std]
  137. features = []
  138. "#
  139. }
  140. pub fn lib_rs(name: &str) -> String {
  141. format!(
  142. r#"use anchor_lang::prelude::*;
  143. declare_id!("{}");
  144. #[program]
  145. pub mod {} {{
  146. use super::*;
  147. pub fn initialize(ctx: Context<Initialize>) -> ProgramResult {{
  148. Ok(())
  149. }}
  150. }}
  151. #[derive(Accounts)]
  152. pub struct Initialize {{}}
  153. "#,
  154. default_program_id(),
  155. name.to_snake_case(),
  156. )
  157. }
  158. pub fn mocha(name: &str) -> String {
  159. format!(
  160. r#"const anchor = require('@project-serum/anchor');
  161. describe('{}', () => {{
  162. // Configure the client to use the local cluster.
  163. anchor.setProvider(anchor.Provider.env());
  164. it('Is initialized!', async () => {{
  165. // Add your test here.
  166. const program = anchor.workspace.{};
  167. const tx = await program.rpc.initialize();
  168. console.log("Your transaction signature", tx);
  169. }});
  170. }});
  171. "#,
  172. name,
  173. name.to_camel_case(),
  174. )
  175. }
  176. pub fn package_json() -> String {
  177. format!(
  178. r#"{{
  179. "dependencies": {{
  180. "@project-serum/anchor": "^{0}"
  181. }},
  182. "devDependencies": {{
  183. "chai": "^4.3.4",
  184. "mocha": "^9.0.3"
  185. }}
  186. }}
  187. "#,
  188. VERSION
  189. )
  190. }
  191. pub fn ts_package_json() -> String {
  192. format!(
  193. r#"{{
  194. "dependencies": {{
  195. "@project-serum/anchor": "^{0}"
  196. }},
  197. "devDependencies": {{
  198. "chai": "^4.3.4",
  199. "mocha": "^9.0.3",
  200. "ts-mocha": "^8.0.0",
  201. "@types/mocha": "^9.0.0",
  202. "typescript": "^4.3.5"
  203. }}
  204. }}
  205. "#,
  206. VERSION
  207. )
  208. }
  209. pub fn ts_mocha(name: &str) -> String {
  210. format!(
  211. r#"import * as anchor from '@project-serum/anchor';
  212. import {{ Program }} from '@project-serum/anchor';
  213. import {{ {} }} from '../target/types/{}';
  214. describe('{}', () => {{
  215. // Configure the client to use the local cluster.
  216. anchor.setProvider(anchor.Provider.env());
  217. const program = anchor.workspace.{} as Program<{}>;
  218. it('Is initialized!', async () => {{
  219. // Add your test here.
  220. const tx = await program.rpc.initialize({{}});
  221. console.log("Your transaction signature", tx);
  222. }});
  223. }});
  224. "#,
  225. name.to_camel_case(),
  226. name.to_snake_case(),
  227. name,
  228. name.to_camel_case(),
  229. name.to_camel_case(),
  230. )
  231. }
  232. pub fn ts_config() -> &'static str {
  233. r#"{
  234. "compilerOptions": {
  235. "types": ["mocha", "chai"],
  236. "typeRoots": ["./node_modules/@types"],
  237. "lib": ["es2015"],
  238. "module": "commonjs",
  239. "target": "es6",
  240. "esModuleInterop": true
  241. }
  242. }
  243. "#
  244. }
  245. pub fn git_ignore() -> &'static str {
  246. r#"
  247. .anchor
  248. .DS_Store
  249. target
  250. **/*.rs.bk
  251. node_modules
  252. "#
  253. }
  254. pub fn node_shell(
  255. cluster_url: &str,
  256. wallet_path: &str,
  257. programs: Vec<ProgramWorkspace>,
  258. ) -> Result<String> {
  259. let mut eval_string = format!(
  260. r#"
  261. const anchor = require('@project-serum/anchor');
  262. const web3 = anchor.web3;
  263. const PublicKey = anchor.web3.PublicKey;
  264. const Keypair = anchor.web3.Keypair;
  265. const __wallet = new anchor.Wallet(
  266. Keypair.fromSecretKey(
  267. Buffer.from(
  268. JSON.parse(
  269. require('fs').readFileSync(
  270. "{}",
  271. {{
  272. encoding: "utf-8",
  273. }},
  274. ),
  275. ),
  276. ),
  277. ),
  278. );
  279. const __connection = new web3.Connection("{}", "processed");
  280. const provider = new anchor.Provider(__connection, __wallet, {{
  281. commitment: "processed",
  282. preflightcommitment: "processed",
  283. }});
  284. anchor.setProvider(provider);
  285. "#,
  286. wallet_path, cluster_url,
  287. );
  288. for program in programs {
  289. eval_string.push_str(&format!(
  290. r#"
  291. anchor.workspace.{} = new anchor.Program({}, new PublicKey("{}"), provider);
  292. "#,
  293. program.name.to_camel_case(),
  294. serde_json::to_string(&program.idl)?,
  295. program.program_id
  296. ));
  297. }
  298. Ok(eval_string)
  299. }