Subscribe to real-time refund, release, and freeze events with the Merchant SDK
The X402rMerchant class provides three subscription methods for watching blockchain events in real-time. Each returns an object with an unsubscribe function for cleanup.
Use watchRefundRequests() to subscribe to RefundRequested events emitted by the RefundRequest contract. The callback receives a RefundRequestEventLog object for each event.
Use watchReleases() to subscribe to ReleaseExecuted events emitted by the PaymentOperator contract. The callback receives a PaymentOperatorEventLog object for each event.
Use watchFreezeEvents() to subscribe to PaymentFrozen and PaymentUnfrozen events from a specific Freeze contract. You must provide the Freeze contract address as the first argument.
Copy
const freezeAddress: `0x${string}` = '0xFreezeContract...';const { unsubscribe } = merchant.watchFreezeEvents( freezeAddress, (event) => { if (event.eventName === 'PaymentFrozen') { console.log('Payment FROZEN:', event.args.paymentInfoHash); console.log('Frozen by:', event.args.caller); // Alert: a dispute may be in progress } else if (event.eventName === 'PaymentUnfrozen') { console.log('Payment UNFROZEN:', event.args.paymentInfoHash); console.log('Unfrozen by:', event.args.caller); // The payment can now be released } });// Later: stop watchingunsubscribe();
The freezeAddress parameter is the address of the Freeze condition contract, not the PaymentOperator. You can retrieve it from your operator config via merchant.getOperatorConfig().
All subscription methods use viem’s watchContractEvent under the hood. For reliable real-time delivery, configure your publicClient with a WebSocket transport.