@radixdlt/atom
Version:
Container for CRUD instructions known as 'Particles' that are sent to the Radix decentralized ledger
138 lines • 7.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isTokenDefinitionParticleBase = exports.baseTokenDefinitionParticle = exports.dsonEncodingMarker = exports.encodableKeyValuesPresent = exports.keyValueIfPrimitivePresent = exports.definedOrNonNull = exports.validateDescription = exports.validateTokenDefinitionName = exports.validateTokenDefinitionSymbol = exports.RADIX_TOKEN_DESCRIPTION_MAX_LENGTH = exports.RADIX_TOKEN_SYMBOL_MAX_LENGTH = exports.RADIX_TOKEN_SYMBOL_MIN_LENGTH = exports.RADIX_TOKEN_NAME_MAX_LENGTH = exports.RADIX_TOKEN_NAME_MIN_LENGTH = exports.onlyUppercasedAlphanumerics = exports.validateURLInput = void 0;
const neverthrow_1 = require("neverthrow");
const primitives_1 = require("@radixdlt/primitives");
const data_formats_1 = require("@radixdlt/data-formats");
const _index_1 = require("./meta/_index");
const resourceIdentifier_1 = require("../resourceIdentifier");
const validateURLInput = (urlInput) => {
if (urlInput === undefined)
return neverthrow_1.ok(undefined);
if (typeof urlInput === 'string') {
try {
new URL(urlInput);
return neverthrow_1.ok(urlInput);
}
catch (_a) {
return neverthrow_1.err(new Error(`Failed to create url from string: '${urlInput}'.`));
}
}
else {
return neverthrow_1.ok(urlInput.toJSON());
}
};
exports.validateURLInput = validateURLInput;
const onlyUppercasedAlphanumerics = (input) => new RegExp('^[A-Z0-9]+$').test(input);
exports.onlyUppercasedAlphanumerics = onlyUppercasedAlphanumerics;
exports.RADIX_TOKEN_NAME_MIN_LENGTH = 2;
exports.RADIX_TOKEN_NAME_MAX_LENGTH = 64;
exports.RADIX_TOKEN_SYMBOL_MIN_LENGTH = 1;
exports.RADIX_TOKEN_SYMBOL_MAX_LENGTH = 14;
exports.RADIX_TOKEN_DESCRIPTION_MAX_LENGTH = 200;
const validateTokenDefinitionSymbol = (symbol) => {
if (symbol.length < exports.RADIX_TOKEN_SYMBOL_MIN_LENGTH ||
symbol.length > exports.RADIX_TOKEN_SYMBOL_MAX_LENGTH) {
return neverthrow_1.err(new Error(`Bad length of token defintion symbol, should be between ${exports.RADIX_TOKEN_SYMBOL_MIN_LENGTH}-${exports.RADIX_TOKEN_SYMBOL_MAX_LENGTH} chars, but was ${symbol.length}.`));
}
return exports.onlyUppercasedAlphanumerics(symbol)
? neverthrow_1.ok(symbol)
: neverthrow_1.err(new Error(`Symbol contains disallowed characters, only uppercase alphanumerics are allowed.`));
};
exports.validateTokenDefinitionSymbol = validateTokenDefinitionSymbol;
const validateTokenDefinitionName = (name) => {
if (name.length < exports.RADIX_TOKEN_NAME_MIN_LENGTH ||
name.length > exports.RADIX_TOKEN_NAME_MAX_LENGTH) {
return neverthrow_1.err(new Error(`Bad length of token defintion name, should be between ${exports.RADIX_TOKEN_NAME_MIN_LENGTH}-${exports.RADIX_TOKEN_NAME_MAX_LENGTH} chars, but was ${name.length}.`));
}
return neverthrow_1.ok(name);
};
exports.validateTokenDefinitionName = validateTokenDefinitionName;
const validateDescription = (description) => {
if (description === undefined)
return neverthrow_1.ok(undefined);
return description.length <= exports.RADIX_TOKEN_DESCRIPTION_MAX_LENGTH
? neverthrow_1.ok(description)
: neverthrow_1.err(new Error(`Bad length of token description, should be less than ${exports.RADIX_TOKEN_DESCRIPTION_MAX_LENGTH}, but was ${description.length}.`));
};
exports.validateDescription = validateDescription;
const notUndefinedOrCrash = (value) => {
if (value === undefined) {
// eslint-disable-next-line functional/no-throw-statement
throw new Error('Incorrect implementation, really expected a value but got undefined. ☢️');
}
return value;
};
const definedOrNonNull = (value) => value !== null && value !== undefined;
exports.definedOrNonNull = definedOrNonNull;
const keyValueIfPrimitivePresent = (input) => {
var _a;
if (!exports.definedOrNonNull(input.value))
return undefined;
const indeed = {
[input.key]: {
value: input.value,
outputMode: (_a = input.outputMode) !== null && _a !== void 0 ? _a : data_formats_1.OutputMode.ALL,
},
};
return indeed;
};
exports.keyValueIfPrimitivePresent = keyValueIfPrimitivePresent;
const encodableKeyValuesPresent = (maybes) => Object.keys(maybes)
.filter((key) => exports.definedOrNonNull(maybes[key]))
.reduce((result, key) => {
result[key] = maybes[key];
return result;
}, {});
exports.encodableKeyValuesPresent = encodableKeyValuesPresent;
exports.dsonEncodingMarker = {
encoding: (outputMode) => {
throw new Error(`impl me using ${outputMode}`);
},
toDSON: (outputMode) => {
throw new Error(`impl me ${outputMode ? `using ${outputMode}` : ''}`);
},
};
// eslint-disable-next-line max-lines-per-function
const baseTokenDefinitionParticle = (input) => {
return neverthrow_1.combine([
exports.validateTokenDefinitionSymbol(input.symbol),
exports.validateTokenDefinitionName(input.name),
exports.validateDescription(input.description),
exports.validateURLInput(input.url).mapErr((e) => new Error(`Invalid token info url. ${e.message}`)),
exports.validateURLInput(input.iconURL).mapErr((e) => new Error(`Invalid token icon url. ${e.message}`)),
]).map((resultList) => {
var _a;
const thisBaseBase = Object.assign({ radixParticleType: input.radixParticleType, name: notUndefinedOrCrash(resultList[1]), description: resultList[2], granularity: (_a = input.granularity) !== null && _a !== void 0 ? _a : primitives_1.granularityDefault, resourceIdentifier: resourceIdentifier_1.ResourceIdentifier.fromAddressAndName({
address: input.address,
name: notUndefinedOrCrash(resultList[0]),
}), url: resultList[3], iconURL: resultList[4], equals: (other) => {
// eslint-disable-next-line functional/no-throw-statement
throw new Error(`Please override and use ${JSON.stringify(other)}`);
} }, exports.dsonEncodingMarker);
const keyValues = Object.assign(Object.assign({}, input.specificEncodableKeyValues), exports.encodableKeyValuesPresent(Object.assign(Object.assign(Object.assign({ rri: thisBaseBase.resourceIdentifier, granularity: thisBaseBase.granularity, name: thisBaseBase.name }, exports.keyValueIfPrimitivePresent({
key: 'iconUrl',
value: thisBaseBase.iconURL,
})), exports.keyValueIfPrimitivePresent({
key: 'url',
value: thisBaseBase.url,
})), exports.keyValueIfPrimitivePresent({
key: 'description',
value: thisBaseBase.description,
}))));
const thisBase = Object.assign(Object.assign(Object.assign({}, thisBaseBase), data_formats_1.JSONEncoding(input.serializer)(keyValues)), data_formats_1.DSONEncoding(input.serializer)(keyValues));
return Object.assign(Object.assign({}, thisBase), { equals: (other) => input.makeEquals(thisBase, other) });
});
};
exports.baseTokenDefinitionParticle = baseTokenDefinitionParticle;
// eslint-disable-next-line complexity
const isTokenDefinitionParticleBase = (something) => {
if (!_index_1.isRadixParticle(something))
return false;
const inspection = something;
return (inspection.resourceIdentifier !== undefined &&
inspection.granularity !== undefined &&
inspection.name !== undefined);
};
exports.isTokenDefinitionParticleBase = isTokenDefinitionParticleBase;
//# sourceMappingURL=tokenDefinitionParticleBase.js.map