Просмотр исходного кода

cli: Initialize with the correct program id (#2509)

acheron 2 лет назад
Родитель
Сommit
383e440788
3 измененных файлов с 29 добавлено и 14 удалено
  1. 2 1
      CHANGELOG.md
  2. 6 5
      cli/src/lib.rs
  3. 21 8
      cli/src/rust_template.rs

+ 2 - 1
CHANGELOG.md

@@ -19,6 +19,8 @@ The minor version will be incremented upon a breaking change and the patch versi
 - cli: `idl set-buffer`, `idl set-authority` and `idl close` take an option `--print-only`. which prints transaction in a base64 Borsh compatible format but not sent to the cluster. It's helpful when managing authority under a multisig, e.g., a user can create a proposal for a `Custom Instruction` in SPL Governance ([#2486](https://github.com/coral-xyz/anchor/pull/2486)).
 - lang: Add `emit_cpi!` and `#[event_cpi]` macros(behind `event-cpi` feature flag) to store event logs in transaction metadata ([#2438](https://github.com/coral-xyz/anchor/pull/2438)).
 - cli: Add `keys sync` command to sync program id declarations ([#2505](https://github.com/coral-xyz/anchor/pull/2505)).
+- cli: Create new programs with correct program ids ([#2509](https://github.com/coral-xyz/anchor/pull/2509)).
+- cli, client, lang, spl: Update Solana toolchain and dependencies to `1.16.0` and specify maximum version of `<1.17.0` ([#2512](https://github.com/coral-xyz/anchor/pull/2512)).
 
 ### Fixes
 
@@ -26,7 +28,6 @@ The minor version will be incremented upon a breaking change and the patch versi
 - lang: Fix inability to use identifiers `program_id`, `accounts`, `ix_data`, `remaining_accounts` in instruction arguments ([#2464](https://github.com/coral-xyz/anchor/pull/2464))
 - cli: Fix incorrect `metadata.address` generation in IDL after deploying with a custom keypair ([#2485](https://github.com/coral-xyz/anchor/pull/2485))
 - cli: IDL commands no longer hang when the payer doesn't have funds to pay for the transaction fee ([#2492](https://github.com/coral-xyz/anchor/pull/2492))
-- cli, client, lang, spl: Update Solana toolchain and dependencies to `1.16.0` and specify maximum version of `<1.17.0` ([#2512](https://github.com/coral-xyz/anchor/pull/2512)).
 
 ### Breaking
 

+ 6 - 5
cli/src/lib.rs

@@ -642,14 +642,15 @@ fn init(
     }
 
     let mut localnet = BTreeMap::new();
+    let program_id = if solidity {
+        solidity_template::default_program_id()
+    } else {
+        rust_template::get_or_create_program_id(&rust_name)
+    };
     localnet.insert(
         rust_name,
         ProgramDeployment {
-            address: if solidity {
-                solidity_template::default_program_id()
-            } else {
-                rust_template::default_program_id()
-            },
+            address: program_id,
             path: None,
             idl: None,
         },

+ 21 - 8
cli/src/rust_template.rs

@@ -3,13 +3,26 @@ use crate::VERSION;
 use anchor_syn::idl::Idl;
 use anyhow::Result;
 use heck::{ToLowerCamelCase, ToSnakeCase, ToUpperCamelCase};
-use solana_sdk::pubkey::Pubkey;
-use std::fmt::Write;
-
-pub fn default_program_id() -> Pubkey {
-    "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"
-        .parse()
-        .unwrap()
+use solana_sdk::{
+    pubkey::Pubkey,
+    signature::{read_keypair_file, write_keypair_file, Keypair},
+    signer::Signer,
+};
+use std::{fmt::Write, path::Path};
+
+/// Read the program keypair file or create a new one if it doesn't exist.
+pub fn get_or_create_program_id(name: &str) -> Pubkey {
+    let keypair_path = Path::new("target")
+        .join("deploy")
+        .join(format!("{}-keypair.json", name.to_snake_case()));
+
+    read_keypair_file(&keypair_path)
+        .unwrap_or_else(|_| {
+            let keypair = Keypair::new();
+            write_keypair_file(&keypair, keypair_path).expect("Unable to create program keypair");
+            keypair
+        })
+        .pubkey()
 }
 
 pub fn virtual_manifest() -> &'static str {
@@ -192,7 +205,7 @@ pub mod {} {{
 #[derive(Accounts)]
 pub struct Initialize {{}}
 "#,
-        default_program_id(),
+        get_or_create_program_id(name),
         name.to_snake_case(),
     )
 }