diff --git a/src/CBAppClient.ts b/src/CBAppClient.ts index 86e4178..0cde0cb 100644 --- a/src/CBAppClient.ts +++ b/src/CBAppClient.ts @@ -68,10 +68,10 @@ export class CBAppClient extends BaseRestClient { * * Get a current user's account by account ID or currency string. */ - getAccount(params: { accountId: string }): Promise<{ + getAccount(params: { account_id: string }): Promise<{ data: CBAppAccount; }> { - return this.getPrivate(`/v2/accounts/${params.accountId}`); + return this.getPrivate(`/v2/accounts/${params.account_id}`); } /** @@ -85,10 +85,10 @@ export class CBAppClient extends BaseRestClient { * * Creates a new address for an account. Addresses can be created for wallet account types. */ - createAddress(params: { accountId: string; name?: string }): Promise<{ + createAddress(params: { account_id: string; name?: string }): Promise<{ data: CBAppAddress; }> { - return this.postPrivate(`/v2/accounts/${params.accountId}/addresses`, { + return this.postPrivate(`/v2/accounts/${params.account_id}/addresses`, { body: params, }); } @@ -101,14 +101,17 @@ export class CBAppClient extends BaseRestClient { * This endpoint is paginated. In case you are calling it first time, leave paginationURL empty. * If you are paginating, provide the paginationURL value from the previous response and you will receive the next page of addresses. */ - getAddresses(params: { accountId: string; paginationURL?: string }): Promise<{ + getAddresses(params: { + account_id: string; + paginationURL?: string; + }): Promise<{ pagination: CBAppPagination; data: CBAppAddress[]; }> { if (params?.paginationURL) { return this.getPrivate(params.paginationURL); } - return this.getPrivate(`/v2/accounts/${params.accountId}/addresses`); + return this.getPrivate(`/v2/accounts/${params.account_id}/addresses`); } /** @@ -119,11 +122,11 @@ export class CBAppClient extends BaseRestClient { * * !! An address can only be associated with one account. See Create Address to create new addresses. */ - getAddress(params: { accountId: string; addressId: string }): Promise<{ + getAddress(params: { account_id: string; addressId: string }): Promise<{ data: CBAppAddress; }> { return this.getPrivate( - `/v2/accounts/${params.accountId}/addresses/${params.addressId}`, + `/v2/accounts/${params.account_id}/addresses/${params.addressId}`, ); } @@ -137,7 +140,7 @@ export class CBAppClient extends BaseRestClient { * If you are paginating, provide the paginationURL value from the previous response and you will receive the next page of transactions. */ getAddressTransactions(params: { - accountId: string; + account_id: string; addressId: string; paginationURL?: string; }): Promise<{ @@ -148,7 +151,7 @@ export class CBAppClient extends BaseRestClient { return this.getPrivate(params.paginationURL); } return this.getPrivate( - `/v2/accounts/${params.accountId}/addresses/${params.addressId}/transactions`, + `/v2/accounts/${params.account_id}/addresses/${params.addressId}/transactions`, ); } @@ -167,8 +170,8 @@ export class CBAppClient extends BaseRestClient { sendMoney( params: CBAppSendMoneyRequest, ): Promise<{ data: CBAppTransaction }> { - const { accountId, ...restParams } = params; - return this.postPrivate(`/v2/accounts/${accountId}/transactions`, { + const { account_id, ...restParams } = params; + return this.postPrivate(`/v2/accounts/${account_id}/transactions`, { body: restParams, }); } @@ -182,8 +185,8 @@ export class CBAppClient extends BaseRestClient { transferMoney( params: CBAppTransferMoneyRequest, ): Promise<{ data: CBAppTransaction }> { - const { accountId, ...restParams } = params; - return this.postPrivate(`/v2/accounts/${accountId}/transactions`, { + const { account_id, ...restParams } = params; + return this.postPrivate(`/v2/accounts/${account_id}/transactions`, { body: restParams, }); } @@ -197,7 +200,7 @@ export class CBAppClient extends BaseRestClient { * If you are paginating, provide the paginationURL value from the previous response and you will receive the next page of transactions. */ getTransactions(params: { - accountId: string; + account_id: string; paginationURL?: string; }): Promise<{ pagination: CBAppPagination; @@ -206,7 +209,7 @@ export class CBAppClient extends BaseRestClient { if (params?.paginationURL) { return this.getPrivate(params.paginationURL); } - return this.getPrivate(`/v2/accounts/${params.accountId}/transactions`); + return this.getPrivate(`/v2/accounts/${params.account_id}/transactions`); } /** @@ -215,13 +218,13 @@ export class CBAppClient extends BaseRestClient { * Get a single transaction for an account. */ getTransaction(params: { - accountId: string; + account_id: string; transactionId: string; }): Promise<{ data: CBAppTransaction; }> { return this.getPrivate( - `/v2/accounts/${params.accountId}/transactions/${params.transactionId}`, + `/v2/accounts/${params.account_id}/transactions/${params.transactionId}`, ); } @@ -239,8 +242,8 @@ export class CBAppClient extends BaseRestClient { depositFunds( params: CBAppDepositFundsRequest, ): Promise<{ data: CBAppDepositWithdrawal }> { - const { accountId, ...restParams } = params; - return this.postPrivate(`/v2/accounts/${accountId}/deposits`, { + const { account_id, ...restParams } = params; + return this.postPrivate(`/v2/accounts/${account_id}/deposits`, { body: restParams, }); } @@ -251,11 +254,11 @@ export class CBAppClient extends BaseRestClient { * Completes a deposit that is created in commit: false state. */ commitDeposit(params: { - accountId: string; - depositId: string; + account_id: string; + deposit_id: string; }): Promise<{ data: CBAppDepositWithdrawal }> { return this.postPrivate( - `/v2/accounts/${params.accountId}/deposits/${params.depositId}/commit`, + `/v2/accounts/${params.account_id}/deposits/${params.deposit_id}/commit`, ); } @@ -267,14 +270,14 @@ export class CBAppClient extends BaseRestClient { * This endpoint is paginated. In case you are calling it first time, leave paginationURL empty. * If you are paginating, provide the paginationURL value from the previous response and you will receive the next page of deposits. */ - getDeposits(params: { accountId: string; paginationURL?: string }): Promise<{ + getDeposits(params: { account_id: string; paginationURL?: string }): Promise<{ pagination: CBAppPagination; data: CBAppDepositWithdrawal[]; }> { if (params?.paginationURL) { return this.getPrivate(params.paginationURL); } - return this.getPrivate(`/v2/accounts/${params.accountId}/deposits`); + return this.getPrivate(`/v2/accounts/${params.account_id}/deposits`); } /** @@ -282,11 +285,11 @@ export class CBAppClient extends BaseRestClient { * * Get one deposit by deposit Id. */ - getDeposit(params: { accountId: string; depositId: string }): Promise<{ + getDeposit(params: { account_id: string; deposit_id: string }): Promise<{ data: CBAppDepositWithdrawal; }> { return this.getPrivate( - `/v2/accounts/${params.accountId}/deposits/${params.depositId}`, + `/v2/accounts/${params.account_id}/deposits/${params.deposit_id}`, ); } @@ -304,8 +307,8 @@ export class CBAppClient extends BaseRestClient { withdrawFunds( params: CBAppWithdrawFundsRequest, ): Promise<{ data: CBAppDepositWithdrawal }> { - const { accountId, ...restParams } = params; - return this.postPrivate(`/v2/accounts/${accountId}/withdrawals`, { + const { account_id, ...restParams } = params; + return this.postPrivate(`/v2/accounts/${account_id}/withdrawals`, { body: restParams, }); } @@ -316,11 +319,11 @@ export class CBAppClient extends BaseRestClient { * Completes a withdrawal that is created in commit: false state. */ commitWithdrawal(params: { - accountId: string; - withdrawalId: string; + account_id: string; + withdrawal_id: string; }): Promise<{ data: CBAppDepositWithdrawal }> { return this.postPrivate( - `/v2/accounts/${params.accountId}/withdrawals/${params.withdrawalId}/commit`, + `/v2/accounts/${params.account_id}/withdrawals/${params.withdrawal_id}/commit`, ); } @@ -333,7 +336,7 @@ export class CBAppClient extends BaseRestClient { * If you are paginating, provide the paginationURL value from the previous response and you will receive the next page of withdrawals. */ getWithdrawals(params: { - accountId: string; + account_id: string; paginationURL?: string; }): Promise<{ pagination: CBAppPagination; @@ -342,7 +345,7 @@ export class CBAppClient extends BaseRestClient { if (params?.paginationURL) { return this.getPrivate(params.paginationURL); } - return this.getPrivate(`/v2/accounts/${params.accountId}/withdrawals`); + return this.getPrivate(`/v2/accounts/${params.account_id}/withdrawals`); } /** @@ -350,11 +353,14 @@ export class CBAppClient extends BaseRestClient { * * Get a single withdrawal. */ - getWithdrawal(params: { accountId: string; withdrawalId: string }): Promise<{ + getWithdrawal(params: { + account_id: string; + withdrawal_id: string; + }): Promise<{ data: CBAppDepositWithdrawal; }> { return this.getPrivate( - `/v2/accounts/${params.accountId}/withdrawals/${params.withdrawalId}`, + `/v2/accounts/${params.account_id}/withdrawals/${params.withdrawal_id}`, ); } diff --git a/src/types/request/coinbase-app-client.ts b/src/types/request/coinbase-app-client.ts index 4987a8c..19bc77b 100644 --- a/src/types/request/coinbase-app-client.ts +++ b/src/types/request/coinbase-app-client.ts @@ -17,7 +17,7 @@ */ export interface CBAppSendMoneyRequest { - accountId: string; + account_id: string; type: 'send'; to: string; amount: string; @@ -31,7 +31,7 @@ export interface CBAppSendMoneyRequest { } export interface CBAppTransferMoneyRequest { - accountId: string; + account_id: string; type: 'transfer'; to: string; amount: string; @@ -45,7 +45,7 @@ export interface CBAppTransferMoneyRequest { */ export interface CBAppDepositFundsRequest { - accountId: string; + account_id: string; amount: string; currency: string; payment_method: string; @@ -59,7 +59,7 @@ export interface CBAppDepositFundsRequest { */ export interface CBAppWithdrawFundsRequest { - accountId: string; + account_id: string; amount: string; currency: string; payment_method: string; diff --git a/test/REST/private.test.ts b/test/CBAdvancedClient/private.test.ts similarity index 56% rename from test/REST/private.test.ts rename to test/CBAdvancedClient/private.test.ts index 1411c4c..f02211a 100644 --- a/test/REST/private.test.ts +++ b/test/CBAdvancedClient/private.test.ts @@ -1,48 +1,48 @@ -import { AdvancedTradeClient } from '../../src/index.js'; +import { CBAdvancedTradeClient } from '../../src/index.js'; -describe('REST PRIVATE', () => { +describe('CBAdvancedTradeClient PRIVATE', () => { const account = { - key: process.env.API_KEY, - secret: process.env.API_SECRET, - passphrase: process.env.API_PASSPHRASE, + key: process.env.API_KEY_NAME, + secret: process.env.API_PRIVATE_KEY, }; - const rest = new AdvancedTradeClient({ + const rest = new CBAdvancedTradeClient({ apiKeyName: account.key, apiPrivateKey: account.secret, - apiPassphrase: account.passphrase, }); it('should have credentials to test with', () => { expect(account.key).toBeDefined(); expect(account.secret).toBeDefined(); - expect(account.passphrase).toBeDefined(); }); describe('public endpoints', () => { it('should succeed making a GET request', async () => { - const res = await rest.getTickers(); - expect(res.data.ticker).toMatchObject(expect.any(Array)); + const res = await rest.getPublicProducts(); + expect(res.products).toMatchObject(expect.any(Array)); }); }); describe('private endpoints', () => { describe('GET requests', () => { test('without params', async () => { - const res = await rest.getBalances(); - // console.log('res without', res); + const res = await rest.getAccounts(); + // console.log('res without params', res); expect(res).toMatchObject({ - code: '200000', - data: expect.any(Array), + accounts: expect.any(Array), }); }); test('with params', async () => { - const res = await rest.getBalances({ currency: 'USDT' }); - // console.log('res with', res); + const res = await rest.getMarketTrades({ + product_id: 'BTC-USDT', + limit: 10, + }); + //console.log('res with params', res); expect(res).toMatchObject({ - code: '200000', - data: expect.any(Array), + trades: expect.any(Array), + best_bid: expect.any(String), + best_ask: expect.any(String), }); }); }); @@ -50,9 +50,8 @@ describe('REST PRIVATE', () => { describe('POST requests', () => { test('with params as request body', async () => { try { - const res = await rest.updateMarginLeverageV3({ - leverage: '1', - symbol: 'BTC-USDT', + const res = await rest.setIntradayMarginSetting({ + setting: 'INTRADAY_MARGIN_SETTING_STANDARD', }); console.log(`res "${expect.getState().currentTestName}"`, res); @@ -64,8 +63,9 @@ describe('REST PRIVATE', () => { // console.log(`err "${expect.getState().currentTestName}"`, e?.body); const responseBody = e?.body; expect(responseBody).toMatchObject({ - code: '400007', - msg: expect.stringContaining('more permission'), + error: expect.any(String), + error_details: 'Missing required scopes', + message: 'Missing required scopes', }); } }); diff --git a/test/CBAdvancedClient/public.test.ts b/test/CBAdvancedClient/public.test.ts new file mode 100644 index 0000000..03d4584 --- /dev/null +++ b/test/CBAdvancedClient/public.test.ts @@ -0,0 +1,12 @@ +import { CBAdvancedTradeClient } from '../../src/index.js'; + +describe('CBAdvancedTradeClient PUBLIC', () => { + const rest = new CBAdvancedTradeClient(); + + describe('public endpoints', () => { + it('should succeed making a GET request', async () => { + const res = await rest.getPublicProducts(); + expect(res.products).toMatchObject(expect.any(Array)); + }); + }); +}); diff --git a/test/CBAppClient/private.test.ts b/test/CBAppClient/private.test.ts new file mode 100644 index 0000000..1cc352c --- /dev/null +++ b/test/CBAppClient/private.test.ts @@ -0,0 +1,80 @@ +import { CBAppClient } from '../../src/index.js'; + +let accId = ''; + +describe('CBAppClient PRIVATE', () => { + const account = { + key: process.env.API_KEY_NAME, + secret: process.env.API_PRIVATE_KEY, + }; + + const rest = new CBAppClient({ + apiKeyName: account.key, + apiPrivateKey: account.secret, + }); + + it('should have credentials to test with', () => { + expect(account.key).toBeDefined(); + expect(account.secret).toBeDefined(); + }); + + describe('public endpoints', () => { + it('should succeed making a GET request', async () => { + const res = await rest.getFiatCurrencies(); + expect(res).toMatchObject({ + data: expect.any(Array), + }); + }); + }); + + describe('private endpoints', () => { + describe('GET requests', () => { + test('without params', async () => { + const res = await rest.getAccounts(); + expect(res).toMatchObject({ + pagination: expect.any(Object), + data: expect.any(Array), + }); + accId = res.data[0].id; + }); + + test('with params', async () => { + const res = await rest.getAccount({ + account_id: accId, + }); + //console.log('res with params', res); + expect(res).toMatchObject({ + data: expect.any(Object), + }); + }); + }); + + describe('POST requests', () => { + test('with params as request body', async () => { + try { + const res = await rest.transferMoney({ + account_id: 'cff1d5a6-9f09-5645-8919-b998e7055170', + type: 'transfer', + to: '58542935-67b5-56e1-a3f9-42686e07fa40', + amount: '1', + currency: 'USDT', + }); + + console.log(`res "${expect.getState().currentTestName}"`, res); + expect(res).toMatchObject({ + whatever: true, + }); + } catch (e: any) { + // These are deliberatly restricted API keys. If the response is a permission error, it confirms the sign + request was OK and permissions were denied. + // console.log(`err "${expect.getState().currentTestName}"`, e?.body); + const responseBody = e?.body.errors[0]; + expect(responseBody).toMatchObject({ + id: 'invalid_scope', + message: expect.any(String), + url: expect.any(String), + }); + } + }); + }); + }); +}); diff --git a/test/CBAppClient/public.test.ts b/test/CBAppClient/public.test.ts new file mode 100644 index 0000000..106a273 --- /dev/null +++ b/test/CBAppClient/public.test.ts @@ -0,0 +1,14 @@ +import { CBAppClient } from '../../src/index.js'; + +describe('CBAppClient PUBLIC', () => { + const rest = new CBAppClient(); + + describe('public endpoints', () => { + it('should succeed making a GET request', async () => { + const res = await rest.getFiatCurrencies(); + expect(res).toMatchObject({ + data: expect.any(Array), + }); + }); + }); +}); diff --git a/test/REST/public.test.ts b/test/REST/public.test.ts deleted file mode 100644 index 462c0ab..0000000 --- a/test/REST/public.test.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { AdvancedTradeClient } from '../../src/index.js'; - -describe('REST PUBLIC', () => { - const rest = new AdvancedTradeClient(); - - describe('public endpoints', () => { - it('should succeed making a GET request', async () => { - const res = await rest.getTickers(); - expect(res.data.ticker).toMatchObject(expect.any(Array)); - }); - - it('should return 24hr stats', async () => { - const res = await rest.get24hrStats({ - symbol: 'BTC-USDT', - }); - - // console.log('res "${expect.getState().currentTestName}"', res); - expect(res.data).toMatchObject({ - averagePrice: expect.any(String), - changePrice: expect.any(String), - volValue: expect.any(String), - }); - }); - }); -});