예제 #1
0
        public async Task <IOffchainRequest> CreateRequestAndLock(string transferId, string clientId, string assetId,
                                                                  RequestType type, OffchainTransferType transferType, DateTime?lockDate)
        {
            var existingRequest = (await GetRequestsForClient(clientId)).FirstOrDefault(x => x.AssetId == assetId &&
                                                                                        x.TransferType == transferType &&
                                                                                        x.StartProcessing == null);

            if (existingRequest == null)
            {
                return(await CreateRequest(transferId, clientId, assetId, type, transferType, lockDate));
            }

            var replaced = await _table.MergeAsync(OffchainRequestEntity.ByRecord.Partition, existingRequest.RequestId, entity =>
            {
                if (entity.StartProcessing != null)
                {
                    return(null);
                }

                entity.ServerLock = lockDate;
                return(entity);
            });

            if (replaced == null)
            {
                return(await CreateRequest(transferId, clientId, assetId, type, transferType, lockDate));
            }

            return(replaced);
        }
예제 #2
0
        private async Task AddRequest(string client, string asset, decimal amount,
                                      OffchainTransferType type = OffchainTransferType.FromHub, string order = null)
        {
            var guid = Guid.NewGuid().ToString();

            var transfer = await _offchainTransferRepository.CreateTransfer(guid, client, asset, amount, type, null, order);

            await _offchainRequestRepository.CreateRequest(transfer.Id, client, asset, RequestType.RequestTransfer, type);
        }
        public async Task CreateOffchainRequestAndNotify(
            string transactionId,
            string clientId,
            string assetId,
            decimal amount,
            string orderId,
            OffchainTransferType type)
        {
            await _offchainTransferRepository.CreateTransfer(transactionId, clientId, assetId, amount, type, null, orderId);

            await _offchainRequestRepository.CreateRequest(transactionId, clientId, assetId, RequestType.RequestTransfer, type, null);
        }
예제 #4
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);
            }
        }
        public async Task <IEnumerable <IOffchainTransfer> > GetTransfersByDate(OffchainTransferType type, DateTimeOffset @from, DateTimeOffset to)
        {
            var filter1 = TableQuery.CombineFilters(
                TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal,
                                                   OffchainTransferEntity.ByCommon.GeneratePartitionKey()),
                TableOperators.And,
                TableQuery.GenerateFilterConditionForInt("Type", QueryComparisons.Equal, (int)type)
                );

            var filter2 = TableQuery.CombineFilters(
                TableQuery.GenerateFilterConditionForDate("CreatedDt", QueryComparisons.GreaterThanOrEqual, from),
                TableOperators.And,
                TableQuery.GenerateFilterConditionForDate("CreatedDt", QueryComparisons.LessThanOrEqual, to)
                );

            var query = new TableQuery <OffchainTransferEntity>().Where(
                TableQuery.CombineFilters(filter1, TableOperators.And, filter2));

            return(await _storage.WhereAsync(query));
        }
 public static OffchainRequestEntity Create(string id, string transferId, string clientId, string assetId, RequestType type, OffchainTransferType transferType)
 {
     return(new OffchainRequestEntity
     {
         RowKey = id,
         PartitionKey = GeneratePartition(clientId),
         TransferId = transferId,
         ClientId = clientId,
         AssetId = assetId,
         Type = type,
         CreateDt = DateTime.UtcNow,
         TransferType = transferType
     });
 }
        public async Task <IOffchainRequest> CreateRequest(string transferId, string clientId, string assetId, RequestType type, OffchainTransferType transferType)
        {
            var id = Guid.NewGuid().ToString();

            var byClient = OffchainRequestEntity.ByClient.Create(id, transferId, clientId, assetId, type, transferType);
            await _table.InsertAsync(byClient);

            var byRecord = OffchainRequestEntity.ByRecord.Create(id, transferId, clientId, assetId, type, transferType);
            await _table.InsertAsync(byRecord);

            return(byRecord);
        }
 public static OffchainTransferEntity Create(string id, string clientId, string assetId, decimal amount, OffchainTransferType type, string externalTransferId,
                                             bool channelClosing = false, bool onchain = false)
 {
     return(new OffchainTransferEntity
     {
         PartitionKey = GeneratePartitionKey(),
         RowKey = id,
         AssetId = assetId,
         Amount = amount,
         ClientId = clientId,
         CreatedDt = DateTime.UtcNow,
         ExternalTransferId = externalTransferId,
         Type = type,
         ChannelClosing = channelClosing,
         Onchain = onchain
     });
 }
예제 #10
0
 public static OffchainRequestEntity CreateNew(string transferId, string clientId, string assetId, RequestType type, OffchainTransferType transferType, DateTime?serverLock = null)
 {
     return(new OffchainRequestEntity
     {
         TransferId = transferId,
         ClientId = clientId,
         AssetId = assetId,
         Type = type,
         CreateDt = DateTime.UtcNow,
         TransferType = transferType,
         ServerLock = serverLock
     });
 }
예제 #11
0
            public static OffchainRequestEntity Create(string id, string transferId, string clientId, string assetId, RequestType type, OffchainTransferType transferType, DateTime?serverLock)
            {
                var item = CreateNew(transferId, clientId, assetId, type, transferType, serverLock);

                item.PartitionKey = GeneratePartition(clientId);
                item.RowKey       = id;

                return(item);
            }
        public async Task <IOffchainTransfer> CreateTransfer(string transactionId, string clientId, string assetId, decimal amount, OffchainTransferType type, string externalTransferId, string orderId, bool channelClosing = false)
        {
            var entity = OffchainTransferEntity.ByCommon.Create(transactionId, clientId, assetId, amount, type, externalTransferId, orderId, channelClosing);

            await _storage.InsertAsync(entity);

            return(entity);
        }
 public async Task CreateOffchainRequestAndUnlock(string transactionId, string clientId, string assetId, decimal amount,
                                                  string orderId, OffchainTransferType type)
 {
     await CreateAndAggregate(transactionId, clientId, assetId, amount, orderId, type, null);
     await NotifyUser(clientId);
 }
 public Task CreateOffchainRequestAndLock(string transactionId, string clientId, string assetId, decimal amount, string orderId,
                                          OffchainTransferType type)
 {
     return(CreateAndAggregate(transactionId, clientId, assetId, amount, orderId, type, DateTime.UtcNow));
 }