solidity_template.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. use crate::create_files;
  2. use anyhow::Result;
  3. use heck::{ToSnakeCase, ToUpperCamelCase};
  4. use std::path::Path;
  5. /// Create a solidity program.
  6. pub fn create_program(name: &str) -> Result<()> {
  7. let files = vec![(
  8. Path::new("solidity").join(name).with_extension("sol"),
  9. solidity(name),
  10. )];
  11. create_files(&files)
  12. }
  13. fn solidity(name: &str) -> String {
  14. format!(
  15. r#"
  16. contract {} {{
  17. bool private value = true;
  18. @payer(payer)
  19. constructor() {{
  20. print("Hello, World!");
  21. }}
  22. /// A message that can be called on instantiated contracts.
  23. /// This one flips the value of the stored `bool` from `true`
  24. /// to `false` and vice versa.
  25. function flip() public {{
  26. value = !value;
  27. }}
  28. /// Simply returns the current value of our `bool`.
  29. function get() public view returns (bool) {{
  30. return value;
  31. }}
  32. }}
  33. "#,
  34. name.to_snake_case(),
  35. )
  36. }
  37. pub fn mocha(name: &str) -> String {
  38. format!(
  39. r#"const anchor = require("@coral-xyz/anchor");
  40. describe("{}", () => {{
  41. // Configure the client to use the local cluster.
  42. anchor.setProvider(anchor.AnchorProvider.env());
  43. it("Is initialized!", async () => {{
  44. // Add your test here.
  45. const program = anchor.workspace.{};
  46. const dataAccount = anchor.web3.Keypair.generate();
  47. const tx = await program.methods
  48. .new()
  49. .accounts({{ dataAccount: dataAccount.publicKey }})
  50. .signers([dataAccount])
  51. .rpc();
  52. console.log("Your transaction signature", tx);
  53. const val1 = await program.methods
  54. .get()
  55. .accounts({{ dataAccount: dataAccount.publicKey }})
  56. .view();
  57. console.log("state", val1);
  58. await program.methods
  59. .flip()
  60. .accounts({{ dataAccount: dataAccount.publicKey }})
  61. .rpc();
  62. const val2 = await program.methods
  63. .get()
  64. .accounts({{ dataAccount: dataAccount.publicKey }})
  65. .view();
  66. console.log("state", val2);
  67. }});
  68. }});
  69. "#,
  70. name,
  71. name.to_upper_camel_case(),
  72. )
  73. }
  74. pub fn jest(name: &str) -> String {
  75. format!(
  76. r#"const anchor = require("@coral-xyz/anchor");
  77. describe("{}", () => {{
  78. // Configure the client to use the local cluster.
  79. anchor.setProvider(anchor.AnchorProvider.env());
  80. it("Is initialized!", async () => {{
  81. // Add your test here.
  82. const program = anchor.workspace.{};
  83. const tx = await program.methods.initialize().rpc();
  84. console.log("Your transaction signature", tx);
  85. }});
  86. }});
  87. "#,
  88. name,
  89. name.to_upper_camel_case(),
  90. )
  91. }
  92. pub fn ts_mocha(name: &str) -> String {
  93. format!(
  94. r#"import * as anchor from "@coral-xyz/anchor";
  95. import {{ Program }} from "@coral-xyz/anchor";
  96. import {{ {} }} from "../target/types/{}";
  97. describe("{}", () => {{
  98. // Configure the client to use the local cluster.
  99. const provider = anchor.AnchorProvider.env();
  100. anchor.setProvider(provider);
  101. const dataAccount = anchor.web3.Keypair.generate();
  102. const program = anchor.workspace.{} as Program<{}>;
  103. it("Is initialized!", async () => {{
  104. // Add your test here.
  105. const tx = await program.methods
  106. .new()
  107. .accounts({{ dataAccount: dataAccount.publicKey }})
  108. .signers([dataAccount])
  109. .rpc();
  110. console.log("Your transaction signature", tx);
  111. const val1 = await program.methods
  112. .get()
  113. .accounts({{ dataAccount: dataAccount.publicKey }})
  114. .view();
  115. console.log("state", val1);
  116. await program.methods
  117. .flip()
  118. .accounts({{ dataAccount: dataAccount.publicKey }})
  119. .rpc();
  120. const val2 = await program.methods
  121. .get()
  122. .accounts({{ dataAccount: dataAccount.publicKey }})
  123. .view();
  124. console.log("state", val2); }});
  125. }});
  126. "#,
  127. name.to_upper_camel_case(),
  128. name.to_snake_case(),
  129. name,
  130. name.to_upper_camel_case(),
  131. name.to_upper_camel_case(),
  132. )
  133. }