cli: Add default program id to init template (#689)
This commit is contained in:
parent
3958533750
commit
3ee33132f5
|
@ -329,12 +329,12 @@ impl Config {
|
||||||
struct _Config {
|
struct _Config {
|
||||||
anchor_version: Option<String>,
|
anchor_version: Option<String>,
|
||||||
solana_version: Option<String>,
|
solana_version: Option<String>,
|
||||||
|
programs: Option<BTreeMap<String, BTreeMap<String, serde_json::Value>>>,
|
||||||
registry: Option<RegistryConfig>,
|
registry: Option<RegistryConfig>,
|
||||||
provider: Provider,
|
provider: Provider,
|
||||||
test: Option<Test>,
|
|
||||||
scripts: Option<ScriptsConfig>,
|
|
||||||
programs: Option<BTreeMap<String, BTreeMap<String, serde_json::Value>>>,
|
|
||||||
workspace: Option<WorkspaceConfig>,
|
workspace: Option<WorkspaceConfig>,
|
||||||
|
scripts: Option<ScriptsConfig>,
|
||||||
|
test: Option<Test>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
@ -409,7 +409,7 @@ fn ser_programs(
|
||||||
.map(|(name, deployment)| {
|
.map(|(name, deployment)| {
|
||||||
(
|
(
|
||||||
name.clone(),
|
name.clone(),
|
||||||
serde_json::to_value(&_ProgramDeployment::from(deployment)).unwrap(),
|
to_value(&_ProgramDeployment::from(deployment)),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.collect::<BTreeMap<String, serde_json::Value>>();
|
.collect::<BTreeMap<String, serde_json::Value>>();
|
||||||
|
@ -417,6 +417,14 @@ fn ser_programs(
|
||||||
})
|
})
|
||||||
.collect::<BTreeMap<String, BTreeMap<String, serde_json::Value>>>()
|
.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(
|
fn deser_programs(
|
||||||
programs: BTreeMap<String, BTreeMap<String, serde_json::Value>>,
|
programs: BTreeMap<String, BTreeMap<String, serde_json::Value>>,
|
||||||
) -> Result<BTreeMap<Cluster, BTreeMap<String, ProgramDeployment>>> {
|
) -> Result<BTreeMap<Cluster, BTreeMap<String, ProgramDeployment>>> {
|
||||||
|
|
|
@ -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_client::Cluster;
|
||||||
use anchor_lang::idl::{IdlAccount, IdlInstruction};
|
use anchor_lang::idl::{IdlAccount, IdlInstruction};
|
||||||
use anchor_lang::{AccountDeserialize, AnchorDeserialize, AnchorSerialize};
|
use anchor_lang::{AccountDeserialize, AnchorDeserialize, AnchorSerialize};
|
||||||
|
@ -23,6 +25,7 @@ use solana_sdk::signature::Keypair;
|
||||||
use solana_sdk::signature::Signer;
|
use solana_sdk::signature::Signer;
|
||||||
use solana_sdk::sysvar;
|
use solana_sdk::sysvar;
|
||||||
use solana_sdk::transaction::Transaction;
|
use solana_sdk::transaction::Transaction;
|
||||||
|
use std::collections::BTreeMap;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs::{self, File};
|
use std::fs::{self, File};
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
|
@ -314,6 +317,16 @@ fn init(cfg_override: &ConfigOverride, name: String, typescript: bool) -> Result
|
||||||
}
|
}
|
||||||
.to_owned(),
|
.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 toml = cfg.to_string();
|
||||||
let mut file = File::create("Anchor.toml")?;
|
let mut file = File::create("Anchor.toml")?;
|
||||||
file.write_all(toml.as_bytes())?;
|
file.write_all(toml.as_bytes())?;
|
||||||
|
|
|
@ -2,6 +2,13 @@ use crate::config::ProgramWorkspace;
|
||||||
use crate::VERSION;
|
use crate::VERSION;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use heck::{CamelCase, SnakeCase};
|
use heck::{CamelCase, SnakeCase};
|
||||||
|
use solana_sdk::pubkey::Pubkey;
|
||||||
|
|
||||||
|
pub fn default_program_id() -> Pubkey {
|
||||||
|
"Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"
|
||||||
|
.parse()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn virtual_manifest() -> &'static str {
|
pub fn virtual_manifest() -> &'static str {
|
||||||
r#"[workspace]
|
r#"[workspace]
|
||||||
|
@ -77,8 +84,7 @@ main();
|
||||||
|
|
||||||
pub fn deploy_ts_script_host(cluster_url: &str, script_path: &str) -> String {
|
pub fn deploy_ts_script_host(cluster_url: &str, script_path: &str) -> String {
|
||||||
format!(
|
format!(
|
||||||
r#"
|
r#"import * as anchor from '@project-serum/anchor';
|
||||||
import * as anchor from '@project-serum/anchor';
|
|
||||||
|
|
||||||
// Deploy script defined by the user.
|
// Deploy script defined by the user.
|
||||||
const userScript = require("{0}");
|
const userScript = require("{0}");
|
||||||
|
@ -104,8 +110,7 @@ main();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deploy_script() -> &'static str {
|
pub fn deploy_script() -> &'static str {
|
||||||
r#"
|
r#"// Migrations are an early feature. Currently, they're nothing more than this
|
||||||
// Migrations are an early feature. Currently, they're nothing more than this
|
|
||||||
// single deploy script that's invoked from the CLI, injecting a provider
|
// single deploy script that's invoked from the CLI, injecting a provider
|
||||||
// configured from the workspace's Anchor.toml.
|
// configured from the workspace's Anchor.toml.
|
||||||
|
|
||||||
|
@ -121,8 +126,7 @@ module.exports = async function (provider) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ts_deploy_script() -> &'static str {
|
pub fn ts_deploy_script() -> &'static str {
|
||||||
r#"
|
r#"// Migrations are an early feature. Currently, they're nothing more than this
|
||||||
// Migrations are an early feature. Currently, they're nothing more than this
|
|
||||||
// single deploy script that's invoked from the CLI, injecting a provider
|
// single deploy script that's invoked from the CLI, injecting a provider
|
||||||
// configured from the workspace's Anchor.toml.
|
// configured from the workspace's Anchor.toml.
|
||||||
|
|
||||||
|
@ -147,6 +151,8 @@ pub fn lib_rs(name: &str) -> String {
|
||||||
format!(
|
format!(
|
||||||
r#"use anchor_lang::prelude::*;
|
r#"use anchor_lang::prelude::*;
|
||||||
|
|
||||||
|
declare_id!("{}");
|
||||||
|
|
||||||
#[program]
|
#[program]
|
||||||
pub mod {} {{
|
pub mod {} {{
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -158,6 +164,7 @@ pub mod {} {{
|
||||||
#[derive(Accounts)]
|
#[derive(Accounts)]
|
||||||
pub struct Initialize {{}}
|
pub struct Initialize {{}}
|
||||||
"#,
|
"#,
|
||||||
|
default_program_id(),
|
||||||
name.to_snake_case(),
|
name.to_snake_case(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -194,7 +201,7 @@ pub fn package_json() -> String {
|
||||||
"chai": "^4.3.4",
|
"chai": "^4.3.4",
|
||||||
"mocha": "^9.0.3"
|
"mocha": "^9.0.3"
|
||||||
}}
|
}}
|
||||||
}}
|
}}
|
||||||
"#,
|
"#,
|
||||||
VERSION
|
VERSION
|
||||||
)
|
)
|
||||||
|
@ -213,7 +220,7 @@ pub fn ts_package_json() -> String {
|
||||||
"@types/mocha": "^9.0.0",
|
"@types/mocha": "^9.0.0",
|
||||||
"typescript": "^4.3.5"
|
"typescript": "^4.3.5"
|
||||||
}}
|
}}
|
||||||
}}
|
}}
|
||||||
"#,
|
"#,
|
||||||
VERSION
|
VERSION
|
||||||
)
|
)
|
||||||
|
@ -261,6 +268,7 @@ pub fn git_ignore() -> &'static str {
|
||||||
.DS_Store
|
.DS_Store
|
||||||
target
|
target
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
|
node_modules
|
||||||
"#
|
"#
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue