Aftermath
  • Aftermath
    • About Aftermath Finance
      • What are we building?
  • Getting Started
    • Creating an account
      • zkLogin
        • Removing a zkLogin account
      • Sui Metamask Snap
      • Native Sui wallets
    • Dynamic Gas
    • Navigating Aftermath
      • Interacting with your Wallet
      • Viewing your Portfolio
      • Changing your Settings
      • Bridge
      • Referrals
  • Trade
    • Smart-Order Router
      • Agg of Aggs
      • Making a trade
      • Exact Out
      • Fees
    • DCA
      • Why should I use DCA
      • How does DCA work
      • Tutorials
        • Creating a DCA order
        • Monitoring DCA progress
        • Advanced Features
      • Fees
      • Contracts
  • Limit Orders
    • Contracts
    • Fees
  • Pools
    • Constant Function Market Maker
      • Tutorials
        • Depositing
        • Withdrawing
        • Creating a Pool
      • Fees
      • Contracts
      • Audit
  • Farms
    • Afterburner Vaults
      • Tutorials
        • Staking into a Farm
        • Claiming Rewards
        • Unstaking
        • Creating a Farm
      • Architecture
        • Vault
        • Stake Position
      • Fees
      • FAQs
  • Liquid Staking
    • afSUI
      • Tutorials
        • Staking
        • Unstaking
      • Architecture
        • Packages & Modules
        • Entry Points
      • Fees
      • FAQs
      • Contracts
      • Audit
  • Perpetuals
    • Aftermath Perpetuals
      • Tutorials
        • Creating an Account
        • Selecting a Market
        • Creating a Market Order
        • Creating a Limit Order
        • Maintaining your Positions
      • Architecture
        • Oracle Prices
        • Margin
        • Account
        • Trading
        • Funding
        • Liquidations
        • Fees
  • GameFi
    • NFT AMM
      • Architecture
        • Fission Vaults
        • AMM Pools
      • Tutorials
        • Buy
        • Sell
        • Deposit
        • Withdraw
      • Sui Overflow
  • Our validator
    • About us
  • Developers
    • Aftermath TS SDK
      • Utils
        • Coin
        • Users Data
        • Authorization
      • Products
        • Prices
        • Router
        • DCA
        • Limit Orders
        • Liquid Staking
        • Pools
        • Farms
    • Aftermath REST API
      • Authorization
  • Egg
    • About Egg
  • Legal
    • Terms of Service
    • Privacy Policy
  • Languages
    • 中文
    • 한국어
  • Links
    • X
    • Telegram
    • Discord
    • Github
    • Medium
    • Aftermath Validator
Powered by GitBook
On this page
  • Initialization
  • Constants
  • Order Management
  • Types
  • Example Usage
  1. Developers
  2. Aftermath TS SDK
  3. Products

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 - deprecated
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 - Deprecated

Current variant is deprecated. Please use createUserAccountMessageToSign() and createUserPublicKey() from UserData package instead.

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;
		};
		// Deprecated
		tnxDigest: string;
		// Replaced by
		txnDigest: string;
		// Deprecated
		tnxDate: number;
		// Replaced by
		txnTimestamp: 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,
});
PreviousRouterNextLimit Orders

Last updated 1 month ago