Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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()
            }));
        }