Browse Source

cli: Add default program id to init template (#689)

Armani Ferrante 4 years ago
parent
commit
3ee33132f5
3 changed files with 42 additions and 13 deletions
  1. 12 4
      cli/src/config.rs
  2. 14 1
      cli/src/lib.rs
  3. 16 8
      cli/src/template.rs

+ 12 - 4
cli/src/config.rs

@@ -329,12 +329,12 @@ impl Config {
 struct _Config {
     anchor_version: Option<String>,
     solana_version: Option<String>,
+    programs: Option<BTreeMap<String, BTreeMap<String, serde_json::Value>>>,
     registry: Option<RegistryConfig>,
     provider: Provider,
-    test: Option<Test>,
-    scripts: Option<ScriptsConfig>,
-    programs: Option<BTreeMap<String, BTreeMap<String, serde_json::Value>>>,
     workspace: Option<WorkspaceConfig>,
+    scripts: Option<ScriptsConfig>,
+    test: Option<Test>,
 }
 
 #[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::<BTreeMap<String, serde_json::Value>>();
@@ -417,6 +417,14 @@ fn ser_programs(
         })
         .collect::<BTreeMap<String, BTreeMap<String, serde_json::Value>>>()
 }
+
+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<String, BTreeMap<String, serde_json::Value>>,
 ) -> Result<BTreeMap<Cluster, BTreeMap<String, ProgramDeployment>>> {

+ 14 - 1
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())?;

+ 16 - 8
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
 "#
 }