DCA

Automated Dollar-Cost Averaging (DCA) strategy to invest steadily over time, minimizing the impact of market volatility and building positions across multiple assets or pools with ease.

A system for automating regular, periodic investments to reduce the impact of volatility.

Initialization

const afSdk = new Aftermath("MAINNET");
await afSdk.init(); // initialize provider

const dca = afSdk.Dca();

Constants

DCA.constants = {
	gasAmount: BigInt(50_000_000),
};

Order Management

Fetch Orders

// Get all DCA orders
const allOrders = await dca.getAllDcaOrders({
	walletAddress: "0x...",
});

// Get only active orders
const activeOrders = await dca.getActiveDcaOrders({
	walletAddress: "0x...",
});

// Get past orders
const pastOrders = await dca.getPastDcaOrders({
	walletAddress: "0x...",
});

Create DCA Order

const tx = await dca.getCreateDcaOrderTx({
	walletAddress: "0x...",
	allocateCoinType: "0x2::sui::SUI",
	allocateCoinAmount: BigInt(10_000_000_000), // 10 SUI
	buyCoinType:
		"0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN",
	frequencyMs: 60000, // Execute trade every minute
	tradesAmount: 5, // Split into 5 trades
	delayTimeMs: 0, // Start immediately
	maxAllowableSlippageBps: 100, // 1% max slippage
	coinPerTradeAmount: BigInt(2_000_000_000), // 2 SUI per trade
	customRecipient: "0x...", // Optional custom recipient
	strategy: {
		minPrice: BigInt(4_000_000), // Min price ~2.0
		maxPrice: BigInt(8_000_000), // Max price ~4.0
	},
	integratorFee: {
		feeBps: 100, // 1% fee
		feeRecipient: "0x...",
	},
	isSponsoredTx: boolean,
});

Cancel DCA Order

// Generate message to sign
const message = dca.closeDcaOrderMessageToSign({
	orderIds: ["0x..."],
});

// Cancel order
const success = await dca.closeDcaOrder({
	walletAddress: "0x...",
	bytes: signedBytes,
	signature: signedSignature,
});

User Management

// Get user's public key
const publicKey = await dca.getUserPublicKey({
	walletAddress: "0x...",
});

// Create user account
const message = dca.createUserAccountMessageToSign();
const success = await dca.createUserPublicKey({
	walletAddress: "0x...",
	bytes: signedBytes,
	signature: signedSignature,
});

Types

Order Types

interface DcaOrderObject {
	objectId: string;
	overview: {
		allocatedCoin: {
			coin: string;
			amount: bigint;
		};
		buyCoin: {
			coin: string;
			amount: bigint;
		};
		totalSpent: bigint;
		intervalMs: number;
		totalTrades: number;
		tradesRemaining: number;
		maxSlippageBps: number;
		progress: number;
		recipient?: string;
		strategy?: {
			minPrice: bigint;
			maxPrice: bigint;
		};
		integratorFee?: {
			feeBps: number;
			feeRecipient: string;
		};
	};
	trades: {
		allocatedCoin: {
			coin: string;
			amount: bigint;
		};
		buyCoin: {
			coin: string;
			amount: bigint;
		};
		tnxDigest: string;
		tnxDate: number;
		rate?: number;
	}[];
	failed: {
		timestamp: number;
		reason?:
			| "INTERNAL"
			| "STRATEGY"
			| "GAS_CAP"
			| "UNKNOWN_USER"
			| "SLIPPAGE";
	}[];
}

Example Usage

const afSdk = new Aftermath("MAINNET");
await afSdk.init();
const dca = afSdk.Dca();

// Create DCA order: Buy 10 SUI worth of USDC in 5 trades
const tx = await dca.getCreateDcaOrderTx({
	walletAddress: "0x...",
	allocateCoinType: "0x2::sui::SUI",
	allocateCoinAmount: BigInt(10_000_000_000),
	buyCoinType:
		"0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN",
	frequencyMs: 3600000, // Every hour
	tradesAmount: 5,
	delayTimeMs: 0,
	maxAllowableSlippageBps: 100,
	coinPerTradeAmount: BigInt(2_000_000_000),
	strategy: {
		minPrice: BigInt(4_000_000), // Min ~2.0 USDC per SUI
		maxPrice: BigInt(8_000_000), // Max ~4.0 USDC per SUI
	},
});

// Check active orders
const activeOrders = await dca.getActiveDcaOrders({
	walletAddress: "0x...",
});

// Cancel an order if needed
const message = dca.closeDcaOrderMessageToSign({
	orderIds: [activeOrders[0].objectId],
});
const success = await dca.closeDcaOrder({
	walletAddress: "0x...",
	bytes: signedBytes,
	signature: signedSignature,
});

Last updated