Skip to content

feat(cli): add support for withdraws and claiming withdraws on erc20 tokens #163

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

Open
wants to merge 5 commits into
base: feat/sdk/support-erc20-withdraw
Choose a base branch
from
Open
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
84 changes: 64 additions & 20 deletions cli/src/commands/l2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use rex_sdk::{
client::{EthClient, eth::get_address_from_secret_key},
l2::{
deposit::{deposit_erc20, deposit_through_contract_call},
withdraw::{claim_withdraw, get_withdraw_merkle_proof, withdraw},
withdraw::{
claim_erc20withdraw, claim_withdraw, get_withdraw_merkle_proof, withdraw,
withdraw_erc20,
},
},
wait_for_transaction_receipt,
};
Expand Down Expand Up @@ -64,6 +67,19 @@ pub(crate) enum Command {
#[clap(value_parser = parse_u256)]
claimed_amount: U256,
l2_withdrawal_tx_hash: H256,
#[clap(
long = "token-l1",
help = "ERC20 token address on L1",
long_help = "Specify the token address, the base token is used as default."
)]
token_l1: Option<Address>,
#[clap(
long = "token-l2",
help = "ERC20 token address on L2",
long_help = "Specify the token address, it is required if you specify a token on L1.",
requires("token-l1")
)]
token_l2: Option<Address>,
#[clap(
long,
short = 'c',
Expand Down Expand Up @@ -208,11 +224,18 @@ pub(crate) enum Command {
#[clap(long = "nonce")]
nonce: Option<u64>,
#[clap(
long = "token",
help = "ERC20 token address",
long = "token-l1",
help = "ERC20 token address on L1",
long_help = "Specify the token address, the base token is used as default."
)]
token_address: Option<Address>,
token_l1: Option<Address>,
#[clap(
long = "token-l2",
help = "ERC20 token address on L2",
long_help = "Specify the token address, it is required if you specify a token on L1.",
requires("token-l1")
)]
token_l2: Option<Address>,
#[clap(
long,
short = 'c',
Expand Down Expand Up @@ -317,6 +340,8 @@ impl Command {
Command::ClaimWithdraw {
claimed_amount,
l2_withdrawal_tx_hash,
token_l1,
token_l2,
cast,
silent,
private_key,
Expand All @@ -338,15 +363,31 @@ impl Command {
"No withdrawal proof found for transaction {l2_withdrawal_tx_hash:#x}"
))?;

let tx_hash = claim_withdraw(
claimed_amount,
from,
private_key,
&eth_client,
&withdrawal_proof,
bridge_address,
)
.await?;
let tx_hash = if let Some(token_l1) = token_l1 {
let token_l2 = token_l2.expect(
"Token address on L2 is required if token address on L1 is specified",
);
claim_erc20withdraw(
token_l1,
token_l2,
claimed_amount,
private_key,
&eth_client,
&withdrawal_proof,
bridge_address,
)
.await?
} else {
claim_withdraw(
claimed_amount,
from,
private_key,
&eth_client,
&withdrawal_proof,
bridge_address,
)
.await?
};

println!("Withdrawal claim sent: {tx_hash:#x}");

Expand All @@ -357,7 +398,8 @@ impl Command {
Command::Withdraw {
amount,
nonce,
token_address,
token_l1,
token_l2,
cast,
silent,
explorer_url,
Expand All @@ -372,12 +414,14 @@ impl Command {
todo!("Display transaction URL in the explorer")
}

if token_address.is_some() {
// withdraw_erc20(..)
todo!("Handle ERC20 withdrawals")
}

let tx_hash = withdraw(amount, from, private_key, &client, nonce).await?;
let tx_hash = if let Some(token_l1) = token_l1 {
let token_l2 = token_l2.expect(
"Token address on L2 is required if token address on L1 is specified",
);
withdraw_erc20(amount, from, private_key, token_l1, token_l2, &client).await?
} else {
withdraw(amount, from, private_key, &client, nonce).await?
};

println!("Withdrawal sent: {tx_hash:#x}");

Expand Down