Skip to content

Commit

Permalink
feat: Add convenience method to fetch a product (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
bennycode authored Aug 10, 2020
1 parent be44e25 commit 693fe2d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
54 changes: 53 additions & 1 deletion src/product/ProductAPI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,60 @@ import SecondCandleBatch from '../test/fixtures/rest/products/BTC-USD/candles/20
describe('ProductAPI', () => {
afterEach(() => nock.cleanAll());

describe('getProduct', () => {
it('returns trading details for a specified product', async () => {
nock(global.REST_URL)
.get(ProductAPI.URL.PRODUCTS)
.query(true)
.reply(
200,
JSON.stringify([
{
base_currency: 'BTC',
base_increment: '0.00000001',
base_max_size: '200',
base_min_size: '0.001',
cancel_only: false,
display_name: 'BTC/EUR',
id: 'BTC-EUR',
limit_only: false,
margin_enabled: false,
max_market_funds: '600000',
min_market_funds: '10',
post_only: false,
quote_currency: 'EUR',
quote_increment: '0.01',
status: 'online',
status_message: '',
},
{
base_currency: 'XRP',
base_increment: '1',
base_max_size: '500000',
base_min_size: '1',
cancel_only: false,
display_name: 'XRP/USD',
id: 'XRP-USD',
limit_only: false,
margin_enabled: false,
max_market_funds: '100000',
min_market_funds: '10',
post_only: false,
quote_currency: 'USD',
quote_increment: '0.0001',
status: 'online',
status_message: '',
},
])
);

const product = await global.client.rest.product.getProduct('XRP-USD');
expect(product!.display_name).toBe('XRP/USD');
});
});

describe('getProducts', () => {
it('returns list of products', async () => {
it('returns trading details for all available products', async () => {
nock(global.REST_URL)
.get(ProductAPI.URL.PRODUCTS)
.query(true)
Expand Down
12 changes: 11 additions & 1 deletion src/product/ProductAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,17 @@ export class ProductAPI {
}

/**
* Get a list of available currency pairs for trading.
* Get trading details for a specified product.
*
* @see https://docs.pro.coinbase.com/#get-products
*/
async getProduct(productId: string): Promise<Product | undefined> {
const products = await this.getProducts();
return products.find(product => product.id === productId);
}

/**
* Get trading details of all available products.
*
* @see https://docs.pro.coinbase.com/#get-products
*/
Expand Down

0 comments on commit 693fe2d

Please sign in to comment.