Skip to main content

Method Signature

optimizer.optimize(params: OptimizeParams): Promise<OptimizeResponse>

Description

Calculates the optimal allocation of capital across DeFi lending protocols to maximize APR while respecting constraints.

Parameters

chainId
Chain
required
Chain to optimize for (Base or Arbitrum).
total_capital
string
required
Total capital to allocate (bigint as string, in token’s smallest unit).
token_address
Address
required
Token to optimize (e.g., USDC address).
current_allocations
Record<string, string>
required
Current distribution across protocols.
protocols
string[]
required
Protocols to consider.
constraints
ConstraintConfig[]
Optional constraints to apply during optimization.Available constraint types:
KindRequired ParamsDescription
MIN_PROTOCOLSmin_protocols (int)Minimum number of protocols to use
MAX_AMOUNT_PER_PROTOCOLprotocol (string), max_ratio (0-1)Cap a specific protocol at a percentage of total
MAX_ALLOCATION_AMOUNT_PER_PROTOCOLprotocol (string), max_amount (int)Cap a specific protocol at an absolute amount
MIN_AMOUNTmin_amount (int)Minimum amount for any used protocol
MIN_ALLOCATION_AMOUNT_PER_PROTOCOLprotocol (string), min_amount (int)Minimum amount for a specific protocol
EXCLUDE_PROTOCOLprotocol (string)Exclude a protocol entirely
wallet_address
Address
Optional wallet address that will execute the transactions.

Return Value

optimization_result
OptimizationResult
Optimal allocations, APR improvement, and gas costs.
action_plan
ActionDetail[]
Step-by-step rebalancing actions.
calldata
CalldataInfo[]
Execution-ready transaction data.

Example

Basic Usage

const result = await giza.optimizer.optimize({
  chainId: Chain.BASE,
  total_capital: "1000000000",
  token_address: USDC_ADDRESS,
  current_allocations: {
    aave: "500000000",
    compound: "500000000"
  },
  protocols: ["aave", "compound", "moonwell"],
});

console.log(`APR improvement: +${result.optimization_result.apr_improvement}%`);
console.log('Optimal allocation:', result.optimization_result.allocations);

With Constraints

import { WalletConstraints } from '@gizatech/agent-sdk';

const result = await giza.optimizer.optimize({
  chainId: Chain.BASE,
  total_capital: "1000000000",
  token_address: USDC_ADDRESS,
  current_allocations: {
    aave: "500000000",
    compound: "500000000"
  },
  protocols: ["aave", "compound", "moonwell", "fluid"],
  constraints: [
    // Require at least 2 protocols
    {
      kind: WalletConstraints.MIN_PROTOCOLS,
      params: { min_protocols: 2 }
    },
    // Cap each protocol at 50% of total capital
    {
      kind: WalletConstraints.MAX_AMOUNT_PER_PROTOCOL,
      params: { protocol: "aave", max_ratio: 0.5 }
    },
    {
      kind: WalletConstraints.MAX_AMOUNT_PER_PROTOCOL,
      params: { protocol: "compound", max_ratio: 0.5 }
    },
    {
      kind: WalletConstraints.MAX_AMOUNT_PER_PROTOCOL,
      params: { protocol: "moonwell", max_ratio: 0.5 }
    },
    {
      kind: WalletConstraints.MAX_AMOUNT_PER_PROTOCOL,
      params: { protocol: "fluid", max_ratio: 0.5 }
    }
  ]
});
Common Mistake: MAX_AMOUNT_PER_PROTOCOL uses max_ratio (0-1), not max_amount. For absolute amounts, use MAX_ALLOCATION_AMOUNT_PER_PROTOCOL instead.
See Optimizer Concept for detailed information.