Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy committed Dec 18, 2024
1 parent 1a978cf commit 8e2b08d
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "crates/katana/contracts/messaging/solidity/lib/forge-std"]
path = crates/katana/contracts/messaging/solidity/lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "crates/katana/contracts/piltover"]
path = crates/katana/contracts/piltover
url = https://github.com/cartridge-gg/piltover.git
4 changes: 4 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions bin/katana/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ repository.workspace = true
version.workspace = true

[dependencies]
katana-cairo.workspace = true
katana-cli.workspace = true
katana-db.workspace = true
katana-node.workspace = true
katana-primitives.workspace = true

anyhow.workspace = true
byte-unit = "5.1.4"
clap.workspace = true
clap_complete.workspace = true
comfy-table = "7.1.1"
dojo-utils.workspace = true
serde_json.workspace = true
shellexpand = "3.1.0"
starknet.workspace = true

[dev-dependencies]
assert_matches.workspace = true
Expand Down
105 changes: 105 additions & 0 deletions bin/katana/src/cli/init/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use anyhow::{anyhow, Result};
use clap::Args;
use dojo_utils::TransactionWaiter;
use katana_cairo::lang::starknet_classes::casm_contract_class::CasmContractClass;
use katana_cairo::lang::starknet_classes::contract_class::ContractClass;
use katana_primitives::{felt, ContractAddress, Felt};
use starknet::accounts::{Account, ConnectedAccount, ExecutionEncoding, SingleOwnerAccount};
use starknet::contract::ContractFactory;
use starknet::core::types::contract::{CompiledClass, SierraClass};
use starknet::core::types::FlattenedSierraClass;
use starknet::providers::jsonrpc::HttpTransport;
use starknet::providers::{JsonRpcClient, Url};
use starknet::signers::{LocalWallet, SigningKey};

struct InitOutcome {
// - The token that will be used to pay for tx fee in the appchain.
// - For now, this must be the native token that is used to pay for tx fee in the settlement chain.
fee_token: ContractAddress,

// - The bridge contract for bridging the fee token to the appchain.
// - This will be part of the initialization process.
bridge_contract: ContractAddress,

settlement_contract: ContractAddress,
}

#[derive(Args)]
pub struct InitArgs {
// wallet
}

impl InitArgs {
pub(crate) fn execute(self) -> Result<()> {
let address = felt!("0x123");
let pk = SigningKey::from_secret_scalar(felt!("0xdeadbeef"));

let provider = JsonRpcClient::new(HttpTransport::new(Url::parse("http://localhost:5050")?));
let account = SingleOwnerAccount::new(
provider,
LocalWallet::from_signing_key(pk),
address,
Felt::ONE,
ExecutionEncoding::New,
);

let _contract_address = deploy_core_contracts(&account);

// TODO: deploy bridge contract

Ok(())
}
}

async fn deploy_core_contracts(
account: &SingleOwnerAccount<JsonRpcClient<HttpTransport>, LocalWallet>,
) -> Result<ContractAddress> {
let class =
include_str!("../../../../../crates/katana/contracts/build/appchain_core_contract.json");
let (contract, compiled_class_hash) = prepare_contract_declaration_params(class)?;

let class_hash = contract.class_hash();
let res = account.declare_v2(contract.into(), compiled_class_hash).send().await?;
let _ = TransactionWaiter::new(res.transaction_hash, account.provider()).await?;

let factory = ContractFactory::new(class_hash, &account);

// appchain::constructor() https://github.com/cartridge-gg/piltover/blob/d373a844c3428383a48518adf468bf83249dec3a/src/appchain.cairo#L119-L125
let request = factory.deploy_v3(
vec![
account.address(), // owner
Felt::ZERO, // state_root
Felt::ZERO, // block_number
Felt::ZERO, // block_hash
],
Felt::ZERO,
false,
);

let res = request.send().await?;
let _ = TransactionWaiter::new(res.transaction_hash, account.provider()).await?;

Ok(request.deployed_address().into())
}

pub fn prepare_contract_declaration_params(artifact: &str) -> Result<(FlattenedSierraClass, Felt)> {
let flattened_class = get_flattened_class(artifact)
.map_err(|e| anyhow!("error flattening the contract class: {e}"))?;
let compiled_class_hash = get_compiled_class_hash(artifact)
.map_err(|e| anyhow!("error computing compiled class hash: {e}"))?;
Ok((flattened_class, compiled_class_hash))
}

fn get_flattened_class(artifact: &str) -> Result<FlattenedSierraClass> {
let contract_artifact: SierraClass = serde_json::from_str(artifact)?;
Ok(contract_artifact.flatten()?)
}

fn get_compiled_class_hash(artifact: &str) -> Result<Felt> {
let casm_contract_class: ContractClass = serde_json::from_str(artifact)?;
let casm_contract =
CasmContractClass::from_contract_class(casm_contract_class, true, usize::MAX)?;
let res = serde_json::to_string(&casm_contract)?;
let compiled_class: CompiledClass = serde_json::from_str(&res)?;
Ok(compiled_class.class_hash()?)
}
13 changes: 9 additions & 4 deletions bin/katana/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
mod db;

use anyhow::Result;
use clap::{Args, CommandFactory, Parser, Subcommand};
use clap_complete::Shell;
use katana_cli::NodeArgs;
use katana_node::version::VERSION;

mod db;
mod init;

#[derive(Parser)]
#[command(name = "katana", author, version = VERSION, about, long_about = None)]
pub struct Cli {
Expand All @@ -22,6 +23,7 @@ impl Cli {
return match cmd {
Commands::Completions(args) => args.execute(),
Commands::Db(args) => args.execute(),
Commands::Init(args) => args.execute(),
};
}

Expand All @@ -31,11 +33,14 @@ impl Cli {

#[derive(Subcommand)]
enum Commands {
#[command(about = "Generate shell completion file for specified shell")]
Completions(CompletionsArgs),
#[command(about = "Initialize chain")]
Init(init::InitArgs),

#[command(about = "Database utilities")]
Db(db::DbArgs),

#[command(about = "Generate shell completion file for specified shell")]
Completions(CompletionsArgs),
}

#[derive(Debug, Args)]
Expand Down
9 changes: 9 additions & 0 deletions crates/katana/contracts/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@ $(BUILD_DIR): ./account/src/*
scarb build -p katana_account
mv target/dev/$(ORIGINAL_CLASS_NAME) $(BUILD_DIR)/$(CLASS_NAME)

## ---- Piltover contracts

ORIGINAL_CLASS_NAME := piltover_appchain$(CONTRACT_CLASS_SUFFIX)
CLASS_NAME := appchain_core_contract.json

$(BUILD_DIR): ./piltover/src/*
cd piltover && scarb build
mv target/dev/$(ORIGINAL_CLASS_NAME) $(BUILD_DIR)/$(CLASS_NAME)

## ----
1 change: 1 addition & 0 deletions crates/katana/contracts/build/appchain_core_contract.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crates/katana/contracts/piltover
Submodule piltover added at d373a8

0 comments on commit 8e2b08d

Please sign in to comment.