UNPKG

@0xsplits/splits-sdk

Version:

SDK for the 0xSplits protocol

196 lines 12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateScaledOfferFactorOverrides = exports.validateScaledOfferFactor = exports.validateCalls = exports.validateUniV3SwapInputAssets = exports.validateOracleParams = exports.validateDiversifierRecipients = exports.validateRecoupNonWaterfallRecipient = exports.validateSplitInputs = exports.validateVestingPeriod = exports.validateRecoupTranches = exports.validateWaterfallTranches = exports.validateAddress = exports.validateDistributorFeePercent = exports.validateSplitRecipients = void 0; var viem_1 = require("viem"); var constants_1 = require("../constants"); var errors_1 = require("../errors"); var _1 = require("."); var getNumDigitsAfterDecimal = function (value) { if (Number.isInteger(value)) return 0; var decimalStr = value.toString().split('.')[1]; return decimalStr.length; }; var validateSplitRecipients = function (recipients, maxPrecisionDecimals, maxRecipients) { var seenAddresses = new Set([]); var totalPercentAllocation = 0; if (recipients.length < 2) throw new errors_1.InvalidRecipientsError('At least two recipients are required'); if (maxRecipients && recipients.length > maxRecipients) throw new errors_1.InvalidRecipientsError("Too many recipients: ".concat(recipients.length, ". Maximum allowed is ").concat(maxRecipients)); recipients.forEach(function (recipient) { if (!(0, viem_1.isAddress)(recipient.address)) throw new errors_1.InvalidRecipientsError("Invalid address: ".concat(recipient.address)); if (seenAddresses.has(recipient.address.toLowerCase())) throw new errors_1.InvalidRecipientsError("Address cannot be used for multiple recipients: ".concat(recipient.address)); if (recipient.percentAllocation <= 0 || recipient.percentAllocation >= 100) throw new errors_1.InvalidRecipientsError("Invalid percent allocation: ".concat(recipient.percentAllocation, ". Must be between 0 and 100")); if (getNumDigitsAfterDecimal(recipient.percentAllocation) > maxPrecisionDecimals) throw new errors_1.InvalidRecipientsError("Invalid precision on percent allocation: ".concat(recipient.percentAllocation, ". Maxiumum allowed precision is ").concat(maxPrecisionDecimals, " decimals")); seenAddresses.add(recipient.address.toLowerCase()); totalPercentAllocation += recipient.percentAllocation; }); // Cutoff any decimals beyond the max precision, they may get introduced due // to javascript floating point precision totalPercentAllocation = (0, _1.roundToDecimals)(totalPercentAllocation, maxPrecisionDecimals); if (totalPercentAllocation !== 100) throw new errors_1.InvalidRecipientsError("Percent allocation must add up to 100. Currently adds up to ".concat(totalPercentAllocation)); }; exports.validateSplitRecipients = validateSplitRecipients; var validateDistributorFeePercent = function (distributorFeePercent) { if (distributorFeePercent < 0 || distributorFeePercent > 10) throw new errors_1.InvalidDistributorFeePercentError("Invalid distributor fee percent: ".concat(distributorFeePercent, ". Distributor fee percent must be >= 0 and <= 10")); if (getNumDigitsAfterDecimal(distributorFeePercent) > constants_1.SPLITS_MAX_PRECISION_DECIMALS) throw new errors_1.InvalidDistributorFeePercentError("Invalid precision on distributor fee: ".concat(distributorFeePercent, ". Maxiumum allowed precision is ").concat(constants_1.SPLITS_MAX_PRECISION_DECIMALS, " decimals")); }; exports.validateDistributorFeePercent = validateDistributorFeePercent; var validateAddress = function (address) { if (!(0, viem_1.isAddress)(address)) throw new errors_1.InvalidArgumentError("Invalid address: ".concat(address)); }; exports.validateAddress = validateAddress; var validateWaterfallTranches = function (tranches) { validateNumTranches(tranches.length); tranches.forEach(function (tranche, index) { if (!(0, viem_1.isAddress)(tranche.recipient)) throw new errors_1.InvalidArgumentError("Invalid recipient address: ".concat(tranche.recipient)); validateTrancheSize(tranches.length, index, tranche.size); }); }; exports.validateWaterfallTranches = validateWaterfallTranches; var validateRecoupTranches = function (tranches) { validateNumTranches(tranches.length); tranches.forEach(function (tranche, index) { if (typeof tranche.recipient === 'string') { if (!(0, viem_1.isAddress)(tranche.recipient)) throw new errors_1.InvalidArgumentError("Invalid recipient address: ".concat(tranche.recipient)); } else { (0, exports.validateSplitInputs)({ recipients: tranche.recipient.recipients, distributorFeePercent: tranche.recipient.distributorFeePercent, controller: tranche.recipient.controller, }); } validateTrancheSize(tranches.length, index, tranche.size); }); }; exports.validateRecoupTranches = validateRecoupTranches; var validateNumTranches = function (numTranches) { if (numTranches < 2) { throw new errors_1.InvalidArgumentError('Invalid number of tranches, at least two are required'); } }; var validateTrancheSize = function (numTranches, index, size) { if (index === numTranches - 1) { if (size !== undefined) throw new errors_1.InvalidArgumentError('Residual tranche cannot have a size. Please leave as undefined.'); } else { if (!size) throw new errors_1.InvalidArgumentError('Size required for all tranches except the residual'); } }; var validateVestingPeriod = function (vestingPeriod) { if (vestingPeriod <= 0) throw new errors_1.InvalidArgumentError('Invalid vesting period, must be greater than 0'); }; exports.validateVestingPeriod = validateVestingPeriod; var validateSplitInputs = function (_a) { var recipients = _a.recipients, distributorFeePercent = _a.distributorFeePercent, _b = _a.controller, controller = _b === void 0 ? viem_1.zeroAddress : _b; (0, exports.validateAddress)(controller); (0, exports.validateSplitRecipients)(recipients, constants_1.SPLITS_MAX_PRECISION_DECIMALS); (0, exports.validateDistributorFeePercent)(distributorFeePercent); }; exports.validateSplitInputs = validateSplitInputs; var validateRecoupNonWaterfallRecipient = function (numTranches, nonWaterfallRecipientAddress, nonWaterfallRecipientTrancheIndex) { (0, exports.validateAddress)(nonWaterfallRecipientAddress); if (nonWaterfallRecipientTrancheIndex !== undefined) { if (nonWaterfallRecipientTrancheIndex < 0 || nonWaterfallRecipientTrancheIndex >= numTranches) { throw new errors_1.InvalidArgumentError("Invalid nonWaterfallRecipientTrancheIndex: ".concat(nonWaterfallRecipientTrancheIndex, ". Must be valid index between 0 and ").concat(numTranches - 1)); } if (nonWaterfallRecipientAddress !== viem_1.zeroAddress) { throw new errors_1.InvalidArgumentError('Cannot set the non-waterfall recipient twice. Either set the nonWaterfallRecipientAddress or set the nonWaterfallRecipientTrancheIndex'); } } }; exports.validateRecoupNonWaterfallRecipient = validateRecoupNonWaterfallRecipient; var validateDiversifierRecipients = function (recipients) { var totalPercentAllocation = 0; if (recipients.length < 2) throw new errors_1.InvalidArgumentError('At least two recipients are required'); recipients.map(function (recipientData) { if (recipientData.address && recipientData.swapperParams) throw new errors_1.InvalidArgumentError('Only one of address or swapperParams allowed'); if (!recipientData.address && !recipientData.swapperParams) throw new errors_1.InvalidArgumentError('One of address or swapperParams required'); if (recipientData.address) (0, exports.validateAddress)(recipientData.address); else { if (!recipientData.swapperParams) throw new Error(); (0, exports.validateAddress)(recipientData.swapperParams.beneficiary); (0, exports.validateAddress)(recipientData.swapperParams.tokenToBeneficiary); } if (recipientData.percentAllocation <= 0 || recipientData.percentAllocation >= 100) throw new errors_1.InvalidArgumentError("Invalid percent allocation: ".concat(recipientData.percentAllocation, ". Must be between 0 and 100")); if (getNumDigitsAfterDecimal(recipientData.percentAllocation) > constants_1.SPLITS_MAX_PRECISION_DECIMALS) throw new errors_1.InvalidArgumentError("Invalid precision on percent allocation: ".concat(recipientData.percentAllocation, ". Maxiumum allowed precision is ").concat(constants_1.SPLITS_MAX_PRECISION_DECIMALS, " decimals")); totalPercentAllocation += recipientData.percentAllocation; }); // Cutoff any decimals beyond the max precision, they may get introduced due // to javascript floating point precision totalPercentAllocation = (0, _1.roundToDecimals)(totalPercentAllocation, constants_1.SPLITS_MAX_PRECISION_DECIMALS); if (totalPercentAllocation !== 100) throw new errors_1.InvalidArgumentError("Percent allocation must add up to 100. Currently adds up to ".concat(totalPercentAllocation)); }; exports.validateDiversifierRecipients = validateDiversifierRecipients; var validateOracleParams = function (oracleParams) { var _a, _b; if (oracleParams.address && oracleParams.createOracleParams) throw new errors_1.InvalidArgumentError('Only one of address or createOracleParams allowed'); if (!oracleParams.address && !oracleParams.createOracleParams) throw new errors_1.InvalidArgumentError('One of address or createOracleParams required'); if (oracleParams.address) (0, exports.validateAddress)(oracleParams.address); else (0, exports.validateAddress)((_b = (_a = oracleParams.createOracleParams) === null || _a === void 0 ? void 0 : _a.factory) !== null && _b !== void 0 ? _b : ''); }; exports.validateOracleParams = validateOracleParams; var validateUniV3SwapInputAssets = function (inputAssets) { if (inputAssets.length === 0) throw new errors_1.InvalidArgumentError('At least one input asset required'); inputAssets.map(function (inputAsset) { // TODO: validate encoded path? (0, exports.validateAddress)(inputAsset.token); }); }; exports.validateUniV3SwapInputAssets = validateUniV3SwapInputAssets; var validateCalls = function (calls) { calls.map(function (call) { (0, exports.validateAddress)(call.to); }); }; exports.validateCalls = validateCalls; var validateScaledOfferFactor = function (scaledOfferFactorPercent, allowMaxPercent) { if (scaledOfferFactorPercent >= 100) if (!allowMaxPercent || scaledOfferFactorPercent > 100) throw new errors_1.InvalidArgumentError('Cannot set scaled offer factor this high, would allow any input token to get traded for 0 of the output token.'); }; exports.validateScaledOfferFactor = validateScaledOfferFactor; var validateScaledOfferFactorOverrides = function (scaledOfferFactorOverrides) { scaledOfferFactorOverrides.map(function (_a) { var baseToken = _a.baseToken, quoteToken = _a.quoteToken, scaledOfferFactorPercent = _a.scaledOfferFactorPercent; (0, exports.validateAddress)(baseToken); (0, exports.validateAddress)(quoteToken); // Allow overrides to have max scaled offer factor (means the default will be used) (0, exports.validateScaledOfferFactor)(scaledOfferFactorPercent, true); }); }; exports.validateScaledOfferFactorOverrides = validateScaledOfferFactorOverrides; //# sourceMappingURL=validation.js.map