Skip to content

Commit

Permalink
Adding deposit final check
Browse files Browse the repository at this point in the history
  • Loading branch information
scx1332 committed Mar 12, 2024
1 parent 3c26ae3 commit 27ba86d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 20 deletions.
8 changes: 3 additions & 5 deletions crates/erc20_payment_lib/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::{err_custom_create, err_from};
use erc20_payment_lib_common::create_sqlite_connection;
use erc20_payment_lib_common::ops::{
cleanup_allowance_tx, cleanup_token_transfer_tx, delete_tx, get_last_unsent_tx,
get_transaction_chain, get_transactions, get_unpaid_token_transfers, insert_token_transfer,
insert_tx,
get_transaction_chain, get_transactions, get_unpaid_token_transfers,
insert_token_transfer_with_deposit_check, insert_tx,
};
use std::collections::BTreeMap;
use std::ops::DerefMut;
Expand Down Expand Up @@ -921,9 +921,7 @@ impl PaymentRuntime {
transfer_args.deposit_id,
);

insert_token_transfer(&self.conn, &token_transfer)
.await
.map_err(err_from!())?;
insert_token_transfer_with_deposit_check(&self.conn, &token_transfer).await?;

if !self.setup.ignore_deadlines {
if let Some(deadline) = transfer_args.deadline {
Expand Down
1 change: 1 addition & 0 deletions crates/erc20_payment_lib/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ pub fn create_token_transfer(
token_addr: token_addr.map(|addr| format!("{addr:#x}")),
token_amount: token_amount.to_string(),
deposit_id,
deposit_final: 0,
create_date: Utc::now(),
tx_id: None,
paid_date: None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ pub struct TokenTransferDbObj {
pub token_amount: String,
/// If set payment done from internal deposit
pub deposit_id: Option<String>,
pub deposit_final: i64,
/// The time when the record is inserted into the database
/// It is overriden when inserting new entry to db
/// It is override when inserting new entry to db
pub create_date: DateTime<Utc>,
pub tx_id: Option<i64>,
pub paid_date: Option<DateTime<Utc>>,
Expand Down
64 changes: 57 additions & 7 deletions crates/erc20_payment_lib_common/src/db/ops/token_transfer_ops.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::model::TokenTransferDbObj;
use crate::db::ops::get_chain_transfers_by_chain_id;
use crate::err_from;
use crate::error::PaymentError;
use crate::error::*;
use crate::{err_custom_create, err_from};
use chrono::{DateTime, Duration, Utc};
use sqlx::Executor;
use sqlx::Sqlite;
Expand All @@ -21,8 +21,8 @@ where
{
sqlx::query_as::<_, TokenTransferDbObj>(
r"INSERT INTO token_transfer
(payment_id, from_addr, receiver_addr, chain_id, token_addr, token_amount, deposit_id, create_date, tx_id, paid_date, fee_paid, error)
VALUES ($1, $2, $3, $4, $5, $6, $7, strftime('%Y-%m-%dT%H:%M:%f', 'now'), $9, $10, $11, $12) RETURNING *;
(payment_id, from_addr, receiver_addr, chain_id, token_addr, token_amount, deposit_id, deposit_final, create_date, tx_id, paid_date, fee_paid, error)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, strftime('%Y-%m-%dT%H:%M:%f', 'now'), $9, $10, $11, $12) RETURNING *;
",
)
.bind(&token_transfer.payment_id)
Expand All @@ -32,6 +32,7 @@ VALUES ($1, $2, $3, $4, $5, $6, $7, strftime('%Y-%m-%dT%H:%M:%f', 'now'), $9, $1
.bind(&token_transfer.token_addr)
.bind(&token_transfer.token_amount)
.bind(&token_transfer.deposit_id)
.bind(token_transfer.deposit_final)
.bind(token_transfer.tx_id)
.bind(token_transfer.paid_date)
.bind(&token_transfer.fee_paid)
Expand All @@ -40,6 +41,53 @@ VALUES ($1, $2, $3, $4, $5, $6, $7, strftime('%Y-%m-%dT%H:%M:%f', 'now'), $9, $1
.await
}

pub async fn insert_token_transfer_with_deposit_check(
conn: &SqlitePool,
token_transfer: &TokenTransferDbObj,
) -> Result<TokenTransferDbObj, PaymentError> {
if let Some(deposit_id) = token_transfer.deposit_id.as_ref() {
let mut transaction = conn.begin().await.map_err(err_from!())?;
let is_finished = sqlx::query_as::<_, (i64,)>(
"SELECT COUNT(*) FROM token_transfer WHERE deposit_id=$1 AND deposit_final=1",
)
.bind(deposit_id)
.fetch_one(&mut *transaction)
.await
.map_err(err_from!())?;
if is_finished.0 > 0 {
return Err(err_custom_create!(
"Cannot add token_transfer to already finished deposit"
));
}
let res = sqlx::query_as::<_, TokenTransferDbObj>(
r"INSERT INTO token_transfer
(payment_id, from_addr, receiver_addr, chain_id, token_addr, token_amount, deposit_id, deposit_final, create_date, tx_id, paid_date, fee_paid, error)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, strftime('%Y-%m-%dT%H:%M:%f', 'now'), $9, $10, $11, $12) RETURNING *;
",
)
.bind(&token_transfer.payment_id)
.bind(&token_transfer.from_addr)
.bind(&token_transfer.receiver_addr)
.bind(token_transfer.chain_id)
.bind(&token_transfer.token_addr)
.bind(&token_transfer.token_amount)
.bind(&token_transfer.deposit_id)
.bind(token_transfer.deposit_final)
.bind(token_transfer.tx_id)
.bind(token_transfer.paid_date)
.bind(&token_transfer.fee_paid)
.bind(&token_transfer.error)
.fetch_one(&mut *transaction)
.await.map_err(err_from!())?;
transaction.commit().await.map_err(err_from!())?;
Ok(res)
} else {
insert_token_transfer(conn, token_transfer)
.await
.map_err(err_from!())
}
}

pub async fn remap_token_transfer_tx<'c, E>(
executor: E,
old_tx_id: i64,
Expand Down Expand Up @@ -96,10 +144,11 @@ chain_id = $5,
token_addr = $6,
token_amount = $7,
deposit_id = $8,
tx_id = $9,
paid_date = $10,
fee_paid = $11,
error = $12
deposit_final = $9,
tx_id = $10,
paid_date = $11,
fee_paid = $12,
error = $13
WHERE id = $1
",
)
Expand All @@ -111,6 +160,7 @@ WHERE id = $1
.bind(&token_transfer.token_addr)
.bind(&token_transfer.token_amount)
.bind(&token_transfer.deposit_id)
.bind(token_transfer.deposit_final)
.bind(token_transfer.tx_id)
.bind(token_transfer.paid_date)
.bind(&token_transfer.fee_paid)
Expand Down
15 changes: 8 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use erc20_payment_lib::signer::PrivateKeySigner;
use erc20_payment_lib_common::create_sqlite_connection;
use erc20_payment_lib_common::error::*;
use erc20_payment_lib_common::ops::{
get_next_transactions_to_process, insert_token_transfer, update_token_transfer,
get_next_transactions_to_process, insert_token_transfer,
insert_token_transfer_with_deposit_check, update_token_transfer,
};
use erc20_payment_lib_common::*;

Expand Down Expand Up @@ -439,9 +440,9 @@ async fn main_internal() -> Result<(), PaymentError> {
.get(account_no)
.expect("No public adss found with specified account_no")
} else {
*public_addrs.first().expect("No public adss found")
*public_addrs.first().expect("No public address found")
};
let mut db_transaction = conn.clone().unwrap().begin().await.unwrap();
//let mut db_transaction = conn.clone().unwrap().begin().await.unwrap();

let amount_str = if let Some(amount) = single_transfer_options.amount {
amount.to_u256_from_eth().unwrap().to_string()
Expand Down Expand Up @@ -490,8 +491,8 @@ async fn main_internal() -> Result<(), PaymentError> {
};
let amount_decimal = amount_str.to_eth().unwrap();

let mut tt = insert_token_transfer(
&mut *db_transaction,
let mut tt = insert_token_transfer_with_deposit_check(
&conn.clone().unwrap(),
&TokenTransferDbObj {
id: 0,
payment_id: None,
Expand All @@ -501,6 +502,7 @@ async fn main_internal() -> Result<(), PaymentError> {
token_addr: token,
token_amount: amount_str,
deposit_id: single_transfer_options.deposit_id,
deposit_final: 0,
create_date: Default::default(),
tx_id: None,
paid_date: None,
Expand All @@ -513,11 +515,10 @@ async fn main_internal() -> Result<(), PaymentError> {

let payment_id = format!("{}_transfer_{}", single_transfer_options.token, tt.id);
tt.payment_id = Some(payment_id.clone());
update_token_transfer(&mut *db_transaction, &tt)
update_token_transfer(&conn.clone().unwrap(), &tt)
.await
.unwrap();

db_transaction.commit().await.unwrap();
log::info!(
"Transfer added to db amount: {}, payment id: {}",
amount_decimal,
Expand Down

0 comments on commit 27ba86d

Please sign in to comment.