static void Main(string[] args) { config = Config.LoadConfig(); //publishMessage();15,564,833 //var lastBlock = IconGateway.GetBlockByHeight(17938724); //ProcessBlock(lastBlock); //var icxValue = lastBlock.ConfirmedTransactionList[1].GetIcxValue(); //Get last block processed var lastBlockProcessed = FirebaseGateway.GetLastBlockProcessed(); lastProcessedHeight = lastBlockProcessed.height; Console.WriteLine("[MAIN] STARTING APPLICATION v1.2"); timer.Elapsed += Timer_Elapsed; timer.Interval = timerInterval; timer.Start(); timerWL.Elapsed += TimerWL_Elapsed; timerWL.Interval = timerWLInterval; timerWL.Start(); while (true) { System.Threading.Thread.Sleep(1000); } }
public static void ProcessDeviceToken(DeviceRegistration device, Address address, TokenRegistration token, decimal amount) { SendResponse sendResponse = null; try { if (string.IsNullOrEmpty(address.Name)) { sendResponse = FirebaseGateway.SendPush(device.token, token.Token, $"{token.Token} Deposit Received", $"You have received a deposit of {amount.ToString("0.##")} {token.Token}"); } else { sendResponse = FirebaseGateway.SendPush(device.token, token.Token, $"{token.Token} Deposit Received", $"{address.Name.ToUpper()} has received a deposit of {amount.ToString("0.##")} {token.Token}"); } //Now update firestore so we dont send the user duplicate messages //token.lastBalance = IconGateway.GetBalance(address, token).ToString(); //token.lastDepositPushSentDate = DateTime.UtcNow; } catch (Exception ex) { Console.WriteLine($"[MAIN] EXCEPTION processing Deposit 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); } } }
public bool IsAddressInToggleList(string address) { if (addressToggles == null) { addressToggles = FirebaseGateway.GetToggleAddresses("awsdeposits"); } return(addressToggles.Any(a => a == address)); }
static public void UpdateBlockStatus(BlockProcessedStatus blockStatus, ICXBlock block) { if (block != null) { blockStatus.processingDelaySeconds = Convert.ToInt32((DateTime.UtcNow.AddHours(8) - block.GetTimeStamp()).TotalSeconds); } blockStatus.processedTimestamp = Google.Cloud.Firestore.Timestamp.GetCurrentTimestamp(); FirebaseGateway.SetBlockProcessed(blockStatus); }
private static void Timer_Elapsed(object sender, ElapsedEventArgs e) { timer.Stop(); try { Console.WriteLine($"TIMER BLOCK START (Threads {Process.GetCurrentProcess().Threads.Count})"); var lastBlock = IconGateway.GetLastBlock(); if (lastBlock.Height % 10 == 0) { var incompleteBlocks = FirebaseGateway.GetAllIncompleteBlocks(); foreach (var block in incompleteBlocks) { ProcessBlockInThread(block.height); } } if (lastProcessedHeight == 0) { ProcessBlockInThread(lastBlock); } else if (lastProcessedHeight == lastBlock.Height) { //Do nothing } else if (lastProcessedHeight == lastBlock.Height - 1) { ProcessBlockInThread(lastBlock); } else { //We have missed some blocks, need to catch up for (long indexHeight = lastProcessedHeight + 1; indexHeight < lastBlock.Height; indexHeight++) { FirebaseGateway.SetBlockProcessed(new BlockProcessedStatus() { completed = false, height = indexHeight, retryAttempts = -1 }); ProcessBlockInThread(indexHeight); lastProcessedHeight = indexHeight; } ProcessBlockInThread(lastBlock); } lastProcessedHeight = lastBlock.Height; } catch (Exception ex) { //Do nothing for now, next timer hit will retry } finally { timer.Start(); } }
public static void ProcessDeviceToken(DeviceRegistration device, Address address, Token token) { SendResponse sendResponse = null; if (address.enablePushDeposits == true && token.isSelected == true) { try { var balance = IconGateway.GetBalance(address, token); if (string.IsNullOrEmpty(token.lastBalance)) { //Store current balance without sending a notification token.lastBalance = balance.ToString(); } else if (token.balanceAsDecimal < balance && balance - token.balanceAsDecimal > 0.005M) //Otherwise user gets a message of receiving 0 { decimal depositReceived = balance - token.balanceAsDecimal; if (string.IsNullOrEmpty(address.Name)) { sendResponse = FirebaseGateway.SendPush(device.token, token.token, $"{token.token} Deposit Received", $"You have received a deposit of {depositReceived.ToString("0.##")} {token.token}"); } else { sendResponse = FirebaseGateway.SendPush(device.token, token.token, $"{token.token} Deposit Received", $"{address.Name.ToUpper()} has received a deposit of {depositReceived.ToString("0.##")} {token.token}"); } //Now update firestore so we dont send the user duplicate messages token.lastBalance = balance.ToString(); token.lastDepositPushSentDate = DateTime.UtcNow; pushNotificationCount++; } else if (token.balanceAsDecimal > balance) { token.lastBalance = balance.ToString(); } } catch (Exception ex) { Console.WriteLine($"[MAIN] EXCEPTION processing Deposit 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); } } }
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); } } }
private static void Timer_Elapsed(object sender, ElapsedEventArgs e) { timer.Stop(); try { Console.WriteLine("[MAIN] TIMER ELAPSED, Checking all devices"); var allDevices = FirebaseGateway.AllDevices(); Console.WriteLine($"[MAIN] Processing all devices {allDevices.Count()}"); var count = 0; pushNotificationCount = 0; foreach (var device in allDevices) { Console.WriteLine($"[MAIN] Processing Device {count++}"); if (device.addresses_v2 != null) { foreach (var address in device.addresses_v2.AsEnumerator()) { Console.WriteLine($"[MAIN] Processing Address {address.Symbol} {address.address}"); ProcessDeviceAddress(device, address); if (address.tokens != null) { foreach (var token in address.tokens.AsEnumerator()) { Console.WriteLine($"[MAIN] Processing Token {token.token} {token.contractAddress}"); ProcessDeviceToken(device, address, token); } } } } FirebaseGateway.UpdateDevice(device); } Console.WriteLine($"[MAIN] Finished processing devices, sent {pushNotificationCount} push notifications"); } finally { timer.Start(); } }
public void ProcessDeviceAddress(DeviceRegistration device, Address address, decimal amount) { SendResponse sendResponse = null; if (!device.registrationDate.HasValue) { device.registrationDate = DateTime.UtcNow; } if (address.enablePushDeposits == true) { try { if (string.IsNullOrEmpty(address.Name)) { sendResponse = FirebaseGateway.SendPush(device.token, address.address, $"{address.Symbol} Deposit Received", $"You have received a deposit of {amount.ToString("0.##")} {address.Symbol}"); } else { sendResponse = FirebaseGateway.SendPush(device.token, address.address, $"{address.Symbol} Deposit Received", $"{address.Name.ToUpper()} has received a deposit of {amount.ToString("0.##")} {address.Symbol}"); } address.lastDepositPushSentDate = DateTime.UtcNow; //This will not always be required address.balance = IconGateway.GetBalance(address).ToString(); } catch (Exception ex) { Console.WriteLine($"[MAIN] EXCEPTION processing Deposit 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); } } }
public void ProcessAddress(ILambdaContext context, string address, decimal amount) { //if (IsAddressInToggleList(address)) //Check if address is in the toggles list { //context.Logger.LogLine($"Address is in toggle list {address}"); var devices = FirebaseGateway.GetDevicesByAddress(address); foreach (var device in devices) { foreach (var addressObj in device.addresses_v2.AsEnumerator()) { if (address == addressObj.address) { ProcessDeviceAddress(device, addressObj, amount); } } FirebaseGateway.UpdateDevice(device); } } }
public async Task ProcessRecordAsync(ILambdaContext context) { context.Logger.LogLine($"[{DateTime.Now.ToString()}] Processed record"); var devices = FirebaseGateway.GetDevicesForIScorePush(); foreach (var device in devices) { foreach (var address in device.addresses_v2.AsEnumerator()) { //if (IsAddressInToggleList(address.address)) //Check if address is in the toggles list { ProcessDeviceAddress(device, address); } } FirebaseGateway.UpdateDevice(device); } await Task.CompletedTask; }
public void ProcessAddress(ILambdaContext context, string address, string tokenAddress, decimal amount) { //if (IsAddressInToggleList(address)) //Check if address is in the toggles list { context.Logger.LogLine($"Address is in toggle list {address}"); var tokenRegistration = FirebaseGateway.GetTokenByAddress(tokenAddress); var devices = FirebaseGateway.GetDevicesByAddress(address); foreach (var device in devices) { foreach (var addressObj in device.addresses_v2.AsEnumerator()) { if (address == addressObj.address && addressObj.enablePushDeposits == true) { ProcessDeviceToken(device, addressObj, tokenRegistration, amount); } } FirebaseGateway.UpdateDevice(device); } } }
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); } }
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); } } }