2021-05-22 16:06:08 -07:00
|
|
|
use crate::config::ProgramWorkspace;
|
2021-04-10 18:15:32 -07:00
|
|
|
use crate::VERSION;
|
2021-10-04 23:16:13 -07:00
|
|
|
use anchor_syn::idl::Idl;
|
2021-05-22 16:06:08 -07:00
|
|
|
use anyhow::Result;
|
2021-10-04 23:16:13 -07:00
|
|
|
use heck::{CamelCase, MixedCase, SnakeCase};
|
2021-09-07 17:57:01 -07:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
|
|
|
|
|
|
|
pub fn default_program_id() -> Pubkey {
|
|
|
|
"Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"
|
|
|
|
.parse()
|
|
|
|
.unwrap()
|
|
|
|
}
|
2021-01-02 22:40:17 -08:00
|
|
|
|
2021-03-08 18:49:11 -08:00
|
|
|
pub fn virtual_manifest() -> &'static str {
|
2021-01-02 22:40:17 -08:00
|
|
|
r#"[workspace]
|
|
|
|
members = [
|
2021-01-11 09:22:25 -08:00
|
|
|
"programs/*"
|
2021-01-02 22:40:17 -08:00
|
|
|
]
|
2022-04-20 13:36:10 -07:00
|
|
|
|
|
|
|
[profile.release]
|
|
|
|
overflow-checks = true
|
2021-01-02 22:40:17 -08:00
|
|
|
"#
|
|
|
|
}
|
|
|
|
|
2021-08-08 02:11:48 -07:00
|
|
|
pub fn credentials(token: &str) -> String {
|
|
|
|
format!(
|
|
|
|
r#"[registry]
|
|
|
|
token = "{}"
|
|
|
|
"#,
|
|
|
|
token
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-10-04 23:16:13 -07:00
|
|
|
pub fn idl_ts(idl: &Idl) -> Result<String> {
|
2022-03-16 15:27:57 -07:00
|
|
|
let mut idl = idl.clone();
|
|
|
|
for acc in idl.accounts.iter_mut() {
|
2021-11-09 22:40:28 -08:00
|
|
|
acc.name = acc.name.to_mixed_case();
|
|
|
|
}
|
2022-03-16 15:27:57 -07:00
|
|
|
let idl_json = serde_json::to_string_pretty(&idl)?;
|
2021-10-04 23:16:13 -07:00
|
|
|
Ok(format!(
|
2021-10-09 18:19:33 -07:00
|
|
|
r#"export type {} = {};
|
|
|
|
|
|
|
|
export const IDL: {} = {};
|
|
|
|
"#,
|
|
|
|
idl.name.to_camel_case(),
|
2022-03-16 15:27:57 -07:00
|
|
|
idl_json,
|
|
|
|
idl.name.to_camel_case(),
|
|
|
|
idl_json
|
2021-10-04 23:16:13 -07:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2021-01-02 22:40:17 -08:00
|
|
|
pub fn cargo_toml(name: &str) -> String {
|
|
|
|
format!(
|
|
|
|
r#"[package]
|
|
|
|
name = "{0}"
|
|
|
|
version = "0.1.0"
|
|
|
|
description = "Created with Anchor"
|
2022-03-28 13:59:53 -07:00
|
|
|
edition = "2021"
|
2021-01-02 22:40:17 -08:00
|
|
|
|
|
|
|
[lib]
|
2021-01-14 15:16:27 -08:00
|
|
|
crate-type = ["cdylib", "lib"]
|
2021-01-02 22:40:17 -08:00
|
|
|
name = "{1}"
|
|
|
|
|
2021-01-14 15:16:27 -08:00
|
|
|
[features]
|
|
|
|
no-entrypoint = []
|
2021-01-29 06:19:00 -08:00
|
|
|
no-idl = []
|
2021-11-24 10:46:17 -08:00
|
|
|
no-log-ix-name = []
|
2021-01-14 17:09:15 -08:00
|
|
|
cpi = ["no-entrypoint"]
|
2021-01-29 06:19:00 -08:00
|
|
|
default = []
|
2021-01-14 15:16:27 -08:00
|
|
|
|
2021-01-02 22:40:17 -08:00
|
|
|
[dependencies]
|
2021-04-10 18:15:32 -07:00
|
|
|
anchor-lang = "{2}"
|
2021-01-02 22:40:17 -08:00
|
|
|
"#,
|
|
|
|
name,
|
|
|
|
name.to_snake_case(),
|
2021-04-10 18:15:32 -07:00
|
|
|
VERSION,
|
2021-01-02 22:40:17 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-07-04 19:57:10 -07:00
|
|
|
pub fn deploy_js_script_host(cluster_url: &str, script_path: &str) -> String {
|
2021-01-27 19:31:15 -08:00
|
|
|
format!(
|
|
|
|
r#"
|
|
|
|
const anchor = require('@project-serum/anchor');
|
|
|
|
|
|
|
|
// Deploy script defined by the user.
|
|
|
|
const userScript = require("{0}");
|
|
|
|
|
2021-07-04 19:57:10 -07:00
|
|
|
async function main() {{
|
|
|
|
const url = "{1}";
|
|
|
|
const preflightCommitment = 'recent';
|
|
|
|
const connection = new anchor.web3.Connection(url, preflightCommitment);
|
|
|
|
const wallet = anchor.Wallet.local();
|
|
|
|
|
2022-04-12 12:02:06 -07:00
|
|
|
const provider = new anchor.AnchorProvider(connection, wallet, {{
|
2021-07-04 19:57:10 -07:00
|
|
|
preflightCommitment,
|
|
|
|
commitment: 'recent',
|
|
|
|
}});
|
|
|
|
|
|
|
|
// Run the user's deploy script.
|
|
|
|
userScript(provider);
|
|
|
|
}}
|
|
|
|
main();
|
|
|
|
"#,
|
|
|
|
script_path, cluster_url,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn deploy_ts_script_host(cluster_url: &str, script_path: &str) -> String {
|
|
|
|
format!(
|
2021-09-07 17:57:01 -07:00
|
|
|
r#"import * as anchor from '@project-serum/anchor';
|
2021-07-04 19:57:10 -07:00
|
|
|
|
|
|
|
// Deploy script defined by the user.
|
|
|
|
const userScript = require("{0}");
|
|
|
|
|
2021-01-27 19:31:15 -08:00
|
|
|
async function main() {{
|
|
|
|
const url = "{1}";
|
|
|
|
const preflightCommitment = 'recent';
|
|
|
|
const connection = new anchor.web3.Connection(url, preflightCommitment);
|
2021-01-27 19:58:26 -08:00
|
|
|
const wallet = anchor.Wallet.local();
|
2021-01-27 19:31:15 -08:00
|
|
|
|
2022-04-12 12:02:06 -07:00
|
|
|
const provider = new anchor.AnchorProvider(connection, wallet, {{
|
2021-01-27 19:31:15 -08:00
|
|
|
preflightCommitment,
|
|
|
|
commitment: 'recent',
|
|
|
|
}});
|
|
|
|
|
|
|
|
// Run the user's deploy script.
|
|
|
|
userScript(provider);
|
|
|
|
}}
|
|
|
|
main();
|
|
|
|
"#,
|
|
|
|
script_path, cluster_url,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-08 18:49:11 -08:00
|
|
|
pub fn deploy_script() -> &'static str {
|
2021-09-07 17:57:01 -07:00
|
|
|
r#"// Migrations are an early feature. Currently, they're nothing more than this
|
2021-01-27 19:58:26 -08:00
|
|
|
// single deploy script that's invoked from the CLI, injecting a provider
|
|
|
|
// configured from the workspace's Anchor.toml.
|
|
|
|
|
|
|
|
const anchor = require("@project-serum/anchor");
|
|
|
|
|
|
|
|
module.exports = async function (provider) {
|
|
|
|
// Configure client to use the provider.
|
|
|
|
anchor.setProvider(provider);
|
|
|
|
|
|
|
|
// Add your deploy script here.
|
2022-02-17 16:25:16 -08:00
|
|
|
};
|
2021-01-27 19:58:26 -08:00
|
|
|
"#
|
|
|
|
}
|
2021-03-30 23:14:15 -07:00
|
|
|
|
|
|
|
pub fn ts_deploy_script() -> &'static str {
|
2021-09-07 17:57:01 -07:00
|
|
|
r#"// Migrations are an early feature. Currently, they're nothing more than this
|
2021-03-30 23:14:15 -07:00
|
|
|
// single deploy script that's invoked from the CLI, injecting a provider
|
|
|
|
// configured from the workspace's Anchor.toml.
|
|
|
|
|
|
|
|
const anchor = require("@project-serum/anchor");
|
|
|
|
|
|
|
|
module.exports = async function (provider) {
|
|
|
|
// Configure client to use the provider.
|
|
|
|
anchor.setProvider(provider);
|
|
|
|
|
|
|
|
// Add your deploy script here.
|
2022-02-17 16:25:16 -08:00
|
|
|
};
|
2021-03-30 23:14:15 -07:00
|
|
|
"#
|
|
|
|
}
|
|
|
|
|
2021-03-08 18:49:11 -08:00
|
|
|
pub fn xargo_toml() -> &'static str {
|
2021-01-02 22:40:17 -08:00
|
|
|
r#"[target.bpfel-unknown-unknown.dependencies.std]
|
2021-06-26 07:57:02 -07:00
|
|
|
features = []
|
|
|
|
"#
|
2021-01-02 22:40:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lib_rs(name: &str) -> String {
|
|
|
|
format!(
|
2021-04-04 00:55:56 -07:00
|
|
|
r#"use anchor_lang::prelude::*;
|
2021-01-02 22:40:17 -08:00
|
|
|
|
2021-09-07 17:57:01 -07:00
|
|
|
declare_id!("{}");
|
|
|
|
|
2021-01-02 22:40:17 -08:00
|
|
|
#[program]
|
2021-01-23 00:18:50 -08:00
|
|
|
pub mod {} {{
|
2021-01-11 09:22:25 -08:00
|
|
|
use super::*;
|
2022-02-21 07:09:12 -08:00
|
|
|
|
|
|
|
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {{
|
2021-01-11 09:22:25 -08:00
|
|
|
Ok(())
|
|
|
|
}}
|
2021-01-02 22:40:17 -08:00
|
|
|
}}
|
|
|
|
|
|
|
|
#[derive(Accounts)]
|
2021-06-26 07:57:02 -07:00
|
|
|
pub struct Initialize {{}}
|
|
|
|
"#,
|
2021-09-07 17:57:01 -07:00
|
|
|
default_program_id(),
|
2021-01-02 22:40:17 -08:00
|
|
|
name.to_snake_case(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mocha(name: &str) -> String {
|
|
|
|
format!(
|
2022-02-17 16:25:16 -08:00
|
|
|
r#"const anchor = require("@project-serum/anchor");
|
2021-01-02 22:40:17 -08:00
|
|
|
|
2022-02-17 16:25:16 -08:00
|
|
|
describe("{}", () => {{
|
2021-01-02 22:40:17 -08:00
|
|
|
// Configure the client to use the local cluster.
|
2022-04-12 12:02:06 -07:00
|
|
|
anchor.setProvider(anchor.AnchorProvider.env());
|
2021-01-02 22:40:17 -08:00
|
|
|
|
2022-02-17 16:25:16 -08:00
|
|
|
it("Is initialized!", async () => {{
|
2021-01-02 22:40:17 -08:00
|
|
|
// Add your test here.
|
|
|
|
const program = anchor.workspace.{};
|
2022-04-04 11:50:45 -07:00
|
|
|
const tx = await program.methods.initialize().rpc();
|
2021-01-02 22:40:17 -08:00
|
|
|
console.log("Your transaction signature", tx);
|
|
|
|
}});
|
|
|
|
}});
|
|
|
|
"#,
|
|
|
|
name,
|
|
|
|
name.to_camel_case(),
|
|
|
|
)
|
|
|
|
}
|
2021-03-01 01:25:59 -08:00
|
|
|
|
2021-08-18 23:50:00 -07:00
|
|
|
pub fn package_json() -> String {
|
|
|
|
format!(
|
|
|
|
r#"{{
|
2022-04-07 06:14:38 -07:00
|
|
|
"scripts": {{
|
|
|
|
"lint:fix": "prettier */*.js \"*/**/*{{.js,.ts}}\" -w",
|
|
|
|
"lint": "prettier */*.js \"*/**/*{{.js,.ts}}\" --check"
|
|
|
|
}},
|
2021-08-18 23:50:00 -07:00
|
|
|
"dependencies": {{
|
|
|
|
"@project-serum/anchor": "^{0}"
|
|
|
|
}},
|
|
|
|
"devDependencies": {{
|
|
|
|
"chai": "^4.3.4",
|
2022-04-07 06:14:38 -07:00
|
|
|
"mocha": "^9.0.3",
|
|
|
|
"prettier": "^2.6.2"
|
2021-08-18 23:50:00 -07:00
|
|
|
}}
|
2021-09-07 17:57:01 -07:00
|
|
|
}}
|
2021-08-18 23:50:00 -07:00
|
|
|
"#,
|
|
|
|
VERSION
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ts_package_json() -> String {
|
|
|
|
format!(
|
|
|
|
r#"{{
|
2022-04-07 06:14:38 -07:00
|
|
|
"scripts": {{
|
|
|
|
"lint:fix": "prettier */*.js \"*/**/*{{.js,.ts}}\" -w",
|
|
|
|
"lint": "prettier */*.js \"*/**/*{{.js,.ts}}\" --check"
|
|
|
|
}},
|
2021-08-18 23:50:00 -07:00
|
|
|
"dependencies": {{
|
|
|
|
"@project-serum/anchor": "^{0}"
|
|
|
|
}},
|
|
|
|
"devDependencies": {{
|
|
|
|
"chai": "^4.3.4",
|
|
|
|
"mocha": "^9.0.3",
|
|
|
|
"ts-mocha": "^8.0.0",
|
2022-04-04 10:24:10 -07:00
|
|
|
"@types/bn.js": "^5.1.0",
|
2022-02-23 02:05:41 -08:00
|
|
|
"@types/chai": "^4.3.0",
|
2021-08-18 23:50:00 -07:00
|
|
|
"@types/mocha": "^9.0.0",
|
2022-04-07 06:14:38 -07:00
|
|
|
"typescript": "^4.3.5",
|
|
|
|
"prettier": "^2.6.2"
|
2021-08-18 23:50:00 -07:00
|
|
|
}}
|
2021-09-07 17:57:01 -07:00
|
|
|
}}
|
2021-08-18 23:50:00 -07:00
|
|
|
"#,
|
|
|
|
VERSION
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-01 01:25:59 -08:00
|
|
|
pub fn ts_mocha(name: &str) -> String {
|
|
|
|
format!(
|
2022-02-17 16:25:16 -08:00
|
|
|
r#"import * as anchor from "@project-serum/anchor";
|
|
|
|
import {{ Program }} from "@project-serum/anchor";
|
|
|
|
import {{ {} }} from "../target/types/{}";
|
2021-03-01 01:25:59 -08:00
|
|
|
|
2022-02-17 16:25:16 -08:00
|
|
|
describe("{}", () => {{
|
2021-03-01 01:25:59 -08:00
|
|
|
// Configure the client to use the local cluster.
|
2022-04-12 12:02:06 -07:00
|
|
|
anchor.setProvider(anchor.AnchorProvider.env());
|
2021-03-01 01:25:59 -08:00
|
|
|
|
2021-10-04 23:16:13 -07:00
|
|
|
const program = anchor.workspace.{} as Program<{}>;
|
|
|
|
|
2022-02-17 16:25:16 -08:00
|
|
|
it("Is initialized!", async () => {{
|
2021-03-01 01:25:59 -08:00
|
|
|
// Add your test here.
|
2022-04-04 11:50:45 -07:00
|
|
|
const tx = await program.methods.initialize().rpc();
|
2021-03-01 01:25:59 -08:00
|
|
|
console.log("Your transaction signature", tx);
|
|
|
|
}});
|
|
|
|
}});
|
|
|
|
"#,
|
2021-10-04 23:16:13 -07:00
|
|
|
name.to_camel_case(),
|
|
|
|
name.to_snake_case(),
|
2021-03-01 01:25:59 -08:00
|
|
|
name,
|
|
|
|
name.to_camel_case(),
|
2021-10-04 23:16:13 -07:00
|
|
|
name.to_camel_case(),
|
2021-03-01 01:25:59 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-08 18:49:11 -08:00
|
|
|
pub fn ts_config() -> &'static str {
|
2021-03-01 01:25:59 -08:00
|
|
|
r#"{
|
|
|
|
"compilerOptions": {
|
|
|
|
"types": ["mocha", "chai"],
|
|
|
|
"typeRoots": ["./node_modules/@types"],
|
|
|
|
"lib": ["es2015"],
|
|
|
|
"module": "commonjs",
|
|
|
|
"target": "es6",
|
|
|
|
"esModuleInterop": true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#
|
2021-03-08 18:49:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn git_ignore() -> &'static str {
|
2021-05-23 10:00:24 -07:00
|
|
|
r#"
|
2021-05-24 14:52:21 -07:00
|
|
|
.anchor
|
2021-03-08 18:49:11 -08:00
|
|
|
.DS_Store
|
|
|
|
target
|
|
|
|
**/*.rs.bk
|
2021-09-07 17:57:01 -07:00
|
|
|
node_modules
|
2022-03-28 13:59:53 -07:00
|
|
|
test-ledger
|
2021-05-23 10:00:24 -07:00
|
|
|
"#
|
2021-03-01 01:25:59 -08:00
|
|
|
}
|
2021-05-22 16:06:08 -07:00
|
|
|
|
2022-04-07 06:14:38 -07:00
|
|
|
pub fn prettier_ignore() -> &'static str {
|
|
|
|
r#"
|
|
|
|
.anchor
|
|
|
|
.DS_Store
|
|
|
|
target
|
|
|
|
node_modules
|
|
|
|
dist
|
|
|
|
build
|
|
|
|
test-ledger
|
|
|
|
"#
|
|
|
|
}
|
|
|
|
|
2021-05-22 16:06:08 -07:00
|
|
|
pub fn node_shell(
|
|
|
|
cluster_url: &str,
|
|
|
|
wallet_path: &str,
|
|
|
|
programs: Vec<ProgramWorkspace>,
|
|
|
|
) -> Result<String> {
|
|
|
|
let mut eval_string = format!(
|
|
|
|
r#"
|
|
|
|
const anchor = require('@project-serum/anchor');
|
|
|
|
const web3 = anchor.web3;
|
|
|
|
const PublicKey = anchor.web3.PublicKey;
|
2021-09-22 13:28:45 -07:00
|
|
|
const Keypair = anchor.web3.Keypair;
|
2021-05-22 16:06:08 -07:00
|
|
|
|
|
|
|
const __wallet = new anchor.Wallet(
|
2021-09-22 13:28:45 -07:00
|
|
|
Keypair.fromSecretKey(
|
|
|
|
Buffer.from(
|
|
|
|
JSON.parse(
|
|
|
|
require('fs').readFileSync(
|
|
|
|
"{}",
|
|
|
|
{{
|
|
|
|
encoding: "utf-8",
|
|
|
|
}},
|
|
|
|
),
|
2021-05-22 16:06:08 -07:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
const __connection = new web3.Connection("{}", "processed");
|
2022-04-12 12:02:06 -07:00
|
|
|
const provider = new anchor.AnchorProvider(__connection, __wallet, {{
|
2021-05-22 16:06:08 -07:00
|
|
|
commitment: "processed",
|
|
|
|
preflightcommitment: "processed",
|
|
|
|
}});
|
|
|
|
anchor.setProvider(provider);
|
|
|
|
"#,
|
|
|
|
wallet_path, cluster_url,
|
|
|
|
);
|
|
|
|
|
|
|
|
for program in programs {
|
|
|
|
eval_string.push_str(&format!(
|
|
|
|
r#"
|
|
|
|
anchor.workspace.{} = new anchor.Program({}, new PublicKey("{}"), provider);
|
|
|
|
"#,
|
2021-05-24 18:23:31 -07:00
|
|
|
program.name.to_camel_case(),
|
2021-05-22 16:06:08 -07:00
|
|
|
serde_json::to_string(&program.idl)?,
|
2021-11-14 06:46:18 -08:00
|
|
|
program.program_id
|
2021-05-22 16:06:08 -07:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(eval_string)
|
|
|
|
}
|