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.

DCA

const dca = new Aftermath("MAINNET").Dca();\

Fetch Orders

Active and Past Orders

const allOrders = await dca.getActiveDcaOrders({
  walletAddress: "0x..",
});

Response

{
  "active": [
    {
      "objectId": "0x..",
      "overview": [
        // ..
      ],
      "failed": [
        // ..
      ],
      "trades": [
        // ..
      ]
    }
  ],
  "past": [
    // ..
  ]
}

Active Orders

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

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

Response

{
  "activeOrders": [
    {
      "objectId": "0x..",
      "overview": {
        "allocatedCoin": {
          "coin": "0x..",
          "amount": "1_000_000n"
        },
        "buyCoin": {
          "coin": "0x..",
          "amount": "1_000_000n"
        },
        "totalSpent": "1_000_000n",
        "intervalMs": 60000,
        "totalTrades": 2,
        "tradesRemaining": 1,
        "maxSlippageBps": 10000,
        "progress": 0.5,
        "recipient": "0x..",
        "created": {
          "time": "1721129837764",
          "tnxDigest": "AZLn7s4X..."
        },
        "nextTrade": {
          "time": 1721129777764,
          "tnxDigest": "AZLn7s4X..."
        }
      },
      "failed": [
        {
          "timestamp": 1721129777764,
          "reason": "INTERNAL"
        }
      ],
      "trades": [
        {
          "allocatedCoin": {
            "coin": "0x..",
            "amount": "1_000_000n"
          },
          "buyCoin": {
            "coin": "0x..",
            "amount": "1_000_000n"
          },
          "tnxDigest": "AZLn7s4X...",
          "tnxDate": "1731751113062",
          "rate": 0.00490028
        }
      ]
    }
  ]
}

Error Codes

Code
Description

INTERNAL

An internal error occured

STRATEGY

Coin price was not within the given strategy bounds

GAS_CAP

Gas cost for trade was too expensive

UNKNOWN_USER

An unknown error occured

Create Order

Variable
Type
Description

walletAddress

string

Wallet address that pays gas, owns the order, and receives money by default unless a custom recipient is set

allocateCoinType

string

Coin you allocate for trade

allocateCoinAmount

bigInt

Amount of allocated coin

buyCoinType

string

Coin you buy

frequencyMs

number

How often the trade should occur

tradesAmount

number

Number of trades into which order should be split

delayTimeMs

number

Delay timeout before trades commence

maxAllowableSlippageBps

number

Slippage tolerance

coinPerTradeAmount

bigInt

Amount of allocated coin divided by number of trades

customRecipient

string?

Optional wallet address where funds could be transferred

strategy

object?

Strategy applied to each trade execution

minPrice

bigInt

Minimum price value per trade (normalized)

maxPrice

bigInt

Maximum price value per trade (normalized)

Final minimum and maximum strategy values should be multipled by "coinPerTradeAmount". E.g. user inputs minimum price equal 1SUI ~ 2.0 wUSDC trading 10 Sui to wUSDC in 5 trades. Final minimum price will be (2.0 * 10 SUI) * decimals (wUSDC)

Code Example

const tx = dca.getCreateDcaOrderTx({
  walletAddress: "0x..",
  allocateCoinType: "0x2::sui::SUI",
  allocateCoinAmount: BigInt(10_000_000_000), // 10 SUI
  buyCoinType: "0x5d4b302506645c37ff133b98c4b50a5ae14841659738d6d733d59d0d217a93bf::coin::COIN",
  frequencyMs: 60000,
  tradesAmount: 5,
  delayTimeMs: 0,
  maxAllowableSlippageBps: 100,
  coinPerTradeAmount: BigInt(2_000_000_000), // 2 SUI
  customRecipient: "0x..",
  strategy: {
    minPrice: BigInt(4_000_000), // 1SUI ~ 2.0
    maxPrice: BigInt(8_000_000), // 1SUI ~ 4.0
  },
});

Cancel Order

Signed Message Inputs

Variable
Type
Description

bytes

string

Bytes of signed message

signature

string

Users signature for signed message

Close DCA Order Inputs

Variable
Type
Description

action

string

Title shown in the pop-up for UX (CANCEL_DCA_ORDER by default)

order_object_id

string

Address of the order that needs to be closed

signedMessage

object

Message the user signs to close the order

walletAddress

string

Wallet address that owns the order

Code Example

const signedMessage = dca.closeDcaOrderMessageToSign({
	action: "CANCEL_DCA_ORDER",
	order_object_id: "0x.."
});

const success = await dca.closeDcaOrder({
	...signedMessage,
	walletAddress: "0x..",
});

Last updated