def accumulation_hook(self, db_session):

        # Check event requirements
        if len(self.event.attributes) == 1 and \
                self.event.attributes[0]['type'] == 'ReferendumIndex':

            referendum_audit = DemocracyReferendumAudit(
                democracy_referendum_id=self.event.attributes[0]['value'],
                block_id=self.event.block_id,
                extrinsic_idx=self.event.extrinsic_idx,
                event_idx=self.event.event_idx,
                type_id=DEMOCRACY_REFERENDUM_AUDIT_TYPE_CANCELLED)

            referendum_audit.save(db_session)
    def sequencing_hook(self, db_session, parent_block_data,
                        parent_sequenced_block_data):

        # TODO force insert on Started status
        for referendum_audit in DemocracyReferendumAudit.query(
                db_session).filter_by(
                    block_id=self.block.id).order_by('event_idx'):

            success = None
            vote_threshold = None
            proposal = None

            if referendum_audit.type_id == DEMOCRACY_REFERENDUM_AUDIT_TYPE_STARTED:
                status = 'Started'
                vote_threshold = referendum_audit.data.get('vote_threshold')
                proposal = referendum_audit.data.get('proposal')

            elif referendum_audit.type_id == DEMOCRACY_REFERENDUM_AUDIT_TYPE_PASSED:
                status = 'Passed'
            elif referendum_audit.type_id == DEMOCRACY_REFERENDUM_AUDIT_TYPE_NOTPASSED:
                status = 'NotPassed'
            elif referendum_audit.type_id == DEMOCRACY_REFERENDUM_AUDIT_TYPE_CANCELLED:
                status = 'Cancelled'
            elif referendum_audit.type_id == DEMOCRACY_REFERENDUM_AUDIT_TYPE_EXECUTED:
                status = 'Executed'
                success = referendum_audit.data.get('success')
            else:
                status = '[unknown]'

            try:
                referendum = DemocracyReferendum.query(db_session).filter_by(
                    id=referendum_audit.democracy_referendum_id).one()

                if proposal:
                    referendum.proposal = proposal

                referendum.status = status
                referendum.updated_at_block = self.block.id
                referendum.success = success

            except NoResultFound:

                referendum = DemocracyReferendum(
                    id=referendum_audit.democracy_referendum_id,
                    vote_threshold=vote_threshold,
                    created_at_block=self.block.id,
                    updated_at_block=self.block.id,
                    proposal=proposal,
                    success=success,
                    status=status)

            referendum.save(db_session)
    def accumulation_hook(self, db_session):

        # Check event requirements
        if len(self.event.attributes) == 2 and \
                self.event.attributes[0]['type'] == 'ReferendumIndex' and \
                self.event.attributes[1]['type'] == 'VoteThreshold':

            # Retrieve proposal from storage
            substrate = SubstrateInterface(SUBSTRATE_RPC_URL)
            storage_call = RuntimeStorage.query(db_session).filter_by(
                module_id='democracy',
                name='ReferendumInfoOf',
            ).order_by(RuntimeStorage.spec_version.desc()).first()

            proposal = substrate.get_storage(
                block_hash=self.block.hash,
                module='Democracy',
                function='ReferendumInfoOf',
                params=self.event.attributes[0]['valueRaw'],
                return_scale_type=storage_call.type_value,
                hasher=storage_call.type_hasher,
                metadata=self.metadata
            )

            referendum_audit = DemocracyReferendumAudit(
                democracy_referendum_id=self.event.attributes[0]['value'],
                block_id=self.event.block_id,
                extrinsic_idx=self.event.extrinsic_idx,
                event_idx=self.event.event_idx,
                type_id=DEMOCRACY_REFERENDUM_AUDIT_TYPE_STARTED,
                data={
                    'vote_threshold': self.event.attributes[1]['value'],
                    'ReferendumIndex': self.event.attributes[0]['valueRaw'],
                    'proposal': proposal
                }
            )

            referendum_audit.save(db_session)