From aa6db6a5e1d02c63af678128f7c68709fb07cea4 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sun, 26 Nov 2023 23:46:28 +0100 Subject: [PATCH] Fix foreign key constraint violation during market creation --- db/market.go | 2 +- server/router/handler/market.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/db/market.go b/db/market.go index d2ae5e2..7ba4eb0 100644 --- a/db/market.go +++ b/db/market.go @@ -19,7 +19,7 @@ func (db *DB) CreateMarket(tx *sql.Tx, ctx context.Context, market *Market) erro return err } // For now, we only support binary markets. - if _, err := db.Exec("INSERT INTO shares(market_id, description) VALUES ($1, 'YES'), ($1, 'NO')", market.Id); err != nil { + if _, err := tx.Exec("INSERT INTO shares(market_id, description) VALUES ($1, 'YES'), ($1, 'NO')", market.Id); err != nil { return err } return nil diff --git a/server/router/handler/market.go b/server/router/handler/market.go index 12f7fcb..c883cdc 100644 --- a/server/router/handler/market.go +++ b/server/router/handler/market.go @@ -81,18 +81,22 @@ func HandleCreateMarket(sc context.ServerContext) echo.HandlerFunc { // TODO: add [market:] for redirect after payment invDescription = fmt.Sprintf("create market \"%s\"", m.Description) if invoice, err = sc.Lnd.CreateInvoice(tx, ctx, sc.Db, u.Pubkey, msats, invDescription); err != nil { + tx.Rollback() return err } if qr, err = lib.ToQR(invoice.PaymentRequest); err != nil { + tx.Rollback() return err } if hash, err = lntypes.MakeHashFromStr(invoice.Hash); err != nil { + tx.Rollback() return err } go sc.Lnd.CheckInvoice(sc.Db, hash) m.InvoiceId = invoice.Id if err := sc.Db.CreateMarket(tx, ctx, &m); err != nil { + tx.Rollback() return err }