Fix foreign key constraint violation during market creation

This commit is contained in:
ekzyis 2023-11-26 23:46:28 +01:00
parent afba1c2bfb
commit aa6db6a5e1
2 changed files with 5 additions and 1 deletions

View File

@ -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

View File

@ -81,18 +81,22 @@ func HandleCreateMarket(sc context.ServerContext) echo.HandlerFunc {
// TODO: add [market:<id>] 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
}