import { IAssetService } from 'core/service/transaction' ; import { IAssetRegister , Transaction , TransactionModel } from 'shared/model/transaction' ; import { Account } from 'shared/model/account' ; import { ResponseEntity } from 'shared/model/response' ; import AccountRepo from 'core/repository/account' ; import ReferredUsersRepo , { ReferredUserFactor } from 'core/repository/referredUsers' ; import config from 'shared/config' ; import BUFFER from 'core/util/buffer' ; class TransactionRegisterService implements IAssetService < IAssetRegister > { create ( trs ) { return { referral : BigInt ( trs . asset . referral ) , } ; } getBytes ( trs ) { const buff = Buffer . alloc ( BUFFER . LENGTH . INT64 ) ; BUFFER . writeUInt64LE ( buff , trs . asset . referral , 0 ) ; return buff ; } validate ( trs ) { const errors = [ ] ; if ( ! trs . asset . referral ) { errors . push ( 'Missing referral' ) ; } return new ResponseEntity < void > ( { errors } ) ; } verifyUnconfirmed ( trs , sender ) { const errors = [ ] ; if ( sender . secondPublicKey || sender . actualBalance !== 0 || sender . delegate || ( sender . votes && sender . votes . length ) || ( sender . referrals && sender . referrals . length ) || ( sender . stakes && sender . stakes . length ) ) { return new ResponseEntity < void > ( { errors : [ 'Account already exists.' ] } ) ; } return new ResponseEntity < void > ( { errors } ) ; } calculateFee ( trs , sender ) { return 0 ; } applyUnconfirmed ( trs , sender ) { let referralAccount = AccountRepo . getByAddress ( trs . asset . referral ) ; if ( ! referralAccount ) { referralAccount = AccountRepo . add ( { address : trs . asset . referral } ) ; ReferredUsersRepo . add ( referralAccount ) ; } const referrals = referralAccount . referrals . slice ( 0 , config . CONSTANTS . REFERRAL . MAX_COUNT - 1 ) ; const targetAccount = AccountRepo . add ( { address : trs . senderAddress , publicKey : trs . senderPublicKey } ) ; targetAccount . referrals = [ referralAccount , ... referrals ] ; ReferredUsersRepo . add ( targetAccount ) ; ReferredUsersRepo . updateCountFactor ( trs ) ; } undoUnconfirmed ( trs , sender , senderOnly ) { ReferredUsersRepo . updateCountFactor ( trs , ReferredUserFactor . ACTION . SUBTRACT ) ; sender . referrals = [ ] ; } } export default new TransactionRegisterService ( ) ;