// noinspection JSUnusedLocalSymbols /** * Basic usage examples for Dragonchain Node.js SDK */ import { DragonchainSDK, loadConfig, getDefaultChain } from '@dragonchain-inc/prime-sdk'; // Example 1: Initialize SDK with direct credentials // @ts-ignore async function directInitialization() { const sdk = new DragonchainSDK( 'your-public-id', 'your-auth-key-id', 'your-auth-key', 'https://your-dragonchain-endpoint.com' ); // Check system health await sdk.system.health(); console.log('System is healthy'); // Get system status const status = await sdk.system.status(); console.log(`Chain ID: ${status.id}, Level: ${status.level}`); } // Example 2: Initialize SDK from config file // @ts-ignore async function configFileInitialization() { // Load credentials from YAML file const config = loadConfig('~/.dragonchain/credentials.yaml'); const chain = getDefaultChain(config); if (!chain) { throw new Error('No default chain found in config'); } const sdk = new DragonchainSDK(chain.publicId, chain.authKeyId, chain.authKey, chain.endpoint); return sdk; } // Example 3: Create a transaction // @ts-ignore async function createTransaction(sdk: DragonchainSDK) { const response = await sdk.transaction.create({ txn_type: 'my-transaction-type', payload: JSON.stringify({ message: 'Hello Dragonchain', timestamp: new Date().toISOString(), }), tag: 'example-tag', }); console.log(`Created transaction: ${response.transaction_id}`); return response.transaction_id; } // Example 4: Create bulk transactions // @ts-ignore async function createBulkTransactions(sdk: DragonchainSDK) { const response = await sdk.transaction.createBulk({ transactions: [ { txn_type: 'type1', payload: JSON.stringify({ data: 'transaction 1' }), }, { txn_type: 'type2', payload: JSON.stringify({ data: 'transaction 2' }), }, { txn_type: 'type3', payload: JSON.stringify({ data: 'transaction 3' }), }, ], }); console.log(`Created ${response.transaction_ids.length} transactions`); return response.transaction_ids; } // Example 5: Get a transaction // @ts-ignore async function getTransaction(sdk: DragonchainSDK, transactionId: string) { const transaction = await sdk.transaction.get(transactionId); console.log(`Transaction: ${transaction.header.txn_id}`); console.log(`Type: ${transaction.header.txn_type}`); console.log(`Block: ${transaction.header.block_id}`); return transaction; } // Example 6: Create and manage transaction types // @ts-ignore async function manageTransactionTypes(sdk: DragonchainSDK) { // Create a new transaction type await sdk.transactionType.create({ txn_type: 'my-new-type', }); console.log('Transaction type created'); // Get the transaction type const txnType = await sdk.transactionType.get('my-new-type'); console.log(`Created at: ${txnType.created}`); // List all transaction types const types = await sdk.transactionType.list(); console.log(`Total transaction types: ${types.transactionTypes.length}`); } // Example 7: Query blocks // @ts-ignore async function queryBlocks(sdk: DragonchainSDK, blockId: string) { const block = await sdk.block.get(blockId); console.log(`Block ID: ${block.block_id}`); console.log(`Timestamp: ${block.timestamp}`); console.log(`Transactions: ${block.transactions.length}`); return block; } // Example 8: Error handling // @ts-ignore async function errorHandlingExample(sdk: DragonchainSDK) { try { await sdk.transaction.get('non-existent-transaction-id'); } catch (error) { if (error instanceof Error) { console.error(`Error: ${error.message}`); // Handle specific error cases if (error.message.includes('404')) { console.log('Transaction not found'); } } } } // Example 9: Complete workflow // @ts-ignore async function completeWorkflow() { // Initialize SDK const sdk = new DragonchainSDK( process.env.DC_PUBLIC_ID || '', process.env.DC_AUTH_KEY_ID || '', process.env.DC_AUTH_KEY || '', process.env.DC_ENDPOINT || '' ); try { // Check system health await sdk.system.health(); console.log('✓ System healthy'); // Create a transaction type await sdk.transactionType.create({ txn_type: 'workflow-example', }); console.log('✓ Transaction type created'); // Create a transaction const txnResponse = await sdk.transaction.create({ txn_type: 'workflow-example', payload: JSON.stringify({ step: 'initialization', timestamp: Date.now(), }), }); console.log(`✓ Transaction created: ${txnResponse.transaction_id}`); // Wait a bit for the transaction to be processed await new Promise((resolve) => setTimeout(resolve, 2000)); // Get the transaction const transaction = await sdk.transaction.get(txnResponse.transaction_id); console.log(`✓ Transaction verified in block: ${transaction.header.block_id}`); console.log('\nWorkflow completed successfully!'); } catch (error) { console.error('Workflow failed:', error); throw error; } } // Run examples (uncomment to execute) // completeWorkflow().catch(console.error);