Files
aitbc/docs/api/examples/js-sdk-examples.md
aitbc e4f1a96172
Some checks failed
Blockchain Synchronization Verification / sync-verification (push) Failing after 8s
CLI Tests / test-cli (push) Successful in 10s
Contract Performance Benchmarks / benchmark-gas-usage (push) Successful in 1m22s
Contract Performance Benchmarks / benchmark-execution-time (push) Successful in 1m11s
Contract Performance Benchmarks / benchmark-throughput (push) Successful in 1m13s
Cross-Chain Functionality Tests / test-cross-chain-sync (push) Failing after 5s
Cross-Chain Functionality Tests / test-cross-chain-transactions (push) Successful in 5s
Cross-Chain Functionality Tests / test-cross-chain-bridge (push) Has been skipped
Cross-Chain Functionality Tests / test-multi-chain-consensus (push) Failing after 3s
Cross-Chain Functionality Tests / aggregate-results (push) Has been skipped
Cross-Node Transaction Testing / transaction-test (push) Successful in 5s
Deploy to Testnet / deploy-testnet (push) Successful in 1m14s
Contract Performance Benchmarks / compare-benchmarks (push) Has been cancelled
Documentation Validation / validate-docs (push) Failing after 10s
Multi-Node Stress Testing / stress-test (push) Has been cancelled
Node Failover Simulation / failover-test (push) Has been cancelled
Security Scanning / security-scan (push) Has been cancelled
Smart Contract Tests / test-solidity (map[name:aitbc-contracts path:contracts]) (push) Has been cancelled
Smart Contract Tests / test-solidity (map[name:aitbc-token path:packages/solidity/aitbc-token]) (push) Has been cancelled
Smart Contract Tests / test-foundry (push) Has been cancelled
Smart Contract Tests / lint-solidity (push) Has been cancelled
Smart Contract Tests / deploy-contracts (push) Has been cancelled
Documentation Validation / validate-policies-strict (push) Successful in 3s
Integration Tests / test-service-integration (push) Failing after 45s
Multi-Chain Island Architecture Tests / test-multi-chain-island (push) Failing after 2s
Multi-Node Blockchain Health Monitoring / health-check (push) Successful in 5s
P2P Network Verification / p2p-verification (push) Successful in 3s
Production Tests / Production Integration Tests (push) Failing after 7s
Python Tests / test-python (push) Failing after 46s
Staking Tests / test-staking-service (push) Failing after 2s
Staking Tests / test-staking-integration (push) Has been skipped
Staking Tests / test-staking-contract (push) Has been skipped
Staking Tests / run-staking-test-runner (push) Has been skipped
Systemd Sync / sync-systemd (push) Successful in 21s
API Endpoint Tests / test-api-endpoints (push) Failing after 12m19s
ci: standardize pytest invocation and add security scanning
- Changed pytest calls to use `venv/bin/python -m pytest` with explicit config
- Added `--rootdir "$PWD"` and `--import-mode=importlib` for consistent imports
- Fixed PYTHONPATH to use absolute paths with $PWD prefix
- Added smart contract security scanning for Solidity files
- Added Circom circuit security checks for ZK proof circuits
- Added ZK proof implementation security validation
- Added contracts/** to security scanning workflow
2026-05-11 13:46:42 +02:00

418 lines
8.2 KiB
Markdown

# JavaScript/TypeScript SDK Examples
This document provides comprehensive examples for using the AITBC JavaScript/TypeScript SDK.
## Installation
```bash
npm install @aitbc/aitbc-sdk
```
## Basic Setup
```typescript
import { AITBCClient } from '@aitbc/aitbc-sdk';
// Initialize client
const client = new AITBCClient({
apiKey: 'your-api-key',
baseUrl: 'http://localhost:8011'
});
```
## Job Submission
### Simple Job Submission
```typescript
// Submit a simple job
const job = await client.submitJob({
payload: {
model: 'llama2',
prompt: 'Hello, world!'
},
ttlSeconds: 900
});
console.log(`Job ID: ${job.jobId}`);
console.log(`State: ${job.state}`);
```
### Job with Constraints
```typescript
// Submit job with GPU constraints
const job = await client.submitJob({
payload: {
model: 'llama2',
prompt: 'Hello, world!'
},
constraints: {
minGpuMemory: 8,
gpuType: 'nvidia-rtx-3090'
},
ttlSeconds: 900
});
```
### Job with Payment
```typescript
// Submit job with payment
const job = await client.submitJob({
payload: {
model: 'llama2',
prompt: 'Hello, world!'
},
paymentAmount: 100.0,
paymentCurrency: 'AITBC',
ttlSeconds: 900
});
console.log(`Payment ID: ${job.paymentId}`);
```
## Job Status Monitoring
### Get Job Status
```typescript
// Get current job status
const status = await client.getJob('your-job-id');
console.log(`State: ${status.state}`);
console.log(`Assigned Miner: ${status.assignedMinerId}`);
console.log(`Error: ${status.error}`);
```
### Poll for Completion
```typescript
async function waitForCompletion(jobId: string): Promise<void> {
while (true) {
const status = await client.getJob(jobId);
console.log(`State: ${status.state}`);
if (['COMPLETED', 'FAILED', 'CANCELLED', 'EXPIRED'].includes(status.state)) {
break;
}
await new Promise(resolve => setTimeout(resolve, 5000));
}
}
waitForCompletion('your-job-id');
```
### WebSocket for Real-time Updates
```typescript
// Monitor job status via WebSocket
const ws = client.watchJob('your-job-id', (update) => {
console.log(`Status update: ${JSON.stringify(update)}`);
});
// Close connection when done
ws.close();
```
## Job Results
### Get Job Result
```typescript
// Get job result
const result = await client.getJobResult('your-job-id');
console.log(`Output: ${JSON.stringify(result.result)}`);
console.log(`Receipt: ${JSON.stringify(result.receipt)}`);
```
### Get Receipts
```typescript
// Get latest receipt
const receipt = await client.getReceipt('your-job-id');
console.log(`Signature: ${receipt.signature}`);
// Get all receipts
const receipts = await client.listReceipts('your-job-id');
for (const receipt of receipts) {
console.log(`Receipt: ${receipt.signature}`);
}
```
## Job Cancellation
```typescript
// Cancel a job
const cancelledJob = await client.cancelJob('your-job-id');
console.log(`State: ${cancelledJob.state}`);
```
## Payment Operations
### Get Payment Status
```typescript
// Get payment information
const payment = await client.getPayment('your-job-id');
console.log(`Status: ${payment.status}`);
console.log(`Amount: ${payment.amount}`);
```
## Blockchain Operations
### Initialize Blockchain Client
```typescript
import { BlockchainClient } from '@aitbc/aitbc-sdk';
const blockchain = new BlockchainClient({
baseUrl: 'http://localhost:8080'
});
```
### Get Block Information
```typescript
// Get head block
const headBlock = await blockchain.getHeadBlock();
console.log(`Current height: ${headBlock.height}`);
// Get block by height
const block = await blockchain.getBlock(12345);
console.log(`Block hash: ${block.hash}`);
```
### Network Status
```typescript
// Get network information
const network = await blockchain.getNetworkInfo();
console.log(`Peer count: ${network.peerCount}`);
console.log(`Chain ID: ${network.chainId}`);
// Get peers
const peers = await blockchain.getPeers();
for (const peer of peers) {
console.log(`Peer: ${peer.address}`);
}
```
### Transaction Operations
```typescript
// Get transaction
const tx = await blockchain.getTransaction('0x...');
console.log(`From: ${tx.from}`);
console.log(`To: ${tx.to}`);
console.log(`Value: ${tx.value}`);
```
## Error Handling
```typescript
import { APIError, AuthenticationError } from '@aitbc/aitbc-sdk';
try {
const job = await client.submitJob({
payload: { model: 'llama2', prompt: 'Hello' }
});
} catch (error) {
if (error instanceof AuthenticationError) {
console.error('Invalid API key');
} else if (error instanceof APIError) {
console.error(`API error: ${error.message}`);
} else {
console.error(`Unexpected error: ${error}`);
}
}
```
## Advanced Examples
### Batch Job Submission
```typescript
// Submit multiple jobs
const prompts = ['Hello', 'World', 'Test'];
const jobs = await Promise.all(
prompts.map(prompt =>
client.submitJob({
payload: { model: 'llama2', prompt },
ttlSeconds: 900
})
)
);
console.log(`Submitted ${jobs.length} jobs`);
```
### Job History
```typescript
// Get job history
const history = await client.getJobHistory({ limit: 10 });
for (const job of history) {
console.log(`Job ${job.jobId}: ${job.state}`);
}
```
### Custom Headers
```typescript
// Use custom headers
const client = new AITBCClient({
apiKey: 'your-api-key',
baseUrl: 'http://localhost:8011',
headers: {
'X-Custom-Header': 'value'
}
});
```
## TypeScript Configuration
```typescript
// Enable strict type checking
{
"compilerOptions": {
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true
}
}
```
## Configuration
### Environment Variables
```typescript
import dotenv from 'dotenv';
dotenv.config();
const client = new AITBCClient({
apiKey: process.env.AITBC_API_KEY || '',
baseUrl: process.env.AITBC_BASE_URL || 'http://localhost:8011'
});
```
### Timeout Configuration
```typescript
const client = new AITBCClient({
apiKey: 'your-api-key',
baseUrl: 'http://localhost:8011',
timeout: 30000 // 30 second timeout
});
```
## React Integration
```typescript
import { useState, useEffect } from 'react';
import { AITBCClient } from '@aitbc/aitbc-sdk';
function JobComponent({ jobId }: { jobId: string }) {
const [status, setStatus] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
const client = new AITBCClient({
apiKey: 'your-api-key',
baseUrl: 'http://localhost:8011'
});
const fetchStatus = async () => {
try {
const job = await client.getJob(jobId);
setStatus(job.state);
} catch (err) {
setError(err instanceof Error ? err.message : 'Unknown error');
}
};
fetchStatus();
}, [jobId]);
if (error) return <div>Error: {error}</div>;
return <div>Job Status: {status}</div>;
}
```
## Node.js Integration
```typescript
import express from 'express';
import { AITBCClient } from '@aitbc/aitbc-sdk';
const app = express();
const client = new AITBCClient({
apiKey: process.env.AITBC_API_KEY!,
baseUrl: process.env.AITBC_BASE_URL!
});
app.post('/api/jobs', async (req, res) => {
try {
const job = await client.submitJob(req.body);
res.json(job);
} catch (error) {
res.status(500).json({ error: error instanceof Error ? error.message : 'Unknown error' });
}
});
app.listen(3000);
```
## Testing
```typescript
import { describe, it, expect, vi } from 'vitest';
import { AITBCClient } from '@aitbc/aitbc-sdk';
describe('AITBCClient', () => {
it('should submit job successfully', async () => {
const mockFetch = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({
job_id: 'test-id',
state: 'QUEUED'
})
});
global.fetch = mockFetch;
const client = new AITBCClient({
apiKey: 'test-key',
baseUrl: 'http://localhost:8011'
});
const job = await client.submitJob({
payload: { model: 'test', prompt: 'Hello' }
});
expect(job.jobId).toBe('test-id');
});
});
```
## Receipt Verification
```typescript
import { verifyReceipt } from '@aitbc/aitbc-sdk';
// Verify receipt signature
const receipt = await client.getReceipt('your-job-id');
const isValid = verifyReceipt(
receipt.signature,
receipt.data,
'miner-public-key'
);
if (isValid) {
console.log('Receipt is valid');
} else {
console.log('Receipt is invalid');
}
```