Scheduled Orders
The Scheduled Orders module allows users to schedule swaps to be executed at a later time, with an optional recurring schedule. This is useful for scheduling recurring swap-based executions, such as dollar cost averaging. This module is an executor that is installed on an account and can be triggered by an automation service at the pre-specified time(s).
You can find the source code for the module in the Core Modules repo (opens in a new tab) and the deployment address in the address book.
RecurringOrder
type
The RecurringOrder
type is an object that represents a recurring order. It has the following properties:
type RecurringOrder = Order & Schedule;
type Order = {
buyToken: ERC20Token;
sellToken: ERC20Token;
amount: number;
orderType: "buy" | "sell";
priceLimit?: string;
maxGasPrice?: string;
expirationDate?: string;
};
type Schedule = {
startDate: number; // UNIX timestamp
repeatEvery: number; // In seconds
numberOfRepeats: number;
};
type ERC20Token = {
token_address: Address;
decimals: number;
};
getSwapOrderData
The getSwapOrderData
function is used to create a hex-encoded struct that represents the swap.
import { getSwapOrderData } from "@rhinestone/module-sdk";
const swapOrderData = getSwapOrderData({
recurringOrder: {
buyToken: {
token_address: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // USDC
decimals: 6,
},
sellToken: {
token_address: "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984", // UNI
decimals: 18,
},
amount: 10,
orderType: "buy",
startDate: 1710759572,
repeatEvery: 60 * 60 * 24, // 1 day
numberOfRepeats: 10,
},
});
getScheduledOrdersExecutor
The getScheduledOrdersExecutor
function is used to create an instance of the Scheduled Orders module. The executionInterval
is a number that designates the interval at which the execution should occur in seconds. The numberOfExecutions
is a number that designates how many times this transfer should be executed. The startDate
is the UNIX timestamp of when the first execution should occur. The executionData
object is a hex-encoded struct that represents the swap (to encode it, use getSwapOrderData
). This function returns a module object.
import { getScheduledOrdersExecutor, getSwapOrderData } from "@rhinestone/module-sdk";
const executeInterval = 60 _ 60 _ 24; // 1 day
const numberOfExecutions = 10;
const startDate = 1710759572; // UNIX timestamp
const executionData = getSwapOrderData({...});
const scheduledOrderExecutor = getScheduledOrdersExecutor({
executeInterval,
numberOfExecutions,
startDate,
executionData,
});
getCreateScheduledOrderAction
The getCreateScheduledOrderAction
function is used to create an execution that can be used to schedule a swap. The recurringOrder
is an object of type RecurringOrder
which includes the details for the scheduled swap. This function returns an execution object.
import { getCreateScheduledOrderAction } from "@rhinestone/module-sdk";
const recurringOrderExecution = getCreateScheduledOrderAction({
recurringOrder: {
buyToken: {
token_address: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // USDC
decimals: 6,
},
sellToken: {
token_address: "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984", // UNI
decimals: 18,
},
amount: 10,
orderType: "buy",
startDate: 1710759572,
repeatEvery: 60 * 60 * 24, // 1 day
numberOfRepeats: 10,
},
});
getExecuteScheduledOrderAction
The getExecuteScheduledOrderAction
function is used to create an execution for execute scheduled order. This function returns an execution object.
import { getExecuteScheduledOrderAction } from "@rhinestone/module-sdk";
const executeScheduledOrderAction = getExecuteScheduledOrderAction({
jobId: 1
});
SCHEDULED_ORDERS_EXECUTER_ADDRESS
The SCHEDULED_ORDERS_EXECUTER_ADDRESS
is a constant that represents the address of the Scheduled Orders module.