Skip to content

Commit

Permalink
feat: add check for existing swap to commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hydra-yse committed Nov 13, 2024
1 parent b3cc289 commit 37d6311
Showing 1 changed file with 227 additions and 98 deletions.
325 changes: 227 additions & 98 deletions lib/core/src/persist/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;
use anyhow::Result;
use rusqlite::{named_params, Connection, Row, Statement, TransactionBehavior};

use super::Persister;
use super::{PaymentState, Persister};
use crate::sync::model::{
data::{ChainSyncData, ReceiveSyncData, SendSyncData},
sync::Record,
Expand Down Expand Up @@ -306,39 +306,83 @@ impl Persister {
Self::check_commit_update(&tx, &sync_state.record_id, last_updated_at)?;
}

tx.execute(
"
UPDATE receive_swaps
SET
invoice = :invoice,
preimage = :preimage,
create_response_json = :create_response_json,
claim_fees_sat = :claim_fees_sat,
claim_private_key = :claim_private_key,
payer_amount_sat = :payer_amount_sat,
receiver_amount_sat = :receiver_amount_sat,
mrh_address = :mrh_address,
mrh_script_pubkey = :mrh_script_pubkey,
created_at = :created_at,
payment_hash = :payment_hash,
description = :description
WHERE id = :id",
named_params! {
":id": &data.swap_id,
":invoice": &data.invoice,
":preimage": &data.preimage,
":create_response_json": &data.create_response_json,
":claim_fees_sat": &data.claim_fees_sat,
":claim_private_key": &data.claim_private_key,
":payer_amount_sat": &data.payer_amount_sat,
":receiver_amount_sat": &data.receiver_amount_sat,
":mrh_address": &data.mrh_address,
":mrh_script_pubkey": &data.mrh_script_pubkey,
":created_at": &data.created_at,
":payment_hash": &data.payment_hash,
":description": &data.description,
},
)?;
let params = named_params! {
":id": &data.swap_id,
":invoice": &data.invoice,
":preimage": &data.preimage,
":create_response_json": &data.create_response_json,
":claim_fees_sat": &data.claim_fees_sat,
":claim_private_key": &data.claim_private_key,
":payer_amount_sat": &data.payer_amount_sat,
":receiver_amount_sat": &data.receiver_amount_sat,
":mrh_address": &data.mrh_address,
":mrh_script_pubkey": &data.mrh_script_pubkey,
":created_at": &data.created_at,
":payment_hash": &data.payment_hash,
":description": &data.description,
":state": PaymentState::Created,
};
match self.fetch_receive_swap_by_id(&data.swap_id)?.is_some() {
true => {
tx.execute(
"
UPDATE receive_swaps
SET
invoice = :invoice,
preimage = :preimage,
create_response_json = :create_response_json,
claim_fees_sat = :claim_fees_sat,
claim_private_key = :claim_private_key,
payer_amount_sat = :payer_amount_sat,
receiver_amount_sat = :receiver_amount_sat,
mrh_address = :mrh_address,
mrh_script_pubkey = :mrh_script_pubkey,
created_at = :created_at,
payment_hash = :payment_hash,
description = :description
WHERE id = :id",
params,
)?;
}
false => {
tx.execute(
"
INSERT INTO receive_swaps(
id,
invoice,
preimage,
create_response_json,
claim_fees_sat,
claim_private_key,
payer_amount_sat,
receiver_amount_sat,
mrh_address,
mrh_script_pubkey,
created_at,
payment_hash,
description,
state
)
VALUES(
:id,
:invoice,
:preimage,
:create_response_json,
:claim_fees_sat,
:claim_private_key,
:payer_amount_sat,
:receiver_amount_sat,
:mrh_address,
:mrh_script_pubkey,
:created_at,
:payment_hash,
:description,
:state
)",
params,
)?;
}
}

Self::set_sync_state_stmt(&tx)?.execute(named_params! {
":data_id": &sync_state.data_id,
Expand All @@ -365,33 +409,71 @@ impl Persister {
Self::check_commit_update(&tx, &sync_state.record_id, last_updated_at)?;
}

tx.execute(
"
UPDATE send_swaps
SET
invoice = :invoice,
create_response_json = :create_response_json,
refund_private_key = :refund_private_key,
payer_amount_sat = :payer_amount_sat,
receiver_amount_sat = :receiver_amount_sat,
created_at = :created_at,
preimage = :preimage,
payment_hash = :payment_hash,
description = :description
WHERE id = :id",
named_params! {
":id": &data.swap_id,
":invoice": &data.invoice,
":create_response_json": &data.create_response_json,
":refund_private_key": &data.refund_private_key,
":payer_amount_sat": &data.payer_amount_sat,
":receiver_amount_sat": &data.receiver_amount_sat,
":created_at": &data.created_at,
":preimage": &data.preimage,
":payment_hash": &data.payment_hash,
":description": &data.description,
},
)?;
let params = named_params! {
":id": &data.swap_id,
":invoice": &data.invoice,
":create_response_json": &data.create_response_json,
":refund_private_key": &data.refund_private_key,
":payer_amount_sat": &data.payer_amount_sat,
":receiver_amount_sat": &data.receiver_amount_sat,
":created_at": &data.created_at,
":preimage": &data.preimage,
":payment_hash": &data.payment_hash,
":description": &data.description,
":state": PaymentState::Created,
};
match self.fetch_send_swap_by_id(&data.swap_id)?.is_some() {
true => {
tx.execute(
"
UPDATE send_swaps
SET
invoice = :invoice,
create_response_json = :create_response_json,
refund_private_key = :refund_private_key,
payer_amount_sat = :payer_amount_sat,
receiver_amount_sat = :receiver_amount_sat,
created_at = :created_at,
preimage = :preimage,
payment_hash = :payment_hash,
description = :description
WHERE id = :id",
params,
)?;
}
false => {
tx.execute(
"
INSERT INTO send_swaps(
id,
invoice,
create_response_json,
refund_private_key,
payer_amount_sat,
receiver_amount_sat,
created_at,
preimage,
payment_hash,
description,
state
)
VALUES(
:id,
:invoice,
:create_response_json,
:refund_private_key,
:payer_amount_sat,
:receiver_amount_sat,
:created_at,
:preimage,
:payment_hash,
:description,
:state
)",
params,
)?;
}
}

Self::set_sync_state_stmt(&tx)?.execute(named_params! {
":data_id": &sync_state.data_id,
Expand All @@ -418,43 +500,90 @@ impl Persister {
Self::check_commit_update(&tx, &sync_state.record_id, last_updated_at)?;
}

tx.execute(
"
UPDATE chain_swaps
SET
preimage = :preimage,
create_response_json = :create_response_json,
direction = :direction,
lockup_address = :lockup_address,
claim_fees_sat = :claim_fees_sat,
claim_private_key = :claim_private_key,
refund_private_key = :refund_private_key,
timeout_block_height = :timeout_block_height,
payer_amount_sat = :payer_amount_sat,
receiver_amount_sat = :receiver_amount_sat,
accept_zero_conf = :accept_zero_conf,
created_at = :created_at,
description = :description,
claim_address = :claim_address
WHERE id = :id",
named_params! {
":id": &data.swap_id,
":preimage": &data.preimage,
":create_response_json": &data.create_response_json,
":direction": &data.direction,
":lockup_address": &data.lockup_address,
":claim_fees_sat": &data.claim_fees_sat,
":claim_private_key": &data.claim_private_key,
":refund_private_key": &data.refund_private_key,
":timeout_block_height": &data.timeout_block_height,
":payer_amount_sat": &data.payer_amount_sat,
":receiver_amount_sat": &data.receiver_amount_sat,
":accept_zero_conf": &data.accept_zero_conf,
":created_at": &data.created_at,
":description": &data.description,
":claim_address": &data.claim_address,
},
)?;
let params = named_params! {
":id": &data.swap_id,
":preimage": &data.preimage,
":create_response_json": &data.create_response_json,
":direction": &data.direction,
":lockup_address": &data.lockup_address,
":claim_fees_sat": &data.claim_fees_sat,
":claim_private_key": &data.claim_private_key,
":refund_private_key": &data.refund_private_key,
":timeout_block_height": &data.timeout_block_height,
":payer_amount_sat": &data.payer_amount_sat,
":receiver_amount_sat": &data.receiver_amount_sat,
":accept_zero_conf": &data.accept_zero_conf,
":created_at": &data.created_at,
":description": &data.description,
":claim_address": &data.claim_address,
":state": PaymentState::Created,
};
match self.fetch_chain_swap_by_id(&data.swap_id)?.is_some() {
true => {
tx.execute(
"
UPDATE chain_swaps
SET
preimage = :preimage,
create_response_json = :create_response_json,
direction = :direction,
lockup_address = :lockup_address,
claim_fees_sat = :claim_fees_sat,
claim_private_key = :claim_private_key,
refund_private_key = :refund_private_key,
timeout_block_height = :timeout_block_height,
payer_amount_sat = :payer_amount_sat,
receiver_amount_sat = :receiver_amount_sat,
accept_zero_conf = :accept_zero_conf,
created_at = :created_at,
description = :description,
claim_address = :claim_address
WHERE id = :id",
params,
)?;
}
false => {
tx.execute(
"
INSERT INTO chain_swaps(
id,
preimage,
create_response_json,
direction,
lockup_address,
claim_fees_sat,
claim_private_key,
refund_private_key,
timeout_block_height,
payer_amount_sat,
receiver_amount_sat,
accept_zero_conf,
created_at,
description,
claim_address,
state
VALUES(
:id,
:preimage,
:create_response_json,
:direction,
:lockup_address,
:claim_fees_sat,
:claim_private_key,
:refund_private_key,
:timeout_block_height,
:payer_amount_sat,
:receiver_amount_sat,
:accept_zero_conf,
:created_at,
:description,
:claim_address,
:state
)",
params,
)?;
}
}

Self::set_sync_state_stmt(&tx)?.execute(named_params! {
":data_id": &sync_state.data_id,
Expand Down

0 comments on commit 37d6311

Please sign in to comment.