mirror of
https://github.com/aljazceru/lspd.git
synced 2025-12-19 06:44:23 +01:00
lsps2: store for buy registrations
This commit is contained in:
24
lsps2/store.go
Normal file
24
lsps2/store.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package lsps2
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/breez/lspd/basetypes"
|
||||
"github.com/breez/lspd/shared"
|
||||
)
|
||||
|
||||
type RegisterBuy struct {
|
||||
LspId string
|
||||
PeerId string
|
||||
Scid basetypes.ShortChannelID
|
||||
OpeningFeeParams shared.OpeningFeeParams
|
||||
PaymentSizeMsat *uint64
|
||||
Mode OpeningMode
|
||||
}
|
||||
|
||||
var ErrScidExists = errors.New("scid exists")
|
||||
|
||||
type Lsps2Store interface {
|
||||
RegisterBuy(ctx context.Context, req *RegisterBuy) error
|
||||
}
|
||||
60
postgresql/lsps2_store.go
Normal file
60
postgresql/lsps2_store.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package postgresql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/breez/lspd/lsps2"
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
)
|
||||
|
||||
type Lsps2Store struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewLsps2Store(pool *pgxpool.Pool) *Lsps2Store {
|
||||
return &Lsps2Store{pool: pool}
|
||||
}
|
||||
|
||||
func (s *Lsps2Store) RegisterBuy(
|
||||
ctx context.Context,
|
||||
req *lsps2.RegisterBuy,
|
||||
) error {
|
||||
_, err := s.pool.Exec(
|
||||
ctx,
|
||||
`INSERT INTO lsps2.buy_registrations (
|
||||
lsp_id
|
||||
, peer_id
|
||||
, scid
|
||||
, mode
|
||||
, payment_size_msat
|
||||
, params_min_fee_msat
|
||||
, params_proportional
|
||||
, params_valid_until
|
||||
, params_min_lifetime
|
||||
, params_max_client_to_self_delay
|
||||
, params_promise
|
||||
)
|
||||
VALUES ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?`,
|
||||
req.LspId,
|
||||
req.PeerId,
|
||||
int64(uint64(req.Scid)),
|
||||
int(req.Mode),
|
||||
req.PaymentSizeMsat,
|
||||
int64(req.OpeningFeeParams.MinFeeMsat),
|
||||
req.OpeningFeeParams.Proportional,
|
||||
req.OpeningFeeParams.ValidUntil,
|
||||
req.OpeningFeeParams.MinLifetime,
|
||||
req.OpeningFeeParams.MaxClientToSelfDelay,
|
||||
req.OpeningFeeParams.Promise,
|
||||
)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "idx_lsps2_buy_registrations_scid") {
|
||||
return lsps2.ErrScidExists
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
4
postgresql/migrations/000014_lsps2_buy.down.sql
Normal file
4
postgresql/migrations/000014_lsps2_buy.down.sql
Normal file
@@ -0,0 +1,4 @@
|
||||
DROP INDEX idx_lsps2_buy_registrations_valid_until;
|
||||
DROP INDEX idx_lsps2_buy_registrations_scid;
|
||||
DROP TABLE lsps2.buy_registrations;
|
||||
DROP SCHEMA lsps2;
|
||||
17
postgresql/migrations/000014_lsps2_buy.up.sql
Normal file
17
postgresql/migrations/000014_lsps2_buy.up.sql
Normal file
@@ -0,0 +1,17 @@
|
||||
CREATE SCHEMA lsps2;
|
||||
CREATE TABLE lsps2.buy_registrations (
|
||||
id bigserial PRIMARY KEY,
|
||||
lsp_id bytea NOT NULL,
|
||||
peer_id bytea NOT NULL,
|
||||
scid bigint NOT NULL,
|
||||
mode smallint NOT NULL,
|
||||
payment_size_msat bigint NULL,
|
||||
params_min_fee_msat bigint NOT NULL,
|
||||
params_proportional bigint NOT NULL,
|
||||
params_valid_until varchar NOT NULL,
|
||||
params_min_lifetime bigint NOT NULL,
|
||||
params_max_client_to_self_delay bigint NOT NULL,
|
||||
params_promise varchar NOT NULL
|
||||
);
|
||||
CREATE UNIQUE INDEX idx_lsps2_buy_registrations_scid ON lsps2.buy_registrations (scid);
|
||||
CREATE INDEX idx_lsps2_buy_registrations_valid_until ON lsps2.buy_registrations (params_valid_until);
|
||||
Reference in New Issue
Block a user