Skip to content

Commit

Permalink
feat: add weekly swap overview consumers
Browse files Browse the repository at this point in the history
  • Loading branch information
CumpsD committed Aug 24, 2024
1 parent 29a4888 commit c1a38fc
Show file tree
Hide file tree
Showing 12 changed files with 481 additions and 2 deletions.
88 changes: 88 additions & 0 deletions chainflip-insights/Consumers/Discord/Discord-WeeklySwapOverview.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
namespace ChainflipInsights.Consumers.Discord
{
using System;
using System.Text;
using ChainflipInsights.Feeders.WeeklySwapOverview;
using global::Discord;
using Microsoft.Extensions.Logging;

public partial class DiscordConsumer
{
private void ProcessWeeklySwapOverviewInfo(WeeklySwapOverviewInfo weeklySwapOverviewInfo)
{
if (!_configuration.DiscordWeeklySwapOverviewEnabled.Value)
{
_logger.LogInformation(
"Weekly Swap Overview disabled for Discord. {StartDate} -> {EndDate}",
weeklySwapOverviewInfo.StartDate.ToString("yyyy-MM-dd"),
weeklySwapOverviewInfo.EndDate.ToString("yyyy-MM-dd"));

return;
}

if (_discordClient.ConnectionState != ConnectionState.Connected)
return;

try
{
_logger.LogInformation(
"Announcing Weekly Swap Overview for {StartDate} -> {EndDate} on Discord.",
weeklySwapOverviewInfo.StartDate.ToString("yyyy-MM-dd"),
weeklySwapOverviewInfo.EndDate.ToString("yyyy-MM-dd"));

var text = new StringBuilder();
text.AppendLine($"💵 Weekly Top Swaps for **{weeklySwapOverviewInfo.StartDate:yyyy-MM-dd}** -> **{weeklySwapOverviewInfo.EndDate:yyyy-MM-dd}** are in!");

var emojis = new[]
{
"🥇",
"🥈",
"🥉",
"🏅",
"🏅",
"🏅",
"🏅",
"🏅",
"🏅",
"🏅",
"🏅",
"🏅",
"🏅"
};

for (var i = 0; i < weeklySwapOverviewInfo.Swaps.Count; i++)
{
var swap = weeklySwapOverviewInfo.Swaps[i].Swap;
var brokerExists = _brokers.TryGetValue(swap.Broker ?? string.Empty, out var broker);

text.AppendLine(
$"{emojis[i]} " +
$"**{swap.DepositAmountFormatted} {swap.SourceAsset}** (*${swap.DepositValueUsdFormatted}*) → " +
$"**{swap.EgressAmountFormatted} {swap.DestinationAsset}** (*${swap.EgressValueUsdFormatted}*) " +
$"{(brokerExists ? $"@ **{broker}** " : string.Empty)}" +
$"// **[#{swap.Id}]({_configuration.ExplorerSwapsUrl}{swap.Id})**");
}

var infoChannel = (ITextChannel)_discordClient
.GetChannel(_configuration.DiscordSwapInfoChannelId.Value);

var message = infoChannel
.SendMessageAsync(
text.ToString(),
flags: MessageFlags.SuppressEmbeds)
.GetAwaiter()
.GetResult();

_logger.LogInformation(
"Announcing Weekly Swap Overview {StartDate} -> {EndDate} on Discord as Message {MessageId}",
weeklySwapOverviewInfo.StartDate.ToString("yyyy-MM-dd"),
weeklySwapOverviewInfo.EndDate.ToString("yyyy-MM-dd"),
message.Id);
}
catch (Exception e)
{
_logger.LogError(e, "Discord meh.");
}
}
}
}
3 changes: 3 additions & 0 deletions chainflip-insights/Consumers/Discord/DiscordConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ private ActionBlock<BroadcastInfo> BuildAnnouncer(
if (input.DailySwapOverviewInfo != null)
ProcessDailySwapOverviewInfo(input.DailySwapOverviewInfo);
if (input.WeeklySwapOverviewInfo != null)
ProcessWeeklySwapOverviewInfo(input.WeeklySwapOverviewInfo);
},
new ExecutionDataflowBlockOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ private ActionBlock<BroadcastInfo> BuildAnnouncer(
if (input.DailySwapOverviewInfo != null)
ProcessDailySwapOverviewInfo(input.DailySwapOverviewInfo, cancellationToken);
if (input.WeeklySwapOverviewInfo != null)
ProcessWeeklySwapOverviewInfo(input.WeeklySwapOverviewInfo, cancellationToken);
Task
.Delay(1500, cancellationToken)
.GetAwaiter()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
namespace ChainflipInsights.Consumers.FullTelegram
{
using System;
using System.Text;
using System.Threading;
using ChainflipInsights.Feeders.WeeklySwapOverview;
using global::Telegram.Bot;
using global::Telegram.Bot.Requests;
using global::Telegram.Bot.Types;
using global::Telegram.Bot.Types.Enums;
using Microsoft.Extensions.Logging;

public partial class FullTelegramConsumer
{
private void ProcessWeeklySwapOverviewInfo(
WeeklySwapOverviewInfo weeklySwapOverviewInfo,
CancellationToken cancellationToken)
{
if (!_configuration.DiscordWeeklySwapOverviewEnabled.Value)
{
_logger.LogInformation(
"Weekly Swap Overview disabled for Full Telegram. {StartDate} -> {EndDate}",
weeklySwapOverviewInfo.StartDate.ToString("yyyy-MM-dd"),
weeklySwapOverviewInfo.EndDate.ToString("yyyy-MM-dd"));

return;
}

try
{
_logger.LogInformation(
"Announcing Weekly Swap Overview for {StartDate} -> {EndDate} on Full Telegram.",
weeklySwapOverviewInfo.StartDate.ToString("yyyy-MM-dd"),
weeklySwapOverviewInfo.EndDate.ToString("yyyy-MM-dd"));

var text = new StringBuilder();
text.AppendLine($"💵 Weekly Top Swaps for **{weeklySwapOverviewInfo.StartDate:yyyy-MM-dd}** -> **{weeklySwapOverviewInfo.EndDate:yyyy-MM-dd}** are in!");

var emojis = new[]
{
"🥇",
"🥈",
"🥉",
"🏅",
"🏅",
"🏅",
"🏅",
"🏅",
"🏅",
"🏅",
"🏅",
"🏅",
"🏅"
};

for (var i = 0; i < weeklySwapOverviewInfo.Swaps.Count; i++)
{
var swap = weeklySwapOverviewInfo.Swaps[i].Swap;

text.AppendLine(
$"{emojis[i]} " +
$"**{swap.DepositAmountFormatted} {swap.SourceAsset}** → " +
$"**{swap.EgressAmountFormatted} {swap.DestinationAsset}** " +
$"// **[#{swap.Id}]({_configuration.ExplorerSwapsUrl}{swap.Id})**");
}

var message = _telegramClient
.SendMessageAsync(
new SendMessageRequest
{
ChatId = new ChatId(_configuration.TelegramInfoChannelId.Value),
Text = text.ToString(),
ParseMode = ParseMode.Markdown,
DisableNotification = true,
LinkPreviewOptions = new LinkPreviewOptions
{
IsDisabled = true
},
ReplyParameters = new ReplyParameters
{
AllowSendingWithoutReply = true,
}
},
cancellationToken)
.GetAwaiter()
.GetResult();

_logger.LogInformation(
"Announcing Weekly Swap Overview {StartDate} -> {EndDate} on Full Telegram as Message {MessageId}",
weeklySwapOverviewInfo.StartDate.ToString("yyyy-MM-dd"),
weeklySwapOverviewInfo.EndDate.ToString("yyyy-MM-dd"),
message.MessageId);
}
catch (Exception e)
{
_logger.LogError(e, "Full Telegram meh.");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ namespace ChainflipInsights.Consumers.Mastodon
{
using System;
using System.Text;
using ChainflipInsights.Feeders.BrokerOverview;
using ChainflipInsights.Feeders.DailySwapOverview;
using Mastonet;
using Microsoft.Extensions.Logging;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
namespace ChainflipInsights.Consumers.Mastodon
{
using System;
using System.Text;
using ChainflipInsights.Feeders.WeeklySwapOverview;
using Mastonet;
using Microsoft.Extensions.Logging;

public partial class MastodonConsumer
{
private void ProcessWeeklySwapOverviewInfo(WeeklySwapOverviewInfo weeklySwapOverviewInfo)
{
if (!_configuration.MastodonWeeklySwapOverviewEnabled.Value)
{
_logger.LogInformation(
"Weekly Swap Overview disabled for Mastodon. {StartDate} -> {EndDate}",
weeklySwapOverviewInfo.StartDate.ToString("yyyy-MM-dd"),
weeklySwapOverviewInfo.EndDate.ToString("yyyy-MM-dd"));

return;
}

try
{
_logger.LogInformation(
"Announcing Weekly Swap Overview for {StartDate} -> {EndDate} on Mastodon.",
weeklySwapOverviewInfo.StartDate.ToString("yyyy-MM-dd"),
weeklySwapOverviewInfo.EndDate.ToString("yyyy-MM-dd"));

var text = new StringBuilder();
text.AppendLine($"💵 Weekly Top Swaps for {weeklySwapOverviewInfo.StartDate:yyyy-MM-dd} -> {weeklySwapOverviewInfo.EndDate:yyyy-MM-dd} are in!");

var emojis = new[]
{
"🥇",
"🥈",
"🥉",
"🏅",
"🏅",
"🏅",
"🏅",
"🏅",
"🏅",
"🏅",
"🏅",
"🏅",
"🏅"
};

for (var i = 0; i < weeklySwapOverviewInfo.Swaps.Count; i++)
{
var swap = weeklySwapOverviewInfo.Swaps[i].Swap;
var brokerExists = _brokers.TryGetValue(swap.Broker ?? string.Empty, out var broker);

text.AppendLine(
$"{emojis[i]} " +
$"{swap.DepositAmountFormatted} #{swap.SourceAsset} (${swap.DepositValueUsdFormatted}) → " +
$"{swap.EgressAmountFormatted} #{swap.DestinationAsset} (${swap.EgressValueUsdFormatted}) " +
$"{(brokerExists ? $"@ {broker}" : string.Empty)}");
}

text.AppendLine("#chainflip #flip");

var status = _mastodonClient
.PublishStatus(
text.ToString(),
Visibility.Public)
.GetAwaiter()
.GetResult();

_logger.LogInformation(
"Announcing Weekly Swap Overview {StartDate} -> {EndDate} on Mastodon as Message {MessageId}",
weeklySwapOverviewInfo.StartDate.ToString("yyyy-MM-dd"),
weeklySwapOverviewInfo.EndDate.ToString("yyyy-MM-dd"),
status.Id);
}
catch (Exception e)
{
_logger.LogError(e, "Mastodon meh.");
}
}
}
}
3 changes: 3 additions & 0 deletions chainflip-insights/Consumers/Mastodon/MastodonConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ private ActionBlock<BroadcastInfo> BuildAnnouncer(
if (input.DailySwapOverviewInfo != null)
ProcessDailySwapOverviewInfo(input.DailySwapOverviewInfo);
if (input.WeeklySwapOverviewInfo != null)
ProcessWeeklySwapOverviewInfo(input.WeeklySwapOverviewInfo);
Task
.Delay(1500, cancellationToken)
.GetAwaiter()
Expand Down
Loading

0 comments on commit c1a38fc

Please sign in to comment.