private async Task <TokenProps> GetTokenFaucetProps(Message m) // verify that user is a part of the master chat { var text = m.Text?.Trim(); var args = text.Split(" "); var cliArgs = CLIHelper.GetNamedArguments(args); var user = m.From; var props = GetTokenPropsFromTextCommand(text); var baseName = props?.name?.ToUpper(); if (props.name.IsNullOrWhitespace() || props.name.Length < 2 || props.name.Length > 10) // validate token name { await _TBC.SendTextMessageAsync(text : $"Token name `${props.name ?? "undefined"}` is invalid.\nCheck description to see allowed parameters.", chatId : new ChatId(m.Chat.Id), replyToMessageId : m.MessageId, parseMode : Telegram.Bot.Types.Enums.ParseMode.Markdown); return(null); } if (props.index < 0 || props.index > 99999999) { props.index = (cliArgs.GetValueOrDefault("index")).ToIntOrDefault(BitcoinEx.GetCoinIndex(baseName)); } if (props.index < 0 || props.index > 99999999) // vlaidate coin index { await _TBC.SendTextMessageAsync(text : $"*index* flag `{props.index}` is invalid.\nCheck description to see allowed parameters.", chatId : new ChatId(m.Chat.Id), replyToMessageId : m.MessageId, parseMode : Telegram.Bot.Types.Enums.ParseMode.Markdown); return(null); } props.address = cliArgs.GetValueOrDefault("address") ?? args.FirstOrDefault(x => Bech32Ex.CanDecode(x)); if (Bech32Ex.TryDecode(props.address, out var hrp, out var addrBytes)) { props.prefix = hrp; } props.prefix = props.prefix ?? cliArgs.GetValueOrDefault("prefix"); if (props.prefix.IsNullOrWhitespace()) { await _TBC.SendTextMessageAsync(text : $"*prefix* ({props.prefix ?? "undefined"}) or *address* ({props.address ?? "undefined"}) flag was not defined.\nCheck description to see allowed parameters.", chatId : new ChatId(m.Chat.Id), replyToMessageId : m.MessageId, parseMode : Telegram.Bot.Types.Enums.ParseMode.Markdown); return(null); } if (!Bech32Ex.CanDecode(props.address)) { var ua = await GetUserAccount(user, createNewAcount : false); var acc = new AsmodatStandard.Cryptography.Cosmos.Account(props.prefix, (uint)props.index); acc.InitializeWithMnemonic(ua.GetSecret()); props.address = acc.CosmosAddress; } var client = new CosmosHub(lcd: props.lcd, timeoutSeconds: _cosmosHubClientTimeout); node_info nodeInfo; try { nodeInfo = await client.GetNodeInfo(); } catch { await _TBC.SendTextMessageAsync(text : $"*lcd* flag `{props.lcd ?? "undefined"}` is invalid or node can NOT be reached.\nCheck description to see allowed parameters.", chatId : new ChatId(m.Chat.Id), replyToMessageId : m.MessageId, parseMode : Telegram.Bot.Types.Enums.ParseMode.Markdown); return(null); } var network = cliArgs.GetValueOrDefault("network"); if (network.IsNullOrWhitespace()) { network = nodeInfo?.network; } if (network.IsNullOrWhitespace()) { network = props.network; } props.network = network; if (props.network.IsNullOrWhitespace() || props.network.Length <= 1) { await _TBC.SendTextMessageAsync(text : $"*network* flag `{props.network ?? "undefined"}` is invalid.\nCheck description to see allowed parameters.", chatId : new ChatId(m.Chat.Id), replyToMessageId : m.MessageId, parseMode : Telegram.Bot.Types.Enums.ParseMode.Markdown); return(null); } props.denom = cliArgs.GetValueOrDefault("denom") ?? props.denom ?? props.name.ToLower(); return(props); }
private async Task <bool> GetAccount(Message m) // verify that user is a part of the master chat { var text = m.Text?.Trim(); var user = m.From; var args = text.Split(" "); var cliArgs = CLIHelper.GetNamedArguments(args); var token = args.TryGetValueOrDefault(2)?.TrimStartSingle("$"); // $ATOM var baseName = token?.ToUpper(); if (token.IsNullOrEmpty()) { token = cliArgs.GetValueOrDefault("token"); } token = token?.ToLower(); var propsVar = Environment.GetEnvironmentVariable($"{token?.ToLower()}_PROPS"); TokenProps props; if (!propsVar.IsNullOrEmpty()) { props = propsVar.JsonDeserialize <TokenProps>(); } else { props = new TokenProps(); } props.name = token; var account = args.TryGetValueOrDefault(3)?.Trim()?.ToLower(); props.memo = cliArgs.GetValueOrDefault("memo") ?? ""; if (!props.memo.IsNullOrEmpty() && props.memo.Length > 256) { await _TBC.SendTextMessageAsync(text : $"*memo* can't have more then 256 characters, but was {props.memo.Length} characters.\nCheck description to see allowed parameters.", chatId : new ChatId(m.Chat.Id), replyToMessageId : m.MessageId, parseMode : Telegram.Bot.Types.Enums.ParseMode.Markdown); return(true); } if (props.index < 0 || props.index > 99999999) { props.index = (cliArgs.GetValueOrDefault("index")).ToIntOrDefault(BitcoinEx.GetCoinIndex(baseName)); } if (props.index < 0 || props.index > 99999999) // vlaidate coin index { await _TBC.SendTextMessageAsync(text : $"*index* flag `{props.index}` is invalid.\nCheck description to see allowed parameters.", chatId : new ChatId(m.Chat.Id), replyToMessageId : m.MessageId, parseMode : Telegram.Bot.Types.Enums.ParseMode.Markdown); return(true); } if (props.prefix.IsNullOrWhitespace()) { props.prefix = cliArgs.GetValueOrDefault("prefix"); } if (props.prefix.IsNullOrWhitespace()) // vlaidate address prefix { await _TBC.SendTextMessageAsync(text : $"*prefix* flag `{props.address ?? "undefined"}` is invalid.\nCheck description to see allowed parameters.", chatId : new ChatId(m.Chat.Id), replyToMessageId : m.MessageId, parseMode : Telegram.Bot.Types.Enums.ParseMode.Markdown); return(true); } var ua = await GetUserAccount(user.Id); var acc = new AsmodatStandard.Cryptography.Cosmos.Account(props.prefix, (uint)props.index); acc.InitializeWithMnemonic(ua.GetSecret()); var cosmosAdress = acc.CosmosAddress; if (account.EquailsAny(StringComparison.OrdinalIgnoreCase, "address", "account", "acc", "addr", "a", "deposit", "d", "adr", "adres", "adress", "addres")) { await _TBC.SendTextMessageAsync(chatId : m.Chat, $"{user.GetMarkDownUsername()} Public Address Is:\n`{cosmosAdress}`", replyToMessageId : m.MessageId, parseMode : Telegram.Bot.Types.Enums.ParseMode.Markdown); return(true); } else if (account.EquailsAny(StringComparison.OrdinalIgnoreCase, "balance")) { var lcd = cliArgs.GetValueOrDefault("lcd"); if (!lcd.IsNullOrWhitespace()) { props.lcd = lcd; } var client = new CosmosHub(lcd: props.lcd, timeoutSeconds: _cosmosHubClientTimeout); node_info nodeInfo; try { nodeInfo = await client.GetNodeInfo(); } catch { await _TBC.SendTextMessageAsync(text : $"*lcd* flag `{props.lcd ?? "undefined"}` is invalid or node can NOT be reached.\nCheck description to see allowed parameters.", chatId : new ChatId(m.Chat.Id), replyToMessageId : m.MessageId, parseMode : Telegram.Bot.Types.Enums.ParseMode.Markdown); return(true); } var network = cliArgs.GetValueOrDefault("network"); if (network.IsNullOrWhitespace()) { network = nodeInfo?.network; } if (network.IsNullOrWhitespace()) { network = props.network; } props.network = network; if (props.network.IsNullOrWhitespace() || props.network.Length <= 1) { await _TBC.SendTextMessageAsync(text : $"*network* flag `{props.network ?? "undefined"}` is invalid.\nCheck description to see allowed parameters.", chatId : new ChatId(m.Chat.Id), replyToMessageId : m.MessageId, parseMode : Telegram.Bot.Types.Enums.ParseMode.Markdown); return(true); } props.denom = cliArgs.GetValueOrDefault("denom") ?? props.denom ?? props.name.ToLower(); var fromAccountInfo = await client.GetAccount(account : cosmosAdress); var fromAccountBalance = fromAccountInfo?.coins?.FirstOrDefault(x => x?.denom?.ToLower() == props.denom.ToLower()); await _TBC.SendTextMessageAsync(chatId : m.Chat, $"Address: `{cosmosAdress}`\n" + $"Amount: `{fromAccountBalance?.amount ?? "0"} {fromAccountBalance?.denom ?? props.denom}`\n" + $"Network: `{props.network}`", replyToMessageId : m.MessageId, parseMode : Telegram.Bot.Types.Enums.ParseMode.Markdown); return(true); } else { await _TBC.SendTextMessageAsync(text : $"Command `{account ?? "undefined"}` is invalid.\nCheck description to see allowed parameters.", chatId : new ChatId(m.Chat.Id), replyToMessageId : m.MessageId, parseMode : Telegram.Bot.Types.Enums.ParseMode.Markdown); return(false); } }
public async Task TxHashDiscovery(Message msg) { var chat = msg?.Chat; var text = $"{msg?.Text} {msg?.ReplyToMessage?.Text} {msg?.ReplyToMessage?.ReplyToMessage?.Text}"; if (chat == null || text.IsNullOrWhitespace()) { return; } var arr = text.Split(); string hash = null; foreach (var s in arr) { hash = s.Trim("*", "`", "\"", "'", " ", "[", "]", "(", ")", "\n", "\r"); if (hash.Length > 32 && hash.IsHex()) { break; } } if (!hash.IsHex()) { return; } var props = GetTokenPropsFromTextCommand(text); var lcds = FHelper.GetAllLCDs(); if (!props.lcd.IsNullOrEmpty()) { lcds = lcds.Prepend(props.lcd).ToArray(); } string error = null, gas = null, height = null, timestamp = null, network = null, output = null, log = null; TxsResponse txs = null; if (lcds.IsNullOrEmpty()) { await _TBC.SendTextMessageAsync(chatId : chat, "*lcd* property was not found", replyToMessageId : msg.MessageId, parseMode : ParseMode.Markdown); return; } for (int i = 0; i < lcds.Length; i++) { error = null; var client = new CosmosHub(lcd: lcds[i], timeoutSeconds: _cosmosHubClientTimeout); var keystrokeTask = _TBC.SendChatActionAsync(chat, ChatAction.Typing); //simulate keystrokes try { var t1 = client.GetNodeInfo(); var t2 = client.GetTxs(hash); props.network = (await t1).network ?? props.network; txs = await t2; if ((txs.height).ToLongOrDefault(-1) > 0) { break; } } catch (Exception ex) { await keystrokeTask; error = $"\nError: `Tx hash was not found.`"; _logger.Log($"[DISCOVERY PROCESS ERROR] => Filed ('{msg?.Chat?.Id ?? 0}') to query tx hash: '{ex.JsonSerializeAsPrettyException(Newtonsoft.Json.Formatting.Indented)}'"); } } if (error.IsNullOrWhitespace() && !(txs?.error).IsNullOrWhitespace()) { error = $"\nError: `{txs.error}`"; } if (error.IsNullOrWhitespace()) { gas = $"\nGas Used: `{txs.gas_used}`\nGas Wanted: `{txs.gas_wanted}`"; height = $"\nHeight: `{txs.height}`"; timestamp = $"\nTimestamp: `{txs.timestamp}`"; network = $"\nNetwork: `{props.network}`"; log = $"\nLog: `{txs.raw_log}`"; var outputJson = (txs?.tx?.value).JsonSerialize(Newtonsoft.Json.Formatting.Indented); outputJson = outputJson.TrimOnTrigger('[', '\n', '\r', ' '); outputJson = outputJson.TrimOnTrigger(']', '\n', '\r', ' '); outputJson = outputJson.TrimOnTrigger('}', '\n', '\r', ' '); if (outputJson.Length > 8 && outputJson.Length < 3072) { output = $"\n\nOutput: `{outputJson}`"; } else if (outputJson.Length >= 3072) { output = $"\n\nOutput:\n`Too long to display 😢`"; } } await _TBC.SendTextMessageAsync(chatId : chat, $"Hash: `{hash}`\n" + height + gas + network + timestamp + log + error + output, replyToMessageId : msg.MessageId, parseMode : ParseMode.Markdown); }