github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/examples/v2/rest-orders/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"log"
     6  	"os"
     7  	"strconv"
     8  
     9  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/common"
    10  	"github.com/bitfinexcom/bitfinex-api-go/v2"
    11  	"github.com/bitfinexcom/bitfinex-api-go/v2/rest"
    12  )
    13  
    14  var (
    15  	orderid = flag.String("id", "", "lookup trades for an order ID")
    16  	api     = flag.String("api", "https://api-pub.bitfinex.com/v2/", "v2 REST API URL")
    17  )
    18  
    19  // Set BFX_APIKEY and BFX_SECRET as :
    20  //
    21  // export BFX_API_KEY=YOUR_API_KEY
    22  // export BFX_API_SECRET=YOUR_API_SECRET
    23  //
    24  // you can obtain it from https://www.bitfinex.com/api
    25  
    26  func main() {
    27  	flag.Parse()
    28  
    29  	key := os.Getenv("BFX_API_KEY")
    30  	secret := os.Getenv("BFX_API_SECRET")
    31  	c := rest.NewClientWithURL(*api).Credentials(key, secret)
    32  
    33  	available, err := c.Platform.Status()
    34  	if err != nil {
    35  		log.Fatalf("getting status: %s", err)
    36  	}
    37  
    38  	if !available {
    39  		log.Fatalf("API not available")
    40  	}
    41  
    42  	if *orderid != "" {
    43  		ordid, err := strconv.ParseInt(*orderid, 10, 64)
    44  		if err != nil {
    45  			log.Fatal(err)
    46  		}
    47  		os, err := c.Orders.OrderTrades(common.TradingPrefix+bitfinex.BTCUSD, ordid)
    48  		if err != nil {
    49  			log.Fatalf("getting order trades: %s", err)
    50  		}
    51  
    52  		log.Printf("order trades: %#v\n", os)
    53  	} else {
    54  		os, err := c.Orders.AllHistory()
    55  		if err != nil {
    56  			log.Fatalf("getting orders: %s", err)
    57  		}
    58  
    59  		log.Printf("orders: %#v\n", os)
    60  	}
    61  }