Пример #1
0
        private async Task Balance(SlackUser slackUser, SlackIMSession imSession)
        {
            var account = await walletRepository.GetAccount(slackUser.Id);

            if (account == null)
            {
                // This could be improved with a fancy "do you want to create new account" - button which exists in the Slack API.
                await SlackConnector.SendMessage(imSession.Id, MessageConstants.NoAccount);

                return;
            }
            string message  = "";
            var    balances = await nxtConnector.GetBalances(account.NxtAccountRs, transferables.NxtTransferables);

            foreach (var balance in balances)
            {
                if (balance.Value > 0 || balance.Key == Nxt.Singleton)
                {
                    if (transferables.ContainsTransferable(balance.Key))
                    {
                        message += MessageConstants.CurrentBalance(balance.Value, balance.Key, false) + "\n";
                    }
                    else
                    {
                        message += MessageConstants.CurrentBalance(balance.Value, balance.Key, true) + "\n";
                    }
                }
            }
            await SlackConnector.SendMessage(imSession.Id, message.TrimEnd());
        }
Пример #2
0
        private async Task <NxtAccount> SendTipRecievedInstantMessage(SlackUser slackUser, string recipientUserId)
        {
            var recipientAccount = await CreateNxtAccount(recipientUserId);

            var imId = await SlackConnector.GetInstantMessageId(recipientUserId);

            await SlackConnector.SendMessage(imId, MessageConstants.TipRecieved(slackUser.Id));

            return(recipientAccount);
        }
Пример #3
0
        private async Task List(SlackIMSession imSession)
        {
            var message = MessageConstants.ListCommandHeader;

            foreach (var transferable in transferables.NxtTransferables)
            {
                message += MessageConstants.ListCommandForTransferable(transferable);
            }
            await SlackConnector.SendMessage(imSession.Id, message.TrimEnd(), false);
        }
Пример #4
0
        private async Task UpdateUserSetting(SlackUser slackUser, SlackIMSession imSession, Match match)
        {
            var key   = match.Groups["key"].Value.ToString().ToLower();
            var value = match.Groups["value"].Value.ToString().ToLower();

            if (key.Equals("reactiontip"))
            {
                var reactionTipValue = value.Equals("on");
                await walletRepository.SetUserReactionTipSetting(slackUser.Id, reactionTipValue);

                await SlackConnector.SendMessage(imSession.Id, MessageConstants.UserReactionSettingUpdated(reactionTipValue));
            }
        }
Пример #5
0
        private async Task SendTransferableSenderMessage(SlackUser slackUser, string recipient, NxtTransferable transferable, NxtAccount recipientAccount)
        {
            if (transferable != Nxt.Singleton)
            {
                var balance = await nxtConnector.GetBalance(Nxt.Singleton, recipientAccount.NxtAccountRs);

                if (balance < 1)
                {
                    var imId = await SlackConnector.GetInstantMessageId(slackUser.Id);

                    var message = MessageConstants.RecipientDoesNotHaveAnyNxtHint(recipientAccount.SlackId, transferable.Name);
                    await SlackConnector.SendMessage(imId, message);
                }
            }
        }
Пример #6
0
        private async Task Deposit(SlackUser slackUser, SlackIMSession imSession)
        {
            var account = await walletRepository.GetAccount(slackUser.Id);

            if (account == null)
            {
                account = await CreateNxtAccount(slackUser.Id);

                await SlackConnector.SendMessage(imSession.Id, MessageConstants.AccountCreated(account.NxtAccountRs));
            }
            else
            {
                await SlackConnector.SendMessage(imSession.Id, MessageConstants.DepositAddress(account.NxtAccountRs));
            }
        }
Пример #7
0
        private async Task <bool> VerifyParameters(NxtTransferable transferable, string unit, NxtAccount account, string slackSessionId, decimal amount, int recipientCount = 1)
        {
            if (transferable == null)
            {
                await SlackConnector.SendMessage(slackSessionId, MessageConstants.UnknownUnit(unit));

                return(false);
            }

            amount *= recipientCount;
            var nxtBalance = await nxtConnector.GetBalance(Nxt.Singleton, account.NxtAccountRs);

            if (transferable == Nxt.Singleton)
            {
                if (nxtBalance >= amount && nxtBalance < amount + 1 && recipientCount == 1)
                {
                    await SlackConnector.SendMessage(slackSessionId, MessageConstants.NotEnoughFundsNeedFee(nxtBalance));

                    return(false);
                }
                if (nxtBalance < amount + recipientCount)
                {
                    await SlackConnector.SendMessage(slackSessionId, MessageConstants.NotEnoughFunds(nxtBalance, transferable.Name));

                    return(false);
                }
            }
            else
            {
                if (nxtBalance < recipientCount)
                {
                    await SlackConnector.SendMessage(slackSessionId, MessageConstants.NotEnoughFundsNeedFee(nxtBalance, recipientCount));

                    return(false);
                }

                var balance = await nxtConnector.GetBalance(transferable, account.NxtAccountRs);

                if (balance < amount)
                {
                    await SlackConnector.SendMessage(slackSessionId, MessageConstants.NotEnoughFunds(balance, transferable.Name));

                    return(false);
                }
            }
            return(true);
        }
Пример #8
0
        private async Task Tip(SlackUser slackUser, SlackChannelSession channelSession, string recipient, List <string> slackUserIds, decimal amountToTip,
                               string unit, string comment, NxtTransferable transferable, NxtAccount account)
        {
            var isRecipientSlackUser = slackUserIds.Any();
            var recipientCount       = isRecipientSlackUser ? slackUserIds.Count : 1;

            if (slackUserIds.Contains(SlackConnector.SelfId))
            {
                await SlackConnector.SendMessage(channelSession.Id, MessageConstants.CantTipBotChannel);

                return;
            }
            if (slackUserIds.Contains(slackUser.Id))
            {
                await SlackConnector.SendMessage(channelSession.Id, MessageConstants.CantTipYourselfChannel);

                return;
            }
            if (!isRecipientSlackUser && !nxtConnector.IsValidAddressRs(recipient))
            {
                await SlackConnector.SendMessage(channelSession.Id, MessageConstants.InvalidAddress);

                return;
            }
            if (account == null)
            {
                await SlackConnector.SendMessage(channelSession.Id, MessageConstants.NoAccountChannel);

                return;
            }
            if (!string.IsNullOrEmpty(comment) && comment.Length > 512)
            {
                await SlackConnector.SendMessage(channelSession.Id, MessageConstants.CommentTooLongChannel);

                return;
            }
            if (!(await VerifyParameters(transferable, unit, account, channelSession.Id, amountToTip, recipientCount)))
            {
                return;
            }

            if (isRecipientSlackUser)
            {
                foreach (var slackUserId in slackUserIds)
                {
                    var recipientAccount = await walletRepository.GetAccount(slackUserId);

                    var recipientPublicKey = "";
                    if (recipientAccount == null)
                    {
                        recipientAccount = await SendTipRecievedInstantMessage(slackUser, slackUserId);

                        recipientPublicKey = recipientAccount.NxtPublicKey;
                    }

                    try
                    {
                        var recipientUserName = SlackConnector.GetUser(slackUserId).Name;
                        var txMessage         = MessageConstants.NxtTipTransactionMessage(slackUser.Name, recipientUserName, comment);
                        var txId = await nxtConnector.Transfer(account, recipientAccount.NxtAccountRs, transferable, amountToTip, txMessage, recipientPublicKey);

                        if (recipientCount == 1)
                        {
                            var reply = MessageConstants.TipSentChannel(slackUser.Id, slackUserId, amountToTip, transferable.Name, txId, comment);
                            await SlackConnector.SendMessage(channelSession.Id, reply, false);
                        }
                        else
                        {
                            var imId = await SlackConnector.GetInstantMessageId(slackUserId);

                            var reply = MessageConstants.TipSentDirectMessage(slackUser.Id, amountToTip, transferable.Name, txId);
                            await SlackConnector.SendMessage(imId, reply);
                        }
                        await SendTransferableRecipientMessage(slackUser, slackUserId, transferable, recipientAccount, amountToTip);
                        await SendTransferableSenderMessage(slackUser, slackUserId, transferable, recipientAccount);
                    }
                    catch (NxtException e)
                    {
                        logger.LogError(0, e, e.Message);
                        throw;
                    }
                }
                if (recipientCount > 1)
                {
                    var reply = MessageConstants.MultitipSentChannel(slackUser.Id, recipient, amountToTip, transferable.Name, comment);
                    await SlackConnector.SendMessage(channelSession.Id, reply);
                }
            }
            else
            {
                try
                {
                    var txMessage = MessageConstants.NxtTipTransactionMessage(slackUser.Name, "", comment);
                    var txId      = await nxtConnector.Transfer(account, recipient, transferable, amountToTip, txMessage, "");

                    var reply = MessageConstants.TipToAddressRsSentChannel(slackUser.Id, recipient, amountToTip, transferable.Name, txId, comment);
                    await SlackConnector.SendMessage(channelSession.Id, reply, false);
                }
                catch (NxtException e)
                {
                    logger.LogError(0, e, e.Message);
                    throw;
                }
            }
        }
Пример #9
0
        private async Task Withdraw(SlackUser slackUser, SlackIMSession imSession, Match match)
        {
            var address          = match.Groups[1].Value;
            var amountToWithdraw = decimal.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture);
            var unit             = string.IsNullOrEmpty(match.Groups[3].Value) ? Nxt.Singleton.Name : match.Groups[3].Value;
            var transferable     = transferables.GetTransferable(unit);
            var account          = await walletRepository.GetAccount(slackUser.Id);

            if (account == null)
            {
                await SlackConnector.SendMessage(imSession.Id, MessageConstants.NoAccount);

                return;
            }
            if (transferable == null && unit.IsNumeric())
            {
                var id = ulong.Parse(unit);
                try
                {
                    var asset = await nxtConnector.GetAsset(new TransferableConfig(id, "", "", new List <string>(), new List <TipReaction>()));

                    transferable = asset;
                }
                catch (Exception)
                {
                    try
                    {
                        var currency = await nxtConnector.GetCurrency(new TransferableConfig(id, "", "", new List <string>(), new List <TipReaction>()));

                        transferable = currency;
                    }
                    catch (Exception) { }
                }
            }
            if (!(await VerifyParameters(transferable, unit, account, imSession.Id, amountToWithdraw)))
            {
                return;
            }
            try
            {
                var txId = await nxtConnector.Transfer(account, address, transferable, amountToWithdraw, "withdraw from slack tipper");

                await SlackConnector.SendMessage(imSession.Id, MessageConstants.Withdraw(amountToWithdraw, transferable.Name, txId), false);
            }
            catch (ArgumentException e)
            {
                if (e.Message.Contains("not a valid reed solomon address"))
                {
                    await SlackConnector.SendMessage(imSession.Id, MessageConstants.InvalidAddress);
                }
                else
                {
                    logger.LogError(0, e, e.Message);
                    throw;
                }
            }
            catch (NxtException e)
            {
                logger.LogError(0, e, e.Message);
                throw;
            }
        }
Пример #10
0
 private async Task Help(SlackIMSession imSession)
 {
     await SlackConnector.SendMessage(imSession.Id, MessageConstants.GetHelpText(SlackConnector.SelfName));
 }