private static async Task Migrate(IBlockchainWalletsRepository walletRepository, ISigningServiceApi signingServiceApi,
                                          IBlockchainSignFacadeClient blockchainSignFacade, ICqrsEngine cqrs,
                                          IBcnCredentialsRecord bcnCredentialsRecord)
        {
            var clientId       = Guid.Parse(bcnCredentialsRecord.ClientId);
            var existingWallet = await walletRepository.TryGetAsync(BlockchainType, clientId);

            if (existingWallet != null)
            {
                return;
            }
            var address    = bcnCredentialsRecord.AssetAddress;
            var privateKey = await GetPrivateKey(signingServiceApi, address);

            if (privateKey == null)
            {
                return;
            }

            await ImportWalletToSignFacade(blockchainSignFacade, privateKey, address);

            await walletRepository.AddAsync(BlockchainType, clientId, address, CreatorType.LykkeWallet);

            var @event = new WalletCreatedEvent
            {
                Address            = address,
                AssetId            = AssetId,
                BlockchainType     = BlockchainType,
                IntegrationLayerId = BlockchainType,
                ClientId           = clientId,
                CreatedBy          = CreatorType.LykkeWallet
            };

            cqrs.PublishEvent(@event, BlockchainWalletsBoundedContext.Name);
        }
Esempio n. 2
0
 private void Apply(WalletCreatedEvent @event)
 {
     Id         = @event.AggregateId;
     InvestorId = @event.InvestorId;
     Currency   = @event.Currency;
     Amount     = @event.Amount;
     StockList  = new List <Stock>();
 }
Esempio n. 3
0
        public async Task HandleAsync(WalletCreatedEvent @event)
        {
            var investor = await _eventRepository.GetByIdAsync <Investor>(@event.InvestorId);

            if (investor == null)
            {
                throw new StocqresException("Investor doesn't exist");
            }
            investor.AssignWallet(@event.AggregateId);
            await _eventRepository.SaveAsync(investor);
        }
Esempio n. 4
0
        public async Task <WalletWithAddressExtensionDto> CreateWalletAsync(string blockchainType, Guid clientId,
                                                                            CreatorType createdBy)
        {
            string address;

            if (blockchainType == SpecialBlockchainTypes.FirstGenerationBlockchain)
            {
                return(null);
            }

            var isAddressMappingRequired = _blockchainExtensionsService.IsAddressMappingRequired(blockchainType);
            var wallet = await _blockchainSignFacadeClient.CreateWalletAsync(blockchainType);

            address = wallet.PublicAddress;


            var underlyingAddress = await _addressService.GetUnderlyingAddressAsync(blockchainType, address);

            if (isAddressMappingRequired.HasValue && isAddressMappingRequired.Value && underlyingAddress == null)
            {
                throw new ArgumentException(
                          $"Failed to get UnderlyingAddress for blockchainType={blockchainType} and address={address}");
            }

            await _walletRepository.AddAsync(blockchainType, clientId, address, createdBy);

            var @event = new WalletCreatedEvent
            {
                Address            = address,
                BlockchainType     = blockchainType,
                IntegrationLayerId = blockchainType,
                CreatedBy          = createdBy,
                ClientId           = clientId
            };

            _cqrsEngine.PublishEvent
            (
                @event,
                BlockchainWalletsBoundedContext.Name
            );

            return(ConvertWalletToWalletWithAddressExtension
                   (
                       new WalletDto
            {
                Address = isAddressMappingRequired.HasValue &&
                          isAddressMappingRequired.Value ? underlyingAddress : address,
                BlockchainType = blockchainType,
                ClientId = clientId,
                CreatorType = createdBy
            }
                   ));
        }
Esempio n. 5
0
 private void Handle(WalletCreatedEvent evnt)
 {
     _cash                  = 0;
     _lockedCash            = 0;
     _benevolence           = 0;
     _bankCards             = new List <BankCard>();
     _withdrawApplys        = new List <WithdrawApply>();
     _rechargeApplys        = new List <RechargeApply>();
     _cashTransfers         = new HashSet <Guid>();
     _benevolenceTransfers  = new HashSet <Guid>();
     _userId                = evnt.UserId;
     _walletStatisticInfo   = new WalletStatisticInfo(0, 0, 0, 0, 0, DateTime.Now);
     _withdrawStatisticInfo = new WithdrawStatisticInfo(0, 0, 0, DateTime.Now);
 }
        private async Task Handle(WalletCreatedEvent evt, ICommandSender sender)
        {
            var address        = evt.Address;
            var assetId        = evt.AssetId;
            var blockchainType = evt.BlockchainType ?? evt.IntegrationLayerId;

            Task <bool> WalletIsSubscribedAsync(MonitoringSubscriptionType subscriptionType)
            {
                return(_monitoringSubscriptionRepository.WalletIsSubscribedAsync
                       (
                           blockchainType: blockchainType,
                           address: address,
                           subscriptionType: subscriptionType
                       ));
            }

            if (!await WalletIsSubscribedAsync(MonitoringSubscriptionType.Balance))
            {
                sender.SendCommand
                (
                    new BeginBalanceMonitoringCommand
                {
                    Address        = address,
                    AssetId        = assetId,
                    BlockchainType = blockchainType
                },
                    BlockchainWalletsBoundedContext.Name
                );
            }

            if (!await WalletIsSubscribedAsync(MonitoringSubscriptionType.TransactionHistory))
            {
                sender.SendCommand
                (
                    new BeginTransactionHistoryMonitoringCommand
                {
                    Address        = address,
                    AssetId        = assetId,
                    BlockchainType = blockchainType
                },
                    BlockchainWalletsBoundedContext.Name
                );
            }
        }
Esempio n. 7
0
 public async Task HandleAsync(WalletCreatedEvent @event)
 {
     await _projectionWriter.UpdateAsync <InvestorProjection>(@event.InvestorId,
                                                              e => e.WalletId = @event.AggregateId);
 }
Esempio n. 8
0
        private static async Task Migrate(string settingsUrl)
        {
            if (!Uri.TryCreate(settingsUrl, UriKind.Absolute, out _))
            {
                Console.WriteLine($"{SettingsUrl} should be a valid uri");

                return;
            }

            var logFactory = LogFactory.Create().AddConsole();

            var settings     = new SettingsServiceReloadingManager <AppSettings>(settingsUrl, p => { });
            var settingsRepo = settings.Nested(x => x.BlockchainWalletsService.Db.DataConnString);

            var builder     = new ContainerBuilder();
            var appSettings = settings;

            builder.RegisterInstance(logFactory).As <ILogFactory>();

            builder
            .RegisterModule(new CqrsModule(appSettings.CurrentValue.BlockchainWalletsService.Cqrs))
            .RegisterModule(new RepositoriesModule(appSettings.Nested(x => x.BlockchainWalletsService.Db)))
            .RegisterModule(new ServiceModule(
                                appSettings.CurrentValue.BlockchainsIntegration,
                                appSettings.CurrentValue.BlockchainSignFacadeClient,
                                appSettings.CurrentValue,
                                appSettings.CurrentValue.AssetsServiceClient,
                                appSettings.CurrentValue.BlockchainWalletsService));

            var container = builder.Build();

            var cqrsEngine = container.Resolve <ICqrsEngine>();

            var archiveWalletsTable = AzureTableStorage <BlockchainWalletEntity> .Create
                                      (
                settingsRepo,
                "BlockchainWalletsArchive",
                logFactory
                                      );

            var defaultWalletsRepository    = (WalletRepository)WalletRepository.Create(settingsRepo, logFactory);
            var blockchainWalletsRepository = (BlockchainWalletsRepository)AzureRepositories.BlockchainWalletsRepository.Create(settingsRepo, logFactory);

            string continuationToken = null;

            Console.WriteLine("Creating indexes for default wallets...");

            var       progressCounter = 0;
            const int batchSize       = 10;

            //Commented code recreates all wallets with indicies
            //do
            //{
            //    try
            //    {
            //        IEnumerable<WalletDto> wallets;
            //        (wallets, continuationToken) = await blockchainWalletsRepository.GetAllAsync(100, continuationToken);

            //        foreach (var batch in wallets.Batch(batchSize))
            //        {
            //            await Task.WhenAll(batch.Select(o =>
            //                blockchainWalletsRepository.DeleteIfExistsAsync(o.BlockchainType, o.ClientId, o.Address)));
            //            progressCounter += batchSize;
            //            Console.SetCursorPosition(0, Console.CursorTop);
            //            Console.Write($"{progressCounter} indexes created");
            //        }
            //    }
            //    catch (Exception e)
            //    {
            //        Console.WriteLine(e.StackTrace + " " + e.Message);
            //    }

            //} while (continuationToken != null);

            //do
            //{
            //    try
            //    {
            //        IEnumerable<BlockchainWalletEntity> wallets;
            //        (wallets, continuationToken) = await archiveWalletsTable.GetDataWithContinuationTokenAsync(100, continuationToken);

            //        foreach (var batch in wallets.Batch(batchSize))
            //        {
            //            await Task.WhenAll(batch.Select(async o =>
            //            {
            //                await blockchainWalletsRepository.AddAsync(o.IntegrationLayerId, o.ClientId, o.Address,
            //                    o.CreatedBy);

            //                var @event = new WalletCreatedEvent
            //                {
            //                    Address = o.Address,
            //                    BlockchainType = o.IntegrationLayerId,
            //                    IntegrationLayerId = o.IntegrationLayerId,
            //                    CreatedBy = CreatorType.LykkeWallet,
            //                    ClientId = o.ClientId
            //                };

            //                cqrsEngine.PublishEvent
            //                (
            //                    @event,
            //                    BlockchainWalletsBoundedContext.Name
            //                );
            //            }));
            //            progressCounter += batchSize;
            //            Console.SetCursorPosition(0, Console.CursorTop);
            //            Console.Write($"{progressCounter} indexes created");
            //        }
            //    }
            //    catch (Exception e)
            //    {
            //        Console.WriteLine(e.StackTrace + " " + e.Message);
            //    }

            //} while (continuationToken != null);

            do
            {
                try
                {
                    IEnumerable <WalletDto> wallets;
                    (wallets, continuationToken) = await defaultWalletsRepository.GetAllAsync(100, continuationToken);

                    foreach (var batch in wallets.Batch(batchSize))
                    {
                        await Task.WhenAll(batch.Select(async o =>
                        {
                            await blockchainWalletsRepository.AddAsync(o.BlockchainType, o.ClientId, o.Address,
                                                                       CreatorType.LykkeWallet);

                            var @event = new WalletCreatedEvent
                            {
                                Address            = o.Address,
                                BlockchainType     = o.BlockchainType,
                                IntegrationLayerId = o.BlockchainType,
                                CreatedBy          = CreatorType.LykkeWallet,
                                ClientId           = o.ClientId
                            };

                            cqrsEngine.PublishEvent
                            (
                                @event,
                                BlockchainWalletsBoundedContext.Name
                            );
                        }));

                        progressCounter += batchSize;
                        Console.SetCursorPosition(0, Console.CursorTop);
                        Console.Write($"{progressCounter} indexes created");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace + " " + e.Message);
                }
            } while (continuationToken != null);
            Console.WriteLine();
            Console.WriteLine("Conversion completed");
        }
Esempio n. 9
0
        public async Task <WalletWithAddressExtensionDto> CreateWalletAsync(string blockchainType, string assetId,
                                                                            Guid clientId)
        {
            string address;

            if (blockchainType != SpecialBlockchainTypes.FirstGenerationBlockchain)
            {
                var wallet = await _blockchainSignFacadeClient.CreateWalletAsync(blockchainType);

                address = wallet.PublicAddress;

                var isAddressMappingRequired = _blockchainExtensionsService.IsAddressMappingRequired(blockchainType);
                var underlyingAddress        = await _addressService.GetUnderlyingAddressAsync(blockchainType, address);

                if (isAddressMappingRequired.HasValue && isAddressMappingRequired.Value && underlyingAddress == null)
                {
                    throw new ArgumentException(
                              $"Failed to get UnderlyingAddress for blockchainType={blockchainType} and address={address}");
                }

                await _walletRepository.AddAsync(blockchainType, clientId, address, CreatorType.LykkeWallet);

                var @event = new WalletCreatedEvent
                {
                    Address            = address,
                    AssetId            = assetId,
                    BlockchainType     = blockchainType,
                    IntegrationLayerId = blockchainType,
                    ClientId           = clientId
                };

                await _firstGenerationBlockchainWalletRepository.InsertOrReplaceAsync(new BcnCredentialsRecord
                {
                    Address      = string.Empty,
                    AssetAddress = address,
                    ClientId     = clientId.ToString(),
                    EncodedKey   = string.Empty,
                    PublicKey    = string.Empty,
                    AssetId      = $"{blockchainType} ({assetId})"
                });

                _cqrsEngine.PublishEvent
                (
                    @event,
                    BlockchainWalletsBoundedContext.Name
                );

                return(ConvertWalletToWalletWithAddressExtension
                       (
                           new WalletDto
                {
                    Address = isAddressMappingRequired.HasValue && isAddressMappingRequired.Value ? underlyingAddress : address,
                    AssetId = assetId,
                    BlockchainType = blockchainType,
                    ClientId = clientId,
                    CreatorType = CreatorType.LykkeWallet
                }
                       ));
            }

            address = await _legacyWalletService.CreateWalletAsync(clientId, assetId);

            return(new WalletWithAddressExtensionDto()
            {
                Address = address,
                AssetId = assetId,
                BlockchainType = blockchainType,
                ClientId = clientId,
                BaseAddress = address
            });
        }