public async Task <IUserTransferWallet> GetUserContractAsync(string userAddress, string transferContractAddress)
        {
            string lowerUserAddress    = userAddress.ToLower();
            IUserTransferWallet wallet =
                await _table.GetDataAsync(UserTransferWalletEntity.GenerateParitionKey(lowerUserAddress), transferContractAddress);

            return(wallet);
        }
Example #2
0
        public static async Task <IUserTransferWallet> GetUserTransferWalletAsync(IUserTransferWalletRepository userTransferWalletRepository,
                                                                                  string contractAddress, string erc20TokenAddress, string userAddress)
        {
            string formattedAddress =
                userTransferWalletRepository.FormatAddressForErc20(contractAddress, erc20TokenAddress).ToLower();
            IUserTransferWallet wallet =
                await userTransferWalletRepository.GetUserContractAsync(userAddress, formattedAddress);

            return(wallet);
        }
 public static UserTransferWalletEntity Create(IUserTransferWallet userTransferWallet)
 {
     return(new UserTransferWalletEntity
     {
         PartitionKey = GenerateParitionKey(userTransferWallet.UserAddress),
         RowKey = userTransferWallet.TransferContractAddress,
         UpdateDate = userTransferWallet.UpdateDate,
         UserAddress = userTransferWallet.UserAddress.ToLower(),
         TransferContractAddress = userTransferWallet.TransferContractAddress,
         LastBalance = userTransferWallet.LastBalance
     });
 }
        public async Task Execute()
        {
            await _transferContractsRepository.ProcessAllAsync(async (item) =>
            {
                try
                {
                    //Check that transfer contract assigned to user
                    if (!string.IsNullOrEmpty(item.UserAddress))
                    {
                        var userAddress = await _transferContractService.GetUserAddressForTransferContract(item.ContractAddress);
                        if (string.IsNullOrEmpty(userAddress) || userAddress == Constants.EmptyEthereumAddress)
                        {
                            await UpdateUserAssignmentFail(item.ContractAddress, item.UserAddress, item.CoinAdapterAddress);

                            throw new Exception($"User assignment was not completed for {item.UserAddress} (coinAdaptertrHash::{item.CoinAdapterAddress}, trHash: {item.AssignmentHash})");
                        }
                        //it is a transfer wallet
                        IUserTransferWallet wallet = await _userTransferWalletRepository.GetUserContractAsync(item.UserAddress, item.ContractAddress);
                        if (wallet == null ||
                            string.IsNullOrEmpty(wallet.LastBalance) ||
                            wallet.LastBalance == "0")
                        {
                            BigInteger balance = await _transferContractService.GetBalance(item.ContractAddress);

                            if (balance > 0)
                            {
                                await _userTransferWalletRepository.ReplaceAsync(new UserTransferWallet()
                                {
                                    LastBalance             = balance.ToString(),
                                    TransferContractAddress = item.ContractAddress,
                                    UserAddress             = item.UserAddress,
                                    UpdateDate = DateTime.UtcNow
                                });

                                await _transferContractTransactionService.PutContractTransferTransaction(new TransferContractTransaction()
                                {
                                    Amount             = balance.ToString(),
                                    UserAddress        = item.UserAddress,
                                    CoinAdapterAddress = item.CoinAdapterAddress,
                                    ContractAddress    = item.ContractAddress,
                                    CreateDt           = DateTime.UtcNow
                                });

                                string currency = item.ContainsEth ? "Wei" : "Tokens";
                                await _logger.WriteInfoAsync("MonitoringTransferContracts", "Execute", "", $"Balance on transfer address - {item.ContractAddress}" +
                                                             $" for adapter contract {item.CoinAdapterAddress} is {balance} ({currency})" +
                                                             $" transfer belongs to user {item.UserAddress}", DateTime.UtcNow);
                            }
                        }
                    }
                    else
                    {
                        await UpdateUserAssignmentFail(item.ContractAddress, item.UserAddress, item.CoinAdapterAddress);
                    }
                }
                catch (Exception e)
                {
                    await _logger.WriteErrorAsync("MonitoringTransferContracts", "Execute", "", e, DateTime.UtcNow);
                }
            });
        }
        public async Task SaveAsync(IUserTransferWallet wallet)
        {
            var entity = UserTransferWalletEntity.Create(wallet);

            await _table.InsertAsync(entity);
        }
        public async Task Execute()
        {
            IList <Erc20Token> erc20Tokens = null;

            try
            {
                var tradeableAssets = await _assetsService.AssetGetAllAsync();

                var supportedTokenAssets = tradeableAssets.Where(asset =>
                                                                 asset.Type == Lykke.Service.Assets.Client.Models.AssetType.Erc20Token && asset.IsTradable);
                var assetIds       = supportedTokenAssets.Select(x => x.Id);
                var tradableTokens = await _assetsService.Erc20TokenGetBySpecificationAsync(new Lykke.Service.Assets.Client.Models.Erc20TokenSpecification()
                {
                    Ids = assetIds.ToList(),
                });

                erc20Tokens = tradableTokens?.Items;
            }
            catch (Exception exc)
            {
                await _logger.WriteErrorAsync(nameof(Erc20DepositMonitoringContracts),
                                              nameof(Execute),
                                              "Assets Service unavailable",
                                              exc,
                                              DateTime.UtcNow);

                return;
            }

            if (erc20Tokens != null && !erc20Tokens.Any())
            {
                await _logger.WriteWarningAsync(nameof(Erc20DepositMonitoringContracts),
                                                nameof(Execute),
                                                "",
                                                "No tokens available for trade",
                                                DateTime.UtcNow);

                return;
            }

            await _erc20DepositContractService.ProcessAllAsync(async (item) =>
            {
                try
                {
                    //Check that deposit contract assigned to user
                    if (!string.IsNullOrEmpty(item.UserAddress))
                    {
                        // null - means we ask for all balances on current address
                        var tokenBalances = await _erc20BalanceService.GetBalancesForAddress(item.ContractAddress, new string[0]);

                        if (tokenBalances != null)
                        {
                            foreach (var tokenBalance in tokenBalances)
                            {
                                string tokenAddress     = tokenBalance.Erc20TokenAddress?.ToLower();
                                string formattedAddress =
                                    _userTransferWalletRepository.FormatAddressForErc20(item.ContractAddress, tokenAddress);
                                IUserTransferWallet wallet =
                                    await _userTransferWalletRepository.GetUserContractAsync(item.UserAddress, formattedAddress);
                                if (wallet == null ||
                                    string.IsNullOrEmpty(wallet.LastBalance) ||
                                    wallet.LastBalance == "0")
                                {
                                    BigInteger balance =
                                        await _ercInterfaceService.GetBalanceForExternalTokenAsync(item.ContractAddress, tokenAddress);

                                    if (balance > 0)
                                    {
                                        await _userTransferWalletRepository.ReplaceAsync(new UserTransferWallet()
                                        {
                                            LastBalance             = balance.ToString(),
                                            TransferContractAddress = formattedAddress,
                                            UserAddress             = item.UserAddress,
                                            UpdateDate = DateTime.UtcNow
                                        });

                                        await _erc20DepositTransactionService.PutContractTransferTransaction(new Erc20DepositContractTransaction()
                                        {
                                            Amount          = balance.ToString(),
                                            UserAddress     = item.UserAddress,
                                            TokenAddress    = tokenAddress,
                                            ContractAddress = item.ContractAddress,
                                            CreateDt        = DateTime.UtcNow,
                                        });

                                        await _logger.WriteInfoAsync(nameof(Erc20DepositMonitoringContracts),
                                                                     nameof(Execute), "", $"Balance on transfer address - {item.ContractAddress} is {balance} (Tokens of {tokenBalance.Erc20TokenAddress})" +
                                                                     $" transfer belongs to user {item.UserAddress}", DateTime.UtcNow);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        //TODO
                        //Notify That deposit contract does not have a user;
                    }
                }
                catch (Exception e)
                {
                    await _logger.WriteErrorAsync(nameof(Erc20DepositMonitoringContracts),
                                                  nameof(Execute), "", e, DateTime.UtcNow);
                }
            });
        }