Skip to content

heirlabs/element-sdk

Repository files navigation

🧩 DEFAI Elements SDK

DEFAI Logo

Build, Deploy, and Monetize AI-Powered Elements for the DEFAI Ecosystem

SDK Version CLI Version License


🌟 Welcome Developers & Projects!

Transform your ideas into powerful, monetizable elements within the DEFAI workspace ecosystem. Our Elements SDK empowers developers to create sophisticated AI-powered components that users can seamlessly integrate into their workspaces while generating sustainable revenue streams.

Why Build with DEFAI Elements?

  • πŸš€ Rapid Development - Pre-built templates and scaffolding get you started in minutes
  • πŸ’° Built-in Monetization - Sell elements as Semi-Fungible Tokens (SFTs) on our marketplace
  • πŸ”’ Secure Sandbox - Run in isolated environments with granular permission controls
  • πŸ€– AI-First - Native integration with AI agents and blockchain services
  • 🎨 React Ecosystem - Build with familiar tools and modern frameworks
  • πŸ“ˆ Analytics & Insights - Track usage, performance, and revenue in real-time

πŸ’Ž DEFAI Builder Grant Program

🎯 1 Billion DEFAI Tokens for Element Builders

We're committed to fostering innovation in the DEFAI ecosystem by allocating 1% of the total DEFAI token supply (1 billion tokens) specifically for element builders and developers.

Grant Tiers & Benefits

πŸ₯‰ Bronze Tier (100 - 1,000 DEFAI)

  • New developers building their first elements
  • Educational projects and proof-of-concepts
  • Community tools and utilities

πŸ₯ˆ Silver Tier (1,000 - 10,000 DEFAI)

  • Production-ready elements with active users
  • Open-source contributions to the SDK
  • Educational content and tutorials

πŸ₯‡ Gold Tier (10,000 - 100,000 DEFAI)

  • High-impact elements with significant user adoption
  • Major SDK features and infrastructure improvements
  • Partnership integrations and enterprise solutions

πŸ’Ž Platinum Tier (100,000+ DEFAI)

  • Ecosystem-defining innovations
  • Major platform integrations
  • Revolutionary AI/DeFi applications

How to Apply

  1. Build Your Element - Use our SDK and sample templates
  2. Deploy & Validate - Publish to our testnet marketplace
  3. Submit Proposal - Include usage metrics and impact assessment
  4. Community Review - Get feedback from DEFAI community
  5. Grant Approval - Receive tokens based on tier qualification

Apply at: grants.defai.com

Success Stories

"The DEFAI grant program allowed us to focus on innovation instead of funding. We built a trading bot element that now has 10,000+ active users!" β€” Maya Chen, DeFi Builder

"Starting with a Bronze grant, we've grown to Gold tier. The community support and token incentives made all the difference." β€” Alex Rodriguez, AI Developer


⚑ Quick Start

Get your first element running in under 5 minutes:

1. Install the CLI

Option A: Homebrew (Recommended for macOS)

brew install defai

Option B: npm

npm install -g @defai/element-cli

2. Try the Sample Workspace Template

Experience the full power of the SDK with our comprehensive trading workspace:

cd sample-workspace-template
npm install
npm run dev

This opens a fully functional AI trading workspace at http://localhost:3000 featuring:

  • πŸ“Š Real-time portfolio tracking
  • πŸ€– AI-powered market analysis
  • πŸ’± Complete trading interface
  • πŸ”„ Inter-element messaging
  • βš™οΈ All SDK APIs demonstrated

3. Create Your First Element

# Create a new element project
defai-element create my-trading-bot --template react

# Navigate to project
cd my-trading-bot

# Install dependencies  
npm install

# Start development server with hot reload
defai-element dev

4. Build & Deploy

# Build for production
npm run build

# Validate your element
npm run validate

# Publish to the marketplace
npm run publish --tier bronze --price 100

πŸ“¦ Installation & Setup

Prerequisites

  • Node.js 16+ and npm/yarn
  • Solana wallet with DEFAI tokens for publishing
  • Basic knowledge of React/TypeScript

Development Environment Setup

# Install DEFAI CLI globally
brew install defai
# OR
npm install -g @defai/element-cli

# Clone the SDK repository
git clone https://github.com/defaiza/element-sdk
cd element-sdk

# Install dependencies and build packages
npm install
npm run build

# Install core SDK packages for your projects
npm install @defai/element-sdk @defai/element-react @defai/element-types

# Verify installation
defai-element --version

Available NPM Packages

Our comprehensive package ecosystem supports every aspect of element development:

# Core Development
npm install @defai/element-sdk        # Core SDK and base classes
npm install @defai/element-react      # React hooks and components  
npm install @defai/element-types      # TypeScript definitions

# Development Tools
npm install -g @defai/element-cli     # CLI for scaffolding and publishing
npm install @defai/element-templates  # Pre-built element templates
npm install @defai/element-testing    # Testing utilities and mocks

# Advanced Features
npm install @defai/element-validator  # Code validation and security checks

πŸ—οΈ Element Architecture

DEFAI Elements are modular, AI-powered components that run in secure sandbox environments within user workspaces. They can integrate with blockchain services, AI agents, and external APIs while maintaining strict security boundaries.

Element Lifecycle

import { DefaiElement } from '@defai/element-sdk';

export default class MyElement extends DefaiElement {
  // Required metadata
  readonly metadata = {
    id: 'my-element',
    name: 'My Awesome Element',
    version: '1.0.0',
    description: 'Does amazing things',
    category: 'Trading'
  };

  // Define permissions needed
  readonly permissions = {
    storage: true,
    wallet: true,
    ai: true,
    network: true
  };

  // Lifecycle hooks
  async onMount(context) {
    // Element initialization
    this.api = context.api;
    console.log('Element mounted!');
  }

  async onUnmount() {
    // Cleanup
    console.log('Element unmounting...');
  }

  // React render method
  render() {
    return <div>Hello DEFAI!</div>;
  }
}

Core APIs Available

  • 🏦 Wallet API - Balance queries, transaction signing, permission management
  • πŸ€– AI Services - Chat, analysis, content generation with various models
  • πŸ’Ύ Storage API - Persistent key-value storage with encryption
  • πŸ“‘ Messaging API - Inter-element communication and event broadcasting
  • πŸ“ˆ Market Data - Real-time price feeds and historical data
  • πŸ”” Notifications - User alerts and confirmation dialogs

πŸ› οΈ Sample Workspace Template

Complete Trading Workspace Example

Our sample workspace demonstrates enterprise-grade element development:

cd sample-workspace-template
npm install
npm run dev      # Start development server
npm run build    # Production build
npm run test     # Run test suite
npm run validate # Validate element

What's Included

πŸ“Š Portfolio Management

  • Real-time balance tracking
  • P&L calculations
  • Position management
  • Trading history

πŸ€– AI Integration

  • Market sentiment analysis
  • Trading signal generation
  • Risk assessment
  • Automated insights

πŸ’± Trading Interface

  • Order execution (market/limit)
  • Multi-asset support
  • Risk controls
  • Quick trade actions

πŸ”„ Real-time Features

  • Live price updates
  • Inter-element messaging
  • Portfolio synchronization
  • AI analysis broadcasting

Key Files

sample-workspace-template/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ index.tsx           # Main element entry
β”‚   └── components/         # React components
β”œβ”€β”€ public/
β”‚   └── index.html          # HTML template
β”œβ”€β”€ webpack.config.js       # Build configuration
β”œβ”€β”€ package.json           # Dependencies
└── README.md              # Template guide

Customization Guide

  1. Fork the Template

    cp -r sample-workspace-template my-element
    cd my-element
  2. Modify the Core Component

    // src/index.tsx
    export default class MyCustomElement extends DefaiElement {
      readonly metadata = {
        id: 'my-custom-element',
        name: 'My Custom Element',
        // ... your metadata
      };
    }
  3. Add Your Features

    • Create new components in src/components/
    • Integrate with additional APIs
    • Customize styling and layout
    • Add tests for new functionality
  4. Deploy

    npm run build
    npm run validate
    defai-element publish --tier bronze

πŸ“š Advanced Features

Element Communication

// Subscribe to messages from other elements
this.api.messaging.subscribe('price-alerts', (alert) => {
  if (alert.symbol === this.state.watchedSymbol) {
    this.showNotification(alert.message);
  }
});

// Broadcast data to other elements
this.api.messaging.broadcast('trade-executed', {
  symbol: 'SOL-USD',
  amount: 100,
  price: 45.67,
  timestamp: Date.now()
});

AI Integration

// Market sentiment analysis
const sentiment = await this.api.ai.analyze('market-sentiment', {
  symbols: ['SOL-USD', 'BTC-USD'],
  timeframe: '1h',
  includeNews: true
});

// Generate trading insights
const insights = await this.api.ai.generate(
  'Analyze the SOL price movement and provide 3 key trading insights'
);

// Chat with AI assistant
const response = await this.api.ai.chat(
  'What are the best DeFi opportunities this week?',
  { context: this.state.portfolio }
);

Wallet Integration

// Check balances
const solBalance = await this.api.wallet.getBalance('SOL');
const usdcBalance = await this.api.wallet.getBalance('USDC');

// Request transaction permission
const permitted = await this.api.wallet.requestPermission('transaction');

if (permitted) {
  // Execute trade
  const txHash = await this.api.wallet.sendTransaction({
    type: 'swap',
    from: 'SOL',
    to: 'USDC',
    amount: 10
  });
}

Data Persistence

// Save user preferences
await this.api.storage.set('user-preferences', {
  theme: 'dark',
  defaultSlippage: 0.5,
  autoTrade: false
});

// Load portfolio data
const portfolio = await this.api.storage.get('user-portfolio');

// List all stored keys
const keys = await this.api.storage.list('trade-history-');

πŸ§ͺ Testing & Validation

Test Your Elements

# Run tests with coverage
npm run test -- --coverage

# Test specific element
cd my-element
npm run test

# Integration testing
npm run test:integration

# Performance testing
npm run test:performance

Element Validation

# Validate element structure
defai-element validate

# Strict validation for marketplace
defai-element validate --strict

# Security audit
defai-element audit

# Performance analysis
defai-element analyze --bundle-size

Automated Testing

import { mockElementAPI } from '@defai/element-testing';

describe('MyElement', () => {
  beforeEach(() => {
    mockElementAPI({
      wallet: {
        getBalance: jest.fn(() => Promise.resolve(1000)),
        sendTransaction: jest.fn(() => Promise.resolve('mock-tx-hash'))
      },
      ai: {
        analyze: jest.fn(() => Promise.resolve({ sentiment: 'bullish' }))
      }
    });
  });

  test('handles trading signals correctly', async () => {
    // Your test implementation
  });
});

πŸ“¦ Publishing & Monetization

Marketplace Publishing

# Prepare for publishing
npm run build
npm run validate --strict

# Publish to marketplace
defai-element publish \
  --tier bronze \
  --price 150 \
  --royalty 10 \
  --description "AI-powered trading workspace"

# Update existing element
defai-element update --version patch

Revenue Models

  • πŸ’° One-time Purchase - Users buy element permanently
  • πŸ”„ Subscription Model - Monthly/yearly recurring revenue
  • πŸ“Š Usage-based Pricing - Pay per transaction/analysis
  • 🎯 Freemium - Basic free, premium features paid
  • πŸ’Ž Premium Tiers - Multiple feature levels

Marketplace Categories

  • πŸ“ˆ Trading & DeFi - Trading tools, portfolio managers, DeFi protocols
  • πŸ€– AI & Analytics - Market analysis, predictive models, data visualization
  • πŸ”§ Productivity Tools - Task managers, calculators, converters
  • πŸ’¬ Communication - Chat widgets, social feeds, collaboration tools

πŸš€ Build Instructions

Local Development

# Clone your element project
git clone https://github.com/yourusername/my-element.git
cd my-element

# Install dependencies
npm install

# Start development server (with hot reload)
npm run dev
# OR using CLI
defai-element dev --port 3000 --open

# Run tests in watch mode
npm run test:watch
# OR using CLI  
defai-element test --watch --coverage

Production Build

# Create optimized build
npm run build
# OR using CLI with analysis
defai-element build --analyze --source-maps

# Validate the build
defai-element validate --strict

# Test the production build locally
defai-element serve dist/

Continuous Integration

# .github/workflows/element-ci.yml
name: Element CI/CD

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: '18'
      
      - name: Install DEFAI CLI
        run: npm install -g @defai/element-cli
      
      - name: Install dependencies
        run: npm ci
      
      - name: Run tests
        run: defai-element test --coverage
      
      - name: Validate element
        run: defai-element validate --strict
      
      - name: Build production
        run: defai-element build
        
      - name: Publish (on main branch)
        if: github.ref == 'refs/heads/main'
        run: defai-element publish
        env:
          DEFAI_API_KEY: ${{ secrets.DEFAI_API_KEY }}

🀝 Contributing to the SDK

We welcome contributions from the community! Here's how to get involved:

Development Setup

# Clone the SDK repository
git clone https://github.com/defaiza/element-sdk
cd element-sdk

# Install dependencies
npm install

# Build all packages
npm run build

# Run tests
npm run test

# Start development
npm run dev

Contribution Guidelines

  1. Fork the Repository - Create your own fork to work on
  2. Create Feature Branch - git checkout -b feature/amazing-feature
  3. Follow Code Standards - Use our ESLint and Prettier configs
  4. Add Tests - Ensure your changes have proper test coverage
  5. Update Documentation - Keep docs in sync with code changes
  6. Submit Pull Request - Describe your changes and their impact

Areas We Need Help With

  • πŸ› Bug Fixes - Help us squash bugs and improve stability
  • πŸ“š Documentation - Improve guides, examples, and API docs
  • πŸ§ͺ Testing - Expand test coverage and add integration tests
  • 🌟 New Features - Propose and implement new SDK capabilities
  • 🎨 Templates - Create new element templates for different use cases

πŸ“ž Support & Community

Getting Help

Community Resources

  • πŸŽ“ Tutorials - Step-by-step guides for all skill levels
  • πŸ’‘ Examples Gallery - Showcase of amazing community elements
  • πŸ—£οΈ Developer Forum - Ask questions and share knowledge
  • πŸ“… Developer Events - Regular workshops and hackathons
  • πŸ† Bounty Program - Earn tokens for completing development tasks

Office Hours

Join our weekly developer office hours:


πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


Ready to build the future of decentralized AI? Start with our sample workspace template and apply for a DEFAI Builder Grant today! πŸš€

Earn up to 100,000 DEFAI tokens while building the elements that will power tomorrow's decentralized economy.

About

SDK for Elements

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •