Esempio n. 1
0
        public async Task Handle(string[] args, CallbackQuery query)
        {
            var user = await Db.Users
                       .SingleOrDefaultAsync(x => x.Id == query.From.Id);

            var address = await Db.UserAddresses
                          .SingleOrDefaultAsync(x => x.Id == args.GetInt(0));

            if (address is null || address.UserId != query.From.Id)
            {
                // TODO: Throw an error
                return;
            }

            var isDelegate = _repo.IsDelegate(address.Address);

            var lang  = user.Language;
            var title = _lang.Get(Res.AddressInfoTitle, lang, new { ua = address });

            var message = new MessageBuilder()
                          .AddLine(title)
                          .AddEmptyLine()
                          .WithHashTag("more_info")
                          .WithHashTag("rating")
                          .WithHashTag(address);

            var linkData = new { address = address.Address };

            if (isDelegate)
            {
                message.AddLine(_lang.Get(Res.AddressLinkTezosNode, lang, linkData));
                message.AddLine(_lang.Get(Res.AddressLinkTzKt, lang, linkData));
            }
            else
            {
                message.AddLine(_lang.Get(Res.AddressLinkBackingBad, lang, linkData));
            }

            var buttons = new InlineKeyboardMarkup(
                InlineKeyboardButton.WithCallbackData(
                    _lang.Get(Res.AddressTransactionListLink, user.Language, new {}),
                    $"address-transaction-list {address.Address} 1"
                    )
                );

            await Bot.SendText(
                query.From.Id,
                message.Build(!user.HideHashTags),
                ParseMode.Html,
                disableWebPagePreview : true,
                replyMarkup : buttons
                );
        }
Esempio n. 2
0
        public async Task Handle(string[] args, CallbackQuery query)
        {
            var page    = args.GetInt(1);
            var userId  = query.From.Id;
            var address = args[0];

            var userAddress = await Db.Set <UserAddress>()
                              .SingleOrDefaultAsync(x => x.Address == address && x.UserId == userId);

            if (userAddress == null)
            {
                throw new ArgumentException($"Address {args[0]} not found");
            }

            var user = await Db.Users.ByIdAsync(userAddress.UserId);

            var pager = TransactionsRepository.GetPage(userAddress.Address, page, takePerPage);

            var message = new MessageBuilder()
                          .AddLine(
                lang.Get(
                    Res.AddressTransactionListTitle,
                    user.Language,
                    new { addressName = userAddress.DisplayName() }
                    )
                )
                          .AddEmptyLine()
                          .WithHashTag("transaction_list")
                          .WithHashTag(userAddress);

            foreach (var tx in pager.Items)
            {
                var isReceive = tx.Target.address == address;
                var txAddress = isReceive ? tx.Sender : tx.Target;
                var data      = new TemplateData
                {
                    Hash        = tx.Hash,
                    Icon        = BuildIcon(tx),
                    Amount      = Utils.AmountToString(tx.Amount / TezosDecimals, null),
                    AddressName = txAddress.DisplayName(),
                    Address     = txAddress.address,
                    Timestamp   = tx.Timestamp.ToLocaleString(user.Language),
                    Explorer    = Explorer.FromId(user.Explorer),
                    IsReceive   = isReceive,
                };

                if (tx.Parameter?.entrypoint != null)
                {
                    if (tx.Parameter.entrypoint == "transfer")
                    {
                        if (tx.Parameter.value is JArray param)
                        {
                            var tokenId = param[0]["txs"]?[0]?["token_id"]?.Value <int>();
                            if (tokenId is null)
                            {
                                continue;
                            }
                            var token = await TokenService.GetToken(tx.Target.address, (int)tokenId);

                            var amount = param[0]["txs"]?[0]?["amount"]?.Value <decimal>();
                            var target = param[0]["txs"]?[0]?["to_"]?.Value <string>();

                            if (token != null && amount != null && target != null)
                            {
                                data.AddressName = target.ShortAddr();
                                data.Address     = target;
                                data.Amount      = Utils.AmountToString((decimal)amount, token);
                            }
                        }
                    }
                    else
                    {
                        continue;
                    }
                }


                message.AddLine(lang.Get(Res.AddressTransactionListItem, user.Language, data));
            }

            await Bot.EditText(
                query.From.Id,
                query.Message.MessageId,
                message.Build(!userAddress.User.HideHashTags),
                parseMode : ParseMode.Html,
                disableWebPagePreview : true,
                replyMarkup : BuildKeyboard()
                );

            string BuildIcon(Transaction transaction)
            {
                if (transaction.Sender.address == address)
                {
                    return("➖");
                }
                if (transaction.Target.address == address)
                {
                    return("➕");
                }
                return("?");
            }

            InlineKeyboardMarkup BuildKeyboard()
            {
                var callback = $"address-transaction-list {userAddress.Address}";

                if (pager.Page == 1 && !pager.HasNext)
                {
                    return(new InlineKeyboardMarkup(
                               InlineKeyboardButton.WithCallbackData(
                                   lang.Get(Res.AddressTransactionListRefresh, user.Language), $"{callback} 1")
                               ));
                }

                var older = InlineKeyboardButton.WithCallbackData(
                    lang.Get(Res.AddressTransactionListOlder, user.Language), $"{callback} {pager.Page + 1}");
                var newer = InlineKeyboardButton.WithCallbackData(
                    lang.Get(Res.AddressTransactionListNewer, user.Language), $"{callback} {pager.Page - 1}");
                var latest =
                    InlineKeyboardButton.WithCallbackData(lang.Get(Res.AddressTransactionListLatest, user.Language),
                                                          $"{callback} 1");

                if (pager.Page == 1)
                {
                    return(new InlineKeyboardMarkup(older));
                }
                if (pager.HasPrev && !pager.HasNext)
                {
                    return(new InlineKeyboardMarkup(new[] { newer, latest, }));
                }

                return(new InlineKeyboardMarkup(new[] { newer, latest, older, }));
            }
        }