Skip to content

Commit

Permalink
chore(): misc cleaning, incl imports
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagosiebler committed Sep 27, 2024
1 parent 2229012 commit f9f5d6b
Show file tree
Hide file tree
Showing 22 changed files with 153 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ ts-exchange-priv.ts
testfile-cbexch.ts
restClientRegex.ts
privaterepotracker.txt
examples/_TiagoTests
48 changes: 36 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ To use any of Coinbase's REST APIs in JavaScript/TypeScript/Node.js, import (or

```javascript
import { CBAdvancedTradeClient } from 'coinbase-api';
/**
* Or, with import:
* import { CBAdvancedTradeClient } from 'coinbase-api';
*/

// insert your API key details here from Coinbase API Key Management
const advancedTradeCdpAPIKey = {
Expand Down Expand Up @@ -134,6 +138,10 @@ try {

```javascript
const { CBAppClient } = require('coinbase-api');
/**
* Or, with import:
* import { CBAppClient } from 'coinbase-api';
*/

// insert your API key details here from Coinbase API Key Management
const CBAppKeys = {
Expand Down Expand Up @@ -171,6 +179,10 @@ try {

```javascript
const { CBInternationalClient } = require('coinbase-api');
/**
* Or, with import:
* import { CBInternationalClient } from 'coinbase-api';
*/

// insert your API key details here from Coinbase API Key Management
const client = new CBInternationalClient({
Expand All @@ -194,6 +206,10 @@ try {

```javascript
const { CBExchangeClient } = require('coinbase-api');
/**
* Or, with import:
* import { CBExchangeClient } from 'coinbase-api';
*/

// insert your API key details here from Coinbase API Key Management
const client = new CBExchangeClient({
Expand All @@ -217,7 +233,7 @@ See all clients [here](./src/) for more information on all the functions or the

## WebSockets

All available WebSockets can be used via a shared `WebsocketClient`. The WebSocket client will automatically open/track/manage connections as needed. Each unique connection (one per server URL) is tracked using a WsKey (each WsKey is a string - see [WS_KEY_MAP](src/lib/websocket/websocket-util.ts) for a list of supported values).
All available WebSockets can be used via a shared `WebsocketClient`. The WebSocket client will automatically open/track/manage connections as needed. Each unique connection (one per server URL) is tracked using a WsKey (each WsKey is a string - see [WS_KEY_MAP](src/lib/websocket/websocket-util.ts) for a list of supported values - `WS_KEY_MAP` can also be used like an enum).

Any subscribe/unsubscribe events will need to include a WsKey, so the WebSocket client understands which connection the event should be routed to. See examples below or in the [examples](./examples/) folder on GitHub.

Expand All @@ -227,6 +243,10 @@ Data events are emitted from the WebsocketClient via the `update` event, see exa

```javascript
const { WebsocketClient } = require('coinbase-api');
/**
* Or, with import:
* import { WebsocketClient } from 'coinbase-api';
*/

// public ws client, doesnt need any api keys to run
const client = new WebsocketClient();
Expand All @@ -248,6 +268,10 @@ client.subscribe(

```javascript
const { WebsocketClient } = require('coinbase-api');
/**
* Or, with import:
* import { WebsocketClient } from 'coinbase-api';
*/

// key name & private key, as returned by coinbase when creating your API keys.
// Note: the below example is a dummy key and won't actually work
Expand Down Expand Up @@ -316,6 +340,8 @@ client.on('exception', (data) => {

// market data
client.subscribe('heartbeats', 'advTradeMarketData');
// This is the same as above, but using WS_KEY_MAP like an enum to reduce any uncertainty on what value to use:
// client.subscribe('heartbeats', WS_KEY_MAP.advTradeMarketData);

// user data
client.subscribe('futures_balance_summary', 'advTradeUserData');
Expand All @@ -324,7 +350,7 @@ client.subscribe('user', 'advTradeUserData');
/**
* Or send a more structured object with parameters, e.g. if parameters are required
*/
const tickerSubscribeRequst = {
const tickerSubscribeRequest = {
topic: 'ticker',
/**
* Anything in the payload will be merged into the subscribe "request",
Expand All @@ -334,10 +360,11 @@ const tickerSubscribeRequst = {
product_ids: ['ETH-USD', 'BTC-USD'],
},
};
client.subscribe(tickerSubscribeRequst, 'advTradeMarketData');

// Other adv trade public websocket topics:
client.subscribe(tickerSubscribeRequest, 'advTradeMarketData');

/**
* Other adv trade public websocket topics:
*/
client.subscribe(
[
{
Expand Down Expand Up @@ -385,6 +412,10 @@ Pass a custom logger which supports the log methods `trace`, `info` and `error`,
```javascript
const { WebsocketClient, DefaultLogger } = require('coinbase-api');
/**
* Or, with import:
* import { WebsocketClient, DefaultLogger } from 'coinbase-api';
*/

// E.g. customise logging for only the trace level:
const logger = {
Expand Down Expand Up @@ -428,13 +459,6 @@ Have my projects helped you? Share the love, there are many ways you can show yo
- Or buy me all the coffee:
- ETH(ERC20): `0xA3Bda8BecaB4DCdA539Dc16F9C54a592553Be06C` <!-- metamask -->
<!---
old ones:
- BTC: `1C6GWZL1XW3jrjpPTS863XtZiXL1aTK7Jk`
- BTC(SegWit): `bc1ql64wr9z3khp2gy7dqlmqw7cp6h0lcusz0zjtls`
- ETH(ERC20): `0xe0bbbc805e0e83341fadc210d6202f4022e50992`
- USDT(TRC20): `TA18VUywcNEM9ahh3TTWF3sFpt9rkLnnQa
-->
<!-- template_contributions_end -->
### Contributions & Pull Requests
Expand Down
20 changes: 12 additions & 8 deletions examples/AdvancedTrade/Private/closePosition.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { CBAdvancedTradeClient } from '../../../src/index.js';
// import { CBAdvancedTradeClient } from 'coinbase-api';

/**
* import { CBAdvancedTradeClient } from 'coinbase-api';
* const { CBAdvancedTradeClient } = require('coinbase-api');
*/

// initialise the client
const client = new CBAdvancedTradeClient({
Expand All @@ -11,15 +15,15 @@ const client = new CBAdvancedTradeClient({
// If you want to close futures, it is recommended to use submitOrder() with the opposite side of the current position

/* Closing Futures Positions - Exchange docs
When a contract expires, we automatically close your open position at the exchange settlement price.
You can also close your position before the contract expires
(for example, you may want to close your position if you’ve reached your profit target,
When a contract expires, we automatically close your open position at the exchange settlement price.
You can also close your position before the contract expires
(for example, you may want to close your position if you’ve reached your profit target,
you want to prevent further losses, or you need to satisfy a margin requirement).
There are two ways to close your futures positions:
(1) Close your position with this endpoint, or
(2) Create a separate trade to take the opposite position in the same futures contract you are currently holding in your account.
For example, to close an open long position in the BTC 23 Feb 24 contract, place an order to sell the same number of BTC 23 Feb 24 contracts.
There are two ways to close your futures positions:
(1) Close your position with this endpoint, or
(2) Create a separate trade to take the opposite position in the same futures contract you are currently holding in your account.
For example, to close an open long position in the BTC 23 Feb 24 contract, place an order to sell the same number of BTC 23 Feb 24 contracts.
If you were short to begin with, go long the same number of contracts to close your position.
More info on https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_closeposition#closing-futures-positions */
Expand Down
6 changes: 5 additions & 1 deletion examples/AdvancedTrade/Private/getAccounts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { CBAdvancedTradeClient } from '../../../src/index.js';
// import { CBAdvancedTradeClient } from 'coinbase-api';

/**
* import { CBAdvancedTradeClient } from 'coinbase-api';
* const { CBAdvancedTradeClient } = require('coinbase-api');
*/

// initialise the client
const client = new CBAdvancedTradeClient({
Expand Down
6 changes: 5 additions & 1 deletion examples/AdvancedTrade/Private/getOrders.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { CBAdvancedTradeClient } from '../../../src/index.js';
// import { CBAdvancedTradeClient } from 'coinbase-api';

/**
* import { CBAdvancedTradeClient } from 'coinbase-api';
* const { CBAdvancedTradeClient } = require('coinbase-api');
*/

// initialise the client
const client = new CBAdvancedTradeClient({
Expand Down
6 changes: 5 additions & 1 deletion examples/AdvancedTrade/Private/submitOrderPerps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { CBAdvancedTradeClient } from '../../../src/index.js';
// import { CBAdvancedTradeClient } from 'coinbase-api';

/**
* import { CBAdvancedTradeClient } from 'coinbase-api';
* const { CBAdvancedTradeClient } = require('coinbase-api');
*/

// initialise the client
const client = new CBAdvancedTradeClient({
Expand Down
6 changes: 5 additions & 1 deletion examples/AdvancedTrade/Private/submitOrderSpot.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { CBAdvancedTradeClient } from '../../../src/index.js';
// import { CBAdvancedTradeClient } from 'coinbase-api';

/**
* import { CBAdvancedTradeClient } from 'coinbase-api';
* const { CBAdvancedTradeClient } = require('coinbase-api');
*/

// initialise the client
const client = new CBAdvancedTradeClient({
Expand Down
6 changes: 5 additions & 1 deletion examples/AdvancedTrade/Public/advanced-public-rest-all.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { CBAdvancedTradeClient } from '../../../src/index.js';
// import { CBAdvancedTradeClient } from 'coinbase-api';

/**
* import { CBAdvancedTradeClient } from 'coinbase-api';
* const { CBAdvancedTradeClient } = require('coinbase-api');
*/

// you can initialise public client without api keys as public calls do not require auth
const client = new CBAdvancedTradeClient({});
Expand Down
13 changes: 12 additions & 1 deletion examples/AdvancedTrade/WebSockets/privateWs.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import {
// DefaultLogger,
WebsocketClient,
// WS_KEY_MAP,
WsTopicRequest,
} from '../../../src/index.js';

/**
* import { WebsocketClient } from 'coinbase-api';
* const { WebsocketClient } = require('coinbase-api');
*/

async function start() {
// key name & private key, as returned by coinbase when creating your API keys.
// Note: the below example is a dummy key and won't actually work
Expand Down Expand Up @@ -89,6 +95,10 @@ async function start() {
*/
// client.subscribe('heartbeats', 'advTradeUserData');
client.subscribe('futures_balance_summary', 'advTradeUserData');
// This is the same as above, but uses WS_KEY_MAP as an enum (do this if you're not sure what value to put)
// client.subscribe('futures_balance_summary', WS_KEY_MAP.advTradeUserData);

// Subscribe to the user feed for the advanced trade websocket
client.subscribe('user', 'advTradeUserData');

// /**
Expand All @@ -106,7 +116,7 @@ async function start() {
* Or send a more structured object with parameters, e.g. if parameters are required
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const tickerSubscribeRequst: WsTopicRequest = {
const tickerSubscribeRequest: WsTopicRequest = {
topic: 'futures_balance_summary',
/**
* Anything in the payload will be merged into the subscribe "request",
Expand All @@ -117,6 +127,7 @@ async function start() {
// product_ids: ['ETH-USD', 'BTC-USD'],
},
};
client.subscribe(tickerSubscribeRequest, 'advTradeUserData');
} catch (e) {
console.error(`Subscribe exception: `, e);
}
Expand Down
9 changes: 7 additions & 2 deletions examples/AdvancedTrade/WebSockets/publicWs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import {
WsTopicRequest,
} from '../../../src/index.js';

/**
* import { WebsocketClient } from 'coinbase-api';
* const { WebsocketClient } = require('coinbase-api');
*/

async function start() {
// Optional: fully customise the logging experience by injecting a custom logger
// const logger: typeof DefaultLogger = {
Expand Down Expand Up @@ -86,7 +91,7 @@ async function start() {
* Or send a more structured object with parameters, e.g. if parameters are required
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const tickerSubscribeRequst: WsTopicRequest = {
const tickerSubscribeRequest: WsTopicRequest = {
topic: 'ticker',
/**
* Anything in the payload will be merged into the subscribe "request",
Expand All @@ -96,7 +101,7 @@ async function start() {
product_ids: ['ETH-USD', 'BTC-USD'],
},
};
client.subscribe(tickerSubscribeRequst, 'advTradeMarketData');
client.subscribe(tickerSubscribeRequest, 'advTradeMarketData');

/**
* Subscribe to the "status" topic for a few symbols
Expand Down
5 changes: 5 additions & 0 deletions examples/CBExchange/WebSockets/privateWs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { WebsocketClient } from '../../../src/index.js';

/**
* import { WebsocketClient } from 'coinbase-api';
* const { WebsocketClient } = require('coinbase-api');
*/

async function start() {
const client = new WebsocketClient(
{
Expand Down
8 changes: 6 additions & 2 deletions examples/CBExchange/WebSockets/publicWs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { WebsocketClient, WsTopicRequest } from '../../../src/index.js';
import { WebsocketClient } from '../../../src/index.js';

/**
* import { WebsocketClient } from 'coinbase-api';
* const { WebsocketClient } = require('coinbase-api');
*/

async function start() {
/**
Expand Down
6 changes: 5 additions & 1 deletion examples/CBExchange/cb-exchange-private.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { CBExchangeClient } from '../../src/index.js';
// import { CBExchangeClient } from 'coinbase-api';

/**
* import { CBAdvancedTradeClient } from 'coinbase-api';
* const { CBAdvancedTradeClient } = require('coinbase-api');
*/

// Initialize the client, you can pass in api keys here if you have them but they are not required for public endpoints
const client = new CBExchangeClient({
Expand Down
6 changes: 5 additions & 1 deletion examples/CBExchange/cb-exchange-public.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { CBExchangeClient } from '../../src/index.js';
// import { CBExchangeClient } from 'coinbase-api';

/**
* import { CBAdvancedTradeClient } from 'coinbase-api';
* const { CBAdvancedTradeClient } = require('coinbase-api');
*/

// Initialize the client, you can pass in api keys here if you have them but they are not required for public endpoints
const client = new CBExchangeClient();
Expand Down
6 changes: 5 additions & 1 deletion examples/CBInternationalExchange/cb-intx-public.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { CBInternationalClient } from '../../src/index.js';
// import { CBInternationalClient } from 'coinbase-api';

/**
* import { CBInternationalClient } from 'coinbase-api';
* const { CBInternationalClient } = require('coinbase-api');
*/

// Initialize the client, you can pass in api keys here if you have them but they are not required for public endpoints
const client = new CBInternationalClient();
Expand Down
6 changes: 5 additions & 1 deletion examples/CoinbaseApp/Private/cb-app-private.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { CBAppClient } from '../../../src/index.js';
// import { CBAppClient } from 'coinbase-api';

/**
* import { CBAppClient } from 'coinbase-api';
* const { CBAppClient } = require('coinbase-api');
*/

// Initialize the client
const client = new CBAppClient({
Expand Down
6 changes: 5 additions & 1 deletion examples/CoinbaseApp/Private/depositFunds.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { CBAppClient } from '../../../src/index.js';
// import { CBAppClient } from 'coinbase-api';

/**
* import { CBAppClient } from 'coinbase-api';
* const { CBAppClient } = require('coinbase-api');
*/

// Initialize the client
const client = new CBAppClient({
Expand Down
6 changes: 5 additions & 1 deletion examples/CoinbaseApp/Private/sendMoney.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { CBAppClient } from '../../../src/index.js';
// import { CBAppClient } from 'coinbase-api';

/**
* import { CBAppClient } from 'coinbase-api';
* const { CBAppClient } = require('coinbase-api');
*/

// Initialize the client
const client = new CBAppClient({
Expand Down
Loading

0 comments on commit f9f5d6b

Please sign in to comment.