From ebcc4eb927d9bfdc855e591c0e34e057017928e3 Mon Sep 17 00:00:00 2001 From: bonedaddy Date: Tue, 5 Jan 2021 23:12:43 -0800 Subject: [PATCH 1/2] record ndx price in main bot nickname --- discord/discord.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/discord/discord.go b/discord/discord.go index 8489291..5ea28b9 100644 --- a/discord/discord.go +++ b/discord/discord.go @@ -55,6 +55,9 @@ func NewClient(ctx context.Context, cfg *Config, bc *bclient.Client, db *db.Data log.Println("failed to udpate streaming status: ", err) } + // updates the main bot's nickname to reflect ndx price + ndxPriceWatchRoutine(ctx, dg, wg, db) + // declare the base router router := dgc.Create(&dgc.Router{ Prefixes: []string{"!ndx"}, @@ -222,6 +225,35 @@ func (c *Client) Close() error { return c.s.Close() } +func ndxPriceWatchRoutine(ctx context.Context, bot *discordgo.Session, wg *sync.WaitGroup, db *db.Database) { + wg.Add(1) + go func() { + defer wg.Done() + ticker := time.NewTicker(time.Second * 10) + defer ticker.Stop() + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + price, err := db.LastPrice("ndx") + if err != nil { + log.Println("failed to get last ndx price: ", err) + continue + } + guilds, err := bot.UserGuilds(0, "", "") + if err != nil { + log.Println("failed to get user guilds error: ", err) + continue + } + for _, guild := range guilds { + bot.GuildMemberNickname(guild.ID, "@me", fmt.Sprintf("NDXBot: $%0.2f", price)) + } + } + } + }() +} + func launchWatchers(ctx context.Context, wg *sync.WaitGroup, cfg *Config, bc *bclient.Client, db *db.Database) error { for _, watcher := range cfg.Watchers { watcherBot, err := discordgo.New("Bot " + watcher.DiscordToken) From f065ed315abd3d7af4cd30c041a86ca448ab8756 Mon Sep 17 00:00:00 2001 From: bonedaddy Date: Wed, 6 Jan 2021 00:49:25 -0800 Subject: [PATCH 2/2] discord: add uniswap command to return price change percentage --- discord/discord.go | 8 ++++++++ discord/uniswap.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/discord/discord.go b/discord/discord.go index 5ea28b9..4a5052f 100644 --- a/discord/discord.go +++ b/discord/discord.go @@ -176,7 +176,15 @@ func NewClient(ctx context.Context, cfg *Config, bc *bclient.Client, db *db.Data Example: " uniswap exchange-rate defi5-dai (returns the value of defi5 in terms of dai)", Handler: client.uniswapExchangeRateHandler, }, + &dgc.Command{ + Name: "price-change", + Description: "returns the percent price change for a given pair. currently only supports windows in day granularity. the only supported pairs are eth-dai, defi5-dai, cc10-dai, ndx-dai", + Usage: " uniswap price-change ", + Example: " uniswap price-change defi5-dai 10 (returns the price change over the last 10 days for defi5)", + Handler: client.uniswapPercentChangeHandler, + }, }, + Handler: func(ctx *dgc.Ctx) { ctx.RespondText("invalid invocation please run a specific subcommand") }, diff --git a/discord/uniswap.go b/discord/uniswap.go index 1bac339..551bbbc 100644 --- a/discord/uniswap.go +++ b/discord/uniswap.go @@ -86,3 +86,54 @@ func (c *Client) uniswapExchangeRateHandler(ctx *dgc.Ctx) { return } } + +func (c *Client) uniswapPercentChangeHandler(ctx *dgc.Ctx) { + arguments := ctx.Arguments + pair := arguments.Get(0).Raw() + window, err := arguments.Get(1).AsInt() + if err != nil { + ctx.RespondText("window argument is not a number") + return + } + // valid the allowed currencies + switch strings.ToLower(pair) { + case "defi5-dai": + price, err := c.db.PriceChangeInRange("defi5", window) + if err != nil { + ctx.RespondText("failed to get price") + log.Println("defi5 dai price change fetch failed: ", err) + return + } + ctx.RespondText(fmt.Sprintf("DEFI5-DAI price has changed %0.2f%% over the last %v days", price*100, window)) + return + case "cc10-dai": + price, err := c.db.PriceChangeInRange("cc10", window) + if err != nil { + ctx.RespondText("failed to get price") + log.Println("cc10 dai price change fetch failed: ", err) + return + } + ctx.RespondText(fmt.Sprintf("CC10-DAI price has changed %0.2f%% over the last %v days", price*100, window)) + return + case "eth-dai": + price, err := c.db.PriceChangeInRange("eth", window) + if err != nil { + ctx.RespondText("failed to get price") + log.Println("cc10 dai price change failed: ", err) + return + } + ctx.RespondText(fmt.Sprintf("ETH-DAI price has changed %0.2f%% over the last %v days", price*100, window)) + return + case "ndx-dai": + price, err := c.db.PriceChangeInRange("ndx", window) + if err != nil { + ctx.RespondText("failed to get price") + log.Println("ndx dai price change failed: ", err) + return + } + ctx.RespondText(fmt.Sprintf("NDX-DAI price has changed %0.2f%% over the last %v days", price*100, window)) + default: + ctx.RespondText("invalid currency requested must be one of: defi5-dai, cc10-dai, eth-dai, ndx-dai") + return + } +}