@robertprp/intents-sdk
Version:
Shogun Network Intent-based cross-chain swaps SDK
63 lines • 2.44 kB
JavaScript
import { ValidationError } from '../errors/index.js';
/**
* BaseValidator - Abstract class implementing common validation logic
* This follows the Template Method pattern
*/
export class BaseValidator {
/**
* Validate source chain specific parameters of the order
*/
async validateSourceChain(order) {
this.validateTokenAddress(order.sourceTokenAddress);
this.validateAmount(order.sourceTokenAmount);
this.validateAmount(order.minStablecoinAmount ?? 1n);
await this.validateCrossChainOrderFeasability({ ...order, user: order.user });
}
async validateSingleChainOrder(order) {
this.validateTokenAddress(order.tokenIn);
this.validateTokenAddress(order.tokenOut);
this.validateAmount(order.amountIn);
this.validateAmount(order.amountOutMin ?? 1n);
if (order.stopLossMaxOut) {
this.validateAmount(order.stopLossMaxOut);
}
if (order.takeProfitMinOut) {
this.validateAmount(order.takeProfitMinOut);
}
await this.validateSingleChainOrderFeasability(order);
}
/**
* Validate destination chain specific parameters of the order
*/
validateDestinationChain(order) {
this.validateAddress(order.destinationAddress);
this.validateTokenAddress(order.destinationTokenAddress);
this.validateAmount(order.destinationTokenMinAmount ?? 1n);
if (order.extraTransfers) {
for (const extraTransfer of order.extraTransfers) {
this.validateAddress(extraTransfer.receiver);
this.validateTokenAddress(extraTransfer.token);
this.validateAmount(extraTransfer.amount);
}
}
}
/**
* Helper methods that throw specific validation errors
*/
validateAddress(address) {
if (!this.isValidAddress(address)) {
throw new ValidationError(`Invalid ${this.getChainName()} address: ${address}`);
}
}
validateTokenAddress(tokenAddress) {
if (!this.isValidTokenAddress(tokenAddress)) {
throw new ValidationError(`Invalid ${this.getChainName()} token address: ${tokenAddress}`);
}
}
validateAmount(amount) {
if (!this.isValidAmount(amount)) {
throw new ValidationError(`Amount must be greater than 0. Value: ${amount}`);
}
}
}
//# sourceMappingURL=base-validator.js.map