1- // ////////////////////////////////////////////////////////////////////////////
1+ // //////////////////////////////////////////////////////////////////////////////
22//
33// example.cpp
44//
55//
66// Bitfinex REST API C++ client - examples
77//
8- // ////////////////////////////////////////////////////////////////////////////
8+ // //////////////////////////////////////////////////////////////////////////////
99
1010// std
1111#include < fstream>
@@ -21,126 +21,122 @@ using std::endl;
2121using std::ifstream;
2222using std::string;
2323
24- using BfxAPI::BitfinexAPI;
25-
2624
2725int main (int argc, char *argv[])
2826{
29- // ///////////////////////////////////////////////////////////////////////
30- // Examples
31- // Note that default values are not mandatory. See BitfinexAPI.hpp
32- // for details.
33- // ///////////////////////////////////////////////////////////////////////
34-
35- // ///////////////////////////////////////////////////////////////////////
36- // GET REQUESTS - unauthenticated endpoints
37- // ///////////////////////////////////////////////////////////////////////
38-
39- BitfinexAPI bfxAPI;
40- string response;
41- int errCode = 0 ;
42-
43- // errCode = bfxAPI.getTicker(response, "btcusd");
44- // errCode = bfxAPI.getStats(response, "btcusd");
45- // errCode = bfxAPI.getFundingBook(response, "USD", 50, 50);
46- // errCode = bfxAPI.getOrderBook(response, "btcusd", 50, 50, 1);
47- // errCode = bfxAPI.getTrades(response, "btcusd", 0L, 50);
48- // errCode = bfxAPI.getLends(response, "USD", 0L, 50);
49- errCode = bfxAPI.getSymbols (response);
50- // errCode = bfxAPI.getSymbolDetails(response);
51-
52- cout << " Response: " << response << endl;
53- cout << " Error code: " << errCode << endl;
54-
55- // ///////////////////////////////////////////////////////////////////////
56- // POST REQUESTS - authenticated endpoints
57- // ///////////////////////////////////////////////////////////////////////
27+ // Create bfxAPI without API keys
28+ BfxAPI::BitfinexAPI bfxAPI;
29+ // Create bfxAPI with API keys
30+ // BfxAPI::BitfinexAPI bfxAPI("accessKey", "secretKey");
5831
32+ // Load API keys from file
5933 ifstream ifs (" key-secret" , ifstream::in);
6034 if (!ifs.is_open ())
6135 {
6236 cout << " Can't open 'key-secret' file. " << endl;
6337 return 1 ;
6438 }
39+ getline (ifs, bfxAPI.setAccessKey ());
40+ getline (ifs, bfxAPI.setSecretKey ());
41+ ifs.close ();
42+
43+ // Fetch API
44+ cout << " Request with error checking: " << endl;
45+ bfxAPI.getTicker (" btcusd" );
46+ if (!bfxAPI.hasApiError ())
47+ cout << bfxAPI.strResponse () << endl;
6548 else
6649 {
67- response.clear ();
68- string accessKey, secretKey;
69- getline (ifs, accessKey);
70- getline (ifs, secretKey);
71- ifs.close ();
72-
73- bfxAPI.setKeys (accessKey, secretKey);
74-
75- // / Account ///
76- // errCode = bfxAPI.getAccountInfo(response);
77- // errCode = bfxAPI.getAccountFees(response);
78- // errCode = bfxAPI.getSummary(response);
79- // errCode = bfxAPI.deposit(response, "bitcoin", "deposit", 1);
80- // errCode = bfxAPI.getKeyPermissions(response);
81- // errCode = bfxAPI.getMarginInfos(response);
82- // errCode = bfxAPI.getBalances(response);
83- // errCode = bfxAPI.transfer(response, 0.1, "BTC", "trading", "deposit");
84- // errCode = bfxAPI.withdraw(response); // configure withdraw.conf file before use
85-
86- // / Orders ///
87- // errCode = bfxAPI.newOrder(response, "btcusd", 0.01, 983, "sell", "exchange limit", 0, 1,
88- // 0, 0, 0);
89- //
90- // How to create vOrders object for newOrders() call
91- // BitfinexAPI::vOrders orders =
92- // {
93- // {"btcusd", 0.1, 950, "sell", "exchange limit"},
94- // {"btcusd", 0.1, 950, "sell", "exchange limit"},
95- // {"btcusd", 0.1, 950, "sell", "exchange limit"}
96- // };
97- // errCode = bfxAPI.newOrders(response, orders);
98- //
99- // errCode = bfxAPI.cancelOrder(response, 13265453586LL);
100- //
101- // How to create ids object for cancelOrders() call
102- // BitfinexAPI::vIds ids =
103- // {
104- // 12324589754LL,
105- // 12356754322LL,
106- // 12354996754LL
107- // };
108- // errCode = bfxAPI.cancelOrders(response, ids);
109- //
110- // errCode = bfxAPI.cancelAllOrders(response);
111- // errCode = bfxAPI.replaceOrder(response, 1321548521LL, "btcusd", 0.05, 1212, "sell",
112- // "exchange limit", 0, 0);
113- // errCode = bfxAPI.getOrderStatus(response, 12113548453LL);
114- // errCode = bfxAPI.getActiveOrders(response);
115- // errCode = bfxAPI.getOrdersHistory(response, 10);
116-
117-
118- // / Positions ///
119- // errCode = bfxAPI.getActivePositions(response);
120- // errCode = bfxAPI.claimPosition(response, 156321412LL, 150);
121-
122- // / Historical data ///
123- // errCode = bfxAPI.getBalanceHistory(response, "USD", 0L, 0L, 500, "all");
124- // errCode = bfxAPI.getWithdrawalHistory(response, "BTC", "all", 0L , 0L, 500);
125- // errCode = bfxAPI.getPastTrades(response, "btcusd", 0L, 0L, 500, 0);
126-
127- // / Margin funding ///
128- // errCode = bfxAPI.newOffer(response, "USD", 12000, 25.2, 30, "lend");
129- // errCode = bfxAPI.cancelOffer(response, 12354245628LL);
130- // errCode = bfxAPI.getOfferStatus(response, 12313541215LL);
131- // errCode = bfxAPI.getActiveCredits(response);
132- // errCode = bfxAPI.getOffers(response);
133- // errCode = bfxAPI.getOffersHistory(response, 50);
134- // errCode = bfxAPI.getPastFundingTrades(response, "BTC", 0, 50);
135- // errCode = bfxAPI.getTakenFunds(response);
136- // errCode = bfxAPI.getUnusedTakenFunds(response);
137- // errCode = bfxAPI.getTotalTakenFunds(response);
138- // errCode = bfxAPI.closeLoan(response, 1235845634LL);
139- // errCode = bfxAPI.closePosition(response, 1235845634LL);
140-
141- cout << " Response: " << response << endl;
142- cout << " Error code: " << errCode << endl;
143-
144- return 0 ;
50+ // see bfxERR enum in BitfinexAPI.hpp::BitfinexAPI
51+ cout << bfxAPI.getBfxApiStatusCode () << endl;
52+ // see https://curl.haxx.se/libcurl/c/libcurl-errors.html
53+ cout << bfxAPI.getCurlStatusCode () << endl;
14554 }
55+
56+ cout << " Request without error checking: " << endl;
57+ cout << bfxAPI.getSummary ().strResponse () << endl;
58+
59+ // //////////////////////////////////////////////////////////////////////////
60+ // / Available unauthenticated requests
61+ // //////////////////////////////////////////////////////////////////////////
62+
63+ // bfxAPI.getTicker("btcusd");
64+ // bfxAPI.getStats("btcusd");
65+ // bfxAPI.getFundingBook("USD", 50, 50);
66+ // bfxAPI.getOrderBook("btcusd", 50, 50, 1);
67+ // bfxAPI.getTrades("btcusd", 0L, 50);
68+ // bfxAPI.getLends("USD", 0L, 50);
69+ // bfxAPI.getSymbols();
70+ // bfxAPI.getSymbolDetails();
71+
72+ // //////////////////////////////////////////////////////////////////////////
73+ // / Available authenticated requests
74+ // //////////////////////////////////////////////////////////////////////////
75+
76+ // / Account ///
77+ // bfxAPI.getAccountInfo();
78+ // bfxAPI.getAccountFees();
79+ // bfxAPI.getSummary();
80+ // bfxAPI.deposit("bitcoin", "deposit", 1);
81+ // bfxAPI.getKeyPermissions();
82+ // bfxAPI.getMarginInfos();
83+ // bfxAPI.getBalances();
84+ // bfxAPI.transfer(0.1, "BTC", "trading", "deposit");
85+ // bfxAPI.withdraw(); // configure withdraw.conf file before use
86+
87+ // / Orders ///
88+ // bfxAPI.newOrder("btcusd", 0.01, 983, "sell", "exchange limit", 0, 1,
89+ // 0, 0, 0);
90+ //
91+ // How to create vOrders object for newOrders() call
92+ // BitfinexAPI::vOrders orders =
93+ // {
94+ // {"btcusd", 0.1, 950, "sell", "exchange limit"},
95+ // {"btcusd", 0.1, 950, "sell", "exchange limit"},
96+ // {"btcusd", 0.1, 950, "sell", "exchange limit"}
97+ // };
98+ // bfxAPI.newOrders(orders);
99+ //
100+ // bfxAPI.cancelOrder(13265453586LL);
101+ //
102+ // How to create ids object for cancelOrders() call
103+ // BitfinexAPI::vIds ids =
104+ // {
105+ // 12324589754LL,
106+ // 12356754322LL,
107+ // 12354996754LL
108+ // };
109+ // bfxAPI.cancelOrders(ids);
110+ //
111+ // bfxAPI.cancelAllOrders();
112+ // bfxAPI.replaceOrder(1321548521LL, "btcusd", 0.05, 1212, "sell",
113+ // "exchange limit", 0, 0);
114+ // bfxAPI.getOrderStatus(12113548453LL);
115+ // bfxAPI.getActiveOrders();
116+ // bfxAPI.getOrdersHistory(10);
117+
118+ // / Positions ///
119+ // bfxAPI.getActivePositions();
120+ // bfxAPI.claimPosition(156321412LL, 150);
121+
122+ // / Historical data ///
123+ // bfxAPI.getBalanceHistory("USD", 0L, 0L, 500, "all");
124+ // bfxAPI.getWithdrawalHistory("BTC", "all", 0L , 0L, 500);
125+ // bfxAPI.getPastTrades("btcusd", 0L, 0L, 500, 0);
126+
127+ // / Margin funding ///
128+ // bfxAPI.newOffer("USD", 12000, 25.2, 30, "lend");
129+ // bfxAPI.cancelOffer(12354245628LL);
130+ // bfxAPI.getOfferStatus(12313541215LL);
131+ // bfxAPI.getActiveCredits();
132+ // bfxAPI.getOffers();
133+ // bfxAPI.getOffersHistory(50);
134+ // bfxAPI.getPastFundingTrades("BTC", 0, 50);
135+ // bfxAPI.getTakenFunds();
136+ // bfxAPI.getUnusedTakenFunds();
137+ // bfxAPI.getTotalTakenFunds();
138+ // bfxAPI.closeLoan(1235845634LL);
139+ // bfxAPI.closePosition(1235845634LL);
140+
141+ return 0 ;
146142}
0 commit comments