Skip to content

Commit

Permalink
fixup! Persist coordinator payments in database
Browse files Browse the repository at this point in the history
  • Loading branch information
da-kami committed Jul 13, 2023
1 parent b930f5e commit 7efa247
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
5 changes: 3 additions & 2 deletions coordinator/migrations/2023-07-10-060138_payments/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ CREATE TABLE IF NOT EXISTS "payments" (
htlc_status "Htlc_Status_Type" NOT NULL,
amount_msat BIGINT,
flow "Payment_Flow_Type" NOT NULL,
created_at BIGINT NOT NULL,
updated_at BIGINT NOT NULL,
payment_timestamp timestamp WITH TIME ZONE NOT NULL,
created_at timestamp WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
description TEXT NOT NULL DEFAULT ''
);
33 changes: 11 additions & 22 deletions coordinator/src/db/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ pub struct Payment {
pub htlc_status: HtlcStatus,
pub amount_msat: Option<i64>,
pub flow: PaymentFlow,
pub created_at: i64,
pub updated_at: i64,
pub payment_timestamp: OffsetDateTime,
pub created_at: OffsetDateTime,
pub updated_at: OffsetDateTime,
pub description: String,
}

Expand Down Expand Up @@ -71,17 +72,14 @@ pub fn get_all(

impl From<(lightning::ln::PaymentHash, ln_dlc_node::PaymentInfo)> for NewPayment {
fn from((payment_hash, info): (lightning::ln::PaymentHash, ln_dlc_node::PaymentInfo)) -> Self {
let timestamp = info.timestamp.unix_timestamp();

Self {
payment_hash: payment_hash.0.to_hex(),
preimage: info.preimage.map(|preimage| preimage.0.to_hex()),
secret: info.secret.map(|secret| secret.0.to_hex()),
htlc_status: info.status.into(),
amount_msat: info.amt_msat.to_inner().map(|amt| amt as i64),
flow: info.flow.into(),
created_at: timestamp,
updated_at: timestamp,
payment_timestamp: info.timestamp,
description: info.description,
}
}
Expand Down Expand Up @@ -112,27 +110,19 @@ impl TryFrom<Payment> for (lightning::ln::PaymentHash, ln_dlc_node::PaymentInfo)
})
.transpose()?;

let status = value.htlc_status.into();

let amt_msat =
ln_dlc_node::MillisatAmount::new(value.amount_msat.map(|amount| amount as u64));

let flow = value.flow.into();

let timestamp = OffsetDateTime::from_unix_timestamp(value.created_at)?;

let description = value.description;

Ok((
payment_hash,
ln_dlc_node::PaymentInfo {
preimage,
secret,
status,
status: value.htlc_status.into(),
amt_msat,
flow,
timestamp,
description,
flow: value.flow.into(),
timestamp: value.payment_timestamp,
description: value.description,
},
))
}
Expand Down Expand Up @@ -179,8 +169,7 @@ pub(crate) struct NewPayment {
#[diesel(sql_type = Nullable<BigInt>)]
pub amount_msat: Option<i64>,
pub flow: PaymentFlow,
pub created_at: i64,
pub updated_at: i64,
pub payment_timestamp: OffsetDateTime,
#[diesel(sql_type = Text)]
pub description: String,
}
Expand Down Expand Up @@ -249,8 +238,8 @@ pub fn update(
preimage: Option<lightning::ln::PaymentPreimage>,
secret: Option<lightning::ln::PaymentSecret>,
conn: &mut PgConnection,
) -> Result<i64> {
let updated_at = OffsetDateTime::now_utc().unix_timestamp();
) -> Result<OffsetDateTime> {
let updated_at = OffsetDateTime::now_utc();

let preimage = preimage.map(|preimage| preimage.0.to_hex());
let secret = secret.map(|secret| secret.0.to_hex());
Expand Down
5 changes: 3 additions & 2 deletions coordinator/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ diesel::table! {
htlc_status -> HtlcStatusType,
amount_msat -> Nullable<Int8>,
flow -> PaymentFlowType,
created_at -> Int8,
updated_at -> Int8,
payment_timestamp -> Timestamptz,
created_at -> Timestamptz,
updated_at -> Timestamptz,
description -> Text,
}
}
Expand Down

0 comments on commit 7efa247

Please sign in to comment.