|
1 | 1 | use crate::{
|
2 | 2 | calldata::{Value, encode_calldata},
|
3 |
| - client::{EthClient, EthClientError, Overrides, eth::L1MessageProof}, |
| 3 | + client::{ |
| 4 | + EthClient, EthClientError, Overrides, |
| 5 | + eth::{L1MessageProof, get_address_from_secret_key}, |
| 6 | + }, |
4 | 7 | l2::{
|
5 |
| - constants::{COMMON_BRIDGE_L2_ADDRESS, L2_WITHDRAW_SIGNATURE}, |
| 8 | + constants::{ |
| 9 | + CLAIM_WITHDRAWAL_ERC20_SIGNATURE, COMMON_BRIDGE_L2_ADDRESS, L2_WITHDRAW_SIGNATURE, |
| 10 | + L2_WITHDRAW_SIGNATURE_ERC20, |
| 11 | + }, |
6 | 12 | merkle_tree::merkle_proof,
|
7 | 13 | },
|
8 | 14 | };
|
@@ -42,6 +48,36 @@ pub async fn withdraw(
|
42 | 48 | .await
|
43 | 49 | }
|
44 | 50 |
|
| 51 | +pub async fn withdraw_erc20( |
| 52 | + amount: U256, |
| 53 | + from: Address, |
| 54 | + from_pk: SecretKey, |
| 55 | + token_l1: Address, |
| 56 | + token_l2: Address, |
| 57 | + l2_client: &EthClient, |
| 58 | +) -> Result<H256, EthClientError> { |
| 59 | + let data = [ |
| 60 | + Value::Address(token_l1), |
| 61 | + Value::Address(token_l2), |
| 62 | + Value::Address(from), |
| 63 | + Value::Uint(amount), |
| 64 | + ]; |
| 65 | + let withdraw_data = encode_calldata(L2_WITHDRAW_SIGNATURE_ERC20, &data) |
| 66 | + .expect("Failed to encode calldata for withdraw ERC20"); |
| 67 | + let withdraw_transaction = l2_client |
| 68 | + .build_eip1559_transaction( |
| 69 | + COMMON_BRIDGE_L2_ADDRESS, |
| 70 | + from, |
| 71 | + Bytes::from(withdraw_data), |
| 72 | + Default::default(), |
| 73 | + ) |
| 74 | + .await?; |
| 75 | + |
| 76 | + l2_client |
| 77 | + .send_eip1559_transaction(&withdraw_transaction, &from_pk) |
| 78 | + .await |
| 79 | +} |
| 80 | + |
45 | 81 | pub async fn claim_withdraw(
|
46 | 82 | amount: U256,
|
47 | 83 | from: Address,
|
@@ -92,6 +128,57 @@ pub async fn claim_withdraw(
|
92 | 128 | .await
|
93 | 129 | }
|
94 | 130 |
|
| 131 | +pub async fn claim_erc20withdraw( |
| 132 | + token_l1: Address, |
| 133 | + token_l2: Address, |
| 134 | + amount: U256, |
| 135 | + from_pk: SecretKey, |
| 136 | + eth_client: &EthClient, |
| 137 | + message_proof: &L1MessageProof, |
| 138 | + bridge_address: Address, |
| 139 | +) -> Result<H256, EthClientError> { |
| 140 | + let from = get_address_from_secret_key(&from_pk)?; |
| 141 | + let calldata_values = vec![ |
| 142 | + Value::Address(token_l1), |
| 143 | + Value::Address(token_l2), |
| 144 | + Value::Uint(amount), |
| 145 | + Value::Uint(U256::from(message_proof.batch_number)), |
| 146 | + Value::Uint(message_proof.message_id), |
| 147 | + Value::Array( |
| 148 | + message_proof |
| 149 | + .merkle_proof |
| 150 | + .clone() |
| 151 | + .into_iter() |
| 152 | + .map(|v| Value::FixedBytes(Bytes::copy_from_slice(v.as_bytes()))) |
| 153 | + .collect(), |
| 154 | + ), |
| 155 | + ]; |
| 156 | + |
| 157 | + let claim_withdrawal_data = |
| 158 | + encode_calldata(CLAIM_WITHDRAWAL_ERC20_SIGNATURE, &calldata_values)?; |
| 159 | + |
| 160 | + println!( |
| 161 | + "Claiming withdrawal with calldata: {}", |
| 162 | + hex::encode(&claim_withdrawal_data) |
| 163 | + ); |
| 164 | + |
| 165 | + let claim_tx = eth_client |
| 166 | + .build_eip1559_transaction( |
| 167 | + bridge_address, |
| 168 | + from, |
| 169 | + claim_withdrawal_data.into(), |
| 170 | + Overrides { |
| 171 | + from: Some(from), |
| 172 | + ..Default::default() |
| 173 | + }, |
| 174 | + ) |
| 175 | + .await?; |
| 176 | + |
| 177 | + eth_client |
| 178 | + .send_eip1559_transaction(&claim_tx, &from_pk) |
| 179 | + .await |
| 180 | +} |
| 181 | + |
95 | 182 | /// Returns the formatted hash of the withdrawal transaction,
|
96 | 183 | /// or None if the transaction is not a withdrawal.
|
97 | 184 | /// The hash is computed as keccak256(to || value || tx_hash)
|
|
0 commit comments