Skip to content

multi: harden portfolio pilot api and guide - #2354

Open
jtobin wants to merge 7 commits into
lightninglabs:mainfrom
jtobin:pp-tlc
Open

jtobin wants to merge 7 commits into
lightninglabs:mainfrom
jtobin:pp-tlc

Conversation

@jtobin

@jtobin jtobin commented Oct 16, 2026

Copy link
Copy Markdown
Member

Performs some general TLC on the user-facing portfolio pilot documentation and example code. The documentation in particular is less of a verbose Opus 4.x-generated architecture guide and more of a concise Opus 5.5-generated user guide, with an emphasis on basic operation and safety. There's less of an emphasis on exotic market-making stuff for the time being.

The example code has been factored out and expanded to illustrate a bunch of interesting or useful applications, e.g. monitoring and circuit breaker functionality.

Along the way I also had Opus make some portfolio pilot tweaks to better-support some of the example flows, e.g.:

  • limit constraint checks used by the built-in pilot are now exported
  • pilot requests now carry the ID of the corresponding quote request
  • SendPayment now accepts an asset_rate_limit ceiling

@jtobin jtobin self-assigned this Oct 16, 2026
@jtobin jtobin added RFQ Work relating to TAP channel Request For Quote (RFQ). oracle no-changelog labels Oct 16, 2026
@litbot-9000

Copy link
Copy Markdown
Collaborator

@Roasbeef: review reminder
@darioAnongba: review reminder

5 similar comments
@litbot-9000

Copy link
Copy Markdown
Collaborator

@Roasbeef: review reminder
@darioAnongba: review reminder

@litbot-9000

Copy link
Copy Markdown
Collaborator

@Roasbeef: review reminder
@darioAnongba: review reminder

@litbot-9000

Copy link
Copy Markdown
Collaborator

@Roasbeef: review reminder
@darioAnongba: review reminder

@litbot-9000

Copy link
Copy Markdown
Collaborator

@Roasbeef: review reminder
@darioAnongba: review reminder

@litbot-9000

Copy link
Copy Markdown
Collaborator

@Roasbeef: review reminder
@darioAnongba: review reminder

CheckConstraints runs the rate bound, min fill, FOK and fill checks
against an rfqmsg.RequestConstraints value, a rate and an optional
fill amount. RejectForStatus maps a constraint status to its reject
error. The internal portfolio pilot calls both with the constraints
of the request.

UnmarshalPortfolioBuyConstraints and UnmarshalPortfolioSellConstraints
return the rfqmsg.RequestConstraints of a portfolio pilot buy or sell
request. A buy request yields a rate floor over asset units, a sell
request a rate ceiling over msat. A zero minimum amount is unset, IOC
is an unset execution policy, and an unknown execution policy is an
error.

MarshalPortfolioQuoteRespStatus converts a QuoteRespStatus to its
portfoliopilotrpc enum and returns an error for a status without an
RPC counterpart. MarshalPortfolioRejectErr converts an
rfqmsg.RejectErr to its portfoliopilotrpc form, mapping a reject code
without an RPC counterpart to REJECT_CODE_UNSPECIFIED.

A property test checks that constraints derived from the
portfoliopilotrpc form of a request equal the request's constraints,
and that CheckConstraints returns the same status for both.
The portfoliopilotrpc BuyRequest and SellRequest messages gain an
rfq_id field holding the ID of the quote request. The RPC portfolio
pilot client sets it for ResolveRequest and VerifyAcceptQuote calls.
SendPaymentRequest gains an optional asset_rate_limit field, a rate
ceiling in asset units per BTC. SendPayment passes it as the
AssetRateLimit of every sell order it negotiates to pay an invoice. A
request that sets asset_rate_limit together with rfq_id, a keysend
record or first-hop custom records is rejected.

The limit constraints itest pays invoices with a rate ceiling above
and below the oracle rate, and with a rate ceiling and an RFQ ID. The
channel RPC itest sends a payment with a rate ceiling and an RFQ ID.
EstimateAssetUnits returns an error when the price oracle is nil.
AddInvoice reaches it through calculateAssetMaxAmount for an invoice
denominated in satoshis.
The example pilot prices a single asset, given by -assetid or
-groupkey and -decimaldisplay, from a BTC/USD reference price polled
from CoinGecko or set with -btcusd. Peer rate hints are ignored, and
requests are rejected while the reference price is missing or more
than two minutes old.

ResolveRequest quotes the reference rate moved by -spreadppm and
checks the requester's constraints and the fill cap with
rfq.CheckConstraints. VerifyAcceptQuote checks the quote's remaining
lifetime, its deviation from the reference rate, and the constraints
of the original request.

Trading runs at three levels. Degraded trading doubles the spread
and rejects requests on a side blocked by an inventory floor; it
returns to normal when its conditions clear. Halted trading rejects
all requests while basic-portfolio-pilot.halt exists in -datadir.
The pilot writes the file, with the reason, when a quote would take
the unexpired quotes above -maxexposureusd, when the price moves more
than -maxpricejumpppm between fetches, after -maxbadquotes peer
quotes outside tolerance within ten minutes, or when the trade edge
over an hour falls below minus -maxtradelossusd.

A monitor reads settled swaps from tapd's forwarding history and
channel balances from lnd, with read-only macaroons. It values each
swap at the reference rate, degrades trading when the trade edge
falls below half of -maxtradelossusd, and degrades trading and blocks
the depleting side when the local asset balance is below -minassetusd
or the local BTC balance below -minbtcsat.

The server writes a TLS certificate, key and macaroon to -datadir on
first start and requires the macaroon on every call. The default
listen address is 127.0.0.1:8096, and the Dockerfile uses /data as
the working directory. The code is split into main.go, pilot.go,
price.go, breaker.go, monitor.go and server.go.

The .gitignore pattern for the example binary is anchored to the
repository root, so files in the example directory are tracked.
The portfolio pilot guide moves to docs/portfolio_pilot.md and covers
safety, configuration, the example pilot, writing a pilot, and
testing. safety.md gains an RFQ section, rfq.md links to the guide,
and sample-tapd.conf marks the RFQ test-only options.
Comment thread rpcserver/rpcserver.go
request := newPaymentSellOrderRequest(
rpcSpecifier, parsedReq, peerPubKey, metadata,
)
request.AssetRateLimit = assetRateLimit

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the returned rate before using the quote. An external pilot can accept a rate above this ceiling, and skipquoteacceptverify bypasses the check entirely.

return nil, err
}

b.mu.Lock()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A request already in the inner pilot can still return a quote after the breaker trips. Synchronize the final halt check and quote registration with the halt transition.

for offset := int32(0); ; offset += pageSize {
resp, err := m.tapd.ForwardingHistory(
ctx, &rfqrpc.ForwardingHistoryRequest{
MinTimestamp: uint64(from.Unix()),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ForwardingHistory filters by opened_at, so this misses swaps that opened before the cutoff and settled inside the loss window. Query by settlement time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-changelog oracle RFQ Work relating to TAP channel Request For Quote (RFQ).

Projects

Status: 🆕 New

Development

Successfully merging this pull request may close these issues.

3 participants