Skip to content

Commit

Permalink
refactor: additional update based on comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ICavlek committed Oct 24, 2024
1 parent 00af73c commit 9e704b2
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 182 deletions.
21 changes: 0 additions & 21 deletions tests/account_katana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ use beerus::{
TxnHash,
},
};
use common::err::Error;
use starknet::katana::Katana;
use starknet::scarb::Compiler;
use starknet::{
constants::{
CLASS_HASH, COMPILED_ACCOUNT_CONTRACT_V2, COMPILED_ACCOUNT_CONTRACT_V3,
CONTRACT_ADDRESS, DECLARE_ACCOUNT_V2, DECLARE_ACCOUNT_V3,
SENDER_ADDRESS,
},
coordinator::{Coordinator, TestMode},
};

mod common;
Expand Down Expand Up @@ -48,24 +45,6 @@ async fn declare_deploy_account_v2() {
deploy(client).await;
}

#[tokio::test]
async fn deploy_new_account_on_katana() -> Result<(), Error> {
let _katana = Katana::init("http://127.0.0.1:0").await?;
let coordinator = Coordinator::new(TestMode::Katana);
coordinator.copy_template_to_target()?;
coordinator.update_account()?;
let compiler = Compiler::new(&coordinator.target_scarb())?;
compiler.compile().await?;
// TODO
// #804 starkli signer keystore new key.json - Storing somewhere or deleting?
// #804 starkli account oz init account.json - Storing somewhere or deleting?
// #804 declare accounts
// #804 #805 fund accounts from pre-funded account
// #804 deploy accounts
// #806 iterate through class hashes and call getClass to see if they are verified
Ok(())
}

async fn declare(
client: &Client<Http>,
compiled_contract: &str,
Expand Down
62 changes: 31 additions & 31 deletions tests/rpc.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
use beerus::gen::{
Address, BlockHash, BlockId, BlockNumber, BlockTag, BroadcastedInvokeTxn,
BroadcastedTxn, Felt, FunctionCall, GetBlockWithTxHashesResult,
GetBlockWithTxsResult, GetClassAtResult, GetClassResult,
GetTransactionByBlockIdAndIndexIndex, InvokeTxn, InvokeTxnV1,
InvokeTxnV1Version, PriceUnit, Rpc, StorageKey, SyncingResult, Txn,
TxnExecutionStatus, TxnHash, TxnReceipt, TxnReceiptWithBlockInfo,
TxnStatus,
use beerus::{
config::MAINNET_STARKNET_CHAINID,
gen::{
Address, BlockHash, BlockId, BlockNumber, BlockTag,
BroadcastedInvokeTxn, BroadcastedTxn, Felt, FunctionCall,
GetBlockWithTxHashesResult, GetBlockWithTxsResult, GetClassAtResult,
GetClassResult, GetTransactionByBlockIdAndIndexIndex, InvokeTxn,
InvokeTxnV1, InvokeTxnV1Version, PriceUnit, Rpc, StorageKey,
SyncingResult, Txn, TxnExecutionStatus, TxnHash, TxnReceipt,
TxnReceiptWithBlockInfo, TxnStatus,
},
};

mod common;
mod starknet;

use common::err::Error;

#[tokio::test]
#[allow(non_snake_case)]
async fn test_specVersion() -> Result<(), Error> {
let ctx = setup!();

let ret = ctx.client.specVersion().await?;
assert_eq!(ret, "0.7.1");
Ok(())
}

#[tokio::test]
#[allow(non_snake_case)]
async fn test_chainId() -> Result<(), Error> {
let ctx = setup!();

let ret = ctx.client.chainId().await?;
assert_eq!(ret.as_ref(), MAINNET_STARKNET_CHAINID);
Ok(())
}

#[tokio::test]
#[allow(non_snake_case)]
async fn test_blockHashAndNumber() -> Result<(), Error> {
Expand Down Expand Up @@ -510,26 +533,3 @@ async fn account_call() -> Result<(), Error> {

Ok(())
}

#[tokio::test]
async fn deploy_new_account_on_sepolia() -> Result<(), Error> {
// TODO #807
// schedule test once each month in separate workflow
// with each test, template account id is incremented by 1
// commit from workflows to update latest state of id

let _ctx = setup!("sepolia");
// let coordinator = Coordinator::new(TestMode::Sepolia);
// coordinator.copy_template_to_target()?;
// coordinator.update_account()?;
// let compiler = Compiler::new(&coordinator.target_scarb())?;
// compiler.compile().await?;

// TODO
// #804 starkli signer keystore new key.json - Storing somewhere or deleting?
// #804 starkli account oz init account.json - Storing somewhere or deleting?
// #804 declare account
// #804 #805 fund account from pre-funded account
// #804 deploy account
Ok(())
}
82 changes: 0 additions & 82 deletions tests/starknet/coordinator.rs

This file was deleted.

2 changes: 1 addition & 1 deletion tests/starknet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod constants;
pub mod coordinator;
pub mod katana;
pub mod scarb;
pub mod utils;
67 changes: 20 additions & 47 deletions tests/starknet/scarb.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{anyhow, Error};
use anyhow::Error;
use scarb::{
core::{Config, PackageId, PackageName, SourceId, TargetKind},
ops::{self, CompileOpts, FeaturesOpts, FeaturesSelector},
Expand All @@ -7,51 +7,24 @@ use semver::Version;
use std::{fs::canonicalize, path::PathBuf};

#[allow(dead_code)]
pub struct Compiler {
toml: PathBuf,
opts: CompileOpts,
packages: Vec<PackageId>,
}

#[allow(dead_code)]
impl Compiler {
pub fn new(toml: &str) -> Result<Self, Error> {
let toml_absolute = canonicalize(PathBuf::from(toml))?;
let opts = CompileOpts {
include_target_kinds: vec![],
exclude_target_kinds: vec![TargetKind::new("test")],
include_target_names: vec![],
features: FeaturesOpts {
features: FeaturesSelector::Features(vec![]),
no_default_features: false,
},
};
let packages = vec![PackageId::new(
PackageName::new("account"),
Version::new(0, 1, 0),
SourceId::for_path(toml_absolute.to_str().unwrap().into())?,
)];

Ok(Compiler { toml: toml_absolute, opts, packages })
}

pub async fn compile(self) -> Result<(), Error> {
let compilation =
tokio::task::spawn_blocking(move || -> Result<(), Error> {
self.run_compilation()
});
match compilation.await {
Ok(val) => Ok(val?),
Err(e) => Err(anyhow!(
"Error during thread execution. Original error message: {:#?}",
e,
)),
}
}
pub fn compile(toml: String) -> Result<(), Error> {
let toml_absolute = canonicalize(PathBuf::from(toml))?;
let opts = CompileOpts {
include_target_kinds: vec![],
exclude_target_kinds: vec![TargetKind::new("test")],
include_target_names: vec![],
features: FeaturesOpts {
features: FeaturesSelector::Features(vec![]),
no_default_features: false,
},
};
let packages = vec![PackageId::new(
PackageName::new("account"),
Version::new(0, 1, 0),
SourceId::for_path(toml_absolute.to_str().unwrap().into())?,
)];
let config = Config::builder(toml_absolute.to_str().unwrap()).build()?;
let ws = ops::read_workspace(config.manifest_path(), &config)?;

fn run_compilation(self) -> Result<(), Error> {
let config = Config::builder(self.toml.to_str().unwrap()).build()?;
let ws = ops::read_workspace(config.manifest_path(), &config)?;
scarb::ops::compile(self.packages, self.opts, &ws)
}
scarb::ops::compile(packages, opts, &ws)
}
28 changes: 28 additions & 0 deletions tests/starknet/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::fs;

use anyhow::Error;
use chrono;

const SOURCE_LIB: &str = "./tests/starknet/contract/account/src/lib.cairo";
const SOURCE_SCARB: &str = "./tests/starknet/contract/account/Scarb.toml";

#[allow(dead_code)]
pub fn prepare_account() -> Result<String, Error> {
let now = chrono::offset::Local::now();
let id = now.format("%Y%m%y%H%M%S").to_string();
let target = "./target/account-".to_string() + &id;
let target_lib = target.clone() + "/src/lib.cairo";
let target_scarb = target.clone() + "/Scarb.toml";
let target_src = target.clone() + "/src";

fs::create_dir(target)?;
fs::create_dir(target_src)?;
fs::copy(SOURCE_LIB, target_lib.clone())?;
fs::copy(SOURCE_SCARB, target_scarb.clone())?;

let account_template = fs::read_to_string(target_lib.clone())?;
let account_new = account_template.replace("<ID>", &id);
fs::write(target_lib, account_new)?;

Ok(target_scarb)
}

0 comments on commit 9e704b2

Please sign in to comment.