Esempio n. 1
0
        public async Task <CommandHandlingResult> Handle(RevokeActiveBatchIdCommand command, IEventPublisher publisher)
        {
            var batch = await _cashoutsBatchRepository.GetAsync(command.BatchId);

            var transitionResult = await batch.RevokeIdAsync(_activeCashoutsBatchIdRepository);

            _chaosKitty.Meow(command.BatchId);

            if (transitionResult.ShouldSaveAggregate())
            {
                await _cashoutsBatchRepository.SaveAsync(batch);

                _chaosKitty.Meow(command.BatchId);
            }

            if (transitionResult.ShouldPublishEvents())
            {
                publisher.PublishEvent
                (
                    new ActiveBatchIdRevokedEvent
                {
                    BatchId = batch.BatchId
                }
                );

                _chaosKitty.Meow(command.BatchId);
            }

            return(CommandHandlingResult.Ok());
        }
Esempio n. 2
0
        public async Task <CommandHandlingResult> Handle(WaitForBatchExpirationCommand command, IEventPublisher publisher)
        {
            var batch = await _cashoutsBatchRepository.GetAsync(command.BatchId);

            if (!batch.HaveToBeExpired && batch.IsStillFillingUp)
            {
                return(CommandHandlingResult.Fail(_batchExpirationMonitoringPeriod));
            }

            var transitionResult = batch.Expire();

            if (transitionResult.ShouldSaveAggregate())
            {
                await _cashoutsBatchRepository.SaveAsync(batch);

                _chaosKitty.Meow(command.BatchId);
            }

            if (transitionResult.ShouldPublishEvents())
            {
                publisher.PublishEvent
                (
                    new BatchExpiredEvent
                {
                    BatchId = batch.BatchId
                }
                );

                _chaosKitty.Meow(command.BatchId);
            }

            return(CommandHandlingResult.Ok());
        }
Esempio n. 3
0
        public async Task <CommandHandlingResult> Handle(CloseBatchCommand command, IEventPublisher publisher)
        {
            var batch = await _cashoutsBatchRepository.GetAsync(command.BatchId);

            var transitionResult = await batch.CloseAsync(command.Reason, _closedBatchedCashoutRepository);

            _chaosKitty.Meow(command.BatchId);

            if (transitionResult.ShouldSaveAggregate())
            {
                await _cashoutsBatchRepository.SaveAsync(batch);

                _chaosKitty.Meow(command.BatchId);
            }

            if (transitionResult.ShouldPublishEvents())
            {
                publisher.PublishEvent
                (
                    new BatchClosedEvent
                {
                    BatchId = batch.BatchId
                }
                );

                _chaosKitty.Meow(command.BatchId);
            }

            return(CommandHandlingResult.Ok());
        }
Esempio n. 4
0
        public async Task <CommandHandlingResult> Handle(CompleteBatchCommand command, IEventPublisher publisher)
        {
            var batch = await _cashoutsBatchRepository.GetAsync(command.BatchId);

            var transitionResult = batch.Complete();

            if (transitionResult.ShouldSaveAggregate())
            {
                await _cashoutsBatchRepository.SaveAsync(batch);

                _chaosKitty.Meow(batch.BatchId);
            }

            if (transitionResult.ShouldPublishEvents())
            {
                if (!batch.FinishMoment.HasValue)
                {
                    throw new InvalidOperationException("Finish moment should be not null here");
                }

                publisher.PublishEvent
                (
                    new CashoutsBatchCompletedEvent
                {
                    BatchId         = batch.BatchId,
                    AssetId         = batch.AssetId,
                    TransactionHash = command.TransactionHash,
                    TransactionFee  = command.TransactionFee,
                    Cashouts        = batch.Cashouts
                                      .Select(c => c.ToContract())
                                      .ToArray(),
                    StartMoment  = batch.StartMoment,
                    FinishMoment = batch.FinishMoment.Value
                }
                );

                _chaosKitty.Meow(batch.BatchId);
            }

            return(CommandHandlingResult.Ok());
        }
Esempio n. 5
0
        private async Task <CommandHandlingResult> StartBatchedCashoutAsync(
            AcceptCashoutCommand command,
            IEventPublisher publisher,
            CashoutsAggregationConfiguration aggregationConfiguration)
        {
            if (await _closedBatchedCashoutRepository.IsCashoutClosedAsync(command.OperationId))
            {
                return(CommandHandlingResult.Ok());
            }

            var activeCashoutBatchId = await _activeCashoutsBatchIdRepository.GetActiveOrNextBatchId
                                       (
                command.BlockchainType,
                command.BlockchainAssetId,
                command.HotWalletAddress,
                CashoutsBatchAggregate.GetNextId
                                       );

            _chaosKitty.Meow(command.OperationId);

            var batch = await _cashoutsBatchRepository.GetOrAddAsync
                        (
                activeCashoutBatchId.BatchId,
                () => CashoutsBatchAggregate.Start
                (
                    activeCashoutBatchId.BatchId,
                    command.BlockchainType,
                    command.AssetId,
                    command.BlockchainAssetId,
                    command.HotWalletAddress,
                    aggregationConfiguration.CountThreshold,
                    aggregationConfiguration.AgeThreshold
                )
                        );

            _chaosKitty.Meow(command.OperationId);

            var cashout = batch.Cashouts.SingleOrDefault(p => p.CashoutId == command.OperationId) ??
                          new BatchedCashoutValueType(command.OperationId, command.ClientId, command.ToAddress, command.Amount, batch.Cashouts.Count, DateTime.UtcNow);

            var isCashoutShouldWaitForNextBatch = !(batch.IsStillFillingUp || batch.Cashouts.Contains(cashout));

            if (isCashoutShouldWaitForNextBatch)
            {
                return(CommandHandlingResult.Fail(_cqrsSettings.RetryDelay));
            }

            var transitionResult = batch.AddCashout(cashout);

            if (transitionResult.ShouldSaveAggregate())
            {
                await _cashoutsBatchRepository.SaveAsync(batch);

                _chaosKitty.Meow(command.OperationId);
            }

            if (transitionResult.ShouldPublishEvents())
            {
                if (batch.State == CashoutsBatchState.FillingUp && cashout.IndexInBatch == 0)
                {
                    publisher.PublishEvent
                    (
                        new BatchFillingStartedEvent
                    {
                        BatchId = batch.BatchId
                    }
                    );
                }
                else if (batch.State == CashoutsBatchState.Filled)
                {
                    publisher.PublishEvent
                    (
                        new BatchFilledEvent
                    {
                        BatchId = batch.BatchId
                    }
                    );
                }

                _chaosKitty.Meow(command.OperationId);

                publisher.PublishEvent
                (
                    new BatchedCashoutStartedEvent
                {
                    BatchId           = batch.BatchId,
                    OperationId       = command.OperationId,
                    BlockchainType    = command.BlockchainType,
                    BlockchainAssetId = command.BlockchainAssetId,
                    AssetId           = command.AssetId,
                    HotWalletAddress  = command.HotWalletAddress,
                    ToAddress         = command.ToAddress,
                    Amount            = command.Amount,
                    ClientId          = command.ClientId
                }
                );

                _chaosKitty.Meow(command.OperationId);
            }

            return(CommandHandlingResult.Ok());
        }