Cookbook
Close a clearing period
Record obligations as a clearing participant, close and settle the period in the sandbox, and read its net positions and signed report.
Needs one test key that acts for a clearing participant and carries the scope clearing.
Participants and their caps are set by Path Global, which also sets which participant a test key acts
for. In the examples the key acts for a participant named sandbox-member, and the counterparty is
the sandbox participant meridian-markets.
Obligations between participants are recorded during a period and netted when it closes. Each participant then settles its net position, not every obligation.
1. The participant of the key
const me = await pg.clearing.participant();
// { id: 'sandbox-member', kind: 'client', status: 'active', caps: [...], ... }A key that acts for no participant answers 404 pathglobal.clearing.participant_unknown on every
clearing route that needs one.
2. Record an obligation as debtor
const owed = await pg.clearing.record(
{ debtor: me.id, creditor: 'meridian-markets', amount: { value: '1200.00', asset: 'USD' }, reference: 'invoice-7781' },
{ idempotencyKey: 'obligation-invoice-7781' },
);{
"id": "ob_…",
"kind": "clearing.obligation",
"set": "USD@book",
"cycle": "cy_…",
"debtor": "sandbox-member",
"creditor": "meridian-markets",
"amount": { "value": "1200", "asset": "USD" },
"reference": "invoice-7781",
"state": "accepted",
"period": "USD@book#1"
}With no set, an obligation goes to the netting set <ASSET>@book, here USD@book. The period is
<set>#<seq>.
3. A creditor's proposal
The same call made by the creditor stores a proposal. It has no effect until its debtor accepts it, with the debtor's own key:
// As creditor: state 'proposed'.
const proposal = await pg.clearing.record({ debtor: 'meridian-markets', creditor: me.id, amount: { value: '800.00', asset: 'USD' }, reference: 'invoice-7782' });
// As debtor, with a client constructed with the debtor's key: state 'accepted'.
await debtorPg.clearing.accept(proposal.id);In the sandbox no key acts for meridian-markets, so a proposal to it is never accepted: it is
rejected once past its time.
The creditor accepting its own proposal answers 403 pathglobal.clearing.not_debtor, as does an
obligation recorded by a participant that is neither its debtor nor its creditor.
4. Positions, headroom and the queue
const positions = await pg.clearing.positions(); // [{ participant: 'sandbox-member', asset: 'USD', net: '-1200' }]
const waiting = await pg.clearing.queue(); // obligations waiting for cap headroomAn obligation that passes a cap of its debtor answers 422 pathglobal.clearing.cap_exceeded. An
obligation that waits for cap headroom is queued; queue() lists the caller's. A suspended
participant answers 409 pathglobal.clearing.participant_suspended.
| Obligation state | |
|---|---|
proposed | Stated by the creditor, waiting for the debtor |
pending | Being posted |
accepted | Recorded in the open period |
queued | Waiting for cap headroom |
rejected | Refused, or a proposal past its time |
settled | Its period is settled |
5. Close and settle the period
Outside the sandbox, a period closes at the cut-offs of its set. In the sandbox, the current cycle closes and settles on request:
const cycle = await pg.clearing.current(); // the cycle of the obligations
await pg.clearing.close(cycle.id); // test mode only: the cycle and its periods close
await pg.clearing.settle(cycle.id); // test mode only: its periods settle| Period state | |
|---|---|
open | Takes obligations |
closed | Netted: the positions are fixed |
settling | Net positions are being paid |
settled | Every net position is paid |
partially_settled | Some positions are carried to the next period |
6. Read the period and its signed report
const period = await pg.clearing.period(owed.period!);
const report = await pg.clearing.report(period.id);{
"kind": "clearing.report",
"period": "USD@book#1",
"set": "USD@book",
"state": "settled",
"gross": { "value": "1200", "asset": "USD" },
"positions": [
{ "participant": "meridian-markets", "asset": "USD", "net": "1200" },
{ "participant": "sandbox-member", "asset": "USD", "net": "-1200" }
],
"transfers": [{ "from": "sandbox-member", "to": "meridian-markets", "amount": { "value": "1200", "asset": "USD" } }],
"netting_efficiency": 0,
"instructions": [],
"carried": [],
"protocol_version": "0.2.0",
"signed_at": "…",
"kid": "pg-test-…",
"signature": "…"
}| Field | |
|---|---|
gross | The sum of the obligations of the period |
positions | The net position of each participant: negative owes, positive is owed |
transfers | The net payments that settle the positions |
netting_efficiency | Between 0 and 1: how much of the gross the netting removed |
instructions | The ids of the executions that settle the period, read with pg.settlement.execution(id). A set that settles on the book has none |
protocol_version, signed_at, kid, signature | The envelope and Path Global's signature; verify it against GET /.well-known/path-keys |
Once settled, the obligation reads settled and the running position is back to 0.
The rail reference of a net debtor
A set that settles on a network is paid by its net debtors under a rail reference of the period:
ns.<asset>.<network>.<seq>.<participant>const railReference = `ns.${period.set.replace('@', '.')}.${period.seq}.${me.id}`; // 'ns.USD.book.1.sandbox-member'Closing a set now
POST /operations/clearing/sets/:id/close (SDK operations.closeSet) closes the open period of
a set before its cut-off. It needs the scopes operations and risk, and in live mode the
signature of a registered operator. A period closed that way settles as its net debtors pay in under
their rail reference. See clearing and
operations.
Credit a deposit to its holder
A verified subject, a PATH address, a holding and a vIBAN anchored to it — a deposit credited to that holding, returned, or held for review, with its payee receipt — and a route that pays the address.
A treasury transfer under dual control
One person proposes a rebalance between two own accounts, a second person approves it, and the transfer executes — with the refusals each side can meet.