Beispiel #1
0
        public async Task ResetBalanceAsync(DepositWalletKey key, long transactionBlock)
        {
            using (var context = new WalletManagerContext(_dbContextOptionsBuilder.Options))
            {
                var existing = await context.EnrolledBalances.FindAsync(key.BlockchainId, key.BlockchainAssetId,
                                                                        key.WalletAddress.ToLower(CultureInfo.InvariantCulture));

                if (existing != null)
                {
                    context.EnrolledBalances.Update(existing);
                }
                else
                {
                    var newEntity = new EnrolledBalanceEntity()
                    {
                        BlockchianId          = key.BlockchainId,
                        BlockchainAssetId     = key.BlockchainAssetId,
                        WalletAddress         = key.WalletAddress.ToLower(CultureInfo.InvariantCulture),
                        Balance               = "0",
                        BlockNumber           = transactionBlock,
                        OriginalWalletAddress = key.WalletAddress
                    };

                    context.EnrolledBalances.Add(newEntity);
                }

                await context.SaveChangesAsync();
            }
        }
        public async Task <IActionResult> DeleteWalletAsync(RegisterWalletRequest request)
        {
            var key = new DepositWalletKey(
                request.BlockchainId,
                request.BlockchainAssetId,
                request.WalletAddress);
            await _walletService.DeleteWalletAsync(key);

            return(Ok());
        }
        public override async Task <EmptyResponse> DeleteWallet(DeleteWalletRequest request, ServerCallContext context)
        {
            var key = new DepositWalletKey(
                request.WalletKey.BlockchainAssetId,
                request.WalletKey.BlockchainId,
                request.WalletKey.WalletAddress);
            await _walletService.DeleteWalletAsync(key);

            return(new EmptyResponse());
        }
        public async Task <EnrolledBalance> TryGetAsync(DepositWalletKey key)
        {
            var partitionKey = EnrolledBalanceEntity.GetPartitionKey(key);
            var rowKey       = EnrolledBalanceEntity.GetRowKey(key);

            var entity = await _storage.GetDataAsync(partitionKey, rowKey);

            return(entity != null
                ? EnrolledBalance.Create(key, entity.Balance, entity.Block)
                : null);
        }
Beispiel #5
0
        public async Task <EnrolledBalance> TryGetAsync(DepositWalletKey key)
        {
            using (var context = new WalletManagerContext(_dbContextOptionsBuilder.Options))
            {
                var result = await context.EnrolledBalances.FindAsync(
                    key.BlockchainId, key.BlockchainAssetId, key.WalletAddress.ToLower(CultureInfo.InvariantCulture));

                var mapped = MapFromEntity(result);

                return(mapped);
            }
        }
        public async Task <EnrolledBalance> TryGetAsync(DepositWalletKey key)
        {
            if (!_memCache.TryGetValue(key, out EnrolledBalance value))
            {
                value = await _enrolledBalanceRepository.TryGetAsync(key);

                using (var entry = _memCache.CreateEntry(key))
                {
                    entry.Value = EnrolledBalance.Create(key, value?.Balance ?? 0, value?.Block ?? 0);
                }
            }

            return(value);
        }
        public async Task <IActionResult> GetBalanceAsync([FromQuery] CheckBalanceRequest request)
        {
            var key     = new DepositWalletKey(request.BlockchainAssetId, request.BlockchainId, request.WalletAddress);
            var balance = await _balanceRepository.TryGetAsync(key);

            return(Ok(new CheckBalanceResponse()
            {
                BlockchainAssetId = key.BlockchainAssetId,
                BlockchainId = key.BlockchainId,
                WalletAddress = key.WalletAddress,
                Block = balance?.Block ?? 0,
                Balance = balance?.Balance.ToString() ?? "0"
            }));
        }
Beispiel #8
0
        public async Task DeleteBalanceAsync(DepositWalletKey key)
        {
            using (var context = new WalletManagerContext(_dbContextOptionsBuilder.Options))
            {
                var result = await context.EnrolledBalances.FindAsync(
                    key.BlockchainId, key.BlockchainAssetId, key.WalletAddress.ToLower(CultureInfo.InvariantCulture));

                if (result != null)
                {
                    context.EnrolledBalances.Remove(result);
                }

                await context.SaveChangesAsync();
            }
        }
        public async Task ResetBalanceAsync(DepositWalletKey key, long transactionBlock)
        {
            await _enrolledBalanceRepository.ResetBalanceAsync(key, transactionBlock);

            if (_memCache.TryGetValue(key, out EnrolledBalance value))
            {
                value.Balance = 0;
                value.Block   = transactionBlock;
            }
            else
            {
                using (var entry = _memCache.CreateEntry(key))
                {
                    entry.Value = EnrolledBalance.Create(key, 0, value.Block);
                }
            }
        }
        public async Task SetBalanceAsync(DepositWalletKey key, BigInteger balance, long balanceBlock)
        {
            await _enrolledBalanceRepository.SetBalanceAsync(key, balance, balanceBlock);

            if (_memCache.TryGetValue(key, out EnrolledBalance value))
            {
                value.Block   = balanceBlock;
                value.Balance = balance;
            }
            else
            {
                using (var entry = _memCache.CreateEntry(key))
                {
                    entry.Value = EnrolledBalance.Create(key, balance, balanceBlock);
                }
            }
        }
        public async Task ResetBalanceAsync(DepositWalletKey key, long transactionBlock)
        {
            var entity = new EnrolledBalanceEntity
            {
                PartitionKey         = EnrolledBalanceEntity.GetPartitionKey(key),
                RowKey               = EnrolledBalanceEntity.GetRowKey(key),
                BlockchainType       = key.BlockchainType,
                BlockchainAssetId    = key.BlockchainAssetId,
                DepositWalletAddress = key.DepositWalletAddress,
                Balance              = 0,
                Block = transactionBlock
            };

            await _storage.InsertOrReplaceAsync
            (
                entity,
                x => x.Block < transactionBlock
            );
        }
        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
            );
        }
        public async Task <CommandHandlingResult> Handle(LockDepositWalletCommand command, IEventPublisher publisher)
        {
            var depositWalletKey = new DepositWalletKey
                                   (
                command.BlockchainAssetId,
                command.BlockchainType,
                command.DepositWalletAddress
                                   );

            var depositWalletLock = await _depositWalletLockRepository.LockAsync
                                    (
                depositWalletKey,
                command.DepositWalletBalance,
                command.DepositWalletBlock,
                CashinAggregate.GetNextId
                                    );

            var enrolledBalance = await _enrolledBalanceRepository.TryGetAsync(depositWalletKey);

            _chaosKitty.Meow(depositWalletLock.OperationId);

            publisher.PublishEvent(new DepositWalletLockedEvent
            {
                OperationId          = depositWalletLock.OperationId,
                BlockchainType       = command.BlockchainType,
                BlockchainAssetId    = command.BlockchainAssetId,
                DepositWalletAddress = command.DepositWalletAddress,
                LockedAtBalance      = depositWalletLock.Balance,
                LockedAtBlock        = depositWalletLock.Block,
                EnrolledBalance      = enrolledBalance?.Balance ?? 0,
                EnrolledBlock        = enrolledBalance?.Block ?? 0,
                AssetId                 = command.AssetId,
                AssetAccuracy           = command.AssetAccuracy,
                BlockchainAssetAccuracy = command.BlockchainAssetAccuracy,
                CashinMinimalAmount     = command.CashinMinimalAmount,
                HotWalletAddress        = command.HotWalletAddress
            });

            return(CommandHandlingResult.Ok());
        }
Beispiel #14
0
 public static string GetRowKey(DepositWalletKey key)
 {
     return(key.DepositWalletAddress);
 }
Beispiel #15
0
 public static string GetPartitionKey(DepositWalletKey key)
 {
     return($"{key.BlockchainType}-{key.BlockchainAssetId}-{key.DepositWalletAddress.CalculateHexHash32(3)}");
 }
        public async Task DeleteBalanceAsync(DepositWalletKey key)
        {
            _memCache.Remove(key);

            await _enrolledBalanceRepository.DeleteBalanceAsync(key);
        }