Skip to content

Q-arz/DefiBrain-SDK

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DefiBrain SDK

Official TypeScript/JavaScript SDK for the DefiBrain API - A unified DeFi router that integrates multiple protocols (Pendle, Curve, 1inch, Aave V3, Morpho Blue, Uniswap) into a single intelligent API.

Installation

npm install @defibrain/sdk

🚀 Quick Start

import { DefiBrainClient } from '@defibrain/sdk';

// Initialize client
const client = new DefiBrainClient({
  apiKey: 'your-api-key',
  apiUrl: 'https://apibrain.oxg.fi/v1', // Optional, defaults to production
  chainId: 1, // Optional, defaults to Ethereum mainnet
});

// Optimize yield automatically (now also builds a ready-to-sign transaction by default)
const result = await client.optimizeYield({
  asset: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC
  amount: '1000000', // 1 USDC
  strategy: 'max_yield',
});

console.log(`Best protocol: ${result.protocol}`);
console.log(`Estimated APR: ${result.estimatedAPR}%`);

// Option A: execute using the transaction returned by optimizeYield (recommended)
if (result.transaction) {
  const tx = await signer.sendTransaction(result.transaction);
  await tx.wait();
}

// Option B: manual executeAction flow (fallback / advanced)
// const tx = await client.executeAction({
//   protocol: result.protocol,
//   action: result.action,
//   params: result.params,
// });

Advanced Usage

Wallet Integration

import { WalletHelper } from '@defibrain/sdk';

const wallet = new WalletHelper();

// Connect wallet
const walletInfo = await wallet.connect();
console.log(`Connected: ${walletInfo.address}`);

// Get balance
const balance = await wallet.getBalance();
console.log(`Balance: ${balance} wei`);

// Get token balance
const usdcBalance = await wallet.getTokenBalance('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48');
console.log(`USDC: ${usdcBalance}`);

Transaction Helper

import { DefiBrainClient, TransactionHelper } from '@defibrain/sdk';

const client = new DefiBrainClient({ apiKey: 'your-key' });
const wallet = new WalletHelper();
await wallet.connect();

const txHelper = new TransactionHelper(wallet.getProvider()!, wallet.getChainId());

// Optimize yield
const result = await client.optimizeYield({
  asset: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
  amount: '1000000',
});

// Sign and send transaction
if (result.transaction) {
  const txHash = await txHelper.signAndSend(result.transaction);
  console.log(`Transaction sent: ${txHash}`);
  
  // Wait for confirmation
  const receipt = await txHelper.waitForConfirmation(txHash);
  console.log(`Confirmed in block: ${receipt.blockNumber}`);
  
  // Or do it all in one call
  // const receipt = await txHelper.signSendAndWait(result.transaction);
}

Pendle Helper

import { DefiBrainClient, PendleHelper, TransactionHelper, WalletHelper } from '@defibrain/sdk';

const client = new DefiBrainClient({ apiKey: 'your-key' });
const wallet = new WalletHelper();
await wallet.connect();

const txHelper = new TransactionHelper(wallet.getProvider()!, wallet.getChainId());
const pendleHelper = new PendleHelper(client, txHelper);

// Optimize yield with Pendle
const result = await pendleHelper.optimizeYieldWithPendle(
  '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC
  '1000000', // 1 USDC
  'max_yield'
);

// Swap PT to YT
const swapResult = await pendleHelper.swapPTtoYT(
  '0x...', // PT address
  '1000000',
  false // Don't execute, just prepare
);

// Redeem PT at maturity
const redeemResult = await pendleHelper.redeemPT(
  '0x...', // PT address
  '1000000',
  false
);

Aave Helper

import { DefiBrainClient, AaveHelper, TransactionHelper, WalletHelper } from '@defibrain/sdk';

const client = new DefiBrainClient({ apiKey: 'your-key' });
const wallet = new WalletHelper();
await wallet.connect();

const txHelper = new TransactionHelper(wallet.getProvider()!, wallet.getChainId());
const aaveHelper = new AaveHelper(client, txHelper);

// Supply to Aave
const supplyResult = await aaveHelper.supply(
  '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC
  '1000000', // 1 USDC
  false // Don't execute, just prepare
);

// Withdraw from Aave
const withdrawResult = await aaveHelper.withdraw(
  '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
  '1000000',
  false
);

Automatic Retry

const client = new DefiBrainClient({
  apiKey: 'your-key',
  retryOptions: {
    maxRetries: 3,
    initialDelay: 1000,
    maxDelay: 10000,
    backoffFactor: 2,
  },
});

// All API calls automatically retry on network errors
const result = await client.optimizeYield({ ... });

Input Validation

import { validateAddress, validateAmount, formatAmount, parseAmount } from '@defibrain/sdk';

// Validate address
validateAddress('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'); // true

// Validate amount
validateAmount('1000000'); // true

// Format amount (wei to readable)
formatAmount('1000000000000000000', 18); // "1"

// Parse amount (readable to wei)
parseAmount('1.5', 18); // "1500000000000000000"

Why DefiBrain?

For End Users

  • Auto-Optimization: Automatically finds the best yield across multiple protocols
  • Simple API: One unified interface for all DeFi protocols
  • Batch Operations: Execute multiple actions in a single transaction
  • Gas Optimization: Built-in gas estimation and optimization

For Developers

  • Easy Integration: Add new protocols with ~70% less code
  • Reusable Infrastructure: No need to rebuild common functionality
  • Type Safety: Full TypeScript support with strong typing
  • Protocol Helpers: Simple, protocol-specific APIs (AaveHelper, PendleHelper, etc.)

See How to Integrate a Protocol for details.

Documentation

Spanish summaries:

  • ./docs/ES/README.md – índice y resumen en español
  • ./docs/ES/API.md – resumen de la API
  • ./docs/ES/CONFIGURATION.md – configuración básica
  • ./docs/ES/EXAMPLES.md – ejemplos rápidos
  • ./docs/ES/CONTRACTS.md – contratos incluidos en el SDK

Full documentation also available at: https://defibrain.oxg.fi/docs.html

License

MIT License - This SDK is open source.

Support

About

A unified DeFi router that integrates multiple protocols (Pendle, Curve, 1inch, Aave V3, Morpho Blue) into a single intelligent API

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors