Giter Club home page Giter Club logo

okex's Introduction

okex

Go Reference GitHub go.mod Go version of a Go module GoReportCard example GitHub license GitHub release PRs Welcome CI CodeQL AutoRelease

NOTICE:

PACKAGE IS CURRENTLY UNDER HEAVY DEVELOPMENT AND THERE IS NO GUARANTY FOR STABILITY.

DISCLAIMER:

This package is provided as-is, without any express or implied warranties. The user assumes all risks associated with the use of this package. The author and contributors to this package shall not be held liable for any damages arising from the use of this package, including but not limited to direct, indirect, incidental, or consequential damages. This package is not intended to be used as a substitute for professional financial advice. Users are responsible for verifying the accuracy and reliability of the data generated by this package. Use of this package constitutes acceptance of these terms and conditions.

Okex V5 Golang API

A complete golang wrapper for Okex V5 API. Pretty simple and easy to use. For more info about Okex V5 API read here.

Installation

go get github.com/amir-the-h/[email protected]

Usage

package main

import (
  "context"
  "github.com/amir-the-h/okex"
  "github.com/amir-the-h/okex/api"
  "github.com/amir-the-h/okex/events"
  "github.com/amir-the-h/okex/events/public"
  ws_public_requests "github.com/amir-the-h/okex/requests/ws/public"
  "log"
)

func main() {
  apiKey := "YOUR-API-KEY"
  secretKey := "YOUR-SECRET-KEY"
  passphrase := "YOUR-PASS-PHRASE"
  dest := okex.NormalServer // The main API server
  ctx := context.Background()
  client, err := api.NewClient(ctx, apiKey, secretKey, passphrase, &dest)
  if err != nil {
    log.Fatalln(err)
  }


  log.Println("Starting")
  errChan := make(chan *events.Error)
  subChan := make(chan *events.Subscribe)
  uSubChan := make(chan *events.Unsubscribe)
  logChan := make(chan *events.Login)
  sucChan := make(chan *events.Success)
  client.Ws.SetChannels(errChan, subChan, uSubChan, logChan, sucChan)

  obCh := make(chan *public.OrderBook)
  err = client.Ws.Public.OrderBook(ws_public_requests.OrderBook{
    InstID: "BTC-USD-SWAP",
    Channel: "books",
  }, obCh)
  if err != nil {
    log.Fatalln(err)
  }

  for {
    select {
    case <-logChan:
      log.Print("[Authorized]")
    case success := <-sucChan:
      log.Printf("[SUCCESS]\t%+v", success)
    case sub := <-subChan:
      channel, _ := sub.Arg.Get("channel")
      log.Printf("[Subscribed]\t%s", channel)
    case uSub := <-uSubChan:
      channel, _ := uSub.Arg.Get("channel")
      log.Printf("[Unsubscribed]\t%s", channel)
    case err := <-client.Ws.ErrChan:
      log.Printf("[Error]\t%+v", err)
      for _, datum := range err.Data {
        log.Printf("[Error]\t\t%+v", datum)
      }
    case i := <-obCh:
      ch, _ := i.Arg.Get("channel")
      log.Printf("[Event]\t%s", ch)
      for _, p := range i.Books {
        for i := len(p.Asks) - 1; i >= 0; i-- {
          log.Printf("\t\tAsk\t%+v", p.Asks[i])
        }
        for _, bid := range p.Bids {
          log.Printf("\t\tBid\t%+v", bid)
        }
      }
    case b := <-client.Ws.DoneChan:
      log.Printf("[End]:\t%v", b)
      return
    }
  }
}

Supporting APIs

Features

  • All requests, responses, and events are well typed and will convert into the language built-in types instead of using API's strings. Note that zero values will be replaced with non-existing data.
  • Fully automated authorization steps for both REST and WS
  • To receive websocket events you can choose RawEventChan , StructuredEventChan, or provide your own channels. More info

okex's People

Contributors

amir-the-h avatar dyamin avatar hardyzp avatar mhxw avatar renat1015 avatar turbokang avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

okex's Issues

WARNING: DATA RACE

run with a --race flag gives warnings on given functions (api.ws.sender and api.ws.receiver) running in goroutines

Few symbols

Hi,

How to subscribe with websocket to many symbols at same time? For example, BTCUSD, ETHUSD, ....

  err = client.Ws.Public.OrderBook(ws_public_requests.OrderBook{
    InstID: "BTC-USD-SWAP",
    Channel: "books",
  }, obCh)

Thanks

Duplicated Mutex

Hi, why we need two mutex mu and rmu? if mu stand for write lock and rmu stand for read lock, why don't we simply use one single sync.RWMutex instead?

[BUG] goroutine leak in websocket api

Describe the bug
go channel reception has never been processed, resulting in continuous goroutine leaks

case "mark-price":
			e := public.MarkPrice{}
			err := json.Unmarshal(data, &e)
			if err != nil {
				return false
			}
			go func() {
				if c.mpCh != nil {
					c.mpCh <- &e
				}
                       //goroutine stuck here
				c.StructuredEventChan <- e
			}()
			return true

To Reproduce
invoke websokcet subscribe api and receive websocket events with my own channels.

Expected behavior
goroutine exit properly

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • Device: [e.g Macbook Pro]
  • OS: [e.g. MacOS]
  • Version [e.g. 14.1]

Golang (please complete the following information):

  • Version: [e.g. 17.1]

Additional context
version = v1.1.4-alpha

nil pointer dereference

Hello @amir-the-h!
/github.com/amir-the-h/[email protected]/api/ws/client.go:238

conn, res, err := websocket.DefaultDialer.Dial(string(c.url[p]), nil)
defer res.Body.Close()

panic: runtime error: invalid memory address or nil pointer dereference

Can this problem be caused due to res nil?
Thanks!

[BUG] panic concurrent map read and map write

Describe the bug

fatal error: concurrent map read and map write

goroutine 28 [running]:
runtime.throw({0x75b602, 0x6edb80})
/usr/local/go/src/runtime/panic.go:1198 +0x71 fp=0xc00015ae10 sp=0xc00015ade0 pc=0x434991
runtime.mapaccess1(0x6fee40, 0xc000179170, 0xa3a700)
/usr/local/go/src/runtime/map.go:411 +0x1f7 fp=0xc00015ae50 sp=0xc00015ae10 pc=0x40f737
github.com/amir-the-h/okex/api/ws.(*ClientWs).sender(0xc00032c000, 0x0)
/go/pkg/mod/github.com/amir-the-h/[email protected]/api/ws/client.go:282 +0x212 fp=0xc00015af88 sp=0xc00015ae50 pc=0x6bf992
github.com/amir-the-h/okex/api/ws.(*ClientWs).dial.func2()
/go/pkg/mod/github.com/amir-the-h/[email protected]/api/ws/client.go:245 +0x25 fp=0xc00015afe0 sp=0xc00015af88 pc=0x6bf645
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1581 +0x1 fp=0xc00015afe8 sp=0xc00015afe0 pc=0x463ee1
created by github.com/amir-the-h/okex/api/ws.(*ClientWs).dial
/go/pkg/mod/github.com/amir-the-h/[email protected]/api/ws/client.go:244 +0x29f

To Reproduce

  • Occasionally happened
  • Sample code for usage scenarios as follows
cli := ws.NewClient()
ch := make(chan *pub.MarkPrice)
// subscribe mark price channel
cli.Public.MarkPrice(req, ch)

for {

		select {
		case mark:= <- ch:
			//
			handlePrice(mark)
			ticker.Reset(time.Minute)
		case <-ticker.C:
			// ticker triggered, the websocket may be broken, try reconnect
			cli.Cancel()
			cli = ws.NewClient()
                        // resubscribe
			cli.Public.MarkPrice(req, ch)
		}
	}


Expected behavior
no panic

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • Device: [e.g Macbook Pro]
  • OS: [e.g. MacOS]
  • Version [e.g. 14.1]

happened in macos and ubuntu

Golang (please complete the following information):

  • Version: 1.17

Additional context
Add any other context about the problem here.

[BUG] Wrong json field value in public market API

Describe the bug
I can't fetch tickers because of wrong JSON field declaration

To Reproduce
Steps to reproduce the behavior:

  1. I took your readme example
  2. I wrote some go code
client.Rest.Market = rest.NewMarket(client.Rest)

getSwapTickers := market.GetTickers{
	InstType: okex.SwapInstrument,
}
swapTickers, err := client.Rest.Market.GetTickers(getSwapTickers)
if err != nil {
	log.Fatalln(err)
}
  1. I saw the error
    json: invalid use of ,string struct tag, trying to unmarshal "SWAP" into okex.InstrumentType

Additional information
Actually I have fixed it already, so it's just representative issue to make a PR

WS orderBook method does not return data

from v1.0.7-alpha version stopped receiving orderBook via web socket
up to v1.0.6-alpha version inclusive method client.Ws.Public.OrderBook(reqWS, obCh) returns data to the channel
so far I haven't looked deeper, maybe I'm doing something wrong

Memory Leak [BUG]

Describe the bug

not consume any data in StructuredEventChan.is this channel is necessary?

Place Order Error

@amir-the-h i am unable to place the order.
in response of Place order function i found below error with status 50101
Msg:Broker id of APIKey does not match current environment.
check API key and secret the value of Key and secret is correct.
Important Point
I am using testmod of API.

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.