Skip to content

Commit

Permalink
Merge pull request #31 from bonedaddy/ndx-price
Browse files Browse the repository at this point in the history
Price And Uniswap Related Changes
  • Loading branch information
bonedaddy authored Jan 7, 2021
2 parents 2362eaf + f065ed3 commit f3ea369
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
40 changes: 40 additions & 0 deletions discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down Expand Up @@ -173,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 <pair> <window-in-days>",
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")
},
Expand Down Expand Up @@ -222,6 +233,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)
Expand Down
51 changes: 51 additions & 0 deletions discord/uniswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit f3ea369

Please sign in to comment.