Git Product home page Git Product logo

bfldb's Introduction

Hi! Welcome to my Github!

My name is Artur and I am backend engineer by day, web3 enthusiast by night.

Want to get in touch? email me, message me on twitter, or on discord at rtuna.


My github stats

bfldb's People

Contributors

rtunazzz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

bfldb's Issues

Add a new User.SetDelay method

What

Add a SetDelay method to the User struct, that will change the monitoring delay used for the user.

bfldb/user.go

Lines 12 to 21 in 399419e

type User struct {
UID string // Encrypted User ID
id string // identified used in logging
log *log.Logger // Logger
pHashes map[string]Position // map of positions user is currently in
d time.Duration // duration between requests updating current positions
c *http.Client // http client
headers map[string]string // headers
isFirst bool // indicating first fetch
}

Why

So we can change the monitoring delay without having to reinitialize User

Add a new User.SetHeaders method

What

Add a SetHeaders method to the User struct

bfldb/user.go

Lines 12 to 21 in 399419e

type User struct {
UID string // Encrypted User ID
id string // identified used in logging
log *log.Logger // Logger
pHashes map[string]Position // map of positions user is currently in
d time.Duration // duration between requests updating current positions
c *http.Client // http client
headers map[string]string // headers
isFirst bool // indicating first fetch
}

Why

So we can change the user's headers inbetween requests, without having to stop monitoring and reconstruct the User instance.

rate limited?

error has occurred: failed to fetch positions: 403 Forbidden
error has occurred: failed to fetch positions: 403 Forbidden

have you run into rate limits before? ive subscribed to 4 and routed to my discord

package main

import (
	"context"
	"fmt"
	"github.com/bwmarrin/discordgo"
	"github.com/rtunazzz/bfldb"
	"time"
)

func main() {
	// Replace YOUR_TOKEN_HERE with your Discord bot's token
	dg, err := discordgo.New("Bot " + "")
	if err != nil {
		fmt.Println("error creating Discord session:", err)
		return
	}
	defer dg.Close()

	// Replace CHANNEL_ID_HERE with the ID of the Discord channel where you want to send the message
	channelID := ""

	// Open a websocket connection to Discord and begin listening.
	err = dg.Open()
	if err != nil {
		fmt.Println("Error opening connection: ", err)
		return
	}

	// You can find this UID (encryptedUid) in the end of a leaderboard profile URL. For example:
	// https://www.binance.com/en/futures-activity/leaderboard/user?encryptedUid=47E6D002EBB1173967A6561F72B9395C
	u := bfldb.NewUser("F3050271F9F3463B0EED70B412D6436F")
	cp, ce := u.SubscribePositions(context.Background())

	for {
		select {
		case position := <-cp:
			now := time.Now()
			timestamp := now.Format("2006-01-02 15:04:05")
			embed := &discordgo.MessageEmbed{
				Fields: []*discordgo.MessageEmbedField{
					&discordgo.MessageEmbedField{
						Name:   "Trader",
						Value:  "kaikai2022\n",
						Inline: false,
					},
					&discordgo.MessageEmbedField{
						Name:   "Ticker",
						Value:  position.Ticker,
						Inline: true,
					},
					&discordgo.MessageEmbedField{
						Name:   "L/S",
						Value:  position.Direction.String(),
						Inline: true,
					},
					&discordgo.MessageEmbedField{
						Name:   "Type",
						Value:  position.Type.String(),
						Inline: true,
					},
					&discordgo.MessageEmbedField{
						Name:   "EntryPrice",
						Value:  fmt.Sprintf("%f", position.EntryPrice),
						Inline: true,
					},
					&discordgo.MessageEmbedField{
						Name:   "Leverage",
						Value:  fmt.Sprintf("%d", position.Leverage),
						Inline: true,
					},
					&discordgo.MessageEmbedField{
						Name:   "PnL",
						Value:  fmt.Sprintf("%f", position.Pnl),
						Inline: true,
					},
					&discordgo.MessageEmbedField{
						Name:   "Timestamp",
						Value:  timestamp,
						Inline: true,
					},
				},
			}
			embed.Color = 10181046
			_, _ = dg.ChannelMessageSendEmbed(channelID, embed)
		case err := <-ce:
			fmt.Println("error has occurred:", err)
			break
		case <-time.After(time.Second * 100):
			fmt.Println("Bot is now running. Press CTRL-C to exit.")
		}
	}
}

Replace ticker with sleep

What

Replace the time.NewTicker and it's relevant usage with time.Sleep

bfldb/logic.go

Lines 17 to 44 in 399419e

t := time.NewTicker(u.d)
defer t.Stop()
defer close(cp)
defer close(ce)
for {
select {
case <-ctx.Done():
return
case <-t.C:
// u.log.Printf("[%s] Checking for new positions\n", u.id)
res, err := u.GetOtherPosition(ctx)
if err != nil {
ce <- fmt.Errorf("failed to fetch positions: %w", err)
continue
}
if !res.Success {
ce <- fmt.Errorf("failed to fetch positions, bad response message: %v", res.Message)
continue
}
// u.log.Printf("[%s] Updating %d positions\n", u.id, len(res.Data.OtherPositionRetList))
u.handlePositions(res.Data.OtherPositionRetList, cp, ce)
}
}

Why

So we can add a SetDelay method on the User struct and have a configurable delay without the need of stopping monitoring

multiple subs

in your subscriber example how would someone subscribe to multiple IDs in the same instance, is that even possible?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.