Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fair-gifts-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@galacticcouncil/sdk-next': patch
---

Added dcaSell function
24 changes: 24 additions & 0 deletions packages/sdk-next/src/sor/TradeScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,30 @@ export class TradeScheduler {
} as TradeOrder;
}

/**
* Build a DCA sell order (TWAP sell executed as DCA)
*
* @param assetIn - assetIn id
* @param assetOut - assetOut id
* @param amountInTotal - order budget
* @returns dca sell trade order
*/
async getDcaSellOrder(
assetIn: number,
assetOut: number,
amountInTotal: string
): Promise<TradeOrder> {
const order = await this.getTwapSellOrder(assetIn, assetOut, amountInTotal);
return {
...order,
type: TradeOrderType.DcaSell,
toHuman: () => ({
...order.toHuman(),
type: TradeOrderType.DcaSell,
}),
};
}

/**
* Build a TWAP (Time-Weighted Average Price) buy order
*
Expand Down
1 change: 1 addition & 0 deletions packages/sdk-next/src/sor/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export interface TradeOrder extends Humanizer {

export enum TradeOrderType {
Dca = 'Dca',
DcaSell = 'DcaSell',
TwapSell = 'TwapSell',
TwapBuy = 'TwapBuy',
}
Expand Down
39 changes: 39 additions & 0 deletions packages/sdk-next/src/tx/OrderTxBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export class OrderTxBuilder extends TxBuilder {
switch (type) {
case TradeOrderType.Dca:
return this.buildDcaTx();
case TradeOrderType.DcaSell:
return this.buildDcaSellTx();
case TradeOrderType.TwapSell:
return this.buildTwapSellTx();
case TradeOrderType.TwapBuy:
Expand Down Expand Up @@ -105,6 +107,43 @@ export class OrderTxBuilder extends TxBuilder {
return this.wrapTx('DcaSchedule', tx);
}

private async buildDcaSellTx(): Promise<Tx> {
const {
amountIn,
assetIn,
assetOut,
tradeAmountIn,
tradePeriod,
tradeRoute,
} = this.order;

let tx: Transaction = this.api.tx.DCA.schedule({
schedule: {
owner: this.beneficiary,
period: tradePeriod,
max_retries: this.maxRetries,
total_amount: amountIn,
slippage: this.slippagePct * 10000,
stability_threshold: undefined,
order: Enum('Sell', {
asset_in: assetIn,
asset_out: assetOut,
amount_in: tradeAmountIn,
min_amount_out: 0n,
route: tradeRoute as any,
}),
},
start_execution_block: undefined,
});

const hasDebt = await this.aaveUtils.hasBorrowPositions(this.beneficiary);
if (hasDebt) {
tx = await this.dispatchWithExtraGas(tx);
}

return this.wrapTx('DcaSchedule.dcaSell', tx);
}

private async buildTwapSellTx(): Promise<Tx> {
const {
amountIn,
Expand Down
Loading