// Package listing is the gate between a maker's money and a swap being lodged. // // The rule is one sentence: nothing is listed until the invoice for it is paid. That needs a record // of which invoice belongs to which listing, because the payment arrives as a lightning payment // with a hash and the listing arrives as an event with an id — the gate is what ties the two // together, and it is the only place that can answer "was this listing paid for". package listing import ( "encoding/json" "errors" "fmt" "os" "path/filepath" "sort" "time" ) // Quote is one listing that is waiting to be paid for, or that has been. type Quote struct { // ID is the swap being listed. It is the maker's identifier, and the key a listing is // published under, so one identity ties the invoice, the payment and the event together. ID string `json:"id"` // PaymentHash is the reference: a lightning invoice is paid by its hash, so the hash is what // the invoice check is asked about. PaymentHash string `json:"payment_hash"` Invoice string `json:"invoice"` AmountSats int64 `json:"amount_sats"` // FeeBPS is the margin that produced AmountSats, kept so a maker can be shown why a fee is // what it is after a price change. FeeBPS int `json:"fee_bps"` CreatedAt string `json:"created_at"` PaidAt string `json:"paid_at,omitempty"` } // Gate is the set of quotes for one gateway, stored beside the rest of its configuration. type Gate struct { Path string `json:"-"` Quotes map[string]*Quote `json:"quotes"` } // Open reads the gate's file. A missing file is an empty gate. func Open(path string) (*Gate, error) { gate := &Gate{Path: path, Quotes: map[string]*Quote{}} body, err := os.ReadFile(path) if errors.Is(err, os.ErrNotExist) { return gate, nil } if err != nil { return nil, fmt.Errorf("listing: %w", err) } if err := json.Unmarshal(body, gate); err != nil { return nil, fmt.Errorf("listing: %s: %w", path, err) } gate.Path = path if gate.Quotes == nil { gate.Quotes = map[string]*Quote{} } return gate, nil } // Save writes the gate atomically, so a crash cannot leave a listing paid for and forgotten. func (g *Gate) Save() error { if g.Path == "" { return nil } body, err := json.MarshalIndent(g, "", " ") if err != nil { return fmt.Errorf("listing: %w", err) } if err := os.MkdirAll(filepath.Dir(g.Path), 0o700); err != nil { return fmt.Errorf("listing: %w", err) } stage, err := os.CreateTemp(filepath.Dir(g.Path), ".listings-*") if err != nil { return fmt.Errorf("listing: %w", err) } name := stage.Name() if _, err := stage.Write(append(body, '\n')); err != nil { stage.Close() os.Remove(name) return fmt.Errorf("listing: %w", err) } if err := stage.Sync(); err != nil { stage.Close() os.Remove(name) return fmt.Errorf("listing: %w", err) } if err := stage.Close(); err != nil { os.Remove(name) return fmt.Errorf("listing: %w", err) } if err := os.Chmod(name, 0o600); err != nil { os.Remove(name) return fmt.Errorf("listing: %w", err) } if err := os.Rename(name, g.Path); err != nil { os.Remove(name) return fmt.Errorf("listing: %w", err) } return nil } // Record stores a quote, refusing to replace one that has already been paid: a paid listing must not // be re-quoted into an unpaid state, which would be a way to un-pay it. func (g *Gate) Record(quote Quote) error { if quote.ID == "" { return errors.New("listing: a quote needs the swap id it is for") } if quote.PaymentHash == "" || quote.Invoice == "" { return errors.New("listing: a quote needs an invoice and its payment hash") } if quote.AmountSats < 0 { return errors.New("listing: a quote cannot have a negative fee") } if existing, ok := g.Quotes[quote.ID]; ok && existing.PaidAt != "" { return fmt.Errorf("listing: %s is already paid for (%s)", quote.ID, existing.PaidAt) } if quote.CreatedAt == "" { quote.CreatedAt = time.Now().UTC().Format(time.RFC3339) } stored := quote g.Quotes[quote.ID] = &stored return g.Save() } // Settle marks a listing paid. It is called only after the wallet says the invoice settled, so it // records a fact rather than a claim. func (g *Gate) Settle(id string, at time.Time) error { quote, ok := g.Quotes[id] if !ok { return fmt.Errorf("listing: no quote for %s", id) } if quote.PaidAt != "" { return nil } quote.PaidAt = at.UTC().Format(time.RFC3339) return g.Save() } // Lodged reports whether a listing may be published: a zero fee is free and needs no quote. func (g *Gate) Lodged(id string) bool { quote, ok := g.Quotes[id] if !ok { return false } return quote.PaidAt != "" } // Pending lists the unpaid quotes, oldest first, for an operator who wants to see what is waiting. func (g *Gate) Pending() []Quote { var out []Quote for _, quote := range g.Quotes { if quote.PaidAt == "" { out = append(out, *quote) } } sort.Slice(out, func(i, j int) bool { return out[i].CreatedAt < out[j].CreatedAt }) return out }