Skip to content

Commit

Permalink
Merge pull request #1874 from c9s/c9s/binance/margin-next-hourly-inte…
Browse files Browse the repository at this point in the history
…rest-rate

FEATURE: [binance] margin next hourly interest rate support
  • Loading branch information
c9s authored Dec 23, 2024
2 parents 9e862e1 + 6a8b628 commit 40bc0ce
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/exchange/binance/binanceapi/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ func TestClient_GetTradeFeeRequest(t *testing.T) {
t.Logf("tradeFees: %+v", tradeFees)
}

func TestClient_GetMarginFutureNextHourlyInterestRate(t *testing.T) {
client := getTestClientOrSkip(t)
ctx := context.Background()

err := client.SetTimeOffsetFromServer(ctx)
if assert.NoError(t, err) {
req := client.NewGetMarginFutureHourlyInterestRateRequest().
Assets("BTC,USDT").
IsIsolated("FALSE")
rates, err := req.Do(ctx)
assert.NoError(t, err)

t.Logf("rates: %+v", rates)
for _, rate := range rates {
t.Logf("%s: %s (annualized: %s)", rate.Asset,
rate.NextHourlyInterestRate.FormatPercentage(4),
rate.GetAnnualizedInterestRate().FormatPercentage(4),
)
}
}
}

func TestClient_GetDepositAddressRequest(t *testing.T) {
client := getTestClientOrSkip(t)
ctx := context.Background()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package binanceapi

import (
"math"

"github.com/c9s/requestgen"

"github.com/c9s/bbgo/pkg/fixedpoint"
)

type HourlyInterestRate struct {
Asset string `json:"asset"`
NextHourlyInterestRate fixedpoint.Value `json:"nextHourlyInterestRate"`
}

func (r *HourlyInterestRate) GetAnnualizedInterestRate() fixedpoint.Value {
rf := r.NextHourlyInterestRate.Float64()
return fixedpoint.NewFromFloat(math.Pow(rf+1.0, 24*365) - 1.0)
}

//go:generate requestgen -method GET -url "/sapi/v1/margin/next-hourly-interest-rate" -type GetMarginFutureHourlyInterestRateRequest -responseType []HourlyInterestRate
type GetMarginFutureHourlyInterestRateRequest struct {
client requestgen.AuthenticatedAPIClient

// assets: List of assets, separated by commas, up to 20
assets string `param:"assets"`

// isIsolated: for isolated margin or not, "TRUE", "FALSE"
isIsolated string `param:"isIsolated"` // TRUE or FALSE
}

func (c *RestClient) NewGetMarginFutureHourlyInterestRateRequest() *GetMarginFutureHourlyInterestRateRequest {
return &GetMarginFutureHourlyInterestRateRequest{client: c}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions pkg/exchange/binance/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,29 @@ func (e *Exchange) NewStream() types.Stream {
return stream
}

func (e *Exchange) QueryMarginFutureHourlyInterestRate(
ctx context.Context, assets []string,
) (rates types.MarginNextHourlyInterestRateMap, err error) {
req := e.client2.NewGetMarginFutureHourlyInterestRateRequest()
req.Assets(strings.Join(assets, ","))
req.IsIsolated("FALSE")
rateSlice, err := req.Do(ctx)
if err != nil {
return nil, err
}

rateMap := make(types.MarginNextHourlyInterestRateMap)
for _, entry := range rateSlice {
rateMap[entry.Asset] = &types.MarginNextHourlyInterestRate{
Asset: entry.Asset,
HourlyRate: entry.NextHourlyInterestRate,
AnnualizedRate: entry.GetAnnualizedInterestRate(),
}
}

return rateMap, nil
}

func (e *Exchange) QueryMarginAssetMaxBorrowable(ctx context.Context, asset string) (amount fixedpoint.Value, err error) {
req := e.client2.NewGetMarginMaxBorrowableRequest()
req.Asset(asset)
Expand Down
8 changes: 8 additions & 0 deletions pkg/types/margin.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,11 @@ type IsolatedUserAsset struct {
RepayEnabled bool `json:"repayEnabled"`
TotalAsset fixedpoint.Value `json:"totalAsset"`
}

type MarginNextHourlyInterestRate struct {
Asset string `json:"asset"`
HourlyRate fixedpoint.Value `json:"hourlyRate"`
AnnualizedRate fixedpoint.Value `json:"annualizedRate"`
}

type MarginNextHourlyInterestRateMap map[string]*MarginNextHourlyInterestRate

0 comments on commit 40bc0ce

Please sign in to comment.