Exemple #1
0
        private async Task <IMerchantWallet> GetCashoutWalletAsync(CashoutCommand cmd)
        {
            string merchantWalletId = cmd.SourceMerchantWalletId;

            return(string.IsNullOrEmpty(merchantWalletId)
                ? await _merchantWalletService.GetDefaultAsync(cmd.MerchantId, cmd.SourceAssetId, PaymentDirection.Outgoing)
                : await _merchantWalletService.GetByIdAsync(merchantWalletId));
        }
Exemple #2
0
        private async Task <string> GetSourceAddressAsync(CashoutCommand cmd)
        {
            IMerchantWallet merchantWallet = await GetCashoutWalletAsync(cmd);

            if (merchantWallet.MerchantId != cmd.MerchantId)
            {
                throw new MerchantWalletOwnershipException(cmd.MerchantId, merchantWallet.WalletAddress);
            }

            return(merchantWallet.WalletAddress);
        }
Exemple #3
0
        public async Task <CashoutResult> ExecuteAsync(CashoutCommand cmd)
        {
            BlockchainType network = await _assetSettingsService.GetNetworkAsync(cmd.SourceAssetId);

            string hotwallet = _bcnSettingsResolver.GetCashoutHotWallet(network);

            if (string.IsNullOrEmpty(hotwallet))
            {
                throw new CashoutHotwalletNotDefinedException(network);
            }

            string sourceAddress = await GetSourceAddressAsync(cmd);

            await _walletBalanceValidator.ValidateTransfer(sourceAddress, cmd.SourceAssetId, cmd.SourceAmount);

            TransferResult toHotWallet = await _retryPolicy
                                         .ExecuteAsync(() => _transferService.CashoutThrowFail(
                                                           cmd.SourceAssetId,
                                                           sourceAddress,
                                                           hotwallet,
                                                           cmd.SourceAmount));

            foreach (var transferTransactionResult in toHotWallet.Transactions)
            {
                string historyId = await _walletHistoryService.PublishCashoutAsync(new WalletHistoryCashoutCommand
                {
                    Blockchain      = toHotWallet.Blockchain,
                    AssetId         = transferTransactionResult.AssetId,
                    Amount          = transferTransactionResult.Amount,
                    DesiredAsset    = cmd.DesiredAsset,
                    EmployeeEmail   = cmd.EmployeeEmail,
                    TransactionHash = transferTransactionResult.Hash,
                    WalletAddress   = string.Join(Constants.Separator, transferTransactionResult.Sources)
                });

                await _transactionsService.CreateTransactionAsync(new CreateTransactionCommand
                {
                    Amount                = transferTransactionResult.Amount,
                    Blockchain            = toHotWallet.Blockchain,
                    AssetId               = transferTransactionResult.AssetId,
                    Confirmations         = 0,
                    Hash                  = transferTransactionResult.Hash,
                    IdentityType          = transferTransactionResult.IdentityType,
                    Identity              = transferTransactionResult.Identity,
                    TransferId            = toHotWallet.Id,
                    Type                  = TransactionType.CashOut,
                    SourceWalletAddresses = transferTransactionResult.Sources.ToArray(),
                    ContextData           = new CashoutTransactionContext
                    {
                        DesiredAsset       = cmd.DesiredAsset,
                        EmployeeEmail      = cmd.EmployeeEmail,
                        HistoryOperationId = historyId
                    }.ToJson()
                });
            }

            return(new CashoutResult
            {
                Amount = cmd.SourceAmount,
                AssetId = cmd.SourceAssetId,
                SourceWalletAddress = sourceAddress,
                DestWalletAddress = hotwallet
            });
        }