Exemple #1
0
        public static void ProcessDeviceAddress(DeviceRegistration device, Address address)
        {
            SendResponse sendResponse = null;

            if (!device.registrationDate.HasValue)
            {
                device.registrationDate = DateTime.UtcNow;
            }

            if (device.enablePushIScoreChange == true)
            {
                try
                {
                    var totalRewards = IconGateway.GetAvailableRewards(address);
                    if (totalRewards > 0)
                    {
                        if (string.IsNullOrEmpty(address.Name))
                        {
                            sendResponse = FirebaseGateway.SendPush(device.token, address.address, $"{address.Symbol} Rewards Available", $"Congratulations! your reward of {totalRewards.ToString("0.##")} {address.Symbol} is ready to be claimed");
                        }
                        else
                        {
                            sendResponse = FirebaseGateway.SendPush(device.token, address.address, $"{address.Symbol} Rewards Available", $"Congratulations! your reward of {totalRewards.ToString("0.##")} {address.Symbol} is ready to be claimed from {address.Name.ToUpper()}");
                        }

                        //Now update firestore so we dont send the user duplicate messages
                        address.availableRewards       = totalRewards.ToString();
                        address.lastIScorePushSentDate = DateTime.UtcNow;
                    }
                    else if (address.availableRewardsAsDecimal > totalRewards)
                    {
                        address.availableRewards = totalRewards.ToString();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"[MAIN] EXCEPTION processing IScore check {ex.Message}");
                }
            }

            if (sendResponse != null && sendResponse.failure > 0)
            {
                if (sendResponse.results.Any(a => a.error == "NotRegistered"))
                {
                    //This token has become stale, need to remove it from firestore
                    FirebaseGateway.DeleteDevice(device);
                }
            }
        }
Exemple #2
0
        private static void ProcessBlock(ICXBlock block, long icxBlockHeight = -1)
        {
            BlockProcessedStatus blockStatus = null;
            ICXBlock             icxBlock    = block;
            long blockHeight = 0;

            try
            {
                //Get block height to process

                if (icxBlockHeight > 0)
                {
                    blockHeight = icxBlockHeight;
                }
                else
                {
                    blockHeight = block.Height;
                }

                //Get block info from Firebase
                Console.WriteLine($"Starting to Process Block {blockHeight} in thread {System.Threading.Thread.CurrentThread.ManagedThreadId}");
                blockStatus = FirebaseGateway.GetBlockProcessed(blockHeight);
                if (blockStatus == null)
                {
                    blockStatus = new BlockProcessedStatus()
                    {
                        height          = blockHeight,
                        eventsPublished = 0,
                        retryAttempts   = 0
                    };
                }
                else
                {
                    if (blockStatus.completed == true)
                    {
                        Console.WriteLine($"Block {blockHeight} is already completed, exiting");
                        return;
                    }

                    blockStatus.retryAttempts++;
                }

                //Get the ICX Block if it not provided
                if (icxBlockHeight > 0)
                {
                    icxBlock = IconGateway.GetBlockByHeight(icxBlockHeight);
                }

                blockStatus.blockTimestamp = Google.Cloud.Firestore.Timestamp.FromDateTimeOffset(icxBlock.GetTimeStamp().AddHours(-8));

                // Check ISCORE
                var rewards = IconGateway.GetAvailableRewards("hxc147caa988765c13eaa6ca43400c27a0372b9436");
                if (rewards < oldRewards || oldRewards == -1)
                {
                    oldRewards = rewards;
                }
                else if (rewards > oldRewards)
                {
                    blockStatus.eventsPublished++;
                    Console.WriteLine($"ISCORE CHANGED, Pushing event update");
                    publishIScoreChange();
                    oldRewards = rewards;
                }

                var blockTimeStamp = DateTimeOffset.FromUnixTimeMilliseconds(icxBlock.TimeStamp / 1000).AddHours(8);
                var oldNess        = Convert.ToInt32((DateTime.UtcNow.AddHours(8) - icxBlock.GetTimeStamp()).TotalSeconds);

                //What do we do
                Console.WriteLine($"block timestamp {icxBlock.GetTimeStamp()}, oldNess {oldNess} seconds, HEIGHT {icxBlock.Height}, transactions {icxBlock.ConfirmedTransactionList.Count}, eventCount {eventCounter}, retries {blockStatus.retryAttempts} in thread {System.Threading.Thread.CurrentThread.ManagedThreadId}");

                foreach (var tx in icxBlock.ConfirmedTransactionList)
                {
                    var txStr = JsonConvert.SerializeObject(tx);

                    if (tx.From != null && tx.From.StartsWith("hx") && tx.To != null && tx.To.StartsWith("hx"))
                    {
                        blockStatus.eventsPublished++;
                        publishTransferMessage(tx);
                    }

                    if (tx.From != null && tx.From.StartsWith("hx") && tx.To != null && tx.To.StartsWith("cx"))
                    {
                        blockStatus.eventsPublished++;
                        var txResult = IconGateway.GetTransactionResult(tx.TxHash);
                        tx.TxResultDetails = txResult;

                        publishContractMethodMessage(tx);
                    }
                }

                blockStatus.completed = true;
                UpdateBlockStatus(blockStatus, icxBlock);
                Console.WriteLine($"COMPLETED {icxBlock.Height} in thread {System.Threading.Thread.CurrentThread.ManagedThreadId}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"EXCEPTION : Error on block {blockHeight} :  {ex.Message} in thread {System.Threading.Thread.CurrentThread.ManagedThreadId}");

                blockStatus.completed        = false;
                blockStatus.lastErrorMessage = ex.Message;
                UpdateBlockStatus(blockStatus, icxBlock);
            }
        }
Exemple #3
0
        public static void ProcessDeviceAddress(DeviceRegistration device, Address address)
        {
            SendResponse sendResponse = null;

            if (!device.registrationDate.HasValue)
            {
                device.registrationDate = DateTime.UtcNow;
            }

            if (device.enablePushIScoreChange == true)
            {
                try
                {
                    var totalRewards = IconGateway.GetAvailableRewards(address);
                    if (address.availableRewardsAsDecimal < totalRewards)
                    {
                        decimal awardedICX = totalRewards - address.availableRewardsAsDecimal;
                        if (string.IsNullOrEmpty(address.Name))
                        {
                            sendResponse = FirebaseGateway.SendPush(device.token, address.address, $"{address.Symbol} Rewards Available", $"Congratulations! your reward of {totalRewards.ToString("0.##")} {address.Symbol} is ready to be claimed");
                        }
                        else
                        {
                            sendResponse = FirebaseGateway.SendPush(device.token, address.address, $"{address.Symbol} Rewards Available", $"Congratulations! your reward of {totalRewards.ToString("0.##")} {address.Symbol} is ready to be claimed from {address.Name.ToUpper()}");
                        }

                        //Now update firestore so we dont send the user duplicate messages
                        address.availableRewards       = totalRewards.ToString();
                        address.lastIScorePushSentDate = DateTime.UtcNow;
                        pushNotificationCount++;
                    }
                    else if (address.availableRewardsAsDecimal > totalRewards)
                    {
                        address.availableRewards = totalRewards.ToString();
                    }
                } catch (Exception ex)
                {
                    Console.WriteLine($"[MAIN] EXCEPTION processing IScore check {ex.Message}");
                }
            }

            if (address.enablePushDeposits == true)
            {
                var addressToggles = FirebaseGateway.GetToggleAddresses("awsdeposits");
                if (!addressToggles.Any(a => a == address.address)) //Check if address is NOT in the toggles list
                {
                    try
                    {
                        var balance = IconGateway.GetBalance(address);
                        if (string.IsNullOrEmpty(address.balance))
                        {
                            //Store current balance without sending a notification
                            address.balance = balance.ToString();
                        }
                        else if (address.balanceAsDecimal < balance && balance - address.balanceAsDecimal > 0.005M) //Otherwise user gets a message of receiving 0
                        {
                            decimal depositReceived = balance - address.balanceAsDecimal;
                            if (string.IsNullOrEmpty(address.Name))
                            {
                                sendResponse = FirebaseGateway.SendPush(device.token, address.address, $"{address.Symbol} Deposit Received", $"You have received a deposit of {depositReceived.ToString("0.##")} {address.Symbol}");
                            }
                            else
                            {
                                sendResponse = FirebaseGateway.SendPush(device.token, address.address, $"{address.Symbol} Deposit Received", $"{address.Name.ToUpper()} has received a deposit of {depositReceived.ToString("0.##")} {address.Symbol}");
                            }

                            //Now update firestore so we dont send the user duplicate messages
                            address.balance = balance.ToString();
                            address.lastDepositPushSentDate = DateTime.UtcNow;
                            pushNotificationCount++;
                        }
                        else if (address.balanceAsDecimal > balance)
                        {
                            address.balance = balance.ToString();
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"[MAIN] EXCEPTION processing Deposit check {ex.Message}");
                    }
                }
                else
                {
                }
            }

            if (!string.IsNullOrEmpty(device.enablePushProductivityDrop) && device.enablePushProductivityDrop != "disabled" && AllPReps != null)
            {
                try
                {
                    lock (AllPReps)
                    {
                        var prodDrop = decimal.Parse(device.enablePushProductivityDrop);
                        var pReps    = IconGateway.GetDelegatedPReps(address);
                        if (pReps != null && pReps.Delegations != null && pReps.Delegations.Length > 0)
                        {
                            foreach (var prep in pReps.Delegations)
                            {
                                var findPrep = AllPReps.Preps.SingleOrDefault(p => p.Address == prep.Address);
                                if (findPrep != null && findPrep.Productivity < prodDrop && findPrep.Grade == 0)
                                {
                                    if (device.lastProductivityPushSentDate == null || (DateTime.UtcNow - device.lastProductivityPushSentDate).Value.Days > 1)
                                    {
                                        sendResponse = FirebaseGateway.SendPush(device.token, address.address, "P-Rep Productivity Warning", $"Warning! Your delegated P-Rep {findPrep.Name}'s productivity has dropped to {findPrep.Productivity.ToString("0.##")}%");
                                        //Now update firestore so we dont send the user duplicate messages
                                        device.lastProductivityPushSentDate = DateTime.UtcNow;
                                        pushNotificationCount++;
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"[MAIN] EXCEPTION processing ProductivityDrop check {ex.Message}");
                }
            }

            if (sendResponse != null && sendResponse.failure > 0)
            {
                if (sendResponse.results.Any(a => a.error == "NotRegistered"))
                {
                    //This token has become stale, need to remove it from firestore
                    FirebaseGateway.DeleteDevice(device);
                }
            }
        }