xandeum 1.0.0 copy "xandeum: ^1.0.0" to clipboard
xandeum: ^1.0.0 copied to clipboard

Dart SDK for Xandeum decentralized storage on Solana. Build file systems, manage files, and interact with pNodes.

example/example.dart

/// Example demonstrating the Xandeum SDK for Dart
///
/// This example shows how to use the SDK to interact with Xandeum's
/// decentralized file system on Solana.
///
/// Key steps:
/// 1. Create messages with SDK functions
/// 2. Compile messages with recent blockhash
/// 3. Sign and send transactions to Solana
library;

import 'package:xandeum/xandeum.dart';
import 'package:solana/solana.dart';

Future<void> main() async {
  print('═══════════════════════════════════════');
  print('  Xandeum SDK Example');
  print('═══════════════════════════════════════\n');

  // Initialize Solana RPC client
  final rpcUrl = 'https://api.devnet.solana.com';

  // Generate a test wallet (in production, use actual wallet)
  print('🔑 Generating test wallet...');
  final wallet = await Ed25519HDKeyPair.random();
  print('   Wallet: ${wallet.publicKey.toBase58()}\n');

  // ═══════════════════════════════════════
  // File System Operations
  // ═══════════════════════════════════════

  print('📁 File System Operations');
  print('─────────────────────────────────────\n');

  // Example 1: Create a new file system
  print('1. Creating file system (bigbang)...');
  final bigbangMessage = bigbang(
    replicaCount: 3, // High availability with 3 replicas
    wallet: wallet.publicKey,
  );
  print('   ✓ Message created with ${bigbangMessage.instructions.length} instruction\n');

  // To actually send this transaction:
  // final blockhash = await client.getRecentBlockhash();
  // final compiled = bigbangMessage.compile(
  //   recentBlockhash: blockhash,
  //   feePayer: wallet.publicKey,
  // );
  // final signedTx = SignedTx(message: compiled, signers: [wallet]);
  // final signature = await client.sendTransaction(signedTx);

  // ═══════════════════════════════════════
  // File Operations
  // ═══════════════════════════════════════

  print('📄 File Operations');
  print('─────────────────────────────────────\n');

  // Example 2: Create a file
  print('2. Creating file...');
  createFile(
    fsid: '12345',
    path: '/documents',
    name: 'readme.txt',
    wallet: wallet.publicKey,
  );
  print('   ✓ Message created for /documents/readme.txt\n');

  // Example 3: Write content to file
  print('3. Writing to file (poke)...');
  final dataKey = await Ed25519HDKeyPair.random(); // Account holding data
  poke(
    fsid: '12345',
    path: '/documents/readme.txt',
    position: 0, // Write at beginning
    wallet: wallet.publicKey,
    dataKey: dataKey.publicKey,
  );
  print('   ✓ Message created to write at position 0\n');

  // Example 4: Read file content
  print('4. Reading from file (peek)...');
  peek(
    fsid: '12345',
    path: '/documents/readme.txt',
    startPosition: 0,
    endPosition: 100, // Read first 100 bytes
    wallet: wallet.publicKey,
  );
  print('   ✓ Message created to read bytes 0-100\n');
  print('Peek message created');

  // Example 5: Check if file exists
  print('\nChecking file existence...');
  final existsResult = await exists(
    rpcUrl: rpcUrl,
    path: '/documents/readme.txt',
  );
  print('File exists: $existsResult');

  // Example 6: Get file metadata
  print('\nGetting file metadata...');
  final metadata = await getMetadata(
    rpcUrl: rpcUrl,
    path: '/documents/readme.txt',
  );
  print('Metadata: $metadata');

  // Example 7: Subscribe to transaction results
  print('\nSubscribing to transaction results...');
  final channel = subscribeResult(
    rpcEndpoint: rpcUrl,
    tx: 'YourTransactionSignatureHere',
    onResult: (result) {
      print('Received result: fsid=${result.fsid}, status=${result.status}');
    },
    onError: (error) {
      print('WebSocket error: $error');
    },
    onClose: () {
      print('WebSocket connection closed');
    },
  );

  // Clean up
  await Future.delayed(Duration(seconds: 10));
  channel.sink.close();

  print('\nExample completed!');
}
0
likes
150
points
11
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Dart SDK for Xandeum decentralized storage on Solana. Build file systems, manage files, and interact with pNodes.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

convert, http, solana, web_socket_channel

More

Packages that depend on xandeum