Files
thuanle 94a11e8d68
All checks were successful
Build Docker Image / build (amd64) (push) Successful in 1m47s
add margin
2024-10-24 10:58:32 +07:00

66 lines
1.5 KiB
Go

package market
import (
"github.com/go-resty/resty/v2"
"github.com/rs/zerolog/log"
"me.thuanle/bbot/internal/utils/convertx"
)
// Structs to parse the JSON response
type MarginInterestResponse struct {
Code string `json:"code"`
Message string `json:"message"`
Data []Asset `json:"data"`
}
type Asset struct {
AssetName string `json:"assetName"`
Specs []Spec `json:"specs"`
}
type Spec struct {
VipLevel string `json:"vipLevel"`
DailyInterestRate string `json:"dailyInterestRate"`
}
func (ms *MarketData) GetMarginInterestRates() map[string]float64 {
data := fetchMarginInterestRate()
// Map to store the interest rates for the requested tokens
rates := make(map[string]float64)
// Iterate over the fetched data and match tokens with VIP Level 0 interest rates
for _, asset := range data {
for _, spec := range asset.Specs {
if spec.VipLevel == "0" {
rateDaily := convertx.String2Float64(spec.DailyInterestRate, 0)
rates[asset.AssetName] = rateDaily
}
}
}
return rates
}
func fetchMarginInterestRate() []Asset {
url := "https://www.binance.com/bapi/margin/v1/friendly/margin/vip/spec/list-all"
// Create a new Resty client
client := resty.New()
// Send the GET request to the API
var response MarginInterestResponse
resp, err := client.R().
SetResult(&response).
Get(url)
if err != nil {
log.Err(err).
Str("status", resp.Status()).
Msg("Failed to fetch margin interest rates")
return nil
}
return response.Data
}