Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
feat: Add generate address endpoint (#728)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonluca committed May 24, 2022
1 parent 071e415 commit b7b45a2
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/CoinbasePro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class CoinbasePro {
SANDBOX: ClientConnection;
} = {
PRODUCTION: {
REST: 'https://api.pro.coinbase.com',
REST: 'https://api.exchange.coinbase.com',
WebSocket: 'wss://ws-feed.pro.coinbase.com',
},
SANDBOX: {
Expand Down
16 changes: 16 additions & 0 deletions src/account/AccountAPI.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import getAccount from '../test/fixtures/rest/accounts/322dfa88-e10d-4678-856d-2930eac3e62d/GET-200.json';
import getAccountHistory from '../test/fixtures/rest/accounts/322dfa88-e10d-4678-856d-2930eac3e62d/ledger/GET-200.json';
import getHolds from '../test/fixtures/rest/accounts/322dfa88-e10d-4678-856d-2930eac3e62d/holds/GET-200.json';
import genAddress from '../test/fixtures/rest/accounts/322dfa88-e10d-4678-856d-2930eac3e62d/addresses/POST-200.json';
import listAccounts from '../test/fixtures/rest/accounts/GET-200.json';
import nock from 'nock';
import {AccountAPI, AccountType} from './AccountAPI';
Expand Down Expand Up @@ -28,6 +29,12 @@ describe('AccountAPI', () => {
.get(`${AccountAPI.URL.ACCOUNTS}/322dfa88-e10d-4678-856d-2930eac3e62d/holds`)
.query(true)
.reply(200, JSON.stringify(getHolds));

nock(global.REST_URL)
.persist()
.post(`${AccountAPI.URL.COINBASE_ACCOUNT}/322dfa88-e10d-4678-856d-2930eac3e62d/addresses`)
.query(true)
.reply(200, JSON.stringify(genAddress));
});

describe('listAccounts', () => {
Expand Down Expand Up @@ -83,4 +90,13 @@ describe('AccountAPI', () => {
expect(holds).toBeDefined();
});
});

describe('generateAddress', () => {
it('generates an address for an account', async () => {
const newDepositAddress = await global.client.rest.account.generateDepositAddress(
'322dfa88-e10d-4678-856d-2930eac3e62d'
);
expect(newDepositAddress).toBeDefined();
});
});
});
39 changes: 39 additions & 0 deletions src/account/AccountAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,33 @@ export interface CoinbaseAccount {
wire_deposit_information?: WireDepositInformation;
}

export interface GeneratedAddress {
address: string;
address_info: AddressInfo;
callback_url: null;
created_at: Date;
deposit_uri: string;
exchange_deposit_address: boolean;
id: string;
name: string;
network: string;
resource: string;
resource_path: string;
updated_at: Date;
uri_scheme: string;
warnings: Warning[];
}

export interface AddressInfo {
address: string;
}

export interface Warning {
details: string;
image_url: string;
title: string;
}

export interface WireDepositInformation {
account_address: string;
account_name: string;
Expand Down Expand Up @@ -167,4 +194,16 @@ export class AccountAPI {
const response = await this.apiClient.get<CoinbaseAccount[]>(resource);
return response.data;
}

/**
* Generate a new deposit address for a given account
*
* @see https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_postcoinbaseaccountaddresses
*/
async generateDepositAddress(accountId: string): Promise<GeneratedAddress> {
const resource = `${AccountAPI.URL.COINBASE_ACCOUNT}/${accountId}/addresses`;

const response = await this.apiClient.post<GeneratedAddress>(resource);
return response.data;
}
}
4 changes: 2 additions & 2 deletions src/product/CandleBucketUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class CandleBucketUtil {
}

static getBucketsInMillis(fromInMillis: number, toInMillis: number, candleSizeInMillis: number): number[] {
const bucketsInMillis = [];
const bucketsInMillis: number[] = [];
const batch = MAXIMUM_HISTORIC_DATA_POINTS * candleSizeInMillis;

let current = fromInMillis;
Expand All @@ -88,7 +88,7 @@ export class CandleBucketUtil {
}

static getBucketsInISO(bucketsInMillis: number[]): CandleBatchBucket[] {
const bucketsInISO = [];
const bucketsInISO: CandleBatchBucket[] = [];

for (let i = 0; i < bucketsInMillis.length - 1; i += 2) {
const start = new Date(bucketsInMillis[i]).toISOString();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"id": "fc9fed1e-d25b-54d8-b52b-7fa250c9ae2d",
"address": "0xd518A6B23D8bCA15B9cC46a00Be8a818E34Ca79E",
"address_info": {
"address": "0xd518A6B23D8bCA15B9cC46a00Be8a818E34Ca79E"
},
"name": "New exchange deposit address",
"created_at": "2020-03-31T02:38:44Z",
"updated_at": "2020-03-31T02:38:44Z",
"network": "ethereum",
"uri_scheme": "ethereum",
"resource": "address",
"resource_path": "/v2/accounts/322dfa88-e10d-4678-856d-2930eac3e62d/addresses/fc9fed1e-d25b-54d8-b52b-7fa250c9ae2d",
"warnings": [
{
"title": "Only send Orchid (OXT) to this address",
"details": "Sending any other digital asset, including Ethereum (ETH), will result in permanent loss.",
"image_url": "https://dynamic-assets.coinbase.com/22b24ad9886150535671f158ccb0dd9d12089803728551c998e17e0f503484e9c38f3e8735354b5e622753684f040488b08d55b8ef5fef51592680f0c572bdfe/asset_icons/023010d790b9b1f47bc285802eafeab3d83c4de2029fe808d59935fbc54cdd7c.png"
}
],
"deposit_uri": "ethereum:0x4575f41308ec1483f3d399aa9a2826d74da13deb/transfer?address=0xd518A6B23D8bCA15B9cC46a00Be8a818E34Ca79E",
"callback_url": null,
"exchange_deposit_address": true
}
4 changes: 4 additions & 0 deletions src/test/helpers/SetupCoinbasePro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ declare global {
REST_URL: string;
}
}
/* eslint-disable no-var */
var client: CoinbasePro;
var REST_URL: string;
/* eslint-enable no-var */
}

// URL to mock a server using "nock":
Expand Down

0 comments on commit b7b45a2

Please sign in to comment.