Пример #1
0
        public async Task <bool> Execute(string address, BlockchainType blockchain)
        {
            IBlockchainApiClient blockchainClient = _blockchainClientProvider.Get(blockchain);

            try
            {
                return(await blockchainClient.ValidateAddressAsync(address));
            }
            catch (WalletAddressValidationException validationEx)
            {
                await _log.WriteErrorAsync(nameof(BlockchainAddressValidator), nameof(Execute), new
                {
                    Blockchain = validationEx.Blockchain.ToString(),
                    validationEx.Address
                }.ToJson(), validationEx);

                throw;
            }
            catch (UnrecognizedApiResponse responseEx)
            {
                await _log.WriteErrorAsync(nameof(BlockchainAddressValidator), nameof(Execute), new
                {
                    responseEx.ResponseType
                }.ToJson(), responseEx);

                throw;
            }
        }
Пример #2
0
        public async Task <bool> Execute(string address, BlockchainType blockchain)
        {
            if (!SupportedNetworks.Contains(blockchain))
            {
                throw new BlockchainTypeNotSupported(blockchain);
            }

            IBlockchainApiClient blockchainClient = _blockchainClientProvider.Get(blockchain);

            try
            {
                return(await blockchainClient.ValidateAddressAsync(address));
            }
            catch (WalletAddressValidationException e)
            {
                _log.ErrorWithDetails(e, new
                {
                    Blockchain = e.Blockchain.ToString(),
                    e.Address
                });

                throw;
            }
            catch (UnrecognizedApiResponse e)
            {
                _log.ErrorWithDetails(e, new { e.ResponseType });

                throw;
            }
        }
Пример #3
0
        public async Task <IVirtualWallet> AddAssetAsync(string merchantId, string walletId, string assetId)
        {
            IVirtualWallet virtualWallet = await _virtualWalletService.GetAsync(merchantId, walletId);

            if (virtualWallet == null)
            {
                throw new WalletNotFoundException(walletId);
            }

            BlockchainType blockchainType = assetId.GetBlockchainType();

            IBlockchainApiClient blockchainClient = _blockchainClientProvider.Get(blockchainType);

            WalletAllocationPolicy policy = _walletAllocationSettings.GetPolicy(blockchainType);

            IBcnWalletUsage walletUsage;

            switch (policy)
            {
            case WalletAllocationPolicy.New:
                string address = await blockchainClient.CreateAddressAsync();

                walletUsage = await _bcnWalletUsageService.OccupyAsync(address, blockchainType, virtualWallet.Id);

                break;

            case WalletAllocationPolicy.Reuse:
                try
                {
                    walletUsage = await _bcnWalletUsageService.OccupyAsync(blockchainType, virtualWallet.Id);
                }
                catch (WalletAddressAllocationException)
                {
                    string newAddress = await blockchainClient.CreateAddressAsync();

                    walletUsage =
                        await _bcnWalletUsageService.OccupyAsync(newAddress, blockchainType, virtualWallet.Id);
                }

                break;

            default:
                throw new UnknownWalletAllocationPolicyException(policy.ToString());
            }

            IVirtualWallet updatedWallet = await _virtualWalletService.AddAddressAsync(
                virtualWallet.MerchantId,
                virtualWallet.Id,
                new BlockchainWallet
            {
                AssetId    = assetId,
                Address    = walletUsage.WalletAddress,
                Blockchain = walletUsage.Blockchain
            });

            await _walletEventsPublisher.PublishAsync(walletUsage.WalletAddress, blockchainType, virtualWallet.DueDate);

            return(updatedWallet);
        }
Пример #4
0
        public async Task <IMerchantWallet> CreateAsync(CreateMerchantWalletCommand cmd)
        {
            IBlockchainApiClient blockchainClient = _blockchainClientProvider.Get(cmd.Network);

            string walletAddress = await blockchainClient.CreateAddressAsync();

            return(await _merchantWalletRespository.CreateAsync(new MerchantWallet
            {
                MerchantId = cmd.MerchantId,
                Network = cmd.Network,
                WalletAddress = walletAddress,
                CreatedOn = DateTime.UtcNow,
                DisplayName = cmd.DisplayName,
                IncomingPaymentDefaults = Enumerable.Empty <string>().ToList(),
                OutgoingPaymentDefaults = Enumerable.Empty <string>().ToList()
            }));
        }
        public async Task <TransferResult> ExecuteAsync(TransferCommand transferCommand)
        {
            BlockchainType blockchainType = await _assetSettingsService.GetNetworkAsync(transferCommand.AssetId);

            IBlockchainApiClient blockchainClient = _blockchainClientProvider.Get(blockchainType);

            BlockchainTransferCommand cmd = new BlockchainTransferCommand(transferCommand.AssetId);

            string lykkeAssetId = transferCommand.AssetId.IsGuid()
                ? transferCommand.AssetId
                : await _lykkeAssetsResolver.GetLykkeId(transferCommand.AssetId);

            foreach (var transferCommandAmount in transferCommand.Amounts)
            {
                decimal balance = await blockchainClient.GetBalanceAsync(transferCommandAmount.Source, lykkeAssetId);

                if (transferCommandAmount.Amount == null)
                {
                    if (balance > 0)
                    {
                        cmd.Amounts.Add(new TransferAmount
                        {
                            Amount      = balance,
                            Source      = transferCommandAmount.Source,
                            Destination = transferCommandAmount.Destination
                        });

                        continue;
                    }

                    throw new InsufficientFundsException(transferCommandAmount.Source, transferCommand.AssetId);
                }

                if (transferCommandAmount.Amount > balance)
                {
                    throw new InsufficientFundsException(transferCommandAmount.Source, transferCommand.AssetId);
                }

                cmd.Amounts.Add(transferCommandAmount);
            }

            BlockchainTransferResult blockchainTransferResult = await blockchainClient.TransferAsync(cmd);

            ITransfer transfer = await _transferRepository.AddAsync(new Transfer
            {
                AssetId      = transferCommand.AssetId,
                Blockchain   = blockchainTransferResult.Blockchain,
                CreatedOn    = DateTime.UtcNow,
                Amounts      = transferCommand.Amounts,
                Transactions = Mapper.Map <IEnumerable <TransferTransaction> >(blockchainTransferResult.Transactions)
            });

            return(Mapper.Map <TransferResult>(transfer));
        }
        public async Task ValidateTransfer(string walletAddress, string assetId, decimal transferAmount)
        {
            assetId = assetId.IsGuid() ? assetId : await _lykkeAssetsResolver.GetLykkeId(assetId);

            BlockchainType network = await _assetSettingsService.GetNetworkAsync(assetId);

            IBlockchainApiClient blockchainApiClient = _blockchainClientProvider.Get(network);

            decimal balance = await blockchainApiClient.GetBalanceAsync(walletAddress, assetId);

            if (balance < transferAmount)
            {
                throw new InsufficientFundsException(walletAddress, assetId);
            }
        }
Пример #7
0
        public async Task <TransferResult> ExecuteAsync(TransferCommand transferCommand)
        {
            BlockchainType blockchainType = transferCommand.AssetId.GetBlockchainType();

            IBlockchainApiClient blockchainClient = _blockchainClientProvider.Get(blockchainType);

            BlockchainTransferResult blockchainTransferResult =
                await blockchainClient.TransferAsync(transferCommand.ToBlockchainTransfer());

            ITransfer transfer = await _transferRepository.AddAsync(new Transfer
            {
                AssetId      = transferCommand.AssetId,
                Blockchain   = blockchainTransferResult.Blockchain,
                CreatedOn    = DateTime.UtcNow,
                Amounts      = transferCommand.Amounts,
                Transactions = Mapper.Map <IEnumerable <TransferTransaction> >(blockchainTransferResult.Transactions)
            });

            return(Mapper.Map <TransferResult>(transfer));
        }