2021-05-24 16:04:17 -07:00
|
|
|
use crate::ConfigOverride;
|
2021-05-22 16:06:08 -07:00
|
|
|
use anchor_client::Cluster;
|
2021-01-02 22:40:17 -08:00
|
|
|
use anchor_syn::idl::Idl;
|
|
|
|
use anyhow::{anyhow, Error, Result};
|
|
|
|
use serde::{Deserialize, Serialize};
|
2021-05-22 16:06:08 -07:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2021-04-09 10:33:59 -07:00
|
|
|
use solana_sdk::signature::Keypair;
|
2021-05-22 16:06:08 -07:00
|
|
|
use std::collections::BTreeMap;
|
2021-06-27 13:17:05 -07:00
|
|
|
use std::convert::TryFrom;
|
2021-01-02 22:40:17 -08:00
|
|
|
use std::fs::{self, File};
|
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
2021-02-22 09:41:40 -08:00
|
|
|
#[derive(Debug, Default)]
|
2021-01-02 22:40:17 -08:00
|
|
|
pub struct Config {
|
2021-05-23 10:00:24 -07:00
|
|
|
pub provider: ProviderConfig,
|
|
|
|
pub clusters: ClustersConfig,
|
2021-06-27 13:17:05 -07:00
|
|
|
pub scripts: ScriptsConfig,
|
2021-05-23 10:00:24 -07:00
|
|
|
pub test: Option<Test>,
|
2021-07-25 17:25:40 -07:00
|
|
|
pub workspace: WorkspaceConfig,
|
2021-05-23 10:00:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct ProviderConfig {
|
2021-01-04 12:27:35 -08:00
|
|
|
pub cluster: Cluster,
|
|
|
|
pub wallet: WalletPath,
|
2021-01-02 22:40:17 -08:00
|
|
|
}
|
|
|
|
|
2021-06-27 13:17:05 -07:00
|
|
|
pub type ScriptsConfig = BTreeMap<String, String>;
|
|
|
|
|
2021-05-23 10:00:24 -07:00
|
|
|
pub type ClustersConfig = BTreeMap<Cluster, BTreeMap<String, ProgramDeployment>>;
|
|
|
|
|
2021-07-25 17:25:40 -07:00
|
|
|
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct WorkspaceConfig {
|
|
|
|
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
|
|
|
pub members: Vec<String>,
|
|
|
|
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
|
|
|
pub exclude: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2021-01-02 22:40:17 -08:00
|
|
|
impl Config {
|
2021-05-24 16:04:17 -07:00
|
|
|
pub fn discover(
|
|
|
|
cfg_override: &ConfigOverride,
|
|
|
|
) -> Result<Option<(Self, PathBuf, Option<PathBuf>)>> {
|
|
|
|
Config::_discover().map(|opt| {
|
|
|
|
opt.map(|(mut cfg, cfg_path, cargo_toml)| {
|
|
|
|
if let Some(cluster) = cfg_override.cluster.clone() {
|
|
|
|
cfg.provider.cluster = cluster;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(wallet) = cfg_override.wallet.clone() {
|
|
|
|
cfg.provider.wallet = wallet;
|
|
|
|
}
|
|
|
|
(cfg, cfg_path, cargo_toml)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-02 22:40:17 -08:00
|
|
|
// Searches all parent directories for an Anchor.toml file.
|
2021-05-24 16:04:17 -07:00
|
|
|
fn _discover() -> Result<Option<(Self, PathBuf, Option<PathBuf>)>> {
|
2021-01-02 22:40:17 -08:00
|
|
|
// Set to true if we ever see a Cargo.toml file when traversing the
|
|
|
|
// parent directories.
|
|
|
|
let mut cargo_toml = None;
|
|
|
|
|
|
|
|
let _cwd = std::env::current_dir()?;
|
|
|
|
let mut cwd_opt = Some(_cwd.as_path());
|
|
|
|
|
|
|
|
while let Some(cwd) = cwd_opt {
|
|
|
|
let files = fs::read_dir(cwd)?;
|
|
|
|
// Cargo.toml file for this directory level.
|
|
|
|
let mut cargo_toml_level = None;
|
|
|
|
let mut anchor_toml = None;
|
|
|
|
for f in files {
|
|
|
|
let p = f?.path();
|
|
|
|
if let Some(filename) = p.file_name() {
|
|
|
|
if filename.to_str() == Some("Cargo.toml") {
|
2021-02-15 21:52:54 -08:00
|
|
|
cargo_toml_level = Some(p);
|
2021-01-02 22:40:17 -08:00
|
|
|
} else if filename.to_str() == Some("Anchor.toml") {
|
|
|
|
let mut cfg_file = File::open(&p)?;
|
|
|
|
let mut cfg_contents = String::new();
|
|
|
|
cfg_file.read_to_string(&mut cfg_contents)?;
|
|
|
|
let cfg = cfg_contents.parse()?;
|
2021-02-15 21:52:54 -08:00
|
|
|
anchor_toml = Some((cfg, p));
|
2021-01-02 22:40:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some((cfg, parent)) = anchor_toml {
|
|
|
|
return Ok(Some((cfg, parent, cargo_toml)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if cargo_toml.is_none() {
|
|
|
|
cargo_toml = cargo_toml_level;
|
|
|
|
}
|
|
|
|
|
|
|
|
cwd_opt = cwd.parent();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|
2021-04-09 10:33:59 -07:00
|
|
|
|
|
|
|
pub fn wallet_kp(&self) -> Result<Keypair> {
|
2021-05-23 10:00:24 -07:00
|
|
|
solana_sdk::signature::read_keypair_file(&self.provider.wallet.to_string())
|
2021-04-09 10:33:59 -07:00
|
|
|
.map_err(|_| anyhow!("Unable to read keypair file"))
|
|
|
|
}
|
2021-07-25 17:25:40 -07:00
|
|
|
|
|
|
|
pub fn get_program_list(&self, path: PathBuf) -> Result<Vec<PathBuf>> {
|
|
|
|
let mut programs = vec![];
|
|
|
|
for f in fs::read_dir(path)? {
|
|
|
|
let path = f?.path();
|
|
|
|
let program = path
|
|
|
|
.components()
|
|
|
|
.last()
|
|
|
|
.map(|c| c.as_os_str().to_string_lossy().into_owned())
|
|
|
|
.expect("failed to get program from path");
|
|
|
|
|
|
|
|
match (
|
|
|
|
self.workspace.members.is_empty(),
|
|
|
|
self.workspace.exclude.is_empty(),
|
|
|
|
) {
|
|
|
|
(true, true) => programs.push(path),
|
|
|
|
(true, false) => {
|
|
|
|
if !self.workspace.exclude.contains(&program) {
|
|
|
|
programs.push(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(false, _) => {
|
|
|
|
if self.workspace.members.contains(&program) {
|
|
|
|
programs.push(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(programs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: this should read idl dir instead of parsing source.
|
|
|
|
pub fn read_all_programs(&self) -> Result<Vec<Program>> {
|
|
|
|
let mut r = vec![];
|
|
|
|
for path in self.get_program_list("programs".into())? {
|
|
|
|
let idl = anchor_syn::idl::file::parse(path.join("src/lib.rs"))?;
|
|
|
|
let lib_name = extract_lib_name(&path.join("Cargo.toml"))?;
|
|
|
|
r.push(Program {
|
|
|
|
lib_name,
|
|
|
|
path,
|
|
|
|
idl,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Ok(r)
|
|
|
|
}
|
2021-01-02 22:40:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pubkey serializes as a byte array so use this type a hack to serialize
|
|
|
|
// into base 58 strings.
|
2021-02-22 09:41:40 -08:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2021-01-02 22:40:17 -08:00
|
|
|
struct _Config {
|
2021-05-23 10:00:24 -07:00
|
|
|
provider: Provider,
|
2021-02-22 09:41:40 -08:00
|
|
|
test: Option<Test>,
|
2021-06-27 13:17:05 -07:00
|
|
|
scripts: Option<ScriptsConfig>,
|
|
|
|
clusters: Option<BTreeMap<String, BTreeMap<String, serde_json::Value>>>,
|
2021-07-25 17:25:40 -07:00
|
|
|
workspace: Option<WorkspaceConfig>,
|
2021-01-02 22:40:17 -08:00
|
|
|
}
|
|
|
|
|
2021-05-23 10:00:24 -07:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
struct Provider {
|
|
|
|
cluster: String,
|
|
|
|
wallet: String,
|
|
|
|
}
|
|
|
|
|
2021-01-02 22:40:17 -08:00
|
|
|
impl ToString for Config {
|
|
|
|
fn to_string(&self) -> String {
|
2021-05-22 16:06:08 -07:00
|
|
|
let clusters = {
|
|
|
|
let c = ser_clusters(&self.clusters);
|
2021-06-27 17:09:46 -07:00
|
|
|
if c.is_empty() {
|
2021-05-22 16:06:08 -07:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(c)
|
|
|
|
}
|
|
|
|
};
|
2021-01-02 22:40:17 -08:00
|
|
|
let cfg = _Config {
|
2021-05-23 10:00:24 -07:00
|
|
|
provider: Provider {
|
|
|
|
cluster: format!("{}", self.provider.cluster),
|
|
|
|
wallet: self.provider.wallet.to_string(),
|
|
|
|
},
|
2021-02-22 09:41:40 -08:00
|
|
|
test: self.test.clone(),
|
2021-06-27 13:17:05 -07:00
|
|
|
scripts: match self.scripts.is_empty() {
|
|
|
|
true => None,
|
|
|
|
false => Some(self.scripts.clone()),
|
|
|
|
},
|
2021-05-22 16:06:08 -07:00
|
|
|
clusters,
|
2021-07-28 14:04:36 -07:00
|
|
|
workspace: (!self.workspace.members.is_empty() || !self.workspace.exclude.is_empty())
|
|
|
|
.then(|| self.workspace.clone()),
|
2021-01-02 22:40:17 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
toml::to_string(&cfg).expect("Must be well formed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for Config {
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
let cfg: _Config = toml::from_str(s)
|
|
|
|
.map_err(|e| anyhow::format_err!("Unable to deserialize config: {}", e.to_string()))?;
|
|
|
|
Ok(Config {
|
2021-05-23 10:00:24 -07:00
|
|
|
provider: ProviderConfig {
|
|
|
|
cluster: cfg.provider.cluster.parse()?,
|
|
|
|
wallet: shellexpand::tilde(&cfg.provider.wallet).parse()?,
|
|
|
|
},
|
2021-06-27 17:09:46 -07:00
|
|
|
scripts: cfg.scripts.unwrap_or_else(BTreeMap::new),
|
2021-02-22 09:41:40 -08:00
|
|
|
test: cfg.test,
|
2021-06-27 17:09:46 -07:00
|
|
|
clusters: cfg.clusters.map_or(Ok(BTreeMap::new()), deser_clusters)?,
|
2021-07-25 17:25:40 -07:00
|
|
|
workspace: cfg.workspace.map(|workspace| {
|
|
|
|
let (members, exclude) = match (workspace.members.is_empty(), workspace.exclude.is_empty()) {
|
|
|
|
(true, true) => (vec![], vec![]),
|
|
|
|
(true, false) => (vec![], workspace.exclude),
|
|
|
|
(false, is_empty) => {
|
|
|
|
if !is_empty {
|
|
|
|
println!("Fields `members` and `exclude` in `[workspace]` section are not compatible, only `members` will be used.");
|
|
|
|
}
|
|
|
|
(workspace.members, vec![])
|
|
|
|
}
|
|
|
|
};
|
|
|
|
WorkspaceConfig { members, exclude }
|
|
|
|
}).unwrap_or_default()
|
2021-01-02 22:40:17 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 16:06:08 -07:00
|
|
|
fn ser_clusters(
|
|
|
|
clusters: &BTreeMap<Cluster, BTreeMap<String, ProgramDeployment>>,
|
2021-06-27 13:17:05 -07:00
|
|
|
) -> BTreeMap<String, BTreeMap<String, serde_json::Value>> {
|
2021-05-22 16:06:08 -07:00
|
|
|
clusters
|
|
|
|
.iter()
|
|
|
|
.map(|(cluster, programs)| {
|
|
|
|
let cluster = cluster.to_string();
|
|
|
|
let programs = programs
|
|
|
|
.iter()
|
2021-06-27 13:17:05 -07:00
|
|
|
.map(|(name, deployment)| {
|
|
|
|
(
|
|
|
|
name.clone(),
|
|
|
|
serde_json::to_value(&_ProgramDeployment::from(deployment)).unwrap(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<BTreeMap<String, serde_json::Value>>();
|
2021-05-22 16:06:08 -07:00
|
|
|
(cluster, programs)
|
|
|
|
})
|
2021-06-27 13:17:05 -07:00
|
|
|
.collect::<BTreeMap<String, BTreeMap<String, serde_json::Value>>>()
|
2021-05-22 16:06:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn deser_clusters(
|
2021-06-27 13:17:05 -07:00
|
|
|
clusters: BTreeMap<String, BTreeMap<String, serde_json::Value>>,
|
2021-05-22 16:06:08 -07:00
|
|
|
) -> Result<BTreeMap<Cluster, BTreeMap<String, ProgramDeployment>>> {
|
|
|
|
clusters
|
|
|
|
.iter()
|
|
|
|
.map(|(cluster, programs)| {
|
|
|
|
let cluster: Cluster = cluster.parse()?;
|
|
|
|
let programs = programs
|
|
|
|
.iter()
|
|
|
|
.map(|(name, program_id)| {
|
|
|
|
Ok((
|
|
|
|
name.clone(),
|
2021-06-27 13:17:05 -07:00
|
|
|
ProgramDeployment::try_from(match &program_id {
|
|
|
|
serde_json::Value::String(address) => _ProgramDeployment {
|
|
|
|
address: address.parse()?,
|
|
|
|
idl: None,
|
|
|
|
},
|
|
|
|
serde_json::Value::Object(_) => {
|
|
|
|
serde_json::from_value(program_id.clone())
|
|
|
|
.map_err(|_| anyhow!("Unable to read toml"))?
|
|
|
|
}
|
|
|
|
_ => return Err(anyhow!("Invalid toml type")),
|
|
|
|
})?,
|
2021-05-22 16:06:08 -07:00
|
|
|
))
|
|
|
|
})
|
|
|
|
.collect::<Result<BTreeMap<String, ProgramDeployment>>>()?;
|
|
|
|
Ok((cluster, programs))
|
|
|
|
})
|
|
|
|
.collect::<Result<BTreeMap<Cluster, BTreeMap<String, ProgramDeployment>>>>()
|
|
|
|
}
|
|
|
|
|
2021-02-22 09:41:40 -08:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct Test {
|
|
|
|
pub genesis: Vec<GenesisEntry>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct GenesisEntry {
|
|
|
|
// Base58 pubkey string.
|
|
|
|
pub address: String,
|
|
|
|
// Filepath to the compiled program to embed into the genesis.
|
|
|
|
pub program: String,
|
|
|
|
}
|
|
|
|
|
2021-01-02 22:40:17 -08:00
|
|
|
pub fn extract_lib_name(path: impl AsRef<Path>) -> Result<String> {
|
|
|
|
let mut toml = File::open(path)?;
|
|
|
|
let mut contents = String::new();
|
|
|
|
toml.read_to_string(&mut contents)?;
|
|
|
|
|
|
|
|
let cargo_toml: toml::Value = contents.parse()?;
|
|
|
|
|
|
|
|
match cargo_toml {
|
|
|
|
toml::Value::Table(t) => match t.get("lib") {
|
|
|
|
None => Err(anyhow!("lib not found in Cargo.toml")),
|
|
|
|
Some(lib) => match lib
|
|
|
|
.get("name")
|
2021-02-15 21:52:54 -08:00
|
|
|
.ok_or_else(|| anyhow!("lib name not found in Cargo.toml"))?
|
2021-01-02 22:40:17 -08:00
|
|
|
{
|
|
|
|
toml::Value::String(n) => Ok(n.to_string()),
|
|
|
|
_ => Err(anyhow!("lib name must be a string")),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
_ => Err(anyhow!("Invalid Cargo.toml")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 06:19:00 -08:00
|
|
|
#[derive(Debug, Clone)]
|
2021-01-02 22:40:17 -08:00
|
|
|
pub struct Program {
|
|
|
|
pub lib_name: String,
|
|
|
|
pub path: PathBuf,
|
|
|
|
pub idl: Idl,
|
|
|
|
}
|
|
|
|
|
2021-01-31 00:59:01 -08:00
|
|
|
impl Program {
|
|
|
|
pub fn anchor_keypair_path(&self) -> PathBuf {
|
|
|
|
std::env::current_dir()
|
|
|
|
.expect("Must have current dir")
|
|
|
|
.join(format!(
|
|
|
|
"target/deploy/anchor-{}-keypair.json",
|
|
|
|
self.lib_name
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn binary_path(&self) -> PathBuf {
|
|
|
|
std::env::current_dir()
|
|
|
|
.expect("Must have current dir")
|
|
|
|
.join(format!("target/deploy/{}.so", self.lib_name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 16:06:08 -07:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct ProgramDeployment {
|
2021-06-27 13:17:05 -07:00
|
|
|
pub address: Pubkey,
|
|
|
|
pub idl: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<_ProgramDeployment> for ProgramDeployment {
|
|
|
|
type Error = anyhow::Error;
|
|
|
|
fn try_from(pd: _ProgramDeployment) -> Result<Self, Self::Error> {
|
|
|
|
Ok(ProgramDeployment {
|
|
|
|
address: pd.address.parse()?,
|
|
|
|
idl: pd.idl,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default, Serialize, Deserialize)]
|
|
|
|
pub struct _ProgramDeployment {
|
|
|
|
pub address: String,
|
|
|
|
pub idl: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&ProgramDeployment> for _ProgramDeployment {
|
|
|
|
fn from(pd: &ProgramDeployment) -> Self {
|
|
|
|
Self {
|
|
|
|
address: pd.address.to_string(),
|
|
|
|
idl: pd.idl.clone(),
|
|
|
|
}
|
|
|
|
}
|
2021-05-22 16:06:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ProgramWorkspace {
|
|
|
|
pub name: String,
|
|
|
|
pub program_id: Pubkey,
|
|
|
|
pub idl: Idl,
|
|
|
|
}
|
|
|
|
|
2021-01-02 22:40:17 -08:00
|
|
|
serum_common::home_path!(WalletPath, ".config/solana/id.json");
|