Skip to main content

Overview

Integrating Recurring Buy involves managing the lifecycle of an order:
  1. Creation: Execute the recurring buy order transaction (Standard or Advanced).
  2. Monitoring: Fetch active orders to display in your UI.
  3. Cancellation: Allow users to close orders and retrieve remaining funds.

SDK Integration Flows

The SDK provides two methods for creating recurring buy orders, depending on your needs:

1. Standard Flow (executeCreateDca)

Use this for a simple, standalone recurring buy creation.
  • Handles everything: Automatically checks allowance, builds the approval transaction (if needed), and executes the recurring buy creation.
  • Best for: Standard UIs where the user just wants to “Create Order”.

2. Advanced Flow (createDcaToCalls)

Use this when you need to compose the recurring buy creation with other actions (multicall).
  • Returns Calls: Generates the necessary Starknet Call objects (including approval if needed).
  • Flexible: You can bundle these calls with other transactions (e.g., a swap or a deposit) and execute them atomically.
  • Best for: Complex flows, batching transactions, or using a custom account implementation.

1. Creating an Order

Use executeCreateDca to create the order. Ensure you import moment for the frequency.
import { executeCreateDca } from '@avnu/avnu-sdk';
import moment from 'moment';

const response = await executeCreateDca({
  provider: account,
  order: {
    sellTokenAddress: '0x...',
    buyTokenAddress: '0x...',
    sellAmount: '1000000000000000000', // 1 ETH
    sellAmountPerCycle: '100000000000000000', // 0.1 ETH
    frequency: moment.duration(1, 'day'),
    pricingStrategy: {},
    traderAddress: account.address
  }
});

2. Monitoring Orders

Use getDcaOrders to fetch a user’s orders. You can filter by status:
  • INDEXING - Order is being indexed after creation (temporary)
  • ACTIVE - Order is currently executing
  • CLOSED - Order completed or cancelled
import { getDcaOrders, DcaOrderStatus } from '@avnu/avnu-sdk';

const { content: orders } = await getDcaOrders({
  traderAddress: account.address,
  status: DcaOrderStatus.ACTIVE,
  page: 0,
  size: 20,
  sort: 'timestamp,desc'
});

orders.forEach(order => {
  console.log(`Order ${order.id}: ${order.amountSold} sold, ${order.amountBought} bought`);
});

3. Cancelling an Order

Users can cancel active orders to stop execution and retrieve unspent funds.
import { executeCancelDca } from '@avnu/avnu-sdk';

await executeCancelDca({
  provider: account,
  orderAddress: order.orderAddress // From the order object
});