UNPKG

@dydxfoundation/governance

Version:
136 lines (135 loc) 7.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.amountGtThan0OrMinus1 = exports.amount0OrPositiveValidator = exports.amountGtThan0Validator = exports.isEthAddressOrEnsValidator = exports.isEthAddressValidator = exports.optionalValidator = void 0; /* eslint-disable prefer-rest-params */ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ const ethers_1 = require("ethers"); const parsings_1 = require("../utils/parsings"); require("reflect-metadata"); const paramValidators_1 = require("./paramValidators"); function optionalValidator(target, propertyName, methodArguments) { const optionalParameters = Reflect.getOwnMetadata(paramValidators_1.optionalMetadataKey, target, propertyName); const isParamOptional = []; if (optionalParameters) { optionalParameters.forEach((parameterIndex) => { if (methodArguments[parameterIndex] == null) { isParamOptional[parameterIndex] = true; } }); } return isParamOptional; } exports.optionalValidator = optionalValidator; function isEthAddressValidator(target, propertyName, methodArguments, isParamOptional) { const addressParameters = Reflect.getOwnMetadata(paramValidators_1.isEthAddressMetadataKey, target, propertyName); if (addressParameters) { addressParameters.forEach((storedParams) => { if (storedParams.field) { if (methodArguments[0][storedParams.field] && !ethers_1.utils.isAddress(methodArguments[0][storedParams.field])) { throw new Error(`Address: ${methodArguments[0][storedParams.field]} is not a valid ethereum Address`); } } else { const isOptional = isParamOptional && isParamOptional[storedParams.index]; if (methodArguments[storedParams.index] && !isOptional && !ethers_1.utils.isAddress(methodArguments[storedParams.index])) { throw new Error(`Address: ${methodArguments[storedParams.index]} is not a valid ethereum Address`); } } }); } } exports.isEthAddressValidator = isEthAddressValidator; function isEthAddressOrEnsValidator(target, propertyName, methodArguments, isParamOptional) { const addressParameters = Reflect.getOwnMetadata(paramValidators_1.isEthAddressOrENSMetadataKey, target, propertyName); if (addressParameters) { addressParameters.forEach((storedParams) => { if (storedParams.field) { if (methodArguments[0][storedParams.field] && !ethers_1.utils.isAddress(methodArguments[0][storedParams.field])) { if (!(0, parsings_1.canBeEnsAddress)(methodArguments[0][storedParams.field])) { throw new Error(`Address ${methodArguments[0][storedParams.field]} is not valid ENS format or a valid ethereum Address`); } } } else { const isOptional = isParamOptional && isParamOptional[storedParams.index]; if (methodArguments[storedParams.index] && !isOptional && !ethers_1.utils.isAddress(methodArguments[storedParams.index])) { if (!(0, parsings_1.canBeEnsAddress)(methodArguments[storedParams.index])) { throw new Error(`Address ${methodArguments[storedParams.index]} is not valid ENS format or a valid ethereum Address`); } } } }); } } exports.isEthAddressOrEnsValidator = isEthAddressOrEnsValidator; function amountGtThan0Validator(target, propertyName, methodArguments, isParamOptional) { const amountParameters = Reflect.getOwnMetadata(paramValidators_1.isPositiveMetadataKey, target, propertyName); if (amountParameters) { amountParameters.forEach((storedParams) => { if (storedParams.field) { if (methodArguments[0][storedParams.field] && !(Number(methodArguments[0][storedParams.field]) > 0)) { throw new Error(`Amount: ${methodArguments[0][storedParams.field]} needs to be greater than 0`); } } else { const isOptional = isParamOptional && isParamOptional[storedParams.index]; if (!isOptional && !(Number(methodArguments[storedParams.index]) > 0)) { throw new Error(`Amount: ${methodArguments[storedParams.index]} needs to be greater than 0`); } } }); } } exports.amountGtThan0Validator = amountGtThan0Validator; function amount0OrPositiveValidator(target, propertyName, methodArguments, isParamOptional) { const amountParameters = Reflect.getOwnMetadata(paramValidators_1.is0OrPositiveMetadataKey, target, propertyName); if (amountParameters) { amountParameters.forEach((storedParams) => { if (storedParams.field) { if (methodArguments[0][storedParams.field] && !(Number(methodArguments[0][storedParams.field]) >= 0)) { throw new Error(`Amount: ${methodArguments[0][storedParams.field]} needs to be greater than 0`); } } else { const isOptional = isParamOptional && isParamOptional[storedParams.index]; if (!isOptional && !(Number(methodArguments[storedParams.index]) >= 0)) { throw new Error(`Amount: ${methodArguments[storedParams.index]} needs to be greater than 0`); } } }); } } exports.amount0OrPositiveValidator = amount0OrPositiveValidator; function amountGtThan0OrMinus1(target, propertyName, methodArguments, isParamOptional) { const amountMinusOneParameters = Reflect.getOwnMetadata(paramValidators_1.isPositiveOrMinusOneMetadataKey, target, propertyName); if (amountMinusOneParameters) { amountMinusOneParameters.forEach((storedParams) => { if (storedParams.field) { if (methodArguments[0][storedParams.field] && !(Number(methodArguments[0][storedParams.field]) > 0 || methodArguments[0][storedParams.field] === '-1')) { throw new Error(`Amount: ${methodArguments[0][storedParams.field]} needs to be greater than 0 or -1`); } } else { const isOptional = isParamOptional && isParamOptional[storedParams.index]; if (!isOptional && !(Number(methodArguments[storedParams.index]) > 0 || methodArguments[storedParams.index] === '-1')) { throw new Error(`Amount: ${methodArguments[storedParams.index]} needs to be greater than 0 or -1`); } } }); } } exports.amountGtThan0OrMinus1 = amountGtThan0OrMinus1;