Skip to content

Commit

Permalink
cli: Initialize with the correct program id (coral-xyz#2509)
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto authored Jun 4, 2023
1 parent 835dc5b commit 383e440
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ 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

- ts: Narrowed `AccountClient` type to it's appropriate account type ([#2440](https://github.com/coral-xyz/anchor/pull/2440))
- 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

Expand Down
11 changes: 6 additions & 5 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
29 changes: 21 additions & 8 deletions cli/src/rust_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -192,7 +205,7 @@ pub mod {} {{
#[derive(Accounts)]
pub struct Initialize {{}}
"#,
default_program_id(),
get_or_create_program_id(name),
name.to_snake_case(),
)
}
Expand Down

0 comments on commit 383e440

Please sign in to comment.