UNPKG

@robertprp/intents-sdk

Version:

Shogun Network Intent-based cross-chain swaps SDK

169 lines 6.06 kB
// src/core/orders/dca-single-chain.ts import { ChainID, chainIdToChainTypeMap, isEvmChain } from '../../chains.js'; import { ValidationError } from '../../errors/index.js'; import { DcaSingleChainOrderValidator } from '../../utils/order-validator.js'; import { BaseSDK } from '../sdk.js'; import { getEVMDcaSingleChainOrderTypedData } from '../evm/order-signature.js'; import {} from './common.js'; import { getSolanaDcaSingleChainOrderInstructions } from '../solana/dca/create-order.js'; export class DcaSingleChainOrder { constructor(params) { Object.defineProperty(this, "user", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "chainId", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "tokenIn", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "tokenOut", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "destinationAddress", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "startTime", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "amountInPerInterval", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "totalIntervals", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "intervalDuration", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "amountOutMin", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "extraTransfers", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "deadline", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.user = params.user; this.chainId = params.chainId; this.tokenIn = params.tokenIn; this.tokenOut = params.tokenOut; this.destinationAddress = params.destinationAddress; this.startTime = params.startTime; this.amountInPerInterval = params.amountInPerInterval; this.totalIntervals = params.totalIntervals; this.intervalDuration = params.intervalDuration; this.amountOutMin = params.amountOutMin ?? 1n; this.extraTransfers = params.extraTransfers; this.deadline = params.deadline; } static async create(input) { new DcaSingleChainOrderValidator().validateOrder(input); const order = new DcaSingleChainOrder({ ...input, user: input.user, }); if (isEvmChain(order.chainId)) { return order; } const preparedRandomData = order.getRandomPreparedData(); const intentRequest = order.toIntentRequest(preparedRandomData); await BaseSDK.validateDcaSingleChainOrder(intentRequest); return order; } /// This is needed because API requires the prepared data to be sent /// In the cases of Solana and Sui, if we want the real data, we must send the order on-chain before validating. /// And that is something we cannot do before validating the order on the API side. getRandomPreparedData() { const chainId = this.chainId; const chainType = chainIdToChainTypeMap[chainId]; const randomNumber = String(Math.floor(Math.random() * 10000000)); switch (chainType) { case 'EVM': { return { nonce: randomNumber, signature: '0x0000000000000000000000000000000000000000000000000000000000000000', }; } case 'Solana': { return { secretNumber: randomNumber, orderPubkey: 'DFNAjFAvS4GF98Tp1kiyLvEHM3wjGXibCfF86nnmhuVc', }; } case 'Sui': { throw new ValidationError('DCA orders are not supported on Sui'); } default: { throw new Error('Chain type not supported'); } } } getTotalAmountIn() { return BigInt(this.amountInPerInterval) * BigInt(this.totalIntervals); } toIntentRequest(preparedData) { const sourceChainType = chainIdToChainTypeMap[this.chainId]; return { genericData: this, chainSpecificData: { [sourceChainType]: preparedData, }, }; } sendToAuctioneer(preparedData) { return BaseSDK.sendDcaSingleChainOrder({ order: this, preparedData, }); } async toEVMTypedData() { if (!isEvmChain(this.chainId)) { throw new ValidationError('Chain id is not an Ethereum compatible chain'); } return getEVMDcaSingleChainOrderTypedData(this); } async toSolanaInstructionsByteArray() { if (this.chainId !== ChainID.Solana) { throw new ValidationError('Chain id is not Solana'); } return getSolanaDcaSingleChainOrderInstructions(this); } } //# sourceMappingURL=dca-single-chain.js.map