defactor.ai

SDK Reference

v7.4.0

The 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

Terminalbash
# Install via npm
npm install @defactor/defactor-sdk

# Or via yarn
yarn add @defactor/defactor-sdk

# Or via pnpm
pnpm add @defactor/defactor-sdk
Requirements: Node.js 18+, TypeScript 5+, ethers.js v6

SelfProvider Mode

For autonomous AI agents that hold their own private keys. The SDK handles transaction signing, gas estimation, and broadcasting automatically.

SelfProvider — Autonomous Agent Modetypescript
1// SelfProvider — Agent signs transactions directly
2// Best for: Autonomous agents with their own wallet keys
3import {
4 ERC20CollateralPool,
5 Pools,
6 Engage,
7 SelfProvider
8} from '@defactor/defactor-sdk'
9
10// Initialize with any contract class
11const poolProvider = new SelfProvider.SelfProvider<ERC20CollateralPool>(
12 ERC20CollateralPool, // Contract class
13 "0x...contractAddress", // Deployed contract address
14 "https://polygon-rpc.com", // RPC endpoint URL
15 "0x...agentPrivateKey" // Agent wallet private key
16)
17
18// Direct method calls — SDK handles signing + broadcasting
19const 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.

AssistedProvider — Delegated Signingtypescript
1// AssistedProvider — Delegated transaction building
2// Best for: Multi-sig wallets, hardware wallets, or when agent
3// builds transactions but another service signs them
4import {
5 ERC20CollateralPool,
6 AssistedProvider
7} from '@defactor/defactor-sdk'
8
9const provider = new AssistedProvider.AssistedProvider<ERC20CollateralPool>(
10 ERC20CollateralPool,
11 "0x...contractAddress",
12 "https://polygon-rpc.com"
13 // No private key — returns unsigned transactions
14)
15
16// Returns unsigned transaction object
17const unsignedTx = await provider.contract.lend(0n, ethers.parseEther("100"))
18
19// Agent or external service signs the transaction
20const signedTx = await externalSigner.signTransaction(unsignedTx)
21
22// Broadcast via any provider
23await ethersProvider.broadcastTransaction(signedTx)

Contract Classes

ERC20CollateralPool

Standard 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)
Pools

Pool factory and management. Create new pools, configure parameters, and manage pool lifecycle.

createPool(params)updatePool(poolId, params)pausePool(poolId)getPoolConfig(poolId)
Engage

Staking and governance contract. Stake FACTR tokens, vote on proposals, and manage rewards.

stake(planId, amount)unstake(planId)getStakingPlans()vote(proposalId, support)claimRewards(planId)
ERC20

Standard ERC-20 token interactions. Approve, transfer, and query balances.

approve(spender, amount)transfer(to, amount)balanceOf(address)allowance(owner, spender)
ERC3643

Compliant 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.

network-config.tstypescript
1// Network Configuration
2const 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:

Error Handlingtypescript
1// Error Handling for AI Agents
2import { DefactorError } from '@defactor/defactor-sdk'
3
4try {
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 tokens
11 break
12 case 'POOL_EXPIRED':
13 // Pool deadline has passed, find another pool
14 break
15 case 'INSUFFICIENT_ALLOWANCE':
16 // Need to approve token spending first
17 await approveToken(tokenAddress, spender, amount)
18 // Retry the operation
19 await provider.contract.lend(poolId, amount)
20 break
21 case 'POOL_FULL':
22 // Pool hard cap reached, find another pool
23 break
24 case 'UNDER_COLLATERALIZED':
25 // Need more collateral before borrowing
26 break
27 default:
28 // Log and escalate unknown errors
29 console.error('Unhandled Defactor error:', error)
30 }
31 }
32}

Resources