Skip to content

Commit

Permalink
feat: add support in test environment for sepolia network (#793)
Browse files Browse the repository at this point in the history
  • Loading branch information
ICavlek authored Oct 3, 2024
1 parent 671a3c9 commit ad721db
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 169 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ jobs:
runs-on: ubuntu-latest
env:
BEERUS_TEST_RUN: 1
BEERUS_TEST_STARKNET_URL: https://starknet-mainnet.g.alchemy.com/starknet/version/rpc/v0_7/${{ secrets.ALCHEMY_KEY }}
STARKNET_MAINNET_URL: https://starknet-mainnet.g.alchemy.com/starknet/version/rpc/v0_7/${{ secrets.ALCHEMY_KEY }}
STARKNET_SEPOLIA_URL: https://starknet-sepolia.g.alchemy.com/starknet/version/rpc/v0_7/${{ secrets.ALCHEMY_KEY }}
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ cargo build --release
cargo test

## Run integration tests against live endpoint
export BEERUS_TEST_STARKNET_URL=https://starknet-mainnet.g.alchemy.com/starknet/version/rpc/v0_7/${ALCHEMY_API_KEY}
export STARKNET_MAINNET_URL=https://starknet-mainnet.g.alchemy.com/starknet/version/rpc/v0_7/${ALCHEMY_API_KEY}
export STARKNET_SEPOLIA_URL=https://starknet-sepolia.g.alchemy.com/starknet/version/rpc/v0_7/${ALCHEMY_API_KEY}
BEERUS_TEST_RUN=1 cargo test
```

Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const DEFAULT_POLL_SECS: u64 = 5;
const MAINNET_ETHEREUM_CHAINID: &str = "0x1";
const SEPOLIA_ETHEREUM_CHAINID: &str = "0xaa36a7";

const MAINNET_STARKNET_CHAINID: &str = "0x534e5f4d41494e";
const SEPOLIA_STARKNET_CHAINID: &str = "0x534e5f5345504f4c4941";
pub const MAINNET_STARKNET_CHAINID: &str = "0x534e5f4d41494e";
pub const SEPOLIA_STARKNET_CHAINID: &str = "0x534e5f5345504f4c4941";

#[cfg(feature = "testing")]
const KATANA_STARKNET_CHAINID: &str = "0x4b4154414e41";
Expand Down
20 changes: 4 additions & 16 deletions tests/account_katana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,14 @@ use beerus::{
TxnHash,
},
};
use common::constants::{
CLASS_HASH, COMPILED_ACCOUNT_CONTRACT_V2, COMPILED_ACCOUNT_CONTRACT_V3,
CONTRACT_ADDRESS, DECLARE_ACCOUNT_V2, DECLARE_ACCOUNT_V3, SENDER_ADDRESS,
};
use common::katana::Katana;

mod common;

pub const COMPILED_ACCOUNT_CONTRACT_V2: &str =
include_str!("./clob/compiled_account_contract_v2.txt");
pub const COMPILED_ACCOUNT_CONTRACT_V3: &str =
include_str!("./clob/compiled_account_contract_v3.txt");
pub const DECLARE_ACCOUNT_V2: &str =
include_str!("./clob/declare_account_v2.txt");
pub const DECLARE_ACCOUNT_V3: &str =
include_str!("./clob/declare_account_v3.txt");

pub const CLASS_HASH: &str =
"0x6b46f84b1bbb779e588a9c5f577907c3dfb66e6b13cf4c4f480d4fb1677c2ba";
pub const CONTRACT_ADDRESS: &str =
"0x5f177e96c32bcc09607d9e2adf1864b8a8dc54c7fb5a283afac58f2b6ddc7da";
pub const SENDER_ADDRESS: &str =
"0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03";

async fn setup() -> (Katana, Client<Http>) {
let katana = Katana::init("http://127.0.0.1:0").await.unwrap();
let url = format!("http://127.0.0.1:{}", katana.port());
Expand Down
2 changes: 1 addition & 1 deletion tests/account_mock.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use beerus::gen::{Address, BlockId, BlockTag, Felt, Rpc, TxnHash};

use common::{
constants::{
dummies::{
declare_transaction, deploy_transaction, estimate_fee_transaction,
invoke_transaction,
},
Expand Down
147 changes: 14 additions & 133 deletions tests/common/constants.rs
Original file line number Diff line number Diff line change
@@ -1,141 +1,22 @@
use beerus::gen::{
Address, BroadcastedDeclareTxn, BroadcastedDeclareTxnV3,
BroadcastedDeclareTxnV3Type, BroadcastedDeclareTxnV3Version,
BroadcastedDeployAccountTxn, BroadcastedInvokeTxn, BroadcastedTxn,
ContractClass, ContractClassEntryPointsByType, DaMode, DeployAccountTxn,
DeployAccountTxnV3, DeployAccountTxnV3Type, DeployAccountTxnV3Version,
Felt, InvokeTxn, InvokeTxnV3, InvokeTxnV3Type, InvokeTxnV3Version,
ResourceBounds, ResourceBoundsMapping, SierraEntryPoint, U128, U64,
};

#[allow(dead_code)]
pub fn declare_transaction() -> BroadcastedDeclareTxn {
BroadcastedDeclareTxn::BroadcastedDeclareTxnV3(
dummy_declare_transaction_v3(),
)
}

pub const COMPILED_ACCOUNT_CONTRACT_V2: &str =
include_str!("../clob/compiled_account_contract_v2.txt");
#[allow(dead_code)]
pub fn estimate_fee_transaction() -> BroadcastedTxn {
BroadcastedTxn::BroadcastedDeclareTxn(
BroadcastedDeclareTxn::BroadcastedDeclareTxnV3(
dummy_declare_transaction_v3(),
),
)
}

pub const COMPILED_ACCOUNT_CONTRACT_V3: &str =
include_str!("../clob/compiled_account_contract_v3.txt");
#[allow(dead_code)]
pub fn invoke_transaction() -> BroadcastedInvokeTxn {
BroadcastedInvokeTxn(InvokeTxn::InvokeTxnV3(dummy_invoke_transaction_v3()))
}

pub const DECLARE_ACCOUNT_V2: &str =
include_str!("../clob/declare_account_v2.txt");
#[allow(dead_code)]
pub fn deploy_transaction() -> BroadcastedDeployAccountTxn {
BroadcastedDeployAccountTxn(DeployAccountTxn::DeployAccountTxnV3(
dummy_deploy_transaction_v3(),
))
}
pub const DECLARE_ACCOUNT_V3: &str =
include_str!("../clob/declare_account_v3.txt");

#[allow(dead_code)]
fn dummy_declare_transaction_v3() -> BroadcastedDeclareTxnV3 {
BroadcastedDeclareTxnV3 {
account_deployment_data: vec![Felt::try_new("0x0").unwrap()],
compiled_class_hash: Felt::try_new("0x0").unwrap(),
contract_class: ContractClass {
sierra_program: vec![Felt::try_new("0x1").unwrap()],
contract_class_version: "0.1.0".to_string(),
entry_points_by_type: ContractClassEntryPointsByType {
constructor: vec![SierraEntryPoint {
selector: Felt::try_new("0x2").unwrap(),
function_idx: 2,
}],
external: vec![
SierraEntryPoint {
selector: Felt::try_new("0x3").unwrap(),
function_idx: 3,
},
SierraEntryPoint {
selector: Felt::try_new("0x4").unwrap(),
function_idx: 4,
},
],
l1_handler: vec![],
},
abi: Some("some_abi".to_string()),
},
fee_data_availability_mode: DaMode::L1,
nonce: Felt::try_new("0x0").unwrap(),
r#type: BroadcastedDeclareTxnV3Type::Declare,
signature: vec![Felt::try_new("0x5").unwrap()],
sender_address: Address(Felt::try_new("0x6").unwrap()),
version:
BroadcastedDeclareTxnV3Version::V0x100000000000000000000000000000003,
nonce_data_availability_mode: DaMode::L1,
paymaster_data: vec![Felt::try_new("0x7").unwrap()],
resource_bounds: ResourceBoundsMapping {
l1_gas: ResourceBounds {
max_amount: U64::try_new("0x0").unwrap(),
max_price_per_unit: U128::try_new("0x0").unwrap(),
},
l2_gas: ResourceBounds {
max_amount: U64::try_new("0x0").unwrap(),
max_price_per_unit: U128::try_new("0x0").unwrap(),
},
},
tip: U64::try_new("0x0").unwrap(),
}
}

pub const CLASS_HASH: &str =
"0x6b46f84b1bbb779e588a9c5f577907c3dfb66e6b13cf4c4f480d4fb1677c2ba";
#[allow(dead_code)]
fn dummy_invoke_transaction_v3() -> InvokeTxnV3 {
InvokeTxnV3 {
account_deployment_data: vec![Felt::try_new("0x0").unwrap()],
calldata: vec![Felt::try_new("0x1").unwrap()],
fee_data_availability_mode: DaMode::L1,
nonce: Felt::try_new("0x2").unwrap(),
nonce_data_availability_mode: DaMode::L1,
paymaster_data: vec![Felt::try_new("0x1").unwrap()],
r#type: InvokeTxnV3Type::Invoke,
resource_bounds: ResourceBoundsMapping {
l1_gas: ResourceBounds {
max_amount: U64::try_new("0x0").unwrap(),
max_price_per_unit: U128::try_new("0x0").unwrap(),
},
l2_gas: ResourceBounds {
max_amount: U64::try_new("0x0").unwrap(),
max_price_per_unit: U128::try_new("0x0").unwrap(),
},
},
sender_address: Address(Felt::try_new("0x3").unwrap()),
signature: vec![Felt::try_new("0x4").unwrap()],
tip: U64::try_new("0x0").unwrap(),
version: InvokeTxnV3Version::V0x3,
}
}

pub const CONTRACT_ADDRESS: &str =
"0x5f177e96c32bcc09607d9e2adf1864b8a8dc54c7fb5a283afac58f2b6ddc7da";
#[allow(dead_code)]
fn dummy_deploy_transaction_v3() -> DeployAccountTxnV3 {
DeployAccountTxnV3 {
class_hash: Felt::try_new("0x0").unwrap(),
constructor_calldata: vec![Felt::try_new("0x1").unwrap()],
contract_address_salt: Felt::try_new("0x2").unwrap(),
fee_data_availability_mode: DaMode::L1,
nonce: Felt::try_new("0x3").unwrap(),
nonce_data_availability_mode: DaMode::L1,
paymaster_data: vec![Felt::try_new("0x4").unwrap()],
r#type: DeployAccountTxnV3Type::DeployAccount,
resource_bounds: ResourceBoundsMapping {
l1_gas: ResourceBounds {
max_amount: U64::try_new("0x0").unwrap(),
max_price_per_unit: U128::try_new("0x0").unwrap(),
},
l2_gas: ResourceBounds {
max_amount: U64::try_new("0x0").unwrap(),
max_price_per_unit: U128::try_new("0x0").unwrap(),
},
},
signature: vec![Felt::try_new("0x5").unwrap()],
tip: U64::try_new("0x0").unwrap(),
version: DeployAccountTxnV3Version::V0x3,
}
}
pub const SENDER_ADDRESS: &str =
"0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03";
43 changes: 37 additions & 6 deletions tests/common/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ pub struct Context {
pub server: Server,
}

#[allow(dead_code)] // used in macros
pub async fn ctx() -> Option<Context> {
let url = std::env::var("BEERUS_TEST_STARKNET_URL").ok()?;
#[allow(dead_code)]
pub async fn ctx_mainnet() -> Option<Context> {
let url = std::env::var("STARKNET_MAINNET_URL").ok()?;

let state = State {
block_number: 652076,
Expand All @@ -29,6 +29,29 @@ pub async fn ctx() -> Option<Context> {
)
.unwrap(),
};
ctx(url, state).await
}

#[allow(dead_code)]
pub async fn ctx_sepolia() -> Option<Context> {
let url = std::env::var("STARKNET_SEPOLIA_URL").ok()?;

let state = State {
block_number: 209582,
block_hash: Felt::try_new(
"0x45fb3ae1436743e74a81eac88e1beee6e8fb34aecb7b2e43e0577406f390f5f",
)
.unwrap(),
root: Felt::try_new(
"0x11dccdce33557ca6e14871d4235a8b65e9bd512722ac9e2cb96ff49bfb9af30",
)
.unwrap(),
};
ctx(url, state).await
}

#[allow(dead_code)] // used in macros
async fn ctx(url: String, state: State) -> Option<Context> {
let state = Arc::new(RwLock::new(state));
let server = serve(&url, "127.0.0.1:0", state.clone()).await.ok()?;
tracing::info!(port = server.port(), "test server is up");
Expand All @@ -40,15 +63,23 @@ pub async fn ctx() -> Option<Context> {

#[macro_export]
macro_rules! setup {
() => {{
() => {
setup!("mainnet")
};
($e:expr) => {{
let run: bool = std::env::var("BEERUS_TEST_RUN")
.ok()
.map(|value| &value == "1")
.unwrap_or_default();
if !run {
return Ok(());
}
if let Some(ctx) = common::ctx::ctx().await {
let context = match $e {
"sepolia" => common::ctx::ctx_sepolia().await,
"mainnet" => common::ctx::ctx_mainnet().await,
unknown => panic!("Unknown network: {unknown}. Supported networks: mainnet, sepolia"),
};
if let Some(ctx) = context {
ctx
} else {
panic!("Invalid test setup");
Expand All @@ -66,7 +97,7 @@ macro_rules! client {
if !run {
return Ok(());
}
if let Ok(url) = std::env::var("BEERUS_TEST_STARKNET_URL") {
if let Ok(url) = std::env::var("STARKNET_MAINNET_URL") {
Client::new(&url, beerus::client::Http::new())
} else {
panic!("Invalid test setup");
Expand Down
Loading

0 comments on commit ad721db

Please sign in to comment.