Skip to content

Commit

Permalink
w
Browse files Browse the repository at this point in the history
  • Loading branch information
scx1332 committed Mar 8, 2024
1 parent b146b83 commit 300a699
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 14 deletions.
6 changes: 6 additions & 0 deletions crates/erc20_payment_lib/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ pub fn deposit_id_from_nonce(funder: Address, nonce: u64) -> U256 {
U256::from_big_endian(&slice)
}

pub fn nonce_from_deposit_id(deposit_id: U256) -> u64 {
let mut slice: [u8; 32] = [0; 32];
deposit_id.to_big_endian(&mut slice);
u64::from_be_bytes(slice[24..32].try_into().unwrap())
}

pub async fn get_deposit_details(
web3: Arc<Web3RpcPool>,
deposit_id: U256,
Expand Down
21 changes: 10 additions & 11 deletions crates/erc20_payment_lib/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::account_balance::{test_balance_loop, BalanceOptions2};
use crate::config::AdditionalOptions;
use crate::contracts::CreateDepositArgs;
use crate::eth::{
deposit_id_from_nonce, get_eth_addr_from_secret, get_latest_block_info, DepositDetails,
get_eth_addr_from_secret, get_latest_block_info, nonce_from_deposit_id, DepositDetails,
};
use crate::sender::service_loop;
use crate::utils::{DecimalConvExt, StringConvExt, U256ConvExt};
Expand Down Expand Up @@ -1131,7 +1131,7 @@ pub struct CloseDepositOptionsInt {
pub struct TerminateDepositOptionsInt {
pub lock_contract_address: Address,
pub skip_deposit_check: bool,
pub deposit_nonce: u64,
pub deposit_id: U256,
}

pub async fn close_deposit(
Expand Down Expand Up @@ -1184,17 +1184,9 @@ pub async fn terminate_deposit(
from: Address,
opt: TerminateDepositOptionsInt,
) -> Result<(), PaymentError> {
let free_deposit_tx_id = create_terminate_deposit(
from,
opt.lock_contract_address,
chain_id,
None,
opt.deposit_nonce,
)?;

//let mut block_info: Option<Web3BlockInfo> = None;
if !opt.skip_deposit_check {
let deposit_id = deposit_id_from_nonce(from, opt.deposit_nonce);
let deposit_id = opt.deposit_id;
let deposit_details =
deposit_details(web3.clone(), deposit_id, opt.lock_contract_address).await?;
if deposit_details.amount_decimal.is_zero() {
Expand All @@ -1218,6 +1210,13 @@ pub async fn terminate_deposit(
));
}
}
let free_deposit_tx_id = create_terminate_deposit(
from,
opt.lock_contract_address,
chain_id,
None,
nonce_from_deposit_id(opt.deposit_id),
)?;

let mut db_transaction = conn.begin().await.map_err(err_from!())?;
let make_deposit_tx = insert_tx(&mut *db_transaction, &free_deposit_tx_id)
Expand Down
3 changes: 2 additions & 1 deletion src/actions/mod.rs → src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use rustc_hex::FromHexError;
use std::str::FromStr;
use web3::types::Address;

pub mod close_deposit;
pub mod check_rpc;
pub mod close_deposit;
pub mod create_deposit;
pub mod deposit_details;
pub mod scan_chain;
pub mod terminate_deposit;

pub fn check_address_name(n: &str) -> Result<Address, FromHexError> {
match n {
Expand Down
2 changes: 1 addition & 1 deletion src/actions/close_deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct CloseDepositOptions {
#[structopt(long = "skip-check", help = "Skip check deposit")]
pub skip_check: bool,

#[structopt(long = "deposit-id", help = "Deposit id to cancel.")]
#[structopt(long = "deposit-id", help = "Deposit id to close")]
pub deposit_id: String,
}

Expand Down
103 changes: 103 additions & 0 deletions src/actions/terminate_deposit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use erc20_payment_lib::config::Config;
use erc20_payment_lib::eth::deposit_id_from_nonce;
use erc20_payment_lib::runtime::{terminate_deposit, TerminateDepositOptionsInt};
use erc20_payment_lib::setup::PaymentSetup;
use erc20_payment_lib_common::err_custom_create;
use erc20_payment_lib_common::error::PaymentError;
use sqlx::SqlitePool;
use std::str::FromStr;
use structopt::StructOpt;
use web3::types::{Address, U256};

#[derive(StructOpt)]
#[structopt(about = "Allocate funds for use by payer")]
pub struct TerminateDepositOptions {
#[structopt(short = "c", long = "chain-name", default_value = "holesky")]
pub chain_name: String,

#[structopt(long = "address", help = "Address (has to have private key)")]
pub address: Option<Address>,

#[structopt(long = "account-no", help = "Address by index (for convenience)")]
pub account_no: Option<usize>,

#[structopt(long = "skip-check", help = "Skip check deposit")]
pub skip_check: bool,

#[structopt(long = "deposit-id", help = "Deposit id to terminate.")]
pub deposit_id: Option<String>,

#[structopt(long = "deposit-nonce", help = "Deposit nonce to terminate.")]
pub deposit_nonce: Option<u64>,
}

pub async fn terminate_deposit_local(
conn: SqlitePool,
terminate_deposit_options: TerminateDepositOptions,
config: Config,
public_addrs: &[Address],
) -> Result<(), PaymentError> {
log::info!("Making deposit...");
let public_addr = if let Some(address) = terminate_deposit_options.address {
address
} else if let Some(account_no) = terminate_deposit_options.account_no {
*public_addrs
.get(account_no)
.expect("No public adss found with specified account_no")
} else {
*public_addrs.first().expect("No public adss found")
};
let chain_cfg = config
.chain
.get(&terminate_deposit_options.chain_name)
.ok_or(err_custom_create!(
"Chain {} not found in config file",
terminate_deposit_options.chain_name
))?;

let payment_setup = PaymentSetup::new_empty(&config)?;
let web3 = payment_setup.get_provider(chain_cfg.chain_id)?;

if terminate_deposit_options.deposit_id.is_some()
&& terminate_deposit_options.deposit_nonce.is_some()
{
return Err(err_custom_create!("Invalid parameters: only one of `deposit_id` or `deposit_nonce` should be provided to terminate a deposit"));
}

let deposit_id = match (
terminate_deposit_options.deposit_id,
terminate_deposit_options.deposit_nonce,
) {
(Some(deposit_id), None) => U256::from_str(&deposit_id)
.map_err(|e| err_custom_create!("Invalid deposit id: {}", e))?,
(None, Some(deposit_nonce)) => deposit_id_from_nonce(public_addr, deposit_nonce),
(Some(_), Some(_)) => {
return Err(err_custom_create!("Invalid parameters: only one of `deposit_id` or `deposit_nonce` should be provided to terminate a deposit"));
}
(None, None) => {
return Err(err_custom_create!("Missing required parameters: either `deposit_id` or `deposit_nonce` must be provided to terminate a deposit"));
}
};

terminate_deposit(
web3,
&conn,
chain_cfg.chain_id as u64,
public_addr,
TerminateDepositOptionsInt {
lock_contract_address: chain_cfg
.lock_contract
.clone()
.map(|c| c.address)
.expect("No lock contract found"),
deposit_id,
skip_deposit_check: terminate_deposit_options.skip_check,
},
)
.await?;
println!(
"terminate_deposit added to queue successfully deposit id: {}",
deposit_id,
);
Ok(())
}
15 changes: 14 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ use erc20_payment_lib::{
use std::env;
use std::str::FromStr;

use crate::actions::close_deposit::close_deposit_local;
use crate::actions::check_address_name;
use crate::actions::check_rpc::check_rpc_local;
use crate::actions::close_deposit::close_deposit_local;
use crate::actions::create_deposit::make_deposit_local;
use crate::actions::deposit_details::deposit_details_local;
use crate::actions::terminate_deposit::terminate_deposit_local;
use crate::stats::{export_stats, run_stats};
use erc20_payment_lib::faucet_client::faucet_donate;
use erc20_payment_lib::misc::gen_private_keys;
Expand Down Expand Up @@ -73,6 +74,7 @@ async fn main_internal() -> Result<(), PaymentError> {
PaymentCommands::MintTestTokens { .. } => {}
PaymentCommands::CreateDeposit { .. } => {}
PaymentCommands::CloseDeposit { .. } => {}
PaymentCommands::TerminateDeposit { .. } => {}
PaymentCommands::CheckDeposit { .. } => {}
PaymentCommands::Transfer { .. } => {}
PaymentCommands::Balance { .. } => {}
Expand Down Expand Up @@ -373,6 +375,17 @@ async fn main_internal() -> Result<(), PaymentError> {
)
.await?;
}
PaymentCommands::TerminateDeposit {
terminate_deposit_options,
} => {
terminate_deposit_local(
conn.clone().unwrap(),
terminate_deposit_options,
config,
&public_addrs,
)
.await?;
}
PaymentCommands::CheckDeposit {
check_deposit_options,
} => {
Expand Down
5 changes: 5 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{fmt::Debug, path::PathBuf};

use crate::actions::close_deposit::CloseDepositOptions;
use crate::actions::create_deposit::CreateDepositOptions;
use crate::actions::terminate_deposit::TerminateDepositOptions;
use erc20_payment_lib_extra::{BalanceOptions, GenerateOptions};
use structopt::StructOpt;
use web3::types::Address;
Expand Down Expand Up @@ -399,6 +400,10 @@ pub enum PaymentCommands {
#[structopt(flatten)]
close_deposit_options: CloseDepositOptions,
},
TerminateDeposit {
#[structopt(flatten)]
terminate_deposit_options: TerminateDepositOptions,
},
CheckDeposit {
#[structopt(flatten)]
check_deposit_options: CheckDepositOptions,
Expand Down

0 comments on commit 300a699

Please sign in to comment.