Receiving payments with Arkade is simple and efficient. Your wallet can generate addresses for both on-chain and off-chain (Ark) payments, allowing your users to send Bitcoin to you through either method.
To receive payments, you first need to generate an address:
// Get both on-chain and off-chain addressesconst addresses = await wallet.getAddress()// The off-chain Ark address for instant paymentsconsole.log('Ark Address:', addresses.offchain)// The on-chain Bitcoin addressconsole.log('Bitcoin Address:', addresses.onchain)
try { // Create an abort controller to manage the subscription lifecycle const abortController = new AbortController(); // Subscribe to updates for your address const addresses = await wallet.getAddress(); const subscription = arkProvider.subscribeForAddress( addresses.offchain, abortController.signal ); // Process updates as they arrive for await (const update of subscription) { const vtxos = [...update.newVtxos, ...update.spentVtxos]; if (vtxos.length === 0) { continue; } // Process the received VTXOs console.log('New VTXOs received:', update.newVtxos.length); console.log('VTXOs spent:', update.spentVtxos.length); // Process new payments await processNewVtxos(update.newVtxos); }} catch (error) { console.error("Error processing address updates:", error);}
When a payment is received, you’ll want to process the update and take appropriate actions:
// Example function to process new VTXOsasync function processNewVtxos(newVtxos) { // Update your application state for (const vtxo of newVtxos) { console.log(`Received payment of ${vtxo.amount} sats`); // You might want to store the VTXO in your database await storeVtxoInDatabase(vtxo); // Notify the user showNotification(`Received ${vtxo.amount} sats!`); // Update the UI await refreshBalanceDisplay(); }}
When your application no longer needs to listen for updates, make sure to clean up the subscription:
// Clean up the subscription when doneabortController.abort();
This is important to prevent memory leaks and unnecessary network traffic, especially when your application is closing or navigating away from the payment screen.