Payments

Cookbook

A payment versus payment

Exchange USD for EUR between two holdings on the book, each side under its own Proof verdict, as one atomic execution.

Needs one test key with the scopes accounts, proof and settlement.

Two parties exchange 1 000.00 USD for 915.00 EUR. Each side pays in from a holding of the caller and receives on another. Both pay-ins are on the book, so the exchange is atomic: both sides settle, or neither does.

1. Two holdings per party, on the book

const acme = await pg.accounts.openOmni({
  holder: { name: 'Acme Treasury' },
  holdings: [{ kind: 'virtual_account', currency: 'USD' }, { kind: 'virtual_account', currency: 'EUR' }],
});
const example = await pg.accounts.openOmni({
  holder: { name: 'Example Treasury Ltd' },
  holdings: [{ kind: 'virtual_account', currency: 'USD' }, { kind: 'virtual_account', currency: 'EUR' }],
});
const [acmeUsd, acmeEur] = acme.holdings.map((h) => h.id);
const [exampleUsd, exampleEur] = example.holdings.map((h) => h.id);

// Test mode only.
await pg.accounts.fund(acmeUsd, { value: '10000.00', asset: 'USD' }, 'sandbox funding usd');
await pg.accounts.fund(exampleEur, { value: '10000.00', asset: 'EUR' }, 'sandbox funding eur');

/** The ledger account of a holding, as a side of an exchange names it. */
const book = (holding: string, asset: string) => `client:-:${holding}:${asset}`;

2. A Proof verdict per side

const usd = { value: '1000.00', asset: 'USD' };
const eur = { value: '915.00', asset: 'EUR' };

const verdictA = await pg.proof.check({ payer: { name: 'Acme Treasury' }, payee: { name: 'Example Treasury Ltd' }, amount: usd, networks: ['book'] });
const verdictB = await pg.proof.check({ payer: { name: 'Example Treasury Ltd' }, payee: { name: 'Acme Treasury' }, amount: eur, networks: ['book'] });
// verdictA.verdict === 'pass', verdictB.verdict === 'pass'

Each side names a passing verdict, asked for its own payer, payee, amount and networks. A verdict is read by the client that asked for it.

3. The exchange

import { createHash } from 'node:crypto';
import type { ExchangeRequest, ExchangeSide, Money } from '@pathglobal/sdk';

const sha256 = (s: string) => createHash('sha256').update(s).digest('hex');

function side(party: string, gives: Money, from: string, receivesAt: string, receives: string, verdict: string, payer: string, payee: string): ExchangeSide {
  return {
    party,
    gives,
    on: { asset: gives.asset, network: 'book' },
    from,
    receives_at: receivesAt,
    verdict,
    subject: {
      amount: gives,
      from: { asset: gives.asset, network: 'book' },
      to: { asset: receives, network: 'book' },
      networks: ['book'],
      payer: { name: payer },
      payee: { name: payee },
      destination_digest: sha256(receivesAt),
    },
  };
}

const request: ExchangeRequest = {
  type: 'pvp',
  reference: 'fx-2026-11-18-001',
  a: side('acme', usd, book(acmeUsd, 'USD'), book(acmeEur, 'EUR'), 'EUR', verdictA.id, 'Acme Treasury', 'Example Treasury Ltd'),
  b: side('example', eur, book(exampleEur, 'EUR'), book(exampleUsd, 'USD'), 'USD', verdictB.id, 'Example Treasury Ltd', 'Acme Treasury'),
};

const execution = await pg.settlement.exchange(request);
Field of a side
partyA name for the side, up to 64 characters of [A-Za-z0-9_.-]
gives, onWhat the side pays, and the asset and network it pays on
fromWhere it pays in from: client:-:<holding>:<asset> for a holding of the caller
receives_atWhere it receives what the other side gives
verdictThe Proof verdict of the side
subject.destination_digestThe SHA-256, in hex, of receives_at

The answer is the execution:

{
  "id": "in_…",
  "kind": "pathglobal.execution",
  "type": "pvp",
  "mode": "commit_first",
  "guarantee": "atomic",
  "legs": [
    { "role": "pay_in", "side": "a", "state": "final", "amount": { "value": "1000.00", "asset": "USD" } },
    { "role": "pay_out", "side": "a", "state": "final", "amount": { "value": "915.00", "asset": "EUR" } },
    { "role": "pay_in", "side": "b", "state": "final", "amount": { "value": "915.00", "asset": "EUR" } },
    { "role": "pay_out", "side": "b", "state": "final", "amount": { "value": "1000.00", "asset": "USD" } }
  ],
  "holding": [],
  "state": "settled"
}

Read it again with pg.settlement.execution(execution.id) until state is settled. The four holdings then read 9 000.00 USD and 915.00 EUR for Acme Treasury, 1 000.00 USD and 9 085.00 EUR for Example Treasury Ltd.

4. The same request returns the same execution

const again = await pg.settlement.exchange(request); // again.id === execution.id

An exchange is idempotent per caller and reference. Other content under the same reference answers 409 pathglobal.core.conflict:

await pg.settlement.exchange({ ...request, timeout_seconds: 600 }); // throws pathglobal.core.conflict

5. Only your own holdings pay in

A from that is not a holding of the caller answers 404 pathglobal.core.not_found, as an account that does not exist. Nothing is held.

The guarantee of an exchange

atomic holds when both pay-ins are on the book. An exchange whose sides arrive on networks takes the weaker of the levels of its two directions:

const { guarantee } = await pg.settlement.guarantees({ in: 'book', out: 'book' }); // 'atomic'

See guarantee levels and settlement.

On this page