From 3ee33132f58fe0eda6b1f967bde0f8b07534a5c6 Mon Sep 17 00:00:00 2001 From: Armani Ferrante Date: Tue, 7 Sep 2021 17:57:01 -0700 Subject: [PATCH] cli: Add default program id to init template (#689) --- cli/src/config.rs | 16 ++++++++++++---- cli/src/lib.rs | 15 ++++++++++++++- cli/src/template.rs | 24 ++++++++++++++++-------- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/cli/src/config.rs b/cli/src/config.rs index e32d6672..a44a64eb 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -329,12 +329,12 @@ impl Config { struct _Config { anchor_version: Option, solana_version: Option, + programs: Option>>, registry: Option, provider: Provider, - test: Option, - scripts: Option, - programs: Option>>, workspace: Option, + scripts: Option, + test: Option, } #[derive(Debug, Serialize, Deserialize)] @@ -409,7 +409,7 @@ fn ser_programs( .map(|(name, deployment)| { ( name.clone(), - serde_json::to_value(&_ProgramDeployment::from(deployment)).unwrap(), + to_value(&_ProgramDeployment::from(deployment)), ) }) .collect::>(); @@ -417,6 +417,14 @@ fn ser_programs( }) .collect::>>() } + +fn to_value(dep: &_ProgramDeployment) -> serde_json::Value { + if dep.path.is_none() && dep.idl.is_none() { + return serde_json::Value::String(dep.address.to_string()); + } + serde_json::to_value(dep).unwrap() +} + fn deser_programs( programs: BTreeMap>, ) -> Result>> { diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 8788a882..94dfdbe4 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -1,4 +1,6 @@ -use crate::config::{AnchorPackage, Config, ConfigOverride, Manifest, ProgramWorkspace, WithPath}; +use crate::config::{ + AnchorPackage, Config, ConfigOverride, Manifest, ProgramDeployment, ProgramWorkspace, WithPath, +}; use anchor_client::Cluster; use anchor_lang::idl::{IdlAccount, IdlInstruction}; use anchor_lang::{AccountDeserialize, AnchorDeserialize, AnchorSerialize}; @@ -23,6 +25,7 @@ use solana_sdk::signature::Keypair; use solana_sdk::signature::Signer; use solana_sdk::sysvar; use solana_sdk::transaction::Transaction; +use std::collections::BTreeMap; use std::collections::HashMap; use std::fs::{self, File}; use std::io::prelude::*; @@ -314,6 +317,16 @@ fn init(cfg_override: &ConfigOverride, name: String, typescript: bool) -> Result } .to_owned(), ); + let mut localnet = BTreeMap::new(); + localnet.insert( + name.to_string(), + ProgramDeployment { + address: template::default_program_id(), + path: None, + idl: None, + }, + ); + cfg.programs.insert(Cluster::Localnet, localnet); let toml = cfg.to_string(); let mut file = File::create("Anchor.toml")?; file.write_all(toml.as_bytes())?; diff --git a/cli/src/template.rs b/cli/src/template.rs index a37f221f..62245df9 100644 --- a/cli/src/template.rs +++ b/cli/src/template.rs @@ -2,6 +2,13 @@ use crate::config::ProgramWorkspace; use crate::VERSION; use anyhow::Result; use heck::{CamelCase, SnakeCase}; +use solana_sdk::pubkey::Pubkey; + +pub fn default_program_id() -> Pubkey { + "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS" + .parse() + .unwrap() +} pub fn virtual_manifest() -> &'static str { r#"[workspace] @@ -77,8 +84,7 @@ main(); pub fn deploy_ts_script_host(cluster_url: &str, script_path: &str) -> String { format!( - r#" -import * as anchor from '@project-serum/anchor'; + r#"import * as anchor from '@project-serum/anchor'; // Deploy script defined by the user. const userScript = require("{0}"); @@ -104,8 +110,7 @@ main(); } pub fn deploy_script() -> &'static str { - r#" -// Migrations are an early feature. Currently, they're nothing more than this + r#"// Migrations are an early feature. Currently, they're nothing more than this // single deploy script that's invoked from the CLI, injecting a provider // configured from the workspace's Anchor.toml. @@ -121,8 +126,7 @@ module.exports = async function (provider) { } pub fn ts_deploy_script() -> &'static str { - r#" -// Migrations are an early feature. Currently, they're nothing more than this + r#"// Migrations are an early feature. Currently, they're nothing more than this // single deploy script that's invoked from the CLI, injecting a provider // configured from the workspace's Anchor.toml. @@ -147,6 +151,8 @@ pub fn lib_rs(name: &str) -> String { format!( r#"use anchor_lang::prelude::*; +declare_id!("{}"); + #[program] pub mod {} {{ use super::*; @@ -158,6 +164,7 @@ pub mod {} {{ #[derive(Accounts)] pub struct Initialize {{}} "#, + default_program_id(), name.to_snake_case(), ) } @@ -194,7 +201,7 @@ pub fn package_json() -> String { "chai": "^4.3.4", "mocha": "^9.0.3" }} -}} +}} "#, VERSION ) @@ -213,7 +220,7 @@ pub fn ts_package_json() -> String { "@types/mocha": "^9.0.0", "typescript": "^4.3.5" }} -}} +}} "#, VERSION ) @@ -261,6 +268,7 @@ pub fn git_ignore() -> &'static str { .DS_Store target **/*.rs.bk +node_modules "# }