Skip to main content

Implement Idempotent Order Creation

Prevent duplicate addresses for the same order:

async function getOrCreatePaymentAddress(orderId, options) {
const order = await db.orders.get(orderId);

// Return existing address if already created
if (order.walletId) {
const wallet = await waas.getWallet(order.walletId);
return wallet.wallet;
}

// Create new address
const result = await waas.createWallet({
network: options.network,
crypto: options.crypto,
metadata: { orderId },
expiresInMinutes: options.expiresInMinutes,
});

// Store wallet ID atomically
await db.orders.update(orderId, {
walletId: result.wallet.id,
depositAddress: result.wallet.address,
});

return result.wallet;
}