@radixdlt/atom
Version:
Container for CRUD instructions known as 'Particles' that are sent to the Radix decentralized ledger
100 lines • 4.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.spunParticlesQueryable = void 0;
const _types_1 = require("./_types");
const radixParticleTypes_1 = require("./meta/radixParticleTypes");
const spunParticle_1 = require("./spunParticle");
const spunParticlesOfTypeWithSpin = (query) => {
const spinFilter = (sp) => {
if (!query.spin)
return true;
return sp.spin === query.spin;
};
return query.unique
.filter(spinFilter)
.filter((sp) => {
if (!radixParticleTypes_1.isRadixParticle(sp.particle)) {
return false;
}
const radixParticle = sp.particle;
return radixParticle.radixParticleType === query.particleType;
})
.map((sp) => {
return spunParticle_1.spunParticle({
spin: sp.spin,
particle: sp.particle,
});
});
};
const tokenDefinitionParticleMatchingIdentifier = (query) => {
const firstTokenDefinitionParticleOfType = (particleType) => {
const tokenParticles = spunParticlesOfTypeWithSpin({
unique: query.unique,
particleType: particleType,
});
const tokenParticlesMatchingRRI = tokenParticles
.map((sp) => sp.particle)
.filter((p) => p.resourceIdentifier.equals(query.resourceIdentifier));
return tokenParticlesMatchingRRI.length >= 1
? tokenParticlesMatchingRRI[0]
: undefined;
};
const maybeMutableSupplyTokDefPart = firstTokenDefinitionParticleOfType(radixParticleTypes_1.RadixParticleType.MUTABLE_SUPPLY_TOKEN_DEFINITION);
if (maybeMutableSupplyTokDefPart) {
return maybeMutableSupplyTokDefPart;
}
const maybeFixedSupplyTokDefPart = firstTokenDefinitionParticleOfType(radixParticleTypes_1.RadixParticleType.FIXED_SUPPLY_TOKEN_DEFINITION);
if (maybeFixedSupplyTokDefPart) {
return maybeFixedSupplyTokDefPart;
}
return undefined;
};
const anySpunParticlesOfTypeWithSpinFrom = (query) => {
if (!query.spin && !query.particleTypes)
return query.unique.map(spunParticle_1.anySpunParticle);
const spinFilter = (sp) => {
if (!query.spin)
return true;
return sp.spin === query.spin;
};
return query.unique
.filter(spinFilter)
.filter((sp) => {
if (!radixParticleTypes_1.isRadixParticle(sp.particle)) {
return false;
}
const radixParticle = sp.particle;
if (!query.particleTypes)
return true;
/* eslint-disable max-params */
return query.particleTypes.reduce((acc, targetType) => acc ||
radixParticle.radixParticleType.valueOf() ===
targetType.valueOf(), false);
/* eslint-enable max-params */
})
.map(spunParticle_1.anySpunParticle);
};
const spunParticlesQueryable = (anySpunParticles) => {
const unique = Array.from(new Set(anySpunParticles));
const anySpunParticlesOfTypeWithSpin = (query) => anySpunParticlesOfTypeWithSpinFrom(Object.assign({ unique }, query));
return {
anySpunParticlesOfTypeWithSpin: anySpunParticlesOfTypeWithSpin,
upParticles: () => anySpunParticlesOfTypeWithSpin({ spin: _types_1.Spin.UP }).map((sp) => spunParticle_1.anyUpParticle(sp.particle)),
transferrableTokensParticles: (spin) => spunParticlesOfTypeWithSpin({
spin,
unique,
particleType: radixParticleTypes_1.RadixParticleType.TRANSFERRABLE_TOKENS,
}),
unallocatedTokensParticles: (spin) => spunParticlesOfTypeWithSpin({
spin,
unique,
particleType: radixParticleTypes_1.RadixParticleType.UNALLOCATED_TOKENS,
}),
tokenDefinitionParticleMatchingIdentifier: (resourceIdentifier) => tokenDefinitionParticleMatchingIdentifier({
resourceIdentifier,
unique,
}),
};
};
exports.spunParticlesQueryable = spunParticlesQueryable;
//# sourceMappingURL=spunParticleQueryable.js.map