コード例 #1
0
        private void InitializeWCF()
        {
            var authentication = new AuthenticationService.AuthenticationClient().AuthenticateAsync(CPLConstant.ProjectEmail, CPLConstant.ProjectName);

            authentication.Wait();

            if (authentication.Result.Status.Code == 0)
            {
                Authentication.Token = authentication.Result.Token;
                Utils.FileAppendThreadSafe(BTCDepositFileName, String.Format("BTC Deposit Thread - Authenticate successfully. Token {0}{1}", authentication.Result.Token, Environment.NewLine));
                //Utils.FileAppendThreadSafe(ETHDepositFileName, String.Format("ETH Deposit Thread - Authenticate successfully. Token {0}{1}", authentication.Result.Token, Environment.NewLine));
                Utils.FileAppendThreadSafe(BTCWithdrawFileName, String.Format("BTC Withdraw Thread - Authenticate successfully. Token {0}{1}", authentication.Result.Token, Environment.NewLine));
                //Utils.FileAppendThreadSafe(ETHWithdrawFileName, String.Format("ETH Withdraw Thread - Authenticate successfully. Token {0}{1}", authentication.Result.Token, Environment.NewLine));
                var bTransaction = _bTransaction.SetAsync(Authentication.Token, new BTransactionService.BTransactionSetting {
                    Environment = (BTransactionService.Environment)((int)CPLConstant.Environment), Platform = BTransactionService.Platform.BTC
                });
                bTransaction.Wait();
                //var eTransaction = _eTransaction.SetAsync(Authentication.Token, new ETransactionService.ETransactionSetting { Environment = (ETransactionService.Environment)((int)CPLConstant.Environment), Platform = ETransactionService.Platform.ETH, ApiKey = CPLConstant.ETransactionAPIKey });
                //eTransaction.Wait();
            }
            else
            {
                Utils.FileAppendThreadSafe(BTCDepositFileName, String.Format("BTC Deposit Thread - Authenticate failed. Error {0}{1}", authentication.Result.Status.Text, Environment.NewLine));
                //Utils.FileAppendThreadSafe(ETHDepositFileName, String.Format("ETH Deposit Thread - Authenticate failed. Error {0}{1}", authentication.Result.Status.Text, Environment.NewLine));
                Utils.FileAppendThreadSafe(BTCWithdrawFileName, String.Format("BTC Withdraw Thread - Authenticate failed. Error {0}{1}", authentication.Result.Status.Text, Environment.NewLine));
                //Utils.FileAppendThreadSafe(ETHWithdrawFileName, String.Format("ETH Withdraw Thread - Authenticate failed. Error {0}{1}", authentication.Result.Status.Text, Environment.NewLine));
            }
        }
コード例 #2
0
ファイル: CPLNotifyService.cs プロジェクト: weedkiller/cpl
        private void InitializeWCF()
        {
            var authentication = new AuthenticationService.AuthenticationClient().AuthenticateAsync(CPLConstant.ProjectEmail, CPLConstant.ProjectName);

            authentication.Wait();

            if (authentication.Result.Status.Code == 0)
            {
                Authentication.Token = authentication.Result.Token;
                //Utils.FileAppendThreadSafe(ETHNotifyFileName, String.Format("ETH Nofity Thread - Authenticate successfully. Token {0}{1}", authentication.Result.Token, Environment.NewLine));
                Utils.FileAppendThreadSafe(BTCNotifyFileName, String.Format("BTC Nofity Thread - Authenticate successfully. Token {0}{1}", authentication.Result.Token, Environment.NewLine));
                //var eBlock = _eBlock.SetAsync(Authentication.Token, new EBlockService.EBlockSetting { Environment = (EBlockService.Environment)((int)CPLConstant.Environment) });
                //eBlock.Wait();
                var bBlock = _bBlock.SetAsync(Authentication.Token, new BBlockService.BBlockSetting {
                    Environment = (BBlockService.Environment)((int)CPLConstant.Environment)
                });
                bBlock.Wait();

                //var eTransaction = _eTransaction.SetAsync(Authentication.Token, new ETransactionService.ETransactionSetting { Environment = (ETransactionService.Environment)((int)CPLConstant.Environment) });
                //eTransaction.Wait();
            }
            else
            {
                //Utils.FileAppendThreadSafe(ETHNotifyFileName, String.Format("ETH Nofity Thread - Authenticate failed. Error {0}{1}", authentication.Result.Status.Text, Environment.NewLine));
                Utils.FileAppendThreadSafe(BTCNotifyFileName, String.Format("BTC Nofity Thread - Authenticate failed. Error {0}{1}", authentication.Result.Status.Text, Environment.NewLine));
            }
        }
コード例 #3
0
        public static bool Authenticate(string Username, string Password)
        {
            string TokenApi = ConfigurationManager.AppSettings["appVersion"];

            AuthenticationService.AuthenticationClient client = new AuthenticationService.AuthenticationClient();
            try
            {
                var user = client.Authenticate(Username, Password, TokenApi);
                TokenUser = user.TokenUser;
                if (!user.IsAuthentified)
                {
                    InformationMessage = "Invalid credentials";
                }
                return(user.IsAuthentified);
            }
            catch
            {
                InformationMessage = "Web service unavailable";
                return(false);
            }
        }
コード例 #4
0
        private void CheckTransaction()
        {
            try
            {
                Utils.FileAppendThreadSafe(FileName, string.Format("Check Tx thread STARTED on {0}{1}", DateTime.Now, Environment.NewLine));

                var authentication = new AuthenticationService.AuthenticationClient().AuthenticateAsync(CPLConstant.ProjectEmail, CPLConstant.ProjectName);
                authentication.Wait();

                if (authentication.Result.Status.Code == 0)
                {
                    Utils.FileAppendThreadSafe(FileName, string.Format("Successful authentication with token {0} at {1}{2}", authentication.Result.Token, DateTime.Now, Environment.NewLine));
                    var eToken = new ETokenService.ETokenClient().SetAsync(authentication.Result.Token, new ETokenService.ETokenSetting {
                        Abi = CPLConstant.Abi, ContractAddress = CPLConstant.SmartContractAddress, Environment = (ETokenService.Environment)((int)CPLConstant.Environment), Platform = ETokenService.Platform.ETH
                    });
                    eToken.Wait();
                    var eTransaction = new ETransactionService.ETransactionClient().SetAsync(authentication.Result.Token, new ETransactionService.ETransactionSetting {
                        ApiKey = CPLConstant.ETransactionAPIKey, Environment = (ETransactionService.Environment)((int)CPLConstant.Environment), Platform = ETransactionService.Platform.ETH
                    });
                    eTransaction.Wait();

                    do
                    {
                        var transactionList = Resolver.LotteryHistoryService.Queryable()
                                              .Where(x => string.IsNullOrEmpty(x.TicketNumber))
                                              .Select(x => x.TxHashId).Distinct().ToList(); // In case user buys multiple lottery tickets

                        foreach (var tx in transactionList)
                        {
                            var txStatus = _eTransaction.GetTransactionStatusAsync(authentication.Result.Token, tx);
                            txStatus.Wait();
                            if (txStatus.Result.Status.Code == 0)
                            {
                                if (txStatus.Result.Receipt == true)
                                {
                                    var duplicateTicketIndexList = new List <int>();
                                    var lotteryPhase             = Resolver.LotteryHistoryService.Queryable().Include(x => x.Lottery).FirstOrDefault(x => x.TxHashId == tx).Lottery.Phase;
                                    var lotteryId   = Resolver.LotteryHistoryService.Queryable().Include(x => x.Lottery).FirstOrDefault(x => x.TxHashId == tx).Lottery.Id;
                                    var userAddress = Resolver.LotteryHistoryService.Queryable().Include(x => x.SysUser).FirstOrDefault(x => x.TxHashId == tx).SysUser.ETHHDWalletAddress;

                                    var lotteryHistories = Resolver.LotteryHistoryService.Queryable().Where(x => x.TxHashId == tx).ToList();
                                    foreach (var lotteryHistory in lotteryHistories)
                                    {
                                        var getTicketParamJson = string.Format(CPLConstant.GetTicketParamInJson, lotteryPhase, userAddress, lotteryHistory.TicketIndex);
                                        var ticketNumber       = _eToken.CallFunctionAsync(authentication.Result.Token, "getTicket", getTicketParamJson);
                                        ticketNumber.Wait();

                                        if (ticketNumber.Result.Status.Code == 0)
                                        {
                                            var tickerNumber = CorrectTicketNumber(ticketNumber.Result.Result);
                                            if (!Resolver.LotteryHistoryService.Queryable().Any(x => x.TicketNumber == tickerNumber && x.LotteryId == lotteryHistory.LotteryId))
                                            {
                                                lotteryHistory.TicketNumber = tickerNumber;
                                            }
                                            else
                                            {
                                                duplicateTicketIndexList.Add(lotteryHistory.TicketIndex);
                                            }
                                        }
                                        Resolver.LotteryHistoryService.Update(lotteryHistory);
                                    }

                                    if (duplicateTicketIndexList.Count > 0)
                                    {
                                        var randomParamJson    = string.Format(CPLConstant.RandomParamInJson, lotteryPhase, userAddress, string.Join(",", duplicateTicketIndexList));
                                        var ticketRandomResult = _eToken.CallTransactionAsync(authentication.Result.Token,
                                                                                              CPLConstant.OwnerAddress,
                                                                                              CPLConstant.OwnerPassword,
                                                                                              "random",
                                                                                              CPLConstant.GasPriceMultiplicator,
                                                                                              CPLConstant.DurationInSecond,
                                                                                              randomParamJson);

                                        ticketRandomResult.Wait();
                                        if (ticketRandomResult.Result.Status.Code == 0)
                                        {
                                            //Performance issue
                                            for (int i = 0; i < duplicateTicketIndexList.Count; i++)
                                            {
                                                var lotteryHistory = Resolver.LotteryHistoryService.Queryable().FirstOrDefault(x => x.TicketIndex == duplicateTicketIndexList[i] && x.LotteryId == lotteryId);
                                                lotteryHistory.TxHashId = ticketRandomResult.Result.TxId;
                                                Resolver.LotteryHistoryService.Update(lotteryHistory);
                                            }
                                        }
                                    }
                                }
                                else if (txStatus.Result.Receipt == false)
                                {
                                    int    lotteryPhase = Resolver.LotteryHistoryService.Queryable().Include(x => x.Lottery).FirstOrDefault(x => x.TxHashId == tx).Lottery.Phase;
                                    string userAddress  = Resolver.LotteryHistoryService.Queryable().Include(x => x.SysUser).FirstOrDefault(x => x.TxHashId == tx).SysUser.ETHHDWalletAddress;

                                    var ticketIndexList = Resolver.LotteryHistoryService.Queryable().Where(x => x.TxHashId == tx).Select(x => x.TicketIndex).ToList();
                                    var randomParamJson = string.Format(CPLConstant.RandomParamInJson, lotteryPhase, userAddress, string.Join(",", ticketIndexList));

                                    var ticketRandomResult = _eToken.CallTransactionAsync(authentication.Result.Token,
                                                                                          CPLConstant.OwnerAddress,
                                                                                          CPLConstant.OwnerPassword,
                                                                                          "random",
                                                                                          CPLConstant.GasPriceMultiplicator,
                                                                                          CPLConstant.DurationInSecond,
                                                                                          randomParamJson);

                                    ticketRandomResult.Wait();
                                    var lotteryId = Resolver.LotteryHistoryService.Queryable().FirstOrDefault(x => x.TxHashId == tx).LotteryId;

                                    if (ticketRandomResult.Result.Status.Code == 0)
                                    {
                                        for (int i = 0; i < ticketIndexList.Count; i++)
                                        {
                                            var lotteryHistoryRecord = Resolver.LotteryHistoryService.Queryable().FirstOrDefault(x => x.TicketIndex == ticketIndexList[i] && x.LotteryId == lotteryId);
                                            lotteryHistoryRecord.TxHashId = ticketRandomResult.Result.TxId;
                                            Resolver.LotteryHistoryService.Update(lotteryHistoryRecord);
                                        }
                                    }
                                }
                            }
                        }
                        Resolver.UnitOfWork.SaveChanges();
                        Thread.Sleep(RunningIntervalInMilliseconds);
                    }while (IsSmartContractServiceRunning);
                }
                else
                {
                    Utils.FileAppendThreadSafe(FileName, string.Format("Authentication failed at {0}. Reason {1}{2}", DateTime.Now, authentication.Result.Status.Text, Environment.NewLine));
                }
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    Utils.FileAppendThreadSafe(FileName, string.Format("Exception {0} at {1}{2}", ex.InnerException.Message, DateTime.Now, Environment.NewLine));
                }
                else
                {
                    Utils.FileAppendThreadSafe(FileName, string.Format("Exception {0} at {1}{2}", ex.Message, DateTime.Now, Environment.NewLine));
                }
            }

            Utils.FileAppendThreadSafe(FileName, string.Format("Check Tx thread STOPPED at {0}{1}", DateTime.Now, Environment.NewLine));
        }