Skip to content

Commit

Permalink
Implement retrieving predeployed accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
broody committed Feb 15, 2024
1 parent 08819c5 commit c68ece8
Show file tree
Hide file tree
Showing 8 changed files with 368 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ katana-primitives = { git = "https://github.com/dojoengine/dojo", rev = "c83936"
] }
anyhow = "1.0.75"
axum = "0.6"
base64 = "0.21.2"
clap = { version = "4.2", features = ["derive"] }
chrono = "0.4.31"
ctrlc = "3.4.1"
Expand Down
192 changes: 191 additions & 1 deletion schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -11569,7 +11569,7 @@
},
{
"defaultValue": null,
"description": "Base64 encoded genesis file with all the classes embedded in the file itself (if any).",
"description": null,
"name": "genesis",
"type": {
"kind": "SCALAR",
Expand Down Expand Up @@ -11869,6 +11869,22 @@
}
}
},
{
"args": [],
"deprecationReason": null,
"description": null,
"isDeprecated": false,
"name": "region",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
}
},
{
"args": [],
"deprecationReason": null,
Expand Down Expand Up @@ -12982,6 +12998,152 @@
}
}
},
{
"defaultValue": null,
"description": "region field predicates",
"name": "region",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
{
"defaultValue": null,
"description": null,
"name": "regionNEQ",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
{
"defaultValue": null,
"description": null,
"name": "regionIn",
"type": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
}
}
},
{
"defaultValue": null,
"description": null,
"name": "regionNotIn",
"type": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
}
}
},
{
"defaultValue": null,
"description": null,
"name": "regionGT",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
{
"defaultValue": null,
"description": null,
"name": "regionGTE",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
{
"defaultValue": null,
"description": null,
"name": "regionLT",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
{
"defaultValue": null,
"description": null,
"name": "regionLTE",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
{
"defaultValue": null,
"description": null,
"name": "regionContains",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
{
"defaultValue": null,
"description": null,
"name": "regionHasPrefix",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
{
"defaultValue": null,
"description": null,
"name": "regionHasSuffix",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
{
"defaultValue": null,
"description": null,
"name": "regionEqualFold",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
{
"defaultValue": null,
"description": null,
"name": "regionContainsFold",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
{
"defaultValue": null,
"description": "auto_upgrade field predicates",
Expand Down Expand Up @@ -18404,6 +18566,34 @@
"ofType": null
}
},
{
"args": [],
"deprecationReason": null,
"description": null,
"isDeprecated": false,
"name": "seed",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
}
},
{
"args": [],
"deprecationReason": null,
"description": null,
"isDeprecated": false,
"name": "genesis",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
{
"args": [],
"deprecationReason": null,
Expand Down
15 changes: 15 additions & 0 deletions src/command/deployments/accounts.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
query KatanaAccounts($project: String!) {
deployment(name: $project, service: katana) {
project
branch
tier
config {
__typename
... on KatanaConfig {
seed
genesis
accounts
}
}
}
}
142 changes: 142 additions & 0 deletions src/command/deployments/accounts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#![allow(clippy::enum_variant_names)]

use anyhow::Result;
use base64::prelude::*;
use clap::Args;
use graphql_client::{GraphQLQuery, Response};
use katana_primitives::contract::ContractAddress;
use katana_primitives::genesis::allocation::DevAllocationsGenerator;
use katana_primitives::genesis::Genesis;
use katana_primitives::genesis::{allocation::GenesisAccountAlloc, json::GenesisJson};
use std::str::FromStr;

use crate::api::ApiClient;

use super::services::KatanaAccountCommands;

use self::katana_accounts::{
KatanaAccountsDeploymentConfig::KatanaConfig, ResponseData, Variables,
};

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "schema.json",
query_path = "src/command/deployments/accounts.graphql",
response_derives = "Debug"
)]
pub struct KatanaAccounts;

#[derive(Debug, Args)]
#[command(next_help_heading = "Accounts options")]
pub struct AccountsArgs {
#[arg(help = "The name of the project.")]
pub project: String,

#[command(subcommand)]
accounts_commands: KatanaAccountCommands,
}

impl AccountsArgs {
pub async fn run(&self) -> Result<()> {
let request_body = KatanaAccounts::build_query(Variables {
project: self.project.clone(),
});

let client = ApiClient::new();
let res: Response<ResponseData> = client.post(&request_body).await?;
if let Some(errors) = res.errors.clone() {
for err in errors {
println!("Error: {}", err.message);
}
}

if let Some(data) = res.data {
if let Some(deployment) = data.deployment {
if let KatanaConfig(config) = deployment.config {
// genesis overrides seed
if let Some(genesis) = config.genesis {
let decoded = BASE64_STANDARD.decode(genesis)?;
let json = GenesisJson::from_str(&String::from_utf8(decoded)?)?;
let genesis = Genesis::try_from(json)?;
print_genesis_accounts(genesis.accounts().peekable(), None);

return Ok(());
}

if !config.seed.is_empty() {
let total = match config.accounts {
Some(accounts) => accounts as u16,
None => 10,
};

let accounts = DevAllocationsGenerator::new(total)
.with_seed(parse_seed(&config.seed))
.generate();

let mut genesis = Genesis::default();
genesis
.extend_allocations(accounts.into_iter().map(|(k, v)| (k, v.into())));
print_genesis_accounts(genesis.accounts().peekable(), Some(&config.seed));
}
}
}
}

Ok(())
}
}

fn print_genesis_accounts<'a, Accounts>(accounts: Accounts, seed: Option<&str>)
where
Accounts: Iterator<Item = (&'a ContractAddress, &'a GenesisAccountAlloc)>,
{
println!(
r"
PREFUNDED ACCOUNTS
=================="
);

for (addr, account) in accounts {
if let Some(pk) = account.private_key() {
println!(
r"
| Account address | {addr}
| Private key | {pk:#x}
| Public key | {:#x}",
account.public_key()
)
} else {
println!(
r"
| Account address | {addr}
| Public key | {:#x}",
account.public_key()
)
}
}

if let Some(seed) = seed {
println!(
r"
ACCOUNTS SEED
=============
{seed}
"
);
}
}

fn parse_seed(seed: &str) -> [u8; 32] {
let seed = seed.as_bytes();

if seed.len() >= 32 {
unsafe { *(seed[..32].as_ptr() as *const [u8; 32]) }
} else {
let mut actual_seed = [0u8; 32];
seed.iter()
.enumerate()
.for_each(|(i, b)| actual_seed[i] = *b);
actual_seed
}
}
Loading

0 comments on commit c68ece8

Please sign in to comment.