SDK Reference
v7.4.0The Defactor SDK is a TypeScript library built on ethers.js v6 that provides type-safe access to all Defactor smart contracts. It offers two provider modes: SelfProvider for autonomous agents that sign their own transactions, and AssistedProvider for delegated signing workflows.
Installation
# Install via npm
npm install @defactor/defactor-sdk
# Or via yarn
yarn add @defactor/defactor-sdk
# Or via pnpm
pnpm add @defactor/defactor-sdkSelfProvider Mode
For autonomous AI agents that hold their own private keys. The SDK handles transaction signing, gas estimation, and broadcasting automatically.
1// SelfProvider — Agent signs transactions directly2// Best for: Autonomous agents with their own wallet keys3import {4 ERC20CollateralPool,5 Pools,6 Engage,7 SelfProvider8} from '@defactor/defactor-sdk'910// Initialize with any contract class11const poolProvider = new SelfProvider.SelfProvider<ERC20CollateralPool>(12 ERC20CollateralPool, // Contract class13 "0x...contractAddress", // Deployed contract address14 "https://polygon-rpc.com", // RPC endpoint URL15 "0x...agentPrivateKey" // Agent wallet private key16)1718// Direct method calls — SDK handles signing + broadcasting19const totalPools = await poolProvider.contract.getTotalPools()20const pools = await poolProvider.contract.getPools(0n, totalPools)21await poolProvider.contract.lend(0n, ethers.parseEther("100"))AssistedProvider Mode
For workflows where the agent builds transactions but signing is delegated to an external service, multi-sig wallet, or hardware wallet.
1// AssistedProvider — Delegated transaction building2// Best for: Multi-sig wallets, hardware wallets, or when agent3// builds transactions but another service signs them4import {5 ERC20CollateralPool,6 AssistedProvider7} from '@defactor/defactor-sdk'89const provider = new AssistedProvider.AssistedProvider<ERC20CollateralPool>(10 ERC20CollateralPool,11 "0x...contractAddress",12 "https://polygon-rpc.com"13 // No private key — returns unsigned transactions14)1516// Returns unsigned transaction object17const unsignedTx = await provider.contract.lend(0n, ethers.parseEther("100"))1819// Agent or external service signs the transaction20const signedTx = await externalSigner.signTransaction(unsignedTx)2122// Broadcast via any provider23await ethersProvider.broadcastTransaction(signedTx)Contract Classes
ERC20CollateralPoolStandard lending pools with ERC-20 collateral. Supports lend, borrow, repay, collect, and liquidate operations.
getTotalPools()getPools(offset, limit)lend(poolId, amount)borrow(poolId, amount)repay(poolId, amount)collectPool(poolId)liquidatePool(poolId)PoolsPool factory and management. Create new pools, configure parameters, and manage pool lifecycle.
createPool(params)updatePool(poolId, params)pausePool(poolId)getPoolConfig(poolId)EngageStaking and governance contract. Stake FACTR tokens, vote on proposals, and manage rewards.
stake(planId, amount)unstake(planId)getStakingPlans()vote(proposalId, support)claimRewards(planId)ERC20Standard ERC-20 token interactions. Approve, transfer, and query balances.
approve(spender, amount)transfer(to, amount)balanceOf(address)allowance(owner, spender)ERC3643Compliant security token standard. Includes identity registry and compliance checks.
isVerified(address)addIdentity(address, identity)removeIdentity(address)transfer(to, amount)Network Configuration
Contract addresses for each supported network. Replace RPC URLs with your own provider endpoints.
1// Network Configuration2const NETWORKS = {3 ethereum: {4 chainId: 1,5 rpc: "https://eth-mainnet.g.alchemy.com/v2/YOUR_KEY",6 contracts: {7 erc20CollateralPool: "0x...",8 engage: "0x...",9 factr: "0x..."10 }11 },12 polygon: {13 chainId: 137,14 rpc: "https://polygon-rpc.com",15 contracts: {16 erc20CollateralPool: "0x...",17 engage: "0x...",18 factr: "0x..."19 }20 },21 base: {22 chainId: 8453,23 rpc: "https://mainnet.base.org",24 contracts: {25 erc20CollateralPool: "0x...",26 engage: "0x...",27 factr: "0x..."28 }29 },30 bsc: {31 chainId: 56,32 rpc: "https://bsc-dataseed.binance.org",33 contracts: {34 erc20CollateralPool: "0x...",35 engage: "0x...",36 factr: "0x..."37 }38 }39}Error Handling for Agents
The SDK throws typed errors that agents can catch and handle programmatically. Common error codes and recovery strategies:
1// Error Handling for AI Agents2import { DefactorError } from '@defactor/defactor-sdk'34try {5 await provider.contract.lend(poolId, amount)6} catch (error) {7 if (error instanceof DefactorError) {8 switch (error.code) {9 case 'INSUFFICIENT_BALANCE':10 // Agent needs more tokens11 break12 case 'POOL_EXPIRED':13 // Pool deadline has passed, find another pool14 break15 case 'INSUFFICIENT_ALLOWANCE':16 // Need to approve token spending first17 await approveToken(tokenAddress, spender, amount)18 // Retry the operation19 await provider.contract.lend(poolId, amount)20 break21 case 'POOL_FULL':22 // Pool hard cap reached, find another pool23 break24 case 'UNDER_COLLATERALIZED':25 // Need more collateral before borrowing26 break27 default:28 // Log and escalate unknown errors29 console.error('Unhandled Defactor error:', error)30 }31 }32}