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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  
     8  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/fundingoffer"
     9  	"github.com/bitfinexcom/bitfinex-api-go/v2/rest"
    10  	"github.com/davecgh/go-spew/spew"
    11  )
    12  
    13  // Set BFX_API_KEY and BFX_API_SECRET as :
    14  //
    15  // export BFX_API_KEY=YOUR_API_KEY
    16  // export BFX_API_SECRET=YOUR_API_SECRET
    17  //
    18  // you can obtain it from https://www.bitfinex.com/api
    19  
    20  func main() {
    21  	key := os.Getenv("BFX_API_KEY")
    22  	secret := os.Getenv("BFX_API_SECRET")
    23  	c := rest.NewClient().Credentials(key, secret)
    24  
    25  	offers(c)
    26  	offerHistory(c)
    27  	loans(c)
    28  	loansHistory(c)
    29  	activeCredits(c)
    30  	creditsHistory(c)
    31  	fundingTrades(c)
    32  	keepFunding(c)
    33  
    34  	/********* submit a new funding offer ***********/
    35  	fo, err := c.Funding.SubmitOffer(&fundingoffer.SubmitRequest{
    36  		Type:   "LIMIT",
    37  		Symbol: "fUSD",
    38  		Amount: 1000,
    39  		Rate:   0.012,
    40  		Period: 7,
    41  		Hidden: true,
    42  	})
    43  	if err != nil {
    44  		panic(err)
    45  	}
    46  	newOffer := fo.NotifyInfo.(*fundingoffer.New)
    47  
    48  	/********* cancel funding offer ***********/
    49  	fc, err := c.Funding.CancelOffer(&fundingoffer.CancelRequest{
    50  		ID: newOffer.ID,
    51  	})
    52  	if err != nil {
    53  		panic(err)
    54  	}
    55  	fmt.Println(fc)
    56  }
    57  
    58  func offers(c *rest.Client) {
    59  	// active funding offers
    60  	snap, err := c.Funding.Offers("fUSD")
    61  	if err != nil {
    62  		panic(err)
    63  	}
    64  	for _, item := range snap.Snapshot {
    65  		fmt.Println(item)
    66  	}
    67  }
    68  
    69  func offerHistory(c *rest.Client) {
    70  	// funding offer history
    71  	snapHist, err := c.Funding.OfferHistory("fUSD")
    72  	if err != nil {
    73  		panic(err)
    74  	}
    75  	for _, item := range snapHist.Snapshot {
    76  		fmt.Println(item)
    77  	}
    78  }
    79  
    80  func loans(c *rest.Client) {
    81  	// active loans
    82  	snapLoans, err := c.Funding.Loans("fUSD")
    83  	if err != nil {
    84  		panic(err)
    85  	}
    86  	for _, item := range snapLoans.Snapshot {
    87  		fmt.Println(item)
    88  	}
    89  }
    90  
    91  func loansHistory(c *rest.Client) {
    92  	napLoansHist, err := c.Funding.LoansHistory("fUSD")
    93  	if err != nil {
    94  		panic(err)
    95  	}
    96  	for _, item := range napLoansHist.Snapshot {
    97  		fmt.Println(item)
    98  	}
    99  }
   100  
   101  func activeCredits(c *rest.Client) {
   102  	// active credits
   103  	snapCredits, err := c.Funding.Credits("fUSD")
   104  	if err != nil {
   105  		panic(err)
   106  	}
   107  	for _, item := range snapCredits.Snapshot {
   108  		fmt.Println(item)
   109  	}
   110  }
   111  
   112  func creditsHistory(c *rest.Client) {
   113  	napCreditsHist, err := c.Funding.CreditsHistory("fUSD")
   114  	if err != nil {
   115  		panic(err)
   116  	}
   117  	for _, item := range napCreditsHist.Snapshot {
   118  		fmt.Println(item)
   119  	}
   120  }
   121  
   122  func fundingTrades(c *rest.Client) {
   123  	napTradesHist, err := c.Funding.Trades("fUSD")
   124  	if err != nil {
   125  		panic(err)
   126  	}
   127  	for _, item := range napTradesHist.Snapshot {
   128  		fmt.Println(item)
   129  	}
   130  }
   131  
   132  func keepFunding(c *rest.Client) {
   133  	// keep funding
   134  	resp, err := c.Funding.KeepFunding(rest.KeepFundingRequest{
   135  		Type: "credit",
   136  		ID:   12345, // Insert correct ID
   137  	})
   138  	if err != nil {
   139  		log.Fatalf("KeepFunding error: %s", err)
   140  	}
   141  
   142  	spew.Dump(resp)
   143  }