Esempio n. 1
0
        private async Task AggregateRequests(string clientId, string asset, OffchainTransferType type)
        {
            var list = (await _offchainRequestRepository.GetRequestsForClient(clientId)).Where(x => x.AssetId == asset && x.TransferType == type).ToList();

            var masterRequest = list.FirstOrDefault(x =>
                                                    (x.StartProcessing == null || (DateTime.UtcNow - x.StartProcessing.Value).TotalMinutes > 5) && x.ServerLock == null);

            if (masterRequest == null)
            {
                return;
            }

            await _offchainRequestRepository.DeleteRequest(masterRequest.RequestId);

            LogToFile(masterRequest.ToJson());

            var masterTransfer = await _offchainTransferRepository.GetTransfer(masterRequest.TransferId);

            int count = 0;

            while (count < 50)
            {
                list = (await _offchainRequestRepository.GetRequestsForClient(clientId))
                       .Where(x => (x.StartProcessing == null || (DateTime.UtcNow - x.StartProcessing.Value).TotalMinutes > 5) && x.ServerLock == null)
                       .Where(x => x.AssetId == asset && x.RequestId != masterRequest.RequestId && x.TransferType == type).ToList();
                if (list.Count < 1)
                {
                    break;
                }

                var current = list.FirstOrDefault();

                await _offchainRequestRepository.DeleteRequest(current.RequestId);

                LogToFile(current.ToJson());

                var currentTransfer = await _offchainTransferRepository.GetTransfer(current.TransferId);

                await _offchainTransferRepository.SetTransferIsChild(currentTransfer.Id, masterTransfer.Id);

                await _offchainTransferRepository.AddChildTransfer(masterTransfer.Id, currentTransfer);

                count++;
            }

            await _offchainRequestRepository.CreateRequest(masterTransfer.Id, masterTransfer.ClientId,
                                                           masterTransfer.AssetId, masterRequest.Type, masterTransfer.Type);
        }
        private async Task CreateAndAggregate(string transactionId, string clientId, string assetId, decimal amount,
                                              string orderId, OffchainTransferType type, DateTime?lockTime)
        {
            var newTransfer = await _offchainTransferRepository.CreateTransfer(transactionId, clientId, assetId, amount, type, null, orderId);

            var request = await _offchainRequestRepository.CreateRequestAndLock(transactionId, clientId, assetId, RequestType.RequestTransfer, type, lockTime);

            var oldTransferId = request.TransferId;

            //aggregate transfers
            if (oldTransferId != newTransfer.Id)
            {
                await _offchainTransferRepository.AddChildTransfer(oldTransferId, newTransfer);

                await _offchainTransferRepository.SetTransferIsChild(newTransfer.Id, oldTransferId);
            }
        }