import { Account , Transaction , TransactionSmartContractSubtype , TransactionType , getAttachmentVersion } from '@signumjs/core' ; import { UtilService } from '../../../util.service' ; import { Amount , ChainTime , convertHexStringToString } from '@signumjs/util' ; export enum CellValueType { AccountAddress = 'AccountAddress' , AccountId = 'AccountId' , AccountInfo = 'AccountInfo' , Asset = 'Asset' , AssetTransfer = 'AssetTransfer' , BlockId = 'BlockId' , Date = 'Date' , Default = 'Default' , EncryptedMessage = 'EncryptedMessage' , Message = 'Message' , MultiSameOutCreation = 'MultiSameOutCreation' , MultiOutCreation = 'MultiOutCreation' , CommitmentAdd = 'CommitmentAdd' , CommitmentRemove = 'CommitmentRemove' } export class CellValue { constructor ( public data , public type = CellValueType . Default ) { } } export class CellValueMapper { private map : object ; constructor ( private transaction , private account , private utilService ) { this . initializeMap ( ) ; } public getValue ( key ) { return this . map [ key ] || { data : this . transaction [ key ] , type : CellValueType . Default } ; } private initializeMap ( ) { this . map = { type : this . getTransactionType ( ) , subtype : this . getTransactionSubtype ( ) , attachment : this . getAttachment ( ) , feeNQT : this . getAmount ( this . transaction . feeNQT ) , amountNQT : this . getAmount ( this . transaction . amountNQT ) , senderRS : this . getTypedValue ( this . transaction . senderRS , CellValueType . AccountAddress ) , sender : this . getTypedValue ( this . transaction . sender , CellValueType . AccountId ) , recipientRS : this . getTypedValue ( this . transaction . recipientRS , CellValueType . AccountAddress ) , recipient : this . getTypedValue ( this . transaction . recipient , CellValueType . AccountId ) , block : this . getTypedValue ( this . transaction . block , CellValueType . BlockId ) , blockTimestamp : this . getTime ( this . transaction . blockTimestamp ) , ecBlockId : this . getTypedValue ( this . transaction . ecBlockId , CellValueType . BlockId ) , timestamp : this . getTime ( this . transaction . timestamp ) , } ; } private getTransactionType ( ) { return new CellValue ( this . utilService . translateTransactionType ( this . transaction ) ) ; } private getTransactionSubtype ( ) { return new CellValue ( this . utilService . translateTransactionSubtype ( this . transaction , this . account ) ) ; } private getAttachment ( ) { const { attachment } = this . transaction ; const versionIdentifier = getAttachmentVersion ( this . transaction ) ; switch ( versionIdentifier ) { case 'AccountInfo' : return new CellValue ( attachment , CellValueType . AccountInfo ) ; case 'EncryptedMessage' : return new CellValue ( attachment , CellValueType . EncryptedMessage ) ; case 'Message' : return this . getMessageAttachment ( this . transaction ) ; case 'Asset' : return new CellValue ( attachment , CellValueType . Asset ) ; case 'AssetTransfer' : return new CellValue ( attachment , CellValueType . AssetTransfer ) ; case 'MultiSameOutCreation' : return new CellValue ( attachment , CellValueType . MultiSameOutCreation ) ; case 'MultiOutCreation' : return new CellValue ( attachment , CellValueType . MultiOutCreation ) ; case 'CommitmentAdd' : return new CellValue ( attachment , CellValueType . CommitmentAdd ) ; case 'CommitmentRemove' : return new CellValue ( attachment , CellValueType . CommitmentRemove ) ; default : return this . transaction [ 'attachment' ] ; } } private getAmount ( nqt ) { const valueStr = `${ Amount . fromPlanck ( nqt ) . toString ( ) }` ; return new CellValue ( valueStr ) ; } private getTypedValue ( value , type ) { return new CellValue ( value , type ) ; } private getTime ( blockTimestamp ) { const date = ChainTime . fromChainTimestamp ( blockTimestamp ) . getDate ( ) ; return new CellValue ( date , CellValueType . Date ) ; } private getMessageAttachment ( transaction ) { const { type , subtype , attachment } = transaction ; if ( type === TransactionType . AT && subtype === TransactionSmartContractSubtype . SmartContractPayment ) { return new CellValue ( convertHexStringToString ( attachment . message . replace ( /00/g , '' ) ) ) ; } return new CellValue ( attachment . message ) ; } }