Code Examples

Conceptual examples showing BSV's atomic state transaction patterns with BEEF workflow for peer-to-peer transmission and Extended Format for network broadcasting.

🔄 BEEF Workflow Patterns

Atomic Token Creation with BEEF

// User application creates atomic token operation
// Wallet generates BEEF transaction for P2P transmission
async function createAtomicToken() {
  // Connect to BRC-100 compliant wallet
  // const wallet = new WalletClient();
  
  // Create atomic token creation transaction
  // const response = await wallet.createAction({
  //   description: 'create event ticket token',
  //   outputs: [{
  //     satoshis: 1,
  //     lockingScript: Script.fromASM('OP_NOP').toHex(),
  //     basket: 'event tickets',
  //     outputDescription: 'VIP concert ticket #1234'
  //   }]
  // });
  
  // Wallet returns BEEF format for P2P transmission
  // Contains all SPV data needed for instant verification
  // No coordination with external services required
  // return response.BEEF; // Ready for counterparty transmission
}

Backend BEEF Reception and Processing

// Backend receives BEEF transaction from user application
// Validates and processes atomically, then converts to Extended Format
async function processBEEFTransaction(beefData: string) {
  // Validate BEEF format and SPV proofs
  // const beefTx = BEEF.fromHex(beefData);
  // const isValid = await beefTx.verify(); // Instant SPV verification
  
  // Apply business logic atomically
  // if (isValid) {
  //   // Process atomic state transition
  //   // Update topic manager state
  //   // Update basket manager allocations
  //   
  //   // Convert BEEF to Extended Format for node broadcasting
  //   const extendedFormat = beefTx.toExtendedFormat();
  //   await broadcastToNetwork(extendedFormat);
  // }
  
  // Atomic success/failure - no partial states
  // Either complete transaction or complete rollback
}

Extended Format Network Broadcasting

// Backend converts BEEF to Extended Format for BSV network
// Extended Format enables broadcast services to validate without node RPC
async function broadcastToNetwork(extendedFormatTx: string) {
  // Extended Format includes previous locking scripts and satoshi amounts
  // Broadcast service can validate without contacting nodes
  // const broadcastService = new BroadcastService();
  // const result = await broadcastService.submit(extendedFormatTx);
  
  // Network propagation via standard BSV protocol
  // Atomic operation complete once broadcast accepted
  // return result.txid;
}

🚀 Atomic State Transition Patterns

Atomic Marketplace Exchange

// Atomic asset purchase - no coordination complexity
async function atomicMarketplacePurchase() {
  // List available assets from basket
  // const assets = await wallet.listOutputs({
  //   basket: 'marketplace items',
  //   include: 'entire transactions' // Gets BEEF data
  // });
  
  // Create atomic purchase transaction
  // const purchase = await wallet.createAction({
  //   description: 'atomic asset purchase',
  //   inputBEEF: assets.BEEF, // SPV data for inputs
  //   inputs: [{
  //     outpoint: assets.outputs[0].outpoint,
  //     unlockingScript: purchaseScript.toHex(),
  //     inputDescription: 'digital asset being purchased'
  //   }],
  //   outputs: [{
  //     satoshis: assetValue,
  //     lockingScript: buyerOwnershipScript.toHex(),
  //     outputDescription: 'asset ownership transfer'
  //   }]
  // });
  
  // Asset ownership and payment happen atomically
  // No race conditions, no partial failure states
  // BEEF format enables P2P transmission to seller
}

Atomic Inventory Management

// Each inventory item as spendable token eliminates overselling
async function atomicInventoryPurchase() {
  // Inventory items are individual UTXOs in baskets
  // const inventory = await wallet.listOutputs({
  //   basket: 'product inventory',
  //   filter: { product: 'iPhone_case_model_47' }
  // });
  
  // Atomic purchase consumes inventory token
  // const sale = await wallet.createAction({
  //   description: 'purchase inventory item',
  //   inputBEEF: inventory.BEEF,
  //   inputs: [{
  //     outpoint: inventory.outputs[0].outpoint, // Specific inventory item
  //     unlockingScript: Script.fromASM('OP_TRUE').toHex(),
  //     inputDescription: 'inventory token consumption'
  //   }],
  //   outputs: [{
  //     satoshis: 1,
  //     lockingScript: customerOwnershipScript.toHex(),
  //     outputDescription: 'customer ownership token'
  //   }]
  // });
  
  // Mathematically impossible to oversell
  // Each item can only be spent once
  // No cache invalidation or coordination needed
}

Atomic Multi-Party Operations

// Complex atomic operations involving multiple parties
async function atomicMultiPartyTransaction() {
  // Topic manager coordinates atomic state transitions
  // const topicManager = new TopicManager('supply-chain-topic');
  
  // Collect inputs from multiple parties via BEEF
  // const manufacturerBEEF = await getManufacturerBEEF();
  // const distributorBEEF = await getDistributorBEEF();
  // const retailerBEEF = await getRetailerBEEF();
  
  // Create atomic multi-party transaction
  // const multiPartyTx = await wallet.createAction({
  //   description: 'atomic supply chain transfer',
  //   inputBEEF: [manufacturerBEEF, distributorBEEF, retailerBEEF],
  //   inputs: [
  //     { /* manufacturer input */ },
  //     { /* distributor input */ },
  //     { /* retailer input */ }
  //   ],
  //   outputs: [
  //     { /* updated manufacturer state */ },
  //     { /* updated distributor state */ },
  //     { /* updated retailer state */ }
  //   ]
  // });
  
  // All parties' state transitions happen atomically
  // No coordination protocols or rollback mechanisms
  // BEEF enables efficient P2P transmission between parties
}

💡 Atomic vs Coordinated Architecture

Traditional Coordinated Systems

// Complex orchestration with partial failure handling
async function traditionalDistributedOperation() {
  // const transaction = await db.beginTransaction();
  // try {
  //   const payment = await paymentService.charge(amount);
  //   const inventory = await inventoryService.reserve(item);
  //   const shipping = await shippingService.create(order);
  //   const notification = await notificationService.send(user);
  //   
  //   await transaction.commit();
  // } catch (error) {
  //   await transaction.rollback();
  //   // Complex compensating transactions
  //   // Partial failure state handling
  //   // Distributed coordination protocols
  // }
  
  // Problems:
  // - Complex coordination between services
  // - Partial failure states require rollback logic
  // - Race conditions and consistency issues
  // - Expensive distributed transaction protocols
}

Atomic Transaction Approach

// Single atomic operation eliminates coordination complexity
async function atomicOperation() {
  // const atomicPurchase = await wallet.createAction({
  //   description: 'complete purchase operation',
  //   inputBEEF: [paymentBEEF, inventoryBEEF],
  //   inputs: [
  //     { outpoint: paymentUTXO, /* payment input */ },
  //     { outpoint: inventoryUTXO, /* inventory input */ }
  //   ],
  //   outputs: [
  //     { /* customer receipt and ownership */ },
  //     { /* merchant payment and inventory update */ }
  //   ]
  // });
  
  // Benefits:
  // - Single atomic operation - succeeds or fails completely
  // - No coordination protocols needed
  // - No rollback logic required
  // - No race conditions possible
  // - BEEF enables efficient P2P transmission
  // - SPV provides instant verification
}

🔗 BEEF Workflow Benefits

Instant Verification

  • SPV Proofs: BEEF contains all data needed for instant validation

  • No Node Dependencies: Validate transactions without contacting BSV nodes

  • P2P Efficiency: Direct transmission between counterparties

  • Reduced Latency: No waiting for network confirmations

Atomic Guarantees

  • All-or-Nothing: Operations succeed or fail completely

  • No Partial States: Eliminates complex state management

  • Mathematical Certainty: Cryptographic guarantees replace coordination

  • Simplified Logic: Atomic operations are easier to reason about

Development Advantages

  • LARS Standardization: Consistent development environment

  • Reduced Complexity: No distributed coordination code

  • Better Performance: Elimination of coordination overhead

  • Fewer Bugs: No partial failure states to handle

🛠️ Atomic Development Stack

The tools that enable atomic state transactions:

  • LARS - Standardized atomic development environment

  • BRC-100 Wallets - BEEF generation and atomic signing

  • BSV TS-SDK - BEEF processing and Extended Format conversion

  • Topic Managers - Atomic operations with shared context

  • Basket Managers - UTXO organization for atomic operations

🎯 Next Steps

  1. Experience Atomic Transactions - Try fast.brc.dev for live BEEF workflow

  2. Set up LARS - Follow our hackathon guide

  3. Master BEEF Workflow - Understand P2P transmission patterns

  4. Learn Atomic Patterns - Explore the technical pathway

  5. Think Atomically - Replace coordination with atomic operations


Ready to eliminate coordination complexity? The Developer FAQ explains how atomic transactions with BEEF workflow fundamentally change distributed system architecture.

Last updated

Was this helpful?