UNPKG

@wasserstoff/tribes-sdk

Version:

SDK for integrating with Tribes by Astrix platform on any EVM compatible chain

102 lines (101 loc) 4.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateAddress = validateAddress; exports.validatePositiveNumber = validatePositiveNumber; exports.validatePositiveBigInt = validatePositiveBigInt; exports.validateNonEmptyString = validateNonEmptyString; exports.validateNonEmptyArray = validateNonEmptyArray; exports.validateTokenAmount = validateTokenAmount; exports.validateTribeId = validateTribeId; exports.isObject = isObject; const ethers_1 = require("ethers"); const core_1 = require("../types/core"); const errors_1 = require("../types/errors"); /** * Validate an Ethereum address * @param address Address to validate * @param paramName Parameter name for error messages * @throws AstrixSDKError if the address is invalid */ function validateAddress(address, paramName = 'address') { if (!address || !ethers_1.ethers.isAddress(address)) { throw new errors_1.AstrixSDKError(core_1.ErrorType.VALIDATION_ERROR, `Invalid ${paramName}: ${address}`); } if (address === ethers_1.ethers.ZeroAddress) { throw new errors_1.AstrixSDKError(core_1.ErrorType.VALIDATION_ERROR, `${paramName} cannot be zero address`); } } /** * Validate a number is positive * @param value Value to validate * @param paramName Parameter name for error messages * @throws AstrixSDKError if the value is not positive */ function validatePositiveNumber(value, paramName = 'value') { if (typeof value !== 'number' || isNaN(value) || value <= 0) { throw new errors_1.AstrixSDKError(core_1.ErrorType.VALIDATION_ERROR, `${paramName} must be a positive number`); } } /** * Validate a bigint is positive * @param value Value to validate * @param paramName Parameter name for error messages * @throws AstrixSDKError if the value is not positive */ function validatePositiveBigInt(value, paramName = 'value') { if (typeof value !== 'bigint' || value <= 0n) { throw new errors_1.AstrixSDKError(core_1.ErrorType.VALIDATION_ERROR, `${paramName} must be a positive bigint`); } } /** * Validate a string is not empty * @param value Value to validate * @param paramName Parameter name for error messages * @throws AstrixSDKError if the string is empty */ function validateNonEmptyString(value, paramName = 'value') { if (typeof value !== 'string' || value.trim() === '') { throw new errors_1.AstrixSDKError(core_1.ErrorType.VALIDATION_ERROR, `${paramName} must be a non-empty string`); } } /** * Validate an array is not empty * @param array Array to validate * @param paramName Parameter name for error messages * @throws AstrixSDKError if the array is empty */ function validateNonEmptyArray(array, paramName = 'array') { if (!Array.isArray(array) || array.length === 0) { throw new errors_1.AstrixSDKError(core_1.ErrorType.VALIDATION_ERROR, `${paramName} must be a non-empty array`); } } /** * Validate a token amount * @param amount Amount to validate * @param paramName Parameter name for error messages * @throws AstrixSDKError if the amount is invalid */ function validateTokenAmount(amount, paramName = 'amount') { validatePositiveBigInt(amount, paramName); // Check for ridiculously large values that might indicate a mistake if (amount > 2n ** 128n) { throw new errors_1.AstrixSDKError(core_1.ErrorType.VALIDATION_ERROR, `${paramName} is unreasonably large, might be a mistake`); } } /** * Validate tribe parameters * @param tribeId Tribe ID to validate * @throws AstrixSDKError if the tribe ID is invalid */ function validateTribeId(tribeId) { if (typeof tribeId !== 'number' || isNaN(tribeId) || tribeId < 1 || !Number.isInteger(tribeId)) { throw new errors_1.AstrixSDKError(core_1.ErrorType.VALIDATION_ERROR, `Invalid tribe ID: ${tribeId}. Must be a positive integer.`); } } /** * Check if a value is an object * @param obj Object to check */ function isObject(obj) { return typeof obj === 'object' && obj !== null && !Array.isArray(obj); }