Welcome to the DeFindex TypeScript SDK documentation! This SDK provides server-side access to DeFindex's vault management system through a comprehensive TypeScript interface. With this SDK, you can:
npm install @defindex/sdk
# or
pnpm install @defindex/sdk
# or
yarn add @defindex/sdk
import { DefindexSDK, SupportedNetworks } from '@defindex/sdk';
// Initialize with API key (recommended for server-side use)
const sdk = new DefindexSDK({
apiKey: process.env.DEFINDEX_API_KEY, // Store securely in environment variables
baseUrl: 'https://api.defindex.io', // Optional: defaults to production API
timeout: 30000 // Optional: request timeout in milliseconds
});
import { DefindexSDK, SupportedNetworks } from '@defindex/sdk';
// Initialize the SDK
const sdk = new DefindexSDK({
apiKey: 'sk_your_api_key_here'
});
async function quickStart() {
try {
// Check API health
const health = await sdk.healthCheck();
console.log('API Status:', health.status.reachable);
// Get factory address
const factory = await sdk.getFactoryAddress(SupportedNetworks.TESTNET);
console.log('Factory Address:', factory.address);
// Get vault information
const vaultAddress = 'CVAULT_CONTRACT_ADDRESS...';
const vaultInfo = await sdk.getVaultInfo(vaultAddress, SupportedNetworks.TESTNET);
console.log(`Vault: ${vaultInfo.name} (${vaultInfo.symbol})`);
// Check user balance
const userAddress = 'GUSER_ADDRESS...';
const balance = await sdk.getVaultBalance(vaultAddress, userAddress, SupportedNetworks.TESTNET);
console.log(`Vault Shares: ${balance.dfTokens}`);
} catch (error) {
console.error('Operation failed:', error.message);
}
}
quickStart();
import {
DefindexSDK,
SupportedNetworks,
CreateDefindexVault,
DepositToVaultParams,
WithdrawFromVaultParams
} from '@defindex/sdk';
const sdk = new DefindexSDK({
apiKey: process.env.DEFINDEX_API_KEY
});
async function completeVaultFlow() {
try {
// 1. Create a new vault
const vaultConfig: CreateDefindexVault = {
roles: {
0: "GEMERGENCY_MANAGER_ADDRESS...", // Emergency Manager
1: "GFEE_RECEIVER_ADDRESS...", // Fee Receiver
2: "GVAULT_MANAGER_ADDRESS...", // Vault Manager
3: "GREBALANCE_MANAGER_ADDRESS..." // Rebalance Manager
},
vault_fee_bps: 100, // 1% fee (100 basis points)
assets: [{
address: "CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC", // XLM asset
strategies: [{
address: "CCEE2VAGPXKVIZXTVIT4O5B7GCUDTZTJ5RIXBPJSZ7JWJCJ2TLK75WVW", // Strategy contract
name: "XLM HODL Strategy",
paused: false
}]
}],
name_symbol: {
name: "My DeFi Vault", //Max 20 characters
symbol: "MDV"
},
upgradable: true,
caller: "GCREATOR_ADDRESS..." // Public key of the signer account
};
const createResponse = await sdk.createVault(vaultConfig, SupportedNetworks.TESTNET);
console.log('Vault XDR for signing:', createResponse.xdr);
// Sign the XDR with your wallet here
// const signedXDR = await yourWallet.sign(createResponse.xdr);
// const txResult = await sdk.sendTransaction(signedXDR, SupportedNetworks.TESTNET);
// 2. Deposit to vault
const vaultAddress = 'CVAULT_CONTRACT_ADDRESS...';
const depositData: DepositToVaultParams = {
amounts: [1000000], // 1 XLM (7 decimals)
caller: 'GUSER_ADDRESS...', // User's public key from which to sign and deposit
invest: true, // Auto-invest after deposit
slippageBps: 100 // 1% slippage tolerance
};
const depositResponse = await sdk.depositToVault(vaultAddress, depositData, SupportedNetworks.TESTNET);
console.log('Deposit XDR for signing:', depositResponse.xdr);
// Sign the deposit XDR with your wallet here
// const signedDepositXDR = await yourWallet.sign(depositResponse.xdr);
// const depositResult = await sdk.sendTransaction(signedDepositXDR, SupportedNetworks.TESTNET);
// 3. Check balance after deposit
const balance = await sdk.getVaultBalance(
vaultAddress,
'GUSER_ADDRESS...',
SupportedNetworks.TESTNET
);
console.log(`New vault shares: ${balance.dfTokens}`);
// 4. Withdraw from vault
const withdrawData: WithdrawFromVaultParams = {
amounts: [500000], // 0.5 XLM
caller: 'GUSER_ADDRESS...',
slippageBps: 100
};
const withdrawResponse = await sdk.withdrawFromVault(vaultAddress, withdrawData, SupportedNetworks.TESTNET);
console.log('Withdrawal XDR for signing:', withdrawResponse.xdr);
} catch (error) {
console.error('Vault operation failed:', error.message);
}
}
const health = await sdk.healthCheck();
if (health.status.reachable) {
console.log('API is healthy and operational');
} else {
console.log('API health issues detected');
}
// Note: Ensure the caller has the necessary role to perform this operation
// Pause a strategy
await sdk.pauseStrategy(vaultAddress, {
strategy_address: 'CSTRATEGY_ADDRESS...',
caller: 'GMANAGER_ADDRESS...'
}, SupportedNetworks.TESTNET);
// Unpause a strategy
await sdk.unpauseStrategy(vaultAddress, {
strategy_address: 'CSTRATEGY_ADDRESS...',
caller: 'GMANAGER_ADDRESS...'
}, SupportedNetworks.TESTNET);
// Submit via Stellar directly
const response = await sdk.sendTransaction(
signedXDR,
SupportedNetworks.TESTNET,
false // Don't use LaunchTube
);
// Submit via LaunchTube
const response = await sdk.sendTransaction(
signedXDR,
SupportedNetworks.TESTNET,
true // Use LaunchTube
);
console.log('Transaction hash:', response.hash);
console.log('Status:', response.status);
# Navigate to SDK directory
cd /path/to/defindex-sdk
# Install dependencies
pnpm install
# Copy environment configuration
cp .env.example .env
# Edit .env with your API key
# DEFINDEX_API_KEY=sk_your_api_key_here
# Run the complete example
pnpm run example