DeFi Primitives

Updated

The SDK defines 33 typed DeFi primitive constants spanning 7 categories. Each constant maps to a discriminated union member of the Primitive type, ensuring compile-time safety when handling transaction actions returned by the API.

All primitives, category arrays, and validators are exported from the package root.

import.ts
import {
  // Constants
  PRIMITIVE_SWAP,
  PRIMITIVE_LENDING_DEPOSIT,
  // Category arrays
  ALL_PRIMITIVES,
  LENDING_PRIMITIVES,
  // Validators
  isValidPrimitive,
  isLendingPrimitive,
  // Types
  type PrimitiveType,
  type Primitive,
  type SwapPrimitive,
} from '@chaoslabs/ai-sdk';

PrimitiveType

A union type derived from the generated Primitive discriminated union. It contains all 33 string literal types.

primitive-type.ts
// Derived from the generated Primitive union — always stays in sync
export type PrimitiveType = Primitive['type'];
 
// Equivalent to:
// 'swap' | 'bridge' | 'transfer' | 'lending_deposit' | 'lending_withdraw' | ...

Primitive Constants

Each constant is typed with as const for full literal type inference.

Trading (1)

ConstantValue
PRIMITIVE_SWAP'swap'

Transfer (2)

ConstantValue
PRIMITIVE_BRIDGE'bridge'
PRIMITIVE_TRANSFER'transfer'

Lending (5)

ConstantValue
PRIMITIVE_LENDING_DEPOSIT'lending_deposit'
PRIMITIVE_LENDING_WITHDRAW'lending_withdraw'
PRIMITIVE_LENDING_BORROW'lending_borrow'
PRIMITIVE_LENDING_REPAY'lending_repay'
PRIMITIVE_LENDING_CLAIM_REWARDS'lending_claim_rewards'

Staking (8)

ConstantValue
PRIMITIVE_STAKING_STAKE'staking_stake'
PRIMITIVE_STAKING_UNSTAKE'staking_unstake'
PRIMITIVE_STAKING_CLAIM'staking_claim'
PRIMITIVE_STAKING_COOLDOWN'staking_cooldown'
PRIMITIVE_STAKING_DELEGATE'staking_delegate'
PRIMITIVE_STAKING_UNDELEGATE'staking_undelegate'
PRIMITIVE_STAKING_WRAP'staking_wrap'
PRIMITIVE_STAKING_UNWRAP'staking_unwrap'

Pendle (6)

ConstantValue
PRIMITIVE_PENDLE_ADD_LIQUIDITY'pendle_add_liquidity'
PRIMITIVE_PENDLE_REMOVE_LIQUIDITY'pendle_remove_liquidity'
PRIMITIVE_PENDLE_MINT_PT_YT'pendle_mint_pt_yt'
PRIMITIVE_PENDLE_REDEEM'pendle_redeem'
PRIMITIVE_PENDLE_SWAP_TO_PT'pendle_swap_to_pt'
PRIMITIVE_PENDLE_SWAP_TO_YT'pendle_swap_to_yt'

Perpetuals (7)

ConstantValue
PRIMITIVE_PERP_OPEN'perp_open'
PRIMITIVE_PERP_CLOSE'perp_close'
PRIMITIVE_PERP_INCREASE'perp_increase'
PRIMITIVE_PERP_DECREASE'perp_decrease'
PRIMITIVE_PERP_DEPOSIT'perp_deposit'
PRIMITIVE_PERP_WITHDRAW'perp_withdraw'
PRIMITIVE_PERP_SET_LEVERAGE'perp_set_leverage'

Vault (4)

ConstantValue
PRIMITIVE_VAULT_DEPOSIT'vault_deposit'
PRIMITIVE_VAULT_WITHDRAW'vault_withdraw'
PRIMITIVE_VAULT_BORROW'vault_borrow'
PRIMITIVE_VAULT_REPAY'vault_repay'

Category Arrays

Pre-built arrays group primitive types by DeFi domain. Each is typed as PrimitiveType[].

category-arrays.ts
export const ALL_PRIMITIVES: PrimitiveType[];        // all 33 primitives
export const TRADING_PRIMITIVES: PrimitiveType[];    // ['swap']
export const TRANSFER_PRIMITIVES: PrimitiveType[];   // ['bridge', 'transfer']
export const LENDING_PRIMITIVES: PrimitiveType[];    // 5 lending primitives
export const STAKING_PRIMITIVES: PrimitiveType[];    // 8 staking primitives
export const PENDLE_PRIMITIVES: PrimitiveType[];     // 6 pendle primitives
export const PERP_PRIMITIVES: PrimitiveType[];       // 7 perpetual primitives
export const VAULT_PRIMITIVES: PrimitiveType[];      // 4 vault primitives
[@portabletext/react] Unknown block type "callout", specify a component for it in the `components.types` prop

Validation Helpers

All validators use internal Set instances for O(1) lookup performance.

isValidPrimitive

Type guard that narrows a string to PrimitiveType.

is-valid-primitive.ts
export function isValidPrimitive(primitive: string): primitive is PrimitiveType;
 
// Example
const userInput = 'lending_deposit';
if (isValidPrimitive(userInput)) {
  // userInput is narrowed to PrimitiveType
  console.log(`Valid primitive: ${userInput}`);
}

Category Validators

Each category has a corresponding boolean validator.

category-validators.ts
export function isLendingPrimitive(primitive: string): boolean;
export function isTradingPrimitive(primitive: string): boolean;
export function isStakingPrimitive(primitive: string): boolean;
export function isPendlePrimitive(primitive: string): boolean;
export function isPerpPrimitive(primitive: string): boolean;
export function isVaultPrimitive(primitive: string): boolean;
export function isTransferPrimitive(primitive: string): boolean;

Usage Example

validator-example.ts
import {
  extractPrimitives,
  isLendingPrimitive,
  isPerpPrimitive,
  isValidPrimitive,
} from '@chaoslabs/ai-sdk';
 
const primitives = extractPrimitives(response);
 
for (const p of primitives) {
  if (isLendingPrimitive(p.type)) {
    console.log('Lending operation:', p.type);
  } else if (isPerpPrimitive(p.type)) {
    console.log('Perp operation:', p.type);
  }
}

Primitive Types (Generated)

Each primitive constant has a corresponding generated type pair: the primitive type (a discriminated union member) and its params type. All are re-exported from the package root.

Primitive TypeParams Type
SwapPrimitiveSwapParams
BridgePrimitiveBridgeParams
TransferPrimitiveTransferParams
LendingDepositPrimitiveLendingDepositParams
LendingWithdrawPrimitiveLendingWithdrawParams
LendingBorrowPrimitiveLendingBorrowParams
LendingRepayPrimitiveLendingRepayParams
LendingClaimRewardsPrimitiveLendingClaimRewardsParams
StakingStakePrimitiveStakingStakeParams
StakingUnstakePrimitiveStakingUnstakeParams
StakingClaimPrimitiveStakingClaimParams
StakingCooldownPrimitiveStakingCooldownParams
StakingDelegatePrimitiveStakingDelegateParams
StakingUndelegatePrimitiveStakingUndelegateParams
StakingWrapPrimitiveStakingWrapParams
StakingUnwrapPrimitiveStakingUnwrapParams
PendleAddLiquidityPrimitivePendleAddLiquidityParams
PendleRemoveLiquidityPrimitivePendleRemoveLiquidityParams
PendleMintPtYtPrimitivePendleMintPtYtParams
PendleRedeemPrimitivePendleRedeemParams
PendleSwapToPtPrimitivePendleSwapToPtParams
PendleSwapToYtPrimitivePendleSwapToYtParams
PerpOpenPrimitivePerpOpenParams
PerpClosePrimitivePerpCloseParams
PerpIncreasePrimitivePerpIncreaseParams
PerpDecreasePrimitivePerpDecreaseParams
PerpDepositPrimitivePerpDepositParams
PerpWithdrawPrimitivePerpWithdrawParams
PerpSetLeveragePrimitivePerpSetLeverageParams
VaultDepositPrimitiveVaultDepositParams
VaultWithdrawPrimitiveVaultWithdrawParams
VaultBorrowPrimitiveVaultBorrowParams
VaultRepayPrimitiveVaultRepayParams

See Type Guards for runtime narrowing of Primitive to specific types.

Was this helpful?