예제 #1
0
        public async Task <IAccount> UpdateBalanceAsync(string operationId, string accountId,
                                                        decimal amountDelta, bool changeLimit)
        {
            AccountEntity account = null;

            var clientId = (await _tableStorage.GetDataAsync(x => x.RowKey == AccountEntity.GenerateRowKey(accountId)))
                           .Single().ClientId;


            await _tableStorage.InsertOrModifyAsync(AccountEntity.GeneratePartitionKey(clientId),
                                                    AccountEntity.GenerateRowKey(accountId),
                                                    () => throw new InvalidOperationException($"Account {accountId} not exists"),
                                                    a =>
            {
                account = a;
                if (!TryUpdateOperationsList(operationId, a))
                {
                    return(false);
                }

                a.Balance += amountDelta;

                if (changeLimit)
                {
                    a.WithdrawTransferLimit += amountDelta;
                }

                return(true);
            });

            return(account);
        }
예제 #2
0
        public Task AddAsync(ICandle candle)
        {
            return(_storage.InsertOrModifyAsync(
                       CandleEntity.GetPartitionKey(candle.AssetPairId, candle.CandleTimestamp),
                       CandleEntity.GetRowKey(candle.CandleTimestamp),
                       () => new CandleEntity(candle),
                       c =>
            {
                if (c.Low > candle.Low)
                {
                    c.Low = candle.Low;
                }

                if (c.High < candle.High)
                {
                    c.High = candle.High;
                }

                if (c.CloseTimestamp < candle.CloseTimestamp)
                {
                    c.Close = candle.Close;
                    c.CloseTimestamp = candle.CloseTimestamp;
                }

                if (c.OpenTimestamp > candle.OpenTimestamp)
                {
                    c.Open = candle.Open;
                    c.OpenTimestamp = candle.OpenTimestamp;
                }

                return true;
            }));
        }
예제 #3
0
        /// <inheritdoc />
        public async Task UpdateAsync(BlockchainSetting settings)
        {
            BlockchainSettingEntity entity = BlockchainSettingEntity.FromDomain(settings);

            string partitionKey = BlockchainSettingEntity.GetPartitionKey(settings.Type);
            string rowKey       = BlockchainSettingEntity.GetRowKey(settings.Type);
            string errorMessage = null;

            bool isUpdated = await _table.InsertOrModifyAsync(partitionKey, rowKey, () => entity, model =>
            {
                if (model.ETag != entity.ETag)
                {
                    errorMessage = $"Entity with type {model.Type} has eTag == {model.ETag}, eTag in update request is {entity.ETag}";

                    return(false);
                }

                model.Type             = entity.Type;
                model.ApiUrl           = entity.ApiUrl;
                model.HotWalletAddress = entity.HotWalletAddress;
                model.SignServiceUrl   = entity.SignServiceUrl;
                model.ETag             = entity.ETag;
                model.PartitionKey     = entity.PartitionKey;
                model.RowKey           = entity.RowKey;

                return(true);
            });

            if (!isUpdated)
            {
                throw new AlreadyUpdatedException(errorMessage);
            }
        }
예제 #4
0
        public async Task <bool> InsertOrModifyAsync(string partitionKey, string rowKey, Func <T> create, Func <T, bool> modify)
        {
            if (await _cache.InsertOrModifyAsync(partitionKey, rowKey, create, modify))
            {
                await _table.InsertOrModifyAsync(partitionKey, rowKey, create, modify);

                return(true);
            }

            return(false);
        }
예제 #5
0
        public async Task <bool> TryLockAsync(IBcnWalletUsage usage)
        {
            string partitionKey = BcnWalletUsageEntity.ByWalletAddress.GeneratePartitionKey(usage.WalletAddress);

            string rowKey = BcnWalletUsageEntity.ByWalletAddress.GenerateRowKey(usage.Blockchain);

            return(await _tableStorage.InsertOrModifyAsync(partitionKey, rowKey,
                                                           () => BcnWalletUsageEntity.ByWalletAddress.Create(usage),
                                                           existing =>
            {
                if (!string.IsNullOrEmpty(existing.OccupiedBy))
                {
                    return false;
                }

                existing.OccupiedBy = usage.OccupiedBy;
                existing.Since = usage.Since;

                return true;
            }));
        }
예제 #6
0
        public async Task <int> GenerateNewIdAsync()
        {
            var result = 0;
            await
            _tableStorage.InsertOrModifyAsync(
                IdentityEntity.GeneratePartitionKey,
                IdentityEntity.GenerateRowKey,
                IdentityEntity.Create, indEnt =>
            {
                result = ++indEnt.Value;
                return(true);
            }
                );

            return(result);
        }
예제 #7
0
        public async Task <int> GenerateNewIdAsync()
        {
            var entity =
                await
                _tableStorage.InsertOrModifyAsync(IdentityEntity.GeneratePartitionKey, IdentityEntity.GenerateRowKey,
                                                  IdentityEntity.Create,
                                                  itm =>
            {
                itm.Value++;
                return(itm);
            }
                                                  );


            return(entity.Value);
        }
 public async Task SaveRangeAsync(IEnumerable <SwiftTransferLimitation> limitations)
 {
     foreach (var limitation in limitations)
     {
         try
         {
             await _tableStorage.InsertOrModifyAsync(
                 GetPartitionKey(),
                 GetRowKey(limitation),
                 () => SwiftTransferLimitationEntity.Create(GetPartitionKey(), limitation),
                 existing => existing.UpdateFrom(limitation)
                 );
         }
         catch (Exception ex)
         {
             _log.Error(ex);
         }
     }
 }
        public async Task SetBalanceAsync(DepositWalletKey key, decimal balance, long balanceBlock)
        {
            var partitionKey = EnrolledBalanceEntity.GetPartitionKey(key);
            var rowKey       = EnrolledBalanceEntity.GetRowKey(key);

            EnrolledBalanceEntity CreateEntity()
            {
                return(new EnrolledBalanceEntity
                {
                    PartitionKey = partitionKey,
                    RowKey = rowKey,
                    BlockchainType = key.BlockchainType,
                    BlockchainAssetId = key.BlockchainAssetId,
                    DepositWalletAddress = key.DepositWalletAddress,
                    Balance = balance,
                    Block = balanceBlock
                });
            }

            // ReSharper disable once ImplicitlyCapturedClosure
            bool UpdateEntity(EnrolledBalanceEntity entity)
            {
                if (balanceBlock >= entity.Block)
                {
                    entity.Balance = balance;
                    entity.Block   = balanceBlock;

                    return(true);
                }

                return(false);
            }

            await _storage.InsertOrModifyAsync
            (
                partitionKey,
                rowKey,
                CreateEntity,
                UpdateEntity
            );
        }
        /// <inheritdoc />
        public async Task UpdateAsync(BlockchainExplorer explorer)
        {
            BlockchainExplorerEntity entity = BlockchainExplorerEntity.FromDomain(explorer);

            string partitionKey = BlockchainExplorerEntity.GetPartitionKey(explorer.BlockchainType);
            string rowKey = BlockchainExplorerEntity.GetRowKey(explorer.RecordId);
            string errorMessage = null;
            bool isUpdated = false;

            try
            {
                isUpdated = await _table.InsertOrModifyAsync(partitionKey, rowKey, () => entity, model =>
                {
                    if (model.ETag != entity.ETag)
                    {
                        errorMessage = $"Entity with type {model.BlockchainType} has eTag == {model.ETag}, eTag in update request is {entity.ETag}";

                        return false;
                    }

                    model.BlockchainType = entity.BlockchainType;
                    model.ExplorerUrlTemplate = entity.ExplorerUrlTemplate;
                    model.RecordId = entity.RecordId;
                    model.ETag = entity.ETag;
                    model.PartitionKey = entity.PartitionKey;
                    model.RowKey = entity.RowKey;
                    model.Name = entity.Name;

                    return true;
                });
            }
            catch (Exception e)
            {
                throw new DoesNotExistException(e.Message);
            }

            if (!isUpdated)
                throw new AlreadyUpdatedException(errorMessage);
        }
        public Task UpdateBalanceAsync(string traderId, string assetId, double balance)
        {
            var partitionKey = WalletEntity.GeneratePartitionKey();
            var rowKey       = WalletEntity.GenerateRowKey(assetId);

            return(_tableStorage.InsertOrModifyAsync(partitionKey, rowKey,

                                                     () =>
            {
                var newEntity = WalletEntity.Create(traderId);
                newEntity.UpdateBalance(assetId, balance);
                return newEntity;
            },

                                                     entity =>
            {
                entity.UpdateBalance(assetId, balance);
                return entity;
            }

                                                     ));
        }
예제 #12
0
        public async Task <long> GenerateIdAsync(string entityType)
        {
            long id     = 0;
            var  result =
                await
                _tableStorage.InsertOrModifyAsync(IdentityEntity.GeneratePartitionKey(entityType),
                                                  IdentityEntity.GenerateRowKey,
                                                  () => IdentityEntity.Create(entityType),
                                                  itm =>
            {
                itm.Value++;
                id = itm.Value;
                return(true);
            }
                                                  );


            if (!result)
            {
                throw new InvalidOperationException("Error generating ID");
            }

            return(id);
        }
 public Task <bool> InsertOrModifyAsync(string partitionKey, string rowKey, Func <T> create, Func <T, bool> modify)
 {
     return(_storage.InsertOrModifyAsync(partitionKey, rowKey, () => Encrypt(create()), Map(modify)));
 }
 public Task <bool> InsertOrModifyAsync(string partitionKey, string rowKey, Func <TEntity> create, Func <TEntity, bool> modify)
 => WrapAsync(() => _impl.InsertOrModifyAsync(partitionKey, rowKey, create, modify), nameof(InsertOrModifyAsync), new { partitionKey, rowKey });
예제 #15
0
 public Task <bool> InsertOrModifyAsync(string partitionKey, string rowKey, Func <TEntity> create, Func <TEntity, bool> modify)
 {
     return(_retryService.RetryAsync(() => _impl.InsertOrModifyAsync(partitionKey, rowKey, create, modify), _onModificationsRetryCount));
 }