0.5.8 • Published 4 months ago

blaze-sdk v0.5.8

Weekly downloads
-
License
-
Repository
github
Last release
4 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

4 months ago

0.5.3

4 months ago

0.5.6

4 months ago

0.5.5

4 months ago

0.5.0

4 months ago

0.5.2

4 months ago

0.5.1

4 months ago

0.5.8

4 months ago

0.5.7

4 months ago

0.1.10

4 months ago

0.1.11

4 months ago

0.1.12

4 months ago

0.1.13

4 months ago

0.1.14

4 months ago

0.1.15

4 months ago

0.2.23

4 months ago

0.2.22

4 months ago

0.2.21

4 months ago

0.2.20

4 months ago

0.2.19

4 months ago

0.1.41

4 months ago

0.1.42

4 months ago

0.1.43

4 months ago

0.1.44

4 months ago

0.2.15

4 months ago

0.2.13

4 months ago

0.2.12

4 months ago

0.2.10

4 months ago

0.1.40

4 months ago

0.1.38

4 months ago

0.1.8

4 months ago

0.1.39

4 months ago

0.1.9

4 months ago

0.1.30

4 months ago

0.1.31

4 months ago

0.1.32

4 months ago

0.1.33

4 months ago

0.1.34

4 months ago

0.1.35

4 months ago

0.1.36

4 months ago

0.1.37

4 months ago

0.1.27

4 months ago

0.1.28

4 months ago

0.1.29

4 months ago

0.1.20

4 months ago

0.1.21

4 months ago

0.1.22

4 months ago

0.1.23

4 months ago

0.1.24

4 months ago

0.1.25

4 months ago

0.1.26

4 months ago

0.2.1

4 months ago

0.2.0

4 months ago

0.2.7

4 months ago

0.1.16

4 months ago

0.2.6

4 months ago

0.1.17

4 months ago

0.2.9

4 months ago

0.1.18

4 months ago

0.2.8

4 months ago

0.1.19

4 months ago

0.2.3

4 months ago

0.4.0

4 months ago

0.2.2

4 months ago

0.2.5

4 months ago

0.2.4

4 months ago

0.1.7

4 months ago

0.1.6

4 months ago

0.1.5

4 months ago

0.1.4

4 months ago

0.1.3

4 months ago

0.1.2

4 months ago

0.1.1

4 months ago

0.1.0

4 months ago