dydx

package module
v0.0.0-...-f5fc5cb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2022 License: MIT Imports: 10 Imported by: 0

README

go-dydx

dYdX exhange DEX

dYdX exchange API version3.

part OnBoarding referred to verichenn/dydx-v3-go.

Description

go-dydx is a go client library for dYdX, dYdX API Document.

Support

  • private/websocket, public/websocket
  • private/users
  • private/accounts
  • private/positions
  • private/orders (get, post, delete)
  • private/fast-withdrawals
  • private/fills
  • private/funding
  • private/historical-pnl
  • public/markets
  • public/orderbook
  • public/trades
  • public/candles
  • public/historical-funding

Usage

Rest
package main

import (
	"fmt"
	"time"

	"github.com/labstack/gommon/log"

	"github.com/Social-Taiwan/go-dydx"
	"github.com/Social-Taiwan/go-dydx/helpers"
	"github.com/Social-Taiwan/go-dydx/private"
	"github.com/Social-Taiwan/go-dydx/types"
)

const (
	EthereumAddress = "0xtest"
)

var userID int64 = 11111
var options = types.Options{
	Host:                      types.ApiHostMainnet,
	StarkPublicKey:            "<please check Google Chrome Developer tool -> application starkkey>",
	StarkPrivateKey:           "<please check Google Chrome Developer tool -> application starkkey>",
	StarkPublicKeyYCoordinate: "<please check Google Chrome Developer tool -> application starkkey>",
	DefaultEthereumAddress:    EthereumAddress,
	ApiKeyCredentials: &types.ApiKeyCredentials{
		Key:        "<please check Google Chrome Developer tool -> application apikey>",
		Secret:     "<please check Google Chrome Developer tool -> application secret>",
		Passphrase: "<please check Google Chrome Developer tool -> application passphrase>",
	},
}

func main() {
	start := time.Now()
	defer func() {
		fmt.Println("exec time: ", time.Since(start))
	}()

	client := dydx.New(options)
	account, err := client.Private.GetAccount(EthereumAddress)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(account)

    // print rate limit numbers
	fmt.Println(client.Public.RateLimit.ToNumber())


	params := &private.ApiOrder{
		ApiBaseOrder: private.ApiBaseOrder{Expiration: helpers.ExpireAfter(5 * time.Minute)},
		Market:       "ETH-USD",
		Side:         "BUY",
		Type:         "LIMIT",
		Size:         "1",
		Price:        "2000",
		ClientId:     helpers.RandomClientId(),
		TimeInForce:  "GTT",
		PostOnly:     true,
		LimitFee:     "0.01",
	}
	res, err := client.Private.CreateOrder(params, userID)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(res)

	// rate limit updated above
    // print rate limit numbers
	fmt.Println(client.Private.RateLimit.ToNumber())
}

Websocket

package main

import (
	"context"
	"fmt"
	"time"
	"log"

	"github.com/Social-Taiwan/go-dydx"
	"github.com/Social-Taiwan/go-dydx/public"
	"github.com/Social-Taiwan/go-dydx/realtime"
)


var userID int64 = 11111
var options = types.Options{
	Host:                      types.ApiHostMainnet,
	StarkPublicKey:            "<please check Google Chrome Developer tool -> application starkkey>",
	StarkPrivateKey:           "<please check Google Chrome Developer tool -> application starkkey>",
	StarkPublicKeyYCoordinate: "<please check Google Chrome Developer tool -> application starkkey>",
	DefaultEthereumAddress:    EthereumAddress,
	ApiKeyCredentials: &types.ApiKeyCredentials{
		Key:        "<please check Google Chrome Developer tool -> application apikey>",
		Secret:     "<please check Google Chrome Developer tool -> application secret>",
		Passphrase: "<please check Google Chrome Developer tool -> application passphrase>",
	},
}
func main() {
	client := dydx.New(options)

	account, err := client.Private.GetAccount(client.Private.DefaultAddress)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(account)

	parent := context.Background()
	ctx, cancel := context.WithCancel(parent)

	r := make(chan realtime.Response)

	// with Private
	go realtime.Connect(ctx, r, []string{realtime.ACCOUNT}, []string{}, client.Private, nil)
	// or without Private
	go realtime.Connect(ctx, r, []string{realtime.TRADES}, []string{"BTC-USD"}, nil, nil)

	for {
		select {
		case v := <-r:
			switch v.Channel {
			case realtime.ACCOUNT:
				fmt.Println(v.Account)
			case realtime.TRADES:
				fmt.Println(v.Trades)
			case realtime.ERROR:
				log.Println(v.Results)
				goto EXIT
			case realtime.UNDEFINED:
				log.Println(v.Results)
			}

		}
	}

EXIT:
	cancel()
}

Author

@_numbP

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Host                      string
	StarkPublicKey            string
	StarkPrivateKey           string
	StarkPublicKeyYCoordinate string
	ApiKeyCredentials         *types.ApiKeyCredentials
	ApiTimeout                time.Duration

	DefaultAddress string
	NetworkId      int
	Web3           *jsonrpc.Client
	EthSigner      helpers.EthSigner

	Private    *private.Private
	Public     *public.Public
	OnBoarding *onboard.OnBoarding

	Logger *log.Logger
	// contains filtered or unexported fields
}

func New

func New(options types.Options) *Client

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL