From 139438d4b8ac622a20908b13e5dcec4abe7ff478 Mon Sep 17 00:00:00 2001 From: scx1332 Date: Mon, 22 Apr 2024 18:19:05 +0200 Subject: [PATCH] fix: fixing deposit id string --- crates/erc20_payment_lib/src/runtime.rs | 2 +- crates/erc20_payment_lib/src/server/web.rs | 5 ++-- crates/erc20_payment_lib/src/transaction.rs | 6 ++--- src/main.rs | 28 +++++++++++++++++++-- src/options.rs | 6 +++++ 5 files changed, 39 insertions(+), 8 deletions(-) diff --git a/crates/erc20_payment_lib/src/runtime.rs b/crates/erc20_payment_lib/src/runtime.rs index ca9ad24b..6186659d 100644 --- a/crates/erc20_payment_lib/src/runtime.rs +++ b/crates/erc20_payment_lib/src/runtime.rs @@ -383,7 +383,7 @@ pub struct TransferArgs { pub amount: U256, pub payment_id: String, pub deadline: Option>, - pub deposit_id: Option, + pub deposit_id: Option, } impl PaymentRuntime { diff --git a/crates/erc20_payment_lib/src/server/web.rs b/crates/erc20_payment_lib/src/server/web.rs index bc0d33d5..b62c19b8 100644 --- a/crates/erc20_payment_lib/src/server/web.rs +++ b/crates/erc20_payment_lib/src/server/web.rs @@ -10,6 +10,7 @@ use actix_web::http::{header, StatusCode}; use actix_web::web::Data; use actix_web::{web, HttpRequest, HttpResponse, Responder, Scope}; use chrono::{DateTime, Utc}; +use erc20_payment_lib_common::model::DepositId; use erc20_payment_lib_common::ops::*; use erc20_payment_lib_common::utils::datetime_from_u256_timestamp; use erc20_payment_lib_common::{export_metrics_to_prometheus, FaucetData}; @@ -582,7 +583,7 @@ struct TransactionRequest { chain: i64, due_date: Option, payment_id: Option, - deposit_id: Option, + deposit_id: Option, } async fn new_transfer( @@ -632,7 +633,7 @@ async fn new_transfer( amount: U256::from_dec_str(&new_transfer.amount).unwrap(), payment_id, deadline: due_date, - deposit_id: new_transfer.deposit_id.clone(), + deposit_id: new_transfer.deposit_id, }; let account = match data diff --git a/crates/erc20_payment_lib/src/transaction.rs b/crates/erc20_payment_lib/src/transaction.rs index 64e089ec..29335324 100644 --- a/crates/erc20_payment_lib/src/transaction.rs +++ b/crates/erc20_payment_lib/src/transaction.rs @@ -10,7 +10,7 @@ use crate::utils::{datetime_from_u256_timestamp, ConversionError, StringConvExt, use crate::{err_custom_create, err_from}; use chrono::Utc; use erc20_payment_lib_common::model::{ - ChainTransferDbObj, ChainTxDbObj, TokenTransferDbObj, TxDbObj, + ChainTransferDbObj, ChainTxDbObj, DepositId, TokenTransferDbObj, TxDbObj, }; use erc20_payment_lib_common::CantSignContent; use erc20_payment_lib_common::{ @@ -119,7 +119,7 @@ pub fn create_token_transfer( payment_id: Option<&str>, token_addr: Option
, token_amount: U256, - deposit_id: Option, + deposit_id: Option, ) -> TokenTransferDbObj { TokenTransferDbObj { id: 0, @@ -129,7 +129,7 @@ pub fn create_token_transfer( chain_id, token_addr: token_addr.map(|addr| format!("{addr:#x}")), token_amount: token_amount.to_string(), - deposit_id, + deposit_id: deposit_id.map(|d| d.to_db_string()), deposit_finish: 0, create_date: Utc::now(), tx_id: None, diff --git a/src/main.rs b/src/main.rs index 5c47372d..6276df64 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,13 +41,14 @@ use erc20_payment_lib::runtime::{ use erc20_payment_lib::server::web::{runtime_web_scope, ServerData}; use erc20_payment_lib::setup::PaymentSetup; use erc20_payment_lib_common::init_metrics; -use erc20_payment_lib_common::model::TokenTransferDbObj; +use erc20_payment_lib_common::model::{DepositId, TokenTransferDbObj}; use erc20_payment_lib_common::utils::{DecimalConvExt, StringConvExt}; use erc20_payment_lib_extra::{account_balance, generate_test_payments}; use rust_decimal::Decimal; use std::sync::Arc; use structopt::StructOpt; use tokio::sync::{broadcast, Mutex}; +use web3::types::U256; async fn main_internal() -> Result<(), PaymentError> { dotenv::dotenv().ok(); @@ -552,6 +553,29 @@ async fn main_internal() -> Result<(), PaymentError> { }; let amount_decimal = amount_str.to_eth().unwrap(); + let deposit_id_str = if let Some(deposit_id) = single_transfer_options.deposit_id { + let lock_contract = + if let Some(lock_contract) = single_transfer_options.lock_contract { + lock_contract + } else { + chain_cfg + .lock_contract + .clone() + .map(|c| c.address) + .expect("No lock contract found") + }; + + Some( + DepositId { + deposit_id: U256::from_str_radix(&deposit_id, 16) + .map_err(|e| err_custom_create!("Invalid deposit id: {}", e))?, + lock_address: lock_contract, + } + .to_db_string(), + ) + } else { + None + }; let mut tt = insert_token_transfer_with_deposit_check( &conn.clone().unwrap(), &TokenTransferDbObj { @@ -562,7 +586,7 @@ async fn main_internal() -> Result<(), PaymentError> { chain_id: chain_cfg.chain_id, token_addr: token, token_amount: amount_str, - deposit_id: single_transfer_options.deposit_id, + deposit_id: deposit_id_str, deposit_finish: 0, create_date: Default::default(), tx_id: None, diff --git a/src/options.rs b/src/options.rs index 8bf87944..027c8884 100644 --- a/src/options.rs +++ b/src/options.rs @@ -229,6 +229,12 @@ pub struct TransferOptions { #[structopt(long = "deposit-id")] pub deposit_id: Option, + + #[structopt( + long = "lock-contract", + help = "Lock contract address (if not specified, it will be taken from config)" + )] + pub lock_contract: Option
, } #[derive(StructOpt)]