Skip to content

Commit

Permalink
feat(): Added tests
Browse files Browse the repository at this point in the history
changed param types for cbapp(accountid -> account_id)
  • Loading branch information
JJ-Cro committed Sep 18, 2024
1 parent ec5c0cb commit 52bbffb
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 88 deletions.
78 changes: 42 additions & 36 deletions src/CBAppClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}

/**
Expand All @@ -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,
});
}
Expand All @@ -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`);
}

/**
Expand All @@ -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}`,
);
}

Expand All @@ -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<{
Expand All @@ -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`,
);
}

Expand All @@ -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,
});
}
Expand All @@ -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,
});
}
Expand All @@ -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;
Expand All @@ -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`);
}

/**
Expand All @@ -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}`,
);
}

Expand All @@ -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,
});
}
Expand All @@ -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`,
);
}

Expand All @@ -267,26 +270,26 @@ 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`);
}

/**
* Show Deposit
*
* 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}`,
);
}

Expand All @@ -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,
});
}
Expand All @@ -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`,
);
}

Expand All @@ -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;
Expand All @@ -342,19 +345,22 @@ 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`);
}

/**
* Show Withdrawal
*
* 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}`,
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/types/request/coinbase-app-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

export interface CBAppSendMoneyRequest {
accountId: string;
account_id: string;
type: 'send';
to: string;
amount: string;
Expand All @@ -31,7 +31,7 @@ export interface CBAppSendMoneyRequest {
}

export interface CBAppTransferMoneyRequest {
accountId: string;
account_id: string;
type: 'transfer';
to: string;
amount: string;
Expand All @@ -45,7 +45,7 @@ export interface CBAppTransferMoneyRequest {
*/

export interface CBAppDepositFundsRequest {
accountId: string;
account_id: string;
amount: string;
currency: string;
payment_method: string;
Expand All @@ -59,7 +59,7 @@ export interface CBAppDepositFundsRequest {
*/

export interface CBAppWithdrawFundsRequest {
accountId: string;
account_id: string;
amount: string;
currency: string;
payment_method: string;
Expand Down
46 changes: 23 additions & 23 deletions test/REST/private.test.ts → test/CBAdvancedClient/private.test.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,57 @@
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),
});
});
});

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);
Expand All @@ -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',
});
}
});
Expand Down
12 changes: 12 additions & 0 deletions test/CBAdvancedClient/public.test.ts
Original file line number Diff line number Diff line change
@@ -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));
});
});
});
Loading

0 comments on commit 52bbffb

Please sign in to comment.