template.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. use heck::CamelCase;
  2. use heck::SnakeCase;
  3. pub fn virtual_manifest() -> String {
  4. r#"[workspace]
  5. members = [
  6. "programs/*"
  7. ]
  8. "#
  9. .to_string()
  10. }
  11. pub fn cargo_toml(name: &str) -> String {
  12. format!(
  13. r#"[package]
  14. name = "{0}"
  15. version = "0.1.0"
  16. description = "Created with Anchor"
  17. edition = "2018"
  18. [lib]
  19. crate-type = ["cdylib", "lib"]
  20. name = "{1}"
  21. [features]
  22. no-entrypoint = []
  23. no-idl = []
  24. cpi = ["no-entrypoint"]
  25. default = []
  26. [dependencies]
  27. anchor-lang = "0.2.1"
  28. "#,
  29. name,
  30. name.to_snake_case(),
  31. )
  32. }
  33. pub fn deploy_script_host(cluster_url: &str, script_path: &str) -> String {
  34. format!(
  35. r#"
  36. const anchor = require('@project-serum/anchor');
  37. // Deploy script defined by the user.
  38. const userScript = require("{0}");
  39. async function main() {{
  40. const url = "{1}";
  41. const preflightCommitment = 'recent';
  42. const connection = new anchor.web3.Connection(url, preflightCommitment);
  43. const wallet = anchor.Wallet.local();
  44. const provider = new anchor.Provider(connection, wallet, {{
  45. preflightCommitment,
  46. commitment: 'recent',
  47. }});
  48. // Run the user's deploy script.
  49. userScript(provider);
  50. }}
  51. main();
  52. "#,
  53. script_path, cluster_url,
  54. )
  55. }
  56. pub fn deploy_script() -> String {
  57. r#"
  58. // Migrations are an early feature. Currently, they're nothing more than this
  59. // single deploy script that's invoked from the CLI, injecting a provider
  60. // configured from the workspace's Anchor.toml.
  61. const anchor = require("@project-serum/anchor");
  62. module.exports = async function (provider) {
  63. // Configure client to use the provider.
  64. anchor.setProvider(provider);
  65. // Add your deploy script here.
  66. }
  67. "#
  68. .to_string()
  69. }
  70. pub fn xargo_toml() -> String {
  71. r#"[target.bpfel-unknown-unknown.dependencies.std]
  72. features = []"#
  73. .to_string()
  74. }
  75. pub fn lib_rs(name: &str) -> String {
  76. format!(
  77. r#"#![feature(proc_macro_hygiene)]
  78. use anchor_lang::prelude::*;
  79. #[program]
  80. pub mod {} {{
  81. use super::*;
  82. pub fn initialize(ctx: Context<Initialize>) -> ProgramResult {{
  83. Ok(())
  84. }}
  85. }}
  86. #[derive(Accounts)]
  87. pub struct Initialize {{}}"#,
  88. name.to_snake_case(),
  89. )
  90. }
  91. pub fn mocha(name: &str) -> String {
  92. format!(
  93. r#"const anchor = require('@project-serum/anchor');
  94. describe('{}', () => {{
  95. // Configure the client to use the local cluster.
  96. anchor.setProvider(anchor.Provider.env());
  97. it('Is initialized!', async () => {{
  98. // Add your test here.
  99. const program = anchor.workspace.{};
  100. const tx = await program.rpc.initialize();
  101. console.log("Your transaction signature", tx);
  102. }});
  103. }});
  104. "#,
  105. name,
  106. name.to_camel_case(),
  107. )
  108. }