Skip to content

Runtime Lifecycle

DeFiRuntime is the execution orchestrator in @stendar/core.

At minimum you provide:

  • chains: one or more ChainAdapter implementations
  • providers: protocol providers (SwapProvider, YieldProvider, BridgeProvider)
  • wallet: one adapter or an array of adapters

Optional config includes policies, dryRun, bestExecution, stateStore, logger, and mutexTimeoutMs.

const runtime = new DeFiRuntime({
chains,
providers,
wallet,
policies,
bestExecution: {
timeoutMs: 10_000,
crossChain: { enabled: true, maxPaths: 3 },
},
});
await runtime.start();
try {
// runtime.getQuote / executeSwap / executeBridge / executeDeposit / executeWithdraw
} finally {
await runtime.stop();
}
  • start() loads state and emits runtime:started
  • stop() persists state and emits runtime:stopped
  • operations throw RUNTIME_NOT_STARTED if called before start()
  • Discovery: getBalance, getQuote, getBestRoute, getTokenPrice, getYieldRates, getPositions
  • Execution: executeSwap, executeBridge, executeDeposit, executeWithdraw
  • Bridging status: getBridgeStatus

Each execution method can return policy-driven statuses like denied and approval_required, not just chain failures.

Runtime serializes state-mutating executions behind an internal mutex so cumulative policy checks (for example daily spend/rate windows) stay consistent.

For detailed policy behavior, see Policies.