Skip to content

Commit

Permalink
add working example
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Sep 21, 2023
1 parent bfc22aa commit 7c34d00
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 30 deletions.
29 changes: 17 additions & 12 deletions Cargo.lock

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

10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "ark-rs"
name = "arkproject"
version = "0.1.0"
edition = "2021"
authors = ["Screenshot Labs", "Starknet community"]
Expand Down Expand Up @@ -27,20 +27,26 @@ ark-starknet = { path = "./crates/ark-starknet" }
ark-metadata = { path = "./crates/ark-metadata" }
ark-storage = { path = "./crates/ark-storage" }
ark-indexer = { path = "./crates/ark-indexer" }
arkchain-indexer = { path = "./crates/ark-chain/indexer" }
async-trait = "0.1.73"
starknet = "0.5.0"
anyhow = "1.0"
tokio = { version = "1", features = ["full"] }
log = "0.4.17"

# Thoses dependencies are used by the examples + the lib.rs.
[dependencies]
futures = "0.3.28"
async-trait.workspace = true
log.workspace = true
anyhow.workspace = true
tokio.workspace = true
ark-starknet.workspace = true
ark-metadata.workspace = true
ark-storage.workspace = true
ark-indexer.workspace = true

arkchain-indexer.workspace = true
starknet.workspace = true

[patch."https://github.com/starkware-libs/blockifier"]
blockifier = { git = "https://github.com/dojoengine/blockifier", rev = "c794d1b" }
Expand Down
30 changes: 30 additions & 0 deletions crates/ark-chain/contracts/arkchain/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
config := --account ../solis_account1.json \
--keystore ../solis_key1.json \
--rpc http://127.0.0.1:7070 \
--keystore-password 1234

ob_cl = $(shell starkli class-hash target/dev/arkchain_orderbook.sierra.json)
ob_addr := 0x008707b3296680b4f67430509ee8655f86ce3c844d7223cb52b01412a6c45eff

ark_executor_sn := 0x0573e38d2b37032172a1f9b370bd81954a0d44934e38154ff75a678700b73430

# Solis accounts
# has token 1 and 2
solis_2 := 0x47abb77acbb2f65006a42b14fed3664bafb0d102c00a97be58618d49d5186ac
# has token 3 and 4
solis_3 := 0x4c077c4085945fa6ea7116ec3c4579de29b10bd65aaf77cf4cf4f85af3ef609
# collection
erc721_address := 0x044b07a8c955296dd52a9d7d31a400b16ad4f6f574aa57db79cac0e87eac48cd

setup:
scarb build
starkli declare target/dev/arkchain_orderbook.sierra.json ${config}
starkli deploy ${ob_cl} 0x3ee9e18edc71a6df30ac3aca2e0b02a198fbce19b7480a63a0d71cbd76652e0 ${config} --salt 0x1234
starkli invoke ${ob_addr} set_executor_sn_address ${ark_executor_sn} ${config}

starkli invoke ${ob_addr} register_broker str:b1 1 str:starknet_testnet ${config}

starkli invoke ${ob_addr} add_order_listing ${solis_2} ${erc721_address} u256:1 u256:77 0 str:b1 0 0 ${config}

starkli invoke ${ob_addr} submit_order_buy 0x51a397add04213bb487668b635bedefdbbe5fb8f5b9d3aad6164031b60958b ${solis_3} str:b1 0 0 ${config}

13 changes: 13 additions & 0 deletions crates/ark-chain/contracts/solis_account1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": 1,
"variant": {
"type": "open_zeppelin",
"version": 1,
"public_key": "0x7f00d91354c3848de72fe99c94e07d89b61362a18a8137f4d0fc6057fded169"
},
"deployment": {
"status": "deployed",
"class_hash": "0x4d07e40e93398ed3c76981e72dd1fd22557a78ce36c0515f679e27f0bb5bc5f",
"address": "0x3c2c23f8389cffce8f698c4f3d3d771a979ddce14de15e6ae14b56c8e4e355b"
}
}
1 change: 1 addition & 0 deletions crates/ark-chain/contracts/solis_key1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"9b94bbb390d9b67ce2a52de1a2828b24"},"ciphertext":"5e2f576ed9e33eec395a96538ea1710114e85fbe7c29138360b8666883794e4e","kdf":"scrypt","kdfparams":{"dklen":32,"n":8192,"p":1,"r":8,"salt":"6a2931cfd19a5ab69874e7fe1e70c6ad12e2984e57711a30801ed1e90aa7c8e8"},"mac":"f4c42c6861134e8a2c5e43f2ede9a1f3ba23609953f46924e3b7f4eacbc2750d"},"id":"ee5ca82e-5e7a-4ae8-ac6e-8a2d178f2197","version":3}
2 changes: 1 addition & 1 deletion crates/ark-chain/indexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const EVENT_SELECTORS: &[FieldElement; 4] = &[
EV_ORDER_BUY_FINALIZED,
];

type IndexerResult<T> = Result<T, IndexerError>;
pub type IndexerResult<T> = Result<T, IndexerError>;

#[derive(Debug, thiserror::Error)]
pub enum IndexerError {
Expand Down
6 changes: 5 additions & 1 deletion crates/ark-chain/indexer/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use ark_starknet::CairoU256;
use async_trait::async_trait;
use starknet::core::types::FieldElement;

type StorageResult<T> = Result<T, StorageError>;
pub type StorageResult<T> = Result<T, StorageError>;

#[derive(Debug, thiserror::Error)]
pub enum StorageError {
Expand Down Expand Up @@ -31,13 +31,15 @@ pub trait ArkchainStorage {
async fn set_order_finalized(&self, order: OrderFinalizedData) -> StorageResult<()>;
}

#[derive(Debug)]
pub struct BrokerData {
pub name: FieldElement,
pub chain_id: FieldElement,
pub timestamp: u64,
pub public_key: FieldElement,
}

#[derive(Debug)]
pub struct OrderListingData {
pub order_hash: FieldElement,
pub broker_name: FieldElement,
Expand All @@ -49,6 +51,7 @@ pub struct OrderListingData {
pub price: CairoU256,
}

#[derive(Debug)]
pub struct OrderBuyExecutingData {
pub order_hash: FieldElement,
pub broker_name: FieldElement,
Expand All @@ -57,6 +60,7 @@ pub struct OrderBuyExecutingData {
pub buyer: FieldElement,
}

#[derive(Debug)]
pub struct OrderFinalizedData {
pub order_hash: FieldElement,
pub timestamp: u64,
Expand Down
32 changes: 24 additions & 8 deletions crates/ark-chain/solis/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ pub struct KatanaArgs {
#[arg(long)]
#[arg(value_name = "PATH")]
#[arg(help = "Dump the state of chain on exit to the given file.")]
#[arg(long_help = "Dump the state of chain on exit to the given file. If the value is a \
directory, the state will be written to `<PATH>/state.bin`.")]
#[arg(
long_help = "Dump the state of chain on exit to the given file. If the value is a \
directory, the state will be written to `<PATH>/state.bin`."
)]
pub dump_state: Option<PathBuf>,

#[arg(long)]
Expand All @@ -55,9 +57,11 @@ pub struct KatanaArgs {
#[arg(long)]
#[arg(value_name = "PATH")]
#[arg(help = "Configure the messaging with an other chain.")]
#[arg(long_help = "Configure the messaging to allow Katana listening/sending messages on a \
#[arg(
long_help = "Configure the messaging to allow Katana listening/sending messages on a \
settlement chain that can be Ethereum or an other Starknet sequencer. \
The configuration file details and examples can be found here: TODO.")]
The configuration file details and examples can be found here: TODO."
)]
pub messaging: Option<PathBuf>,

#[command(flatten)]
Expand Down Expand Up @@ -140,7 +144,11 @@ impl KatanaArgs {
fork_block_number: self.fork_block_number,
env: Environment {
chain_id: self.starknet.environment.chain_id.clone(),
gas_price: self.starknet.environment.gas_price.unwrap_or(DEFAULT_GAS_PRICE),
gas_price: self
.starknet
.environment
.gas_price
.unwrap_or(DEFAULT_GAS_PRICE),
invoke_max_steps: self
.starknet
.environment
Expand All @@ -163,7 +171,9 @@ fn parse_seed(seed: &str) -> [u8; 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);
seed.iter()
.enumerate()
.for_each(|(i, b)| actual_seed[i] = *b);
actual_seed
}
}
Expand All @@ -178,8 +188,14 @@ mod test {
let block_context = args.starknet_config().block_context();
assert_eq!(block_context.gas_price, DEFAULT_GAS_PRICE);
assert_eq!(block_context.chain_id.0, "KATANA".to_string());
assert_eq!(block_context.validate_max_n_steps, DEFAULT_VALIDATE_MAX_STEPS);
assert_eq!(block_context.invoke_tx_max_n_steps, DEFAULT_INVOKE_MAX_STEPS);
assert_eq!(
block_context.validate_max_n_steps,
DEFAULT_VALIDATE_MAX_STEPS
);
assert_eq!(
block_context.invoke_tx_max_n_steps,
DEFAULT_INVOKE_MAX_STEPS
);
}

#[test]
Expand Down
5 changes: 2 additions & 3 deletions crates/ark-chain/solis/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::fs;
use std::process::exit;
use std::sync::Arc;
use std::{fs};

use clap::{Parser};
use clap::Parser;
use console::Style;
use katana_core::sequencer::{KatanaSequencer, Sequencer};
use katana_rpc::{spawn, KatanaApi, NodeHandle, StarknetApi};
Expand Down Expand Up @@ -110,4 +110,3 @@ pub async fn shutdown_handler(sequencer: Arc<impl Sequencer>, config: KatanaArgs
}
};
}

2 changes: 1 addition & 1 deletion crates/ark-indexer/src/managers/block_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl<'a, T: StorageManager, C: StarknetClient> BlockManager<'a, T, C> {
if *do_force {
return self.storage.clean_block(block_number).is_ok();
}

match self.storage.get_block_info(block_number) {
Ok(info) => {
if self.indexer_version > info.indexer_version {
Expand Down
4 changes: 3 additions & 1 deletion crates/ark-starknet/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use std::marker::Sized;
#[async_trait]
pub trait StarknetClient {
///
fn new(rpc_url: &str) -> Result<Self> where Self: Sized;
fn new(rpc_url: &str) -> Result<Self>
where
Self: Sized;

///
async fn block_id_to_u64(&self, id: &BlockId) -> Result<u64>;
Expand Down
1 change: 1 addition & 0 deletions crates/ark-starknet/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod client;

#[derive(Debug)]
pub struct CairoU256 {
pub low: u128,
pub high: u128,
Expand Down
70 changes: 70 additions & 0 deletions examples/arkchain_indexer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//! How to use arkchain indexer library.
//!
//! Can be run with `cargo run --example arkchain_indexer`.
//!
use anyhow::Result;
use arkproject::{
arkchain::{storage::*, ArkchainIndexer},
starknet::client::{StarknetClient, StarknetClientHttp},
};
use async_trait::async_trait;
use starknet::core::types::BlockId;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<()> {
let client = StarknetClientHttp::new("http://127.0.0.1:7070").unwrap();
let storage = DefaultStorage {};

// Here, we can define any logic we want to index blocks range etc..
// We can then use the same reference across threads to work with only one
// ArkchainIndexer instance.
let indexer = Arc::new(ArkchainIndexer::new(Arc::new(client), Arc::new(storage)));

let mut handles = vec![];

for i in 0..3 {
let indexer = Arc::clone(&indexer);
let handle = tokio::spawn(async move {
let from = BlockId::Number(i * 10);
let to = BlockId::Number(i * 10 + 5);
println!("Indexer [{:?} - {:?}] started!", from, to);
match indexer.index_block_range(from, to).await {
Ok(_) => println!("Indexer [{:?} - {:?}] completed!", from, to),
Err(e) => println!("Indexer [{:?} - {:?}] failed! [{:?}]", from, to, e),
}
});

handles.push(handle);
}

futures::future::join_all(handles).await;

Ok(())
}

// Default storate that logs stuff.
struct DefaultStorage;

#[async_trait]
impl ArkchainStorage for DefaultStorage {
async fn register_broker(&self, broker: BrokerData) -> StorageResult<()> {
println!("\n*** register broker\n{:?}", broker);
Ok(())
}

async fn add_listing_order(&self, order: OrderListingData) -> StorageResult<()> {
println!("\n*** add listing order \n{:?}", order);
Ok(())
}

async fn set_order_buy_executing(&self, order: OrderBuyExecutingData) -> StorageResult<()> {
println!("\n*** buy executing order \n{:?}", order);
Ok(())
}

async fn set_order_finalized(&self, order: OrderFinalizedData) -> StorageResult<()> {
println!("\n*** order finalized \n{:?}", order);
Ok(())
}
}
2 changes: 1 addition & 1 deletion examples/nft_indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Can be run with `cargo run --example nft_indexer`.
//!
use anyhow::Result;
use ark_rs::{nft_indexer, nft_storage::DefaultStorage};
use arkproject::{nft_indexer, nft_storage::DefaultStorage};

#[tokio::main]
async fn main() -> Result<()> {
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ pub mod nft_indexer {
pub mod nft_storage {
pub use ark_storage::*;
}

// TODO: need to rework the organization of those crates.
pub mod arkchain {
pub use arkchain_indexer::*;
}

0 comments on commit 7c34d00

Please sign in to comment.