Skip to content

Commit

Permalink
Add examples into cmd ccy-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
dasbd72 committed Mar 3, 2024
1 parent c6f8dc4 commit ffeaa45
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 24 deletions.
229 changes: 205 additions & 24 deletions cmd/ccy-cli/bitfinex.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ package main

import (
"context"
"crypto/hmac"
"crypto/sha512"
"encoding/hex"
"encoding/json"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"time"

bitfinexRest "github.com/dasbd72/go-exchange-sdk/bitfinex/rest"
"github.com/gorilla/websocket"
"github.com/spf13/cobra"
)

Expand All @@ -18,45 +26,113 @@ func Bitfinex(cmd *cobra.Command, args []string) {
os.Getenv("BFX_API_SECRET"),
)

// data, err := c.CallAPI(ctx, bfxRest.Request_builder{
// Method: http.MethodGet,
// Endpoint: "/ticker",
// SecType: bfxRest.SecTypePrivate,
// Params: map[string]interface{}{},
// }.Build())
// if err != nil {
// log.Fatal(err)
// }
// log.Println(string(data))
// Raw(ctx, c)

SDK(ctx, c)
}

func Raw(ctx context.Context, c *bitfinexRest.Client) {
{
data, err := c.GetWallets(ctx)
data, err := c.CallAPI(ctx, bitfinexRest.Request_builder{
Method: http.MethodPost,
Endpoint: "/auth/r/funding/offers/Symbol",
Version: bitfinexRest.Version2,
SecType: bitfinexRest.SecTypePrivate,
Params: map[string]interface{}{},
}.Build())
if err != nil {
log.Fatal(err)
}
b, err := json.MarshalIndent(data, "", " ")
log.Println(string(data))
}
{
data, err := c.CallAPI(ctx, bitfinexRest.Request_builder{
Method: http.MethodPost,
Endpoint: "/auth/r/funding/loans/Symbol",
Version: bitfinexRest.Version2,
SecType: bitfinexRest.SecTypePrivate,
Params: map[string]interface{}{},
}.Build())
if err != nil {
log.Fatal(err)
}
log.Println(string(data))
}
{
data, err := c.CallAPI(ctx, bitfinexRest.Request_builder{
Method: http.MethodPost,
Endpoint: "/auth/r/funding/offers/fUSD/hist",
Version: bitfinexRest.Version2,
SecType: bitfinexRest.SecTypePrivate,
Params: map[string]interface{}{},
}.Build())
if err != nil {
log.Fatal(err)
}
// log.Println(string(data))
container := [][]interface{}{}
err = json.Unmarshal(data, &container)
if err != nil {
log.Fatal(err)
}
b, err := json.MarshalIndent(container, "", " ")
if err != nil {
log.Fatal(err)
}
log.Println(string(b))
}
// {
// data, err := c.GetFundingStats(ctx, "fUST")
// data, err := c.CallAPI(ctx, bitfinexRest.Request_builder{
// Method: http.MethodPost,
// Endpoint: "/auth/w/funding/offer/submit",
// Version: bitfinexRest.Version2,
// SecType: bitfinexRest.SecTypePrivate,
// Params: map[string]interface{}{},
// }.Build())
// if err != nil {
// log.Fatal(err)
// }
// for _, v := range data.FundingStatArray {
// if v.MTS.Int() != data.FundingStatArray[0].MTS.Int() {
// break
// }
// b, err := json.MarshalIndent(v, "", " ")
// if err != nil {
// log.Fatal(err)
// }
// log.Println(string(b))
// }
// log.Println(string(data))
// }
}

func SDK(ctx context.Context, c *bitfinexRest.Client) {
{
// dummy
data, err := c.GetWallets(ctx)
if err != nil {
log.Fatal(err)
}
_, err = json.MarshalIndent(data, "", " ")
if err != nil {
log.Fatal(err)
}
}
{
data, err := c.GetWallets(ctx)
if err != nil {
log.Fatal(err)
}
b, err := json.MarshalIndent(data, "", " ")
if err != nil {
log.Fatal(err)
}
log.Println(string(b))
}
{
data, err := c.GetAllActiveFundingOffers(ctx)
// data, err := c.GetActiveFundingOffers(ctx, "fUSD")
if err != nil {
log.Fatal(err)
}
b, err := json.MarshalIndent(data, "", " ")
if err != nil {
log.Fatal(err)
}
log.Println(string(b))
}
// {
// data, err := c.GetActiveFundingOffers(ctx, "fUSD")
// data, err := c.SubmitFundingOffer(ctx, models.NewSubmitFundingOfferRequest(models.FRRLIMIT, "fUSD", "150", "0.01", 2))
// if err != nil {
// log.Fatal(err)
// }
Expand All @@ -67,3 +143,108 @@ func Bitfinex(cmd *cobra.Command, args []string) {
// log.Println(string(b))
// }
}

func WS() {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)

wsEndpoint := "wss://api-pub.bitfinex.com/ws/2"
log.Printf("connecting to %s", wsEndpoint)

c, _, err := websocket.DefaultDialer.Dial(wsEndpoint, nil)
if err != nil {
log.Fatal("dial:", err)
}
defer c.Close()

done := make(chan struct{})

// Read messages from the websocket connection
go func() {
type event struct {
Event string `json:"event"`
}
type lst []interface{}
defer close(done)
for {
mt, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
// log.Printf("recv: %s, type: %v", message, mt)
var e event
var l lst
if err := json.Unmarshal(message, &e); err == nil {
log.Printf("[Event] recv: %s, type: %v", message, mt)
} else if err := json.Unmarshal(message, &l); err == nil {
switch l[1].(string) {
case "hb":
case "ws":
log.Printf("[WalletSnapshot] recv: %s, type: %v", message, mt)
case "wu":
log.Printf("[WalletUpdate] recv: %s, type: %v", message, mt)
default:
log.Printf("[Default] recv: %s, type: %v", message, mt)
}
} else {
log.Printf("[Unknown] recv: %s, type: %v", message, mt)
}
}
}()

// Write messages to the websocket connection
nonce := strconv.FormatInt(time.Now().Unix(), 10)
payload := "AUTH" + nonce
err = c.WriteJSON(struct {
APIKey string `json:"apiKey"`
AuthSig string `json:"authSig"`
AuthNonce string `json:"authNonce"`
AuthPayload string `json:"authPayload"`
Event string `json:"event"`
Filter []string `json:"filter"`
}{
APIKey: os.Getenv("BFX_API_KEY"),
AuthSig: sign(os.Getenv("BFX_API_SECRET"), payload),
AuthNonce: nonce,
AuthPayload: payload,
Event: "auth",
Filter: []string{"funding"},
})
if err != nil {
log.Println("write:", err)
}

// Termination handler
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-done:
return
case <-interrupt:
log.Println("interrupt")

// Cleanly close the connection by sending a close message and then
// waiting (with timeout) for the server to close the connection.
err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Println("write close:", err)
return
}
select {
// Wait for the server to close the connection.
case <-done:
// Or force close the connection after a timeout.
case <-time.After(time.Second):
}
return
}
}
}

func sign(secret, message string) string {
mac := hmac.New(sha512.New384, []byte(secret))
mac.Write([]byte(message))
return hex.EncodeToString(mac.Sum(nil))
}
2 changes: 2 additions & 0 deletions cmd/ccy-cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ require (
github.com/dasbd72/go-exchange-sdk/manager v0.0.0-00010101000000-000000000000
github.com/dasbd72/go-exchange-sdk/max v0.0.0-00010101000000-000000000000
github.com/dasbd72/go-exchange-sdk/okx v0.0.0-00010101000000-000000000000
github.com/gorilla/websocket v1.5.1
github.com/joho/godotenv v1.5.1
github.com/spf13/cobra v1.8.0
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.21.0 // indirect
)
4 changes: 4 additions & 0 deletions cmd/ccy-cli/go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
Expand All @@ -10,5 +12,7 @@ github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
8 changes: 8 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=

0 comments on commit ffeaa45

Please sign in to comment.