Skip to content

Commit

Permalink
support added for native token, config hash updated, withdrawal funct…
Browse files Browse the repository at this point in the history
…ionality uncommented
  • Loading branch information
mohiiit committed Oct 15, 2024
1 parent 90dd1cd commit 97e9d90
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 76 deletions.
10 changes: 8 additions & 2 deletions src/contract_clients/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/tests/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
70 changes: 37 additions & 33 deletions src/tests/erc20_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@ 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(
clients: &Config,
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;
Expand Down Expand Up @@ -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(())
}
68 changes: 31 additions & 37 deletions src/tests/eth_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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(())
}
5 changes: 3 additions & 2 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 97e9d90

Please sign in to comment.