import { includeJSON , includeDSON , RadixSerializableObject , RadixSpunParticle , RadixECSignature , RadixSerializer , RadixAddress , RadixSpin , RadixParticle , RadixParticleGroup , RadixAID , } from '..' ; import { StringifySet } from '../../common/StringifySet' ; @ RadixSerializer . registerClass ( 'radix.atom' ) export class RadixAtom extends RadixSerializableObject { public static METADATA_POW_NONCE_KEY = 'powNonce' ; @ includeJSON @ includeDSON public particleGroups : RadixParticleGroup [ ] = [ ] ; @ includeJSON public signatures : { [ id ] : RadixECSignature ; } ; @ includeDSON @ includeJSON public metaData : { [ s ] : string ; } = { } ; public getParticles ( ) { const particles = [ ] ; for ( const particleGroup of this . particleGroups ) { for ( const particle of particleGroup . particles ) { particles . push ( particle ) ; } } return particles ; } public getAddresses ( ) { const addressSet = new StringifySet < RadixAddress > ( ) ; for ( const particleGroup of this . particleGroups ) { for ( const particle of particleGroup . particles ) { if ( particle . particle . serializer === 'radix.particles.system_particle' ) continue ; if ( particle . particle . serializer === 'radix.particles.unregistered_validator' ) continue ; if ( particle . particle . serializer === 'radix.particles.registered_validator' ) continue ; if ( particle . particle . serializer === 'radix.particles.staked_tokens' ) continue ; if ( particle . particle . serializer === ':str:radix.particles.system_particle' ) continue ; if ( particle . particle . serializer === ':str:radix.particles.unregistered_validator' ) continue ; if ( particle . particle . serializer === ':str:radix.particles.registered_validator' ) continue ; if ( particle . particle . serializer === ':str:radix.particles.staked_tokens' ) continue ; if ( particle . particle . serializer === ':str:radix.particles.system_particle' ) continue ; if ( particle . particle . serializer === ':str:radix.particles.unregistered_validator' ) continue ; const addresses = particle . particle . getAddresses ( ) ; for ( const address of addresses ) { addressSet . add ( address ) ; } } } return addressSet . values ( ) ; } public setPowNonce ( nonce ) { this . metaData [ RadixAtom . METADATA_POW_NONCE_KEY ] = nonce . toString ( ) ; } public clearPowNonce ( ) { delete this . metaData [ RadixAtom . METADATA_POW_NONCE_KEY ] ; } public getSpunParticlesOfType ( ... types ) { return this . getParticles ( ) . filter ( s => { for ( const type of types ) { if ( s . particle instanceof type ) { return true ; } } } ) ; } public getParticlesOfSpin ( spin ) { return this . getParticles ( ) . filter ( spunParticle => spunParticle . spin === spin ) . map ( spunParticle => spunParticle . particle ) ; } public getParticlesOfType < T extends RadixParticle > ( type , spin ? ) { let particles = this . getParticles ( ) . filter ( spunParticle => spunParticle . particle instanceof type ) ; if ( spin ) { particles = particles . filter ( spunParticle => spunParticle . spin === spin ) ; } return particles . map ( spunParticle => spunParticle . particle ) as T [ ] ; } public getFirstParticleOfType < T extends RadixParticle > ( type ) { return this . getParticles ( ) . find ( spunParticle => spunParticle . particle instanceof type ) . particle as T ; } public containsParticle ( ... types ) { for ( const spunParticle of this . getParticles ( ) ) { for ( const type of types ) { if ( spunParticle . particle instanceof type ) { return true ; } } } return false ; } public getParticleGroups ( ) { return this . particleGroups ; } public getAid ( ) { return RadixAID . from ( this . getHash ( ) ) ; } public getAidString ( ) { return this . getAid ( ) . toString ( ) ; } }