0.5.8 • Published 9 months ago

blaze-sdk v0.5.8

Weekly downloads
-
License
-
Repository
github
Last release
9 months ago

Blaze: Layer 2 Scaling for Stacks

Blaze is a lightweight SDK for managing off-chain transfers with on-chain settlement for Stacks dapps. It provides a simple interface for fast, secure transfers while maintaining the security guarantees of the Stacks blockchain.

Features

  • ⚡️ Fast Transfers: Process transfers off-chain with immediate feedback
  • 🔒 Secure: All transfers are cryptographically signed and verified
  • 🎯 Simple API: Easy-to-use SDK for both client and server
  • 📊 Batch Processing: Efficient on-chain settlement in batches
  • 💰 Cost Effective: Reduce transaction fees through batching

Documentation

Quick Start

Installation

pnpm add blaze-sdk

Client Usage

import { Blaze } from 'blaze-sdk';

// Initialize the client
const blaze = new Blaze({
    subnet: 'SP2ZNGJ85ENDY6QRHQ5P2D4FXKGZWCKTB2T0Z55KS.blaze-welsh-v0'
});

// Make a transfer
await blaze.transfer({
    to: 'RECIPIENT_ADDRESS',
    amount: 100
});

// Get user balance
const balance = await blaze.getBalance();
console.log('Balance:', balance);

// Refresh balance after on-chain transactions (deposits/withdrawals)
await blaze.refreshBalance();

Server Usage

import { Subnet } from 'blaze-sdk';

// Initialize the subnet node
const subnet = new Subnet();
subnet.subnet = 'SP2ZNGJ85ENDY6QRHQ5P2D4FXKGZWCKTB2T0Z55KS.blaze-welsh-v0';
subnet.signer = 'OPERATOR_ADDRESS';
subnet.privateKey = process.env.PRIVATE_KEY;

// Get user balance
const balance = await subnet.getBalance('USER_ADDRESS');
console.log('User balance:', balance);

// Process a transfer request from a user
const transfer = {
    signer: 'USER_ADDRESS',
    to: 'RECIPIENT_ADDRESS',
    amount: 100,
    nonce: 12345,
    signature: 'signed_message_hex'
};
await subnet.processTxRequest(transfer);

// Mine a block to settle transactions in the mempool
await subnet.mineBlock(200); // Process up to 200 transactions

// Explicitly refresh balances when needed (e.g., after blockchain confirmations)
await subnet.refreshBalances('USER_ADDRESS');
// Or refresh all known balances
await subnet.refreshBalances();

API Reference

Client SDK

Blaze

The main client class for interacting with Blaze subnets.

class Blaze {
    constructor(options?: { nodeUrl?: string, subnet?: string });
    
    // Wallet management
    async connectWallet(): Promise<string>;
    disconnectWallet(): void;
    isWalletConnected(): boolean;
    getWalletAddress(): string;
    
    // Core operations
    async transfer(options: TransferOptions): Promise<TransactionResult>;
    async deposit(amount: number): Promise<TransactionResult>;
    async withdraw(amount: number): Promise<TransactionResult>;
    
    // Balance management
    async getBalance(): Promise<number>;
    async refreshBalance(): Promise<number>;
}

Server SDK

Subnet

The main server class for operating a Blaze subnet node.

class Subnet {
    subnet: string;
    signer: string;
    privateKey: string | undefined;
    balances: Map<string, number>;
    mempool: Mempool;
    
    // Core operations
    async processTxRequest(txRequest: Transfer): Promise<void>;
    async mineBlock(batchSize?: number): Promise<TransactionResult>;
    
    // Balance management
    async getBalance(user?: string): Promise<number>;
    async getBalances(): Promise<Record<string, number>>;
    async refreshBalances(user?: string): Promise<void>;
    
    // On-chain operations
    async deposit(amount: number): Promise<TransactionResult>;
    async withdraw(amount: number): Promise<TransactionResult>;
}

Mempool

Manages unconfirmed transactions waiting to be mined into blocks.

class Mempool {
    // Transaction management
    getQueue(): Transaction[];
    addTransaction(transaction: Transaction): void;
    getBatchToMine(maxBatchSize?: number): Transaction[];
    removeProcessedTransactions(count: number): void;
    
    // Balance calculations
    getPendingBalanceChanges(): Map<string, number>;
    getTotalBalances(): Map<string, number>;
    async getBalance(user: string): Promise<number>;
}

Examples

Balance Management

import { Blaze } from 'blaze-sdk';

async function manageBalances() {
    const blaze = new Blaze({
        subnet: 'SP2ZNGJ85ENDY6QRHQ5P2D4FXKGZWCKTB2T0Z55KS.blaze-welsh-v0'
    });

    // Get user balance
    const balance = await blaze.getBalance();
    console.log('Balance:', balance);
}

Making a Transfer

import { Blaze } from 'blaze-sdk';

async function makeTransfer() {
    const blaze = new Blaze({
        subnet: 'SP2ZNGJ85ENDY6QRHQ5P2D4FXKGZWCKTB2T0Z55KS.blaze-welsh-v0'
    });
    
    // Make the transfer - this will be added to the mempool
    const result = await blaze.transfer({
        to: 'RECIPIENT_ADDRESS',
        amount: 100
    });
    
    console.log('Transfer submitted:', result);
}

Running a Subnet Node

import { Subnet } from 'blaze-sdk';

async function runSubnetNode() {
    const subnet = new Subnet();
    subnet.subnet = 'SP2ZNGJ85ENDY6QRHQ5P2D4FXKGZWCKTB2T0Z55KS.blaze-welsh-v0';
    subnet.signer = 'OPERATOR_ADDRESS';
    subnet.privateKey = process.env.PRIVATE_KEY;
    
    // Process transfers every minute
    setInterval(async () => {
        try {
            // Mine a block to process transactions in the mempool
            await subnet.mineBlock(200);
            console.log('Successfully mined a block');
            
            // After some time, refresh balances for all users
            // (blockchain confirmations take ~30 seconds)
            setTimeout(async () => {
                await subnet.refreshBalances();
                console.log('Refreshed on-chain balances for all users');
            }, 35000);
        } catch (error) {
            console.error('Error processing transfers:', error);
        }
    }, 60000);
}
0.5.4

10 months ago

0.5.3

10 months ago

0.5.6

10 months ago

0.5.5

10 months ago

0.5.0

10 months ago

0.5.2

10 months ago

0.5.1

10 months ago

0.5.8

9 months ago

0.5.7

10 months ago

0.1.10

10 months ago

0.1.11

10 months ago

0.1.12

10 months ago

0.1.13

10 months ago

0.1.14

10 months ago

0.1.15

10 months ago

0.2.23

10 months ago

0.2.22

10 months ago

0.2.21

10 months ago

0.2.20

10 months ago

0.2.19

10 months ago

0.1.41

10 months ago

0.1.42

10 months ago

0.1.43

10 months ago

0.1.44

10 months ago

0.2.15

10 months ago

0.2.13

10 months ago

0.2.12

10 months ago

0.2.10

10 months ago

0.1.40

10 months ago

0.1.38

10 months ago

0.1.8

10 months ago

0.1.39

10 months ago

0.1.9

10 months ago

0.1.30

10 months ago

0.1.31

10 months ago

0.1.32

10 months ago

0.1.33

10 months ago

0.1.34

10 months ago

0.1.35

10 months ago

0.1.36

10 months ago

0.1.37

10 months ago

0.1.27

10 months ago

0.1.28

10 months ago

0.1.29

10 months ago

0.1.20

10 months ago

0.1.21

10 months ago

0.1.22

10 months ago

0.1.23

10 months ago

0.1.24

10 months ago

0.1.25

10 months ago

0.1.26

10 months ago

0.2.1

10 months ago

0.2.0

10 months ago

0.2.7

10 months ago

0.1.16

10 months ago

0.2.6

10 months ago

0.1.17

10 months ago

0.2.9

10 months ago

0.1.18

10 months ago

0.2.8

10 months ago

0.1.19

10 months ago

0.2.3

10 months ago

0.4.0

10 months ago

0.2.2

10 months ago

0.2.5

10 months ago

0.2.4

10 months ago

0.1.7

10 months ago

0.1.6

10 months ago

0.1.5

10 months ago

0.1.4

10 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago