diff --git a/src/contract_clients/utils.rs b/src/contract_clients/utils.rs index 2ce68f6..2e5aa60 100644 --- a/src/contract_clients/utils.rs +++ b/src/contract_clients/utils.rs @@ -66,8 +66,13 @@ pub fn field_element_to_u256(input: Felt) -> U256 { U256::from_big_endian(&input.to_bytes_be()) } -pub fn generate_config_hash(config_hash_version: Felt, chain_id: Felt, fee_token_address: Felt) -> Felt { - Pedersen::hash_array(&[config_hash_version, chain_id, fee_token_address]) +pub fn generate_config_hash( + config_hash_version: Felt, + chain_id: Felt, + fee_token_address: Felt, + native_fee_token_address: Felt, +) -> Felt { + Pedersen::hash_array(&[config_hash_version, chain_id, fee_token_address, native_fee_token_address]) } pub fn get_bridge_init_configs(config: &CliArgs) -> (Felt, Felt) { @@ -77,6 +82,7 @@ pub fn get_bridge_init_configs(config: &CliArgs) -> (Felt, Felt) { Felt::from_hex(&encode(config.config_hash_version.as_str())).expect("error in config_hash_version"), Felt::from_hex(&encode(config.app_chain_id.as_str())).expect("error in app_chain_id"), Felt::from_hex(config.fee_token_address.as_str()).expect("error in fee_token_address"), + Felt::from_hex(config.native_fee_token_address.as_str()).expect("error in fee_token_address"), ); (program_hash, config_hash) } diff --git a/src/main.rs b/src/main.rs index 5829b31..da31191 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,7 +53,9 @@ pub struct CliArgs { app_chain_id: String, #[clap(long, env, default_value = "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7")] fee_token_address: String, - #[clap(long, env, default_value_t = 80)] + #[clap(long, env, default_value = "0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d")] + native_fee_token_address: String, + #[clap(long, env, default_value_t = 10)] cross_chain_wait_time: u64, // Default test address value taken from anvil // IMP : Not to be used in prod environment diff --git a/src/tests/constants.rs b/src/tests/constants.rs index 7c842dc..e1144af 100644 --- a/src/tests/constants.rs +++ b/src/tests/constants.rs @@ -6,8 +6,9 @@ pub const ETH_CHAIN_ID: &str = "31337"; pub const L1_DEPLOYER_ADDRESS: &str = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"; pub const L1_WAIT_TIME: &str = "15"; pub const FEE_TOKEN_ADDRESS: &str = "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"; +pub const NATIVE_FEE_TOKEN_ADDRESS: &str = "0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d"; pub const SN_OS_PROGRAM_HASH: &str = "0x41fc2a467ef8649580631912517edcab7674173f1dbfa2e9b64fbcd82bc4d79"; -pub const SN_OS_CONFIG_HASH_VERSION: &str = "StarknetOsConfig1"; +pub const SN_OS_CONFIG_HASH_VERSION: &str = "StarknetOsConfig2"; pub const APP_CHAIN_ID: &str = "MADARA_DEVNET"; pub const L2_DEPLOYER_ADDRESS: &str = "0x4fe5eea46caa0a1f344fafce82b39d66b552f00d3cd12e89073ef4b4ab37860"; pub const L1_MULTISIG_ADDRESS: &str = "0x70997970C51812dc3A010C7d01b50e0d17dc79C8"; diff --git a/src/tests/erc20_bridge.rs b/src/tests/erc20_bridge.rs index b1ef7fa..8d7e4b4 100644 --- a/src/tests/erc20_bridge.rs +++ b/src/tests/erc20_bridge.rs @@ -2,15 +2,16 @@ use std::str::FromStr; use std::time::Duration; use ethers::prelude::U256; +use ethers::types::Address; use starknet::core::types::Felt; use tokio::time::sleep; use crate::contract_clients::config::Config; use crate::contract_clients::token_bridge::StarknetTokenBridge; -use crate::contract_clients::utils::read_erc20_balance; +use crate::contract_clients::utils::{build_single_owner_account, read_erc20_balance}; use crate::tests::constants::L2_DEPLOYER_ADDRESS; +use crate::utils::invoke_contract; use crate::CliArgs; - // TODO : Uncomment L2 --> L1 part after this part is added in the madara-orchestrator. pub async fn erc20_bridge_test_helper( @@ -18,7 +19,7 @@ pub async fn erc20_bridge_test_helper( arg_config: &CliArgs, l2_erc20_token_address: Felt, token_bridge: StarknetTokenBridge, - _l2_bridge_address: Felt, + l2_bridge_address: Felt, ) -> Result<(), anyhow::Error> { token_bridge.approve(token_bridge.bridge_address(), 100000000.into()).await; sleep(Duration::from_secs(arg_config.l1_wait_time.parse().unwrap())).await; @@ -47,37 +48,40 @@ pub async fn erc20_bridge_test_helper( read_erc20_balance(clients.provider_l2(), l2_erc20_token_address, Felt::from_str(L2_DEPLOYER_ADDRESS).unwrap()) .await; - assert_eq!(balance_before[0] + Felt::from_dec_str("10").unwrap(), balance_after[0]); + assert_eq!(balance_before[0] + Felt::from(10), balance_after[0]); + + let l1_recipient = Felt::from_hex(&arg_config.l1_deployer_address).unwrap(); + let account = + build_single_owner_account(clients.provider_l2(), &arg_config.rollup_priv_key, L2_DEPLOYER_ADDRESS, false) + .await; + + log::debug!("Initiated token withdraw on L2 [⏳]"); + invoke_contract( + l2_bridge_address, + "initiate_token_withdraw", + vec![ + Felt::from_byte_slice_be(token_bridge.address().as_bytes()).unwrap(), + l1_recipient, + Felt::from_dec_str("5").unwrap(), + Felt::ZERO, + ], + &account, + ) + .await; + + sleep(Duration::from_secs(arg_config.l1_wait_time.parse().unwrap())).await; + log::debug!("Waiting for message to be consumed on l2 [⏳]"); + sleep(Duration::from_secs(arg_config.cross_chain_wait_time)).await; + sleep(Duration::from_secs(arg_config.l1_wait_time.parse().unwrap())).await; + + let l1_recipient: Address = Address::from_str(&arg_config.l1_deployer_address).unwrap(); + let balance_before = token_bridge.token_balance(l1_recipient).await; + token_bridge.withdraw(token_bridge.address(), 5.into(), l1_recipient).await; + let balance_after = token_bridge.token_balance(l1_recipient).await; + + assert_eq!(balance_before + U256::from_dec_str("5").unwrap(), balance_after); - // let l1_recipient = FieldElement::from_hex_be(&arg_config.l1_deployer_address).unwrap(); - // - // log::debug!("Initiated token withdraw on L2 [⏳]"); - // invoke_contract( - // l2_bridge_address, - // "initiate_token_withdraw", - // vec![ - // FieldElement::from_byte_slice_be(token_bridge.address().as_bytes()).unwrap(), - // l1_recipient, - // FieldElement::from_dec_str("5").unwrap(), - // FieldElement::ZERO, - // ], - // &account, - // ) - // .await; - // - // sleep(Duration::from_secs(arg_config.l1_wait_time.parse().unwrap())).await; - // log::debug!("Waiting for message to be consumed on l2 [⏳]"); - // sleep(Duration::from_secs(arg_config.cross_chain_wait_time)).await; - // sleep(Duration::from_secs(arg_config.l1_wait_time.parse().unwrap())).await; - // - // let l1_recipient: Address = Address::from_str(&arg_config.l1_deployer_address).unwrap(); - // let balance_before = token_bridge.token_balance(l1_recipient).await; - // token_bridge.withdraw(token_bridge.address(), 5.into(), l1_recipient).await; - // let balance_after = token_bridge.token_balance(l1_recipient).await; - // - // assert_eq!(balance_before + U256::from_dec_str("5").unwrap(), balance_after); - // - // log::debug!("Token withdraw successful [✅]"); + log::debug!("Token withdraw successful [✅]"); anyhow::Ok(()) } diff --git a/src/tests/eth_bridge.rs b/src/tests/eth_bridge.rs index 8ef3669..d6afbc9 100644 --- a/src/tests/eth_bridge.rs +++ b/src/tests/eth_bridge.rs @@ -2,23 +2,24 @@ use std::str::FromStr; use std::time::Duration; use ethers::prelude::U256; +use ethers::types::Address; // use ethers::types::Address; use starknet::core::types::Felt; use tokio::time::sleep; use crate::contract_clients::config::Config; use crate::contract_clients::eth_bridge::StarknetLegacyEthBridge; -use crate::contract_clients::utils::read_erc20_balance; +use crate::contract_clients::utils::{build_single_owner_account, read_erc20_balance}; use crate::tests::constants::L2_DEPLOYER_ADDRESS; +use crate::utils::invoke_contract; use crate::CliArgs; - // TODO : Uncomment L2 --> L1 part after this part is added in the madara-orchestrator. pub async fn eth_bridge_test_helper( clients: &Config, arg_config: &CliArgs, l2_eth_address: Felt, - _l2_bridge_address: Felt, + l2_bridge_address: Felt, eth_bridge: StarknetLegacyEthBridge, ) -> Result<(), anyhow::Error> { let balance_before = @@ -36,40 +37,33 @@ pub async fn eth_bridge_test_helper( assert_eq!(balance_before[0] + Felt::from_dec_str("10").unwrap(), balance_after[0]); - // let l1_receipient = FieldElement::from_hex_be(&arg_config.l1_deployer_address).unwrap(); - // - // sleep(Duration::from_secs((arg_config.l1_wait_time).parse()?)).await; - // - // let account = - // build_single_owner_account(clients.provider_l2(), &arg_config.rollup_priv_key, - // L2_DEPLOYER_ADDRESS, false) .await; - // - // invoke_contract( - // l2_bridge_address, - // "initiate_withdraw", - // vec![l1_receipient, FieldElement::from_dec_str("5").unwrap(), FieldElement::ZERO], - // &account, - // ) - // .await; - // log::debug!("ETH withdrawal initiated on l2 [💰]"); - // log::debug!("Waiting for message to be consumed on l2 [⏳]"); - // sleep(Duration::from_secs(arg_config.cross_chain_wait_time)).await; - // sleep(Duration::from_secs((arg_config.l1_wait_time).parse()?)).await; - // - // let balance_before = - // eth_bridge.eth_balance(Address::from_str(&arg_config.l1_deployer_address).unwrap()).await; - // log::debug!("Withdraw initiated on ETH Bridge [⏳]"); - // eth_bridge.withdraw(5.into(), Address::from_str(&arg_config.l1_deployer_address).unwrap()).await; - // log::debug!("Withdraw completed on ETH Bridge [✅]"); - // let balance_after = - // eth_bridge.eth_balance(Address::from_str(&arg_config.l1_deployer_address).unwrap()).await; - // - // let decimals_eth = U256::from_dec_str("1000000000000000000").unwrap(); - // - // assert_eq!( - // U256::checked_div(balance_before + U256::from_dec_str("5").unwrap(), decimals_eth).unwrap(), - // U256::checked_div(balance_after, decimals_eth).unwrap() - // ); + let l1_receipient = Felt::from_hexe(&arg_config.l1_deployer_address).unwrap(); + + sleep(Duration::from_secs((arg_config.l1_wait_time).parse()?)).await; + + let account = + build_single_owner_account(clients.provider_l2(), &arg_config.rollup_priv_key, L2_DEPLOYER_ADDRESS, false) + .await; + + invoke_contract(l2_bridge_address, "initiate_withdraw", vec![l1_receipient, Felt::from(5), Felt::ZERO], &account) + .await; + log::debug!("ETH withdrawal initiated on l2 [💰]"); + log::debug!("Waiting for message to be consumed on l2 [⏳]"); + sleep(Duration::from_secs(arg_config.cross_chain_wait_time)).await; + sleep(Duration::from_secs((arg_config.l1_wait_time).parse()?)).await; + + let balance_before = eth_bridge.eth_balance(Address::from_str(&arg_config.l1_deployer_address).unwrap()).await; + log::debug!("Withdraw initiated on ETH Bridge [⏳]"); + eth_bridge.withdraw(5.into(), Address::from_str(&arg_config.l1_deployer_address).unwrap()).await; + log::debug!("Withdraw completed on ETH Bridge [✅]"); + let balance_after = eth_bridge.eth_balance(Address::from_str(&arg_config.l1_deployer_address).unwrap()).await; + + let decimals_eth = U256::from_dec_str("1000000000000000000").unwrap(); + + assert_eq!( + U256::checked_div(balance_before + U256::from_dec_str("5").unwrap(), decimals_eth).unwrap(), + U256::checked_div(balance_after, decimals_eth).unwrap() + ); Ok(()) } diff --git a/src/tests/mod.rs b/src/tests/mod.rs index cc08e4f..af68b5e 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -4,7 +4,7 @@ mod eth_bridge; use constants::{ APP_CHAIN_ID, ETH_CHAIN_ID, ETH_PRIV_KEY, ETH_RPC, FEE_TOKEN_ADDRESS, L1_DEPLOYER_ADDRESS, L1_WAIT_TIME, - ROLLUP_PRIV_KEY, ROLLUP_SEQ_URL, SN_OS_CONFIG_HASH_VERSION, SN_OS_PROGRAM_HASH, + NATIVE_FEE_TOKEN_ADDRESS, ROLLUP_PRIV_KEY, ROLLUP_SEQ_URL, SN_OS_CONFIG_HASH_VERSION, SN_OS_PROGRAM_HASH, }; use rstest::rstest; @@ -106,7 +106,8 @@ fn get_config() -> CliArgs { config_hash_version: String::from(SN_OS_CONFIG_HASH_VERSION), app_chain_id: String::from(APP_CHAIN_ID), fee_token_address: String::from(FEE_TOKEN_ADDRESS), - cross_chain_wait_time: 120, + native_fee_token_address: String::from(NATIVE_FEE_TOKEN_ADDRESS), + cross_chain_wait_time: 20, l1_multisig_address: String::from(L1_MULTISIG_ADDRESS), l2_multisig_address: String::from(L2_MULTISIG_ADDRESS), verifier_address: String::from(VERIFIER_ADDRESS),