Пример #1
0
        public async Task RetryCashoutAsync(IHotWalletOperation hotWalletCashout)
        {
            HotWalletCashoutMessage message = new HotWalletCashoutMessage()
            {
                OperationId = hotWalletCashout.OperationId
            };

            await _hotWalletCashoutRepository.SaveAsync(hotWalletCashout);

            await _hotWalletCashoutQueue.PutRawMessageAsync(Newtonsoft.Json.JsonConvert.SerializeObject(message));
        }
Пример #2
0
        /// <param name="depositContractAddress"></param>
        /// <param name="erc20TokenAddress"></param>
        /// <param name="destinationAddress"></param>
        /// <returns>TransactionHash</returns>
        public async Task <string> RecievePaymentFromDepositContract(string depositContractAddress,
                                                                     string erc20TokenAddress, string destinationAddress)
        {
            var depositContract = await _contractRepository.GetByContractAddress(depositContractAddress);

            if (depositContract == null)
            {
                throw new ClientSideException(ExceptionType.WrongParams, $"DepositContractAddress {depositContractAddress} does not exist");
            }

            var userWallet = await TransferWalletSharedService.GetUserTransferWalletAsync(_userTransferWalletRepository,
                                                                                          depositContractAddress, erc20TokenAddress, depositContract.UserAddress);

            if (userWallet != null && !string.IsNullOrEmpty(userWallet.LastBalance))
            {
                throw new ClientSideException(ExceptionType.TransferInProcessing, $"Transfer from {depositContractAddress} was started before wait for it to complete");
            }

            var balance = await _ercInterfaceService.GetBalanceForExternalTokenAsync(depositContractAddress, erc20TokenAddress);

            if (balance == 0)
            {
                throw new ClientSideException(ExceptionType.NotEnoughFunds, $"No tokens detected at deposit address {depositContractAddress}");
            }

            var guidStr = Guid.NewGuid().ToString();

            var message = new Lykke.Service.EthereumCore.Core.Messages.LykkePay.LykkePayErc20TransferMessage()
            {
                OperationId = guidStr
            };

            var existingOperation = await _operationsRepository.GetAsync(guidStr);

            if (existingOperation != null)
            {
                throw new ClientSideException(ExceptionType.EntityAlreadyExists, "Try again later");
            }
            var transactionSenderAddress = _appSettings.LykkePay.LykkePayAddress;
            var estimationResult         = await Erc20SharedService.EstimateDepositTransferAsync(_web3,
                                                                                                 _settings.Erc20DepositContract.Abi,
                                                                                                 transactionSenderAddress,
                                                                                                 depositContractAddress,
                                                                                                 erc20TokenAddress,
                                                                                                 destinationAddress);

            if (!estimationResult)
            {
                throw new ClientSideException(ExceptionType.WrongDestination, $"Can't estimate transfer {depositContractAddress}, {erc20TokenAddress}, {destinationAddress}");
            }

            await _operationsRepository.SaveAsync(new HotWalletOperation()
            {
                Amount        = balance,
                FromAddress   = depositContractAddress,
                OperationId   = guidStr,
                OperationType = HotWalletOperationType.Cashin,
                ToAddress     = destinationAddress,
                TokenAddress  = erc20TokenAddress
            });

            await _transferQueue.PutRawMessageAsync(Newtonsoft.Json.JsonConvert.SerializeObject(message));

            await TransferWalletSharedService.UpdateUserTransferWalletAsync(_userTransferWalletRepository,
                                                                            depositContractAddress, erc20TokenAddress, depositContract.UserAddress, balance.ToString());

            return(guidStr);
        }
        /// <param name="depositContractAddress"></param>
        /// <param name="erc20TokenAddress"></param>
        /// <param name="destinationAddress"></param>
        /// <returns>TransactionHash</returns>
        public async Task <string> RecievePaymentFromDepositContractAsync(string depositContractAddress,
                                                                          string erc20TokenAddress, string destinationAddress, string tokenAmount)
        {
            var depositContract = await _contractRepository.GetByContractAddress(depositContractAddress);

            if (depositContract == null)
            {
                throw new ClientSideException(ExceptionType.WrongParams, $"DepositContractAddress {depositContractAddress} does not exist");
            }

            var amount  = System.Numerics.BigInteger.Parse(tokenAmount);
            var balance = await _ercInterfaceService.GetBalanceForExternalTokenAsync(depositContractAddress, erc20TokenAddress);

            if (balance == 0 || amount > balance)
            {
                throw new ClientSideException(ExceptionType.NotEnoughFunds,
                                              $"Not enough tokens to proceed with withdrawal detected at deposit address {depositContractAddress}. " +
                                              $"Current balance: {balance}");
            }

            var guidStr = Guid.NewGuid().ToString();

            var message = new Lykke.Service.EthereumCore.Core.Messages.LykkePay.LykkePayErc20TransferMessage()
            {
                OperationId = guidStr
            };

            var existingOperation = await _operationsRepository.GetAsync(guidStr);

            if (existingOperation != null)
            {
                throw new ClientSideException(ExceptionType.EntityAlreadyExists, "Try again later");
            }

            var transactionSenderAddress = _appSettings.Airlines.AirlinesAddress;
            var estimationResult         = await Erc223SharedService.EstimateDepositTransferAsync(_web3,
                                                                                                  _appSettings.EthereumCore.Erc223DepositContract.Abi,
                                                                                                  transactionSenderAddress,
                                                                                                  depositContractAddress,
                                                                                                  erc20TokenAddress,
                                                                                                  destinationAddress,
                                                                                                  amount);

            if (!estimationResult)
            {
                throw new ClientSideException(ExceptionType.WrongDestination, $"Can't estimate transfer {depositContractAddress}, {erc20TokenAddress}, {destinationAddress}");
            }

            await _operationsRepository.SaveAsync(new HotWalletOperation()
            {
                Amount        = amount,
                FromAddress   = depositContractAddress,
                OperationId   = guidStr,
                OperationType = HotWalletOperationType.Cashin,
                ToAddress     = destinationAddress,
                TokenAddress  = erc20TokenAddress
            });

            await _transferQueue.PutRawMessageAsync(Newtonsoft.Json.JsonConvert.SerializeObject(message));

            return(guidStr);
        }