Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat : Safe Proxy changes and Wallet init changes #5

Merged
merged 4 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ ROLLUP_PRIV_KEY=""
ROLLUP_SEQ_URL=""
SN_OS_CONFIG_HASH_VERSION=""
SN_OS_PROGRAM_HASH=""
CROSS_CHAIN_WAIT_TIME=0
CROSS_CHAIN_WAIT_TIME=0
L1_MULTISIG_ADDRESS=""
L2_MULTISIG_ADDRESS=""
VERIFIER_ADDRESS=""
24 changes: 13 additions & 11 deletions Cargo.lock

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

19 changes: 10 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ sp-core = { git = "https://github.com/massalabs/polkadot-sdk", branch = "release
] }

# Zaun Deps
ethereum-instance = { git = "https://github.com/karnotxyz/zaun", package = "ethereum-instance", branch = "main" }
starkgate-manager-client = { git = "https://github.com/karnotxyz/zaun", package = "starkgate-manager-client", branch = "main" }
starkgate-registry-client = { git = "https://github.com/karnotxyz/zaun", package = "starkgate-registry-client", branch = "main" }
starknet-core-contract-client = { git = "https://github.com/karnotxyz/zaun", package = "starknet-core-contract-client", branch = "main" }
starknet-erc20-client = { git = "https://github.com/karnotxyz/zaun", package = "starknet-erc20-client", branch = "main" }
starknet-eth-bridge-client = { git = "https://github.com/karnotxyz/zaun", package = "starknet-eth-bridge-client", branch = "main" }
starknet-proxy-client = { git = "https://github.com/karnotxyz/zaun", package = "starknet-proxy-client", branch = "main" }
starknet-token-bridge-client = { git = "https://github.com/karnotxyz/zaun", package = "starknet-token-bridge-client", branch = "main" }
zaun-utils = { git = "https://github.com/karnotxyz/zaun", package = "utils", branch = "main" }
ethereum-instance = { git = "https://github.com/karnotxyz/zaun", package = "ethereum-instance", branch = "feat/proxy-update" }
starkgate-manager-client = { git = "https://github.com/karnotxyz/zaun", package = "starkgate-manager-client", branch = "feat/proxy-update" }
starkgate-registry-client = { git = "https://github.com/karnotxyz/zaun", package = "starkgate-registry-client", branch = "feat/proxy-update" }
starknet-core-contract-client = { git = "https://github.com/karnotxyz/zaun", package = "starknet-core-contract-client", branch = "feat/proxy-update" }
starknet-erc20-client = { git = "https://github.com/karnotxyz/zaun", package = "starknet-erc20-client", branch = "feat/proxy-update" }
starknet-eth-bridge-client = { git = "https://github.com/karnotxyz/zaun", package = "starknet-eth-bridge-client", branch = "feat/proxy-update" }
starknet-proxy-client = { git = "https://github.com/karnotxyz/zaun", package = "starknet-proxy-client", branch = "feat/proxy-update" }
starknet-token-bridge-client = { git = "https://github.com/karnotxyz/zaun", package = "starknet-token-bridge-client", branch = "feat/proxy-update" }
zaun-utils = { git = "https://github.com/karnotxyz/zaun", package = "utils", branch = "feat/proxy-update" }

# Starknet Deps
blockifier = { git = "https://github.com/bidzyyys/blockifier", branch = "feature/scale-codec" }
Expand Down Expand Up @@ -68,6 +68,7 @@ thiserror = "1.0.50"
tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread"] }

# Subxt Dependencies
bytes = "1.6.0"
flate2 = "1.0.30"
indexmap = "2.2.6"
inline_colorization = "0.1.6"
Expand Down
71 changes: 65 additions & 6 deletions src/contract_clients/eth_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ethers::providers::Middleware;
use ethers::types::{Bytes, U256};
use starknet_accounts::{Account, ConnectedAccount};
use starknet_eth_bridge_client::clients::eth_bridge::StarknetEthBridgeContractClient;
use starknet_eth_bridge_client::deploy_starknet_eth_bridge_behind_unsafe_proxy;
use starknet_eth_bridge_client::deploy_starknet_eth_bridge_behind_safe_proxy;
use starknet_eth_bridge_client::interfaces::eth_bridge::StarknetEthBridgeTrait;
use starknet_ff::FieldElement;
use starknet_providers::jsonrpc::HttpTransport;
Expand All @@ -30,7 +30,7 @@ pub struct StarknetLegacyEthBridge {
#[async_trait]
impl BridgeDeployable for StarknetLegacyEthBridge {
async fn deploy(client: Arc<LocalWalletSignerMiddleware>) -> Self {
let eth_bridge = deploy_starknet_eth_bridge_behind_unsafe_proxy(client.clone())
let eth_bridge = deploy_starknet_eth_bridge_behind_safe_proxy(client.clone())
.await
.expect("Failed to deploy starknet contract");

Expand All @@ -43,6 +43,10 @@ impl StarknetLegacyEthBridge {
self.eth_bridge.address()
}

pub fn implementation_address(&self) -> Address {
self.eth_bridge.implementation_address()
}

pub fn client(&self) -> Arc<LocalWalletSignerMiddleware> {
self.eth_bridge.client()
}
Expand Down Expand Up @@ -72,7 +76,7 @@ impl StarknetLegacyEthBridge {
.unwrap();
let contract_address = get_contract_address_from_deploy_tx(account.provider(), &deploy_tx).await.unwrap();

log::debug!("contract address (eth bridge) : {:?}", contract_address);
log::debug!("🎡 contract address (eth bridge) : {:?}", contract_address);

let add_implementation_txn = invoke_contract(
legacy_eth_bridge_proxy_address,
Expand Down Expand Up @@ -110,6 +114,7 @@ impl StarknetLegacyEthBridge {
}

/// Initialize Starknet Legacy Eth Bridge
/// IMP : only need to be called when using unsafe proxy
pub async fn initialize(&self, messaging_contract: Address) {
let empty_bytes = [0u8; 32];

Expand All @@ -127,6 +132,60 @@ impl StarknetLegacyEthBridge {
self.eth_bridge.initialize(Bytes::from(calldata)).await.expect("Failed to initialize eth bridge");
}

/// Add Implementation Starknet Legacy Eth Bridge
pub async fn add_implementation_eth_bridge(&self, messaging_contract: Address) {
let empty_bytes = [0u8; 32];

let messaging_bytes = messaging_contract.as_bytes();

let mut padded_messaging_bytes = Vec::with_capacity(32);
padded_messaging_bytes.extend(vec![0u8; 32 - messaging_bytes.len()]);
padded_messaging_bytes.extend_from_slice(messaging_bytes);

let mut calldata = Vec::new();
// `empty_bytes` act as an empty params for the calldata we are passing in bytes.
// Here in this case of ETH Bridge it represents the EIC contract address, Token Address (ETH)
// EIC = 0x0000000000000000000000000000000000000000
// ETH Address to be passed in bridge = 0x0000000000000000000000000000000000000000
calldata.extend(empty_bytes);
calldata.extend(empty_bytes);
Comment on lines +150 to +151
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's add comments on what these empty bytes represent

calldata.extend(padded_messaging_bytes);

log::debug!("🎡 add_implementation_eth_bridge : bytes : {:?}", Bytes::from(calldata.clone()));

self.eth_bridge
.add_implementation(Bytes::from(calldata), self.implementation_address(), false)
.await
.expect("Failed to initialize eth bridge");
}

/// Upgrade To Starknet Legacy Eth Bridge
pub async fn upgrade_to_eth_bridge(&self, messaging_contract: Address) {
let empty_bytes = [0u8; 32];

let messaging_bytes = messaging_contract.as_bytes();

let mut padded_messaging_bytes = Vec::with_capacity(32);
padded_messaging_bytes.extend(vec![0u8; 32 - messaging_bytes.len()]);
padded_messaging_bytes.extend_from_slice(messaging_bytes);

let mut calldata = Vec::new();
// `empty_bytes` act as an empty params for the calldata we are passing in bytes.
// Here in this case of ETH Bridge it represents the EIC contract address, Token Address (ETH)
// EIC = 0x0000000000000000000000000000000000000000
// ETH Address to be passed in bridge = 0x0000000000000000000000000000000000000000
calldata.extend(empty_bytes);
calldata.extend(empty_bytes);
Comment on lines +177 to +178
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

calldata.extend(padded_messaging_bytes);

log::debug!("🎡 upgrade_to_eth_bridge : bytes : {:?}", Bytes::from(calldata.clone()));

self.eth_bridge
.upgrade_to(Bytes::from(calldata), self.implementation_address(), false)
.await
.expect("Failed to initialize eth bridge");
}

/// Sets up the Eth bridge with the specified data
pub async fn setup_l1_bridge(&self, max_total_balance: &str, max_deposit: &str, l2_bridge: FieldElement) {
self.eth_bridge.set_max_total_balance(U256::from_dec_str(max_total_balance).unwrap()).await.unwrap();
Expand All @@ -150,12 +209,12 @@ impl StarknetLegacyEthBridge {
)
.await;

log::debug!("setup_l2_bridge : l2 bridge initialized //");
log::debug!("🎡 setup_l2_bridge : l2 bridge initialized //");
wait_for_transaction(rpc_provider, tx.transaction_hash, "setup_l2_bridge : initialize").await.unwrap();

let tx = invoke_contract(l2_bridge_address, "set_l2_token", vec![erc20_address], account).await;

log::debug!("setup_l2_bridge : l2 token set //");
log::debug!("🎡 setup_l2_bridge : l2 token set //");
wait_for_transaction(rpc_provider, tx.transaction_hash, "setup_l2_bridge : set_l2_token").await.unwrap();

let tx = invoke_contract(
Expand All @@ -166,7 +225,7 @@ impl StarknetLegacyEthBridge {
)
.await;

log::debug!("setup_l2_bridge : l1 bridge set //");
log::debug!("🎡 setup_l2_bridge : l1 bridge set //");
wait_for_transaction(rpc_provider, tx.transaction_hash, "setup_l2_bridge : set_l1_bridge").await.unwrap();
}

Expand Down
Loading
Loading