Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Ability to get withdrawal fee estimate #284

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/withdraw/WithdrawAPI.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import nock from 'nock';
import {WithdrawAPI, CryptoWithdrawal} from './WithdrawAPI';
import {WithdrawAPI, CryptoWithdrawal, WithdrawalFeeEstimate} from './WithdrawAPI';

describe('WithdrawAPI', () => {
const currency = 'BTC';
const cryptoAddress = '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa';
const expectedFeeEstimate: WithdrawalFeeEstimate = {fee: '.01'};
const withdrawalId = 'fake-withdrawal-id-abcd-01234';

afterAll(() => nock.cleanAll());
Expand All @@ -18,13 +21,14 @@ describe('WithdrawAPI', () => {
id: withdrawalId,
};
return [200, JSON.stringify(response)];
});
})
.get(WithdrawAPI.URL.WITHDRAWALS.FEE_ESTIMATE)
.query({crypto_address: cryptoAddress, currency})
.reply(200, JSON.stringify(expectedFeeEstimate));
});

describe('postCryptoWithdrawal', () => {
const amount = 1.23;
const currency = 'BTC';
const cryptoAddress = '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa';
const destinationTag = 'satoshi-nakamoto';

it('creates a new withdrawal to crypto address', async () => {
Expand All @@ -42,4 +46,11 @@ describe('WithdrawAPI', () => {
expect(withdrawal).toEqual({amount, currency, id: withdrawalId});
});
});

describe('getFeeEstimate', () => {
it('gets a fee estimate', async () => {
const estimate = await global.client.rest.withdraw.getFeeEstimate(currency, cryptoAddress);
expect(estimate).toEqual(expectedFeeEstimate);
});
});
});
20 changes: 20 additions & 0 deletions src/withdraw/WithdrawAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ interface CryptoWithdrawalRequest {
no_destination_tag?: boolean;
}

export interface WithdrawalFeeEstimate {
fee: string;
}

export class WithdrawAPI {
static readonly URL = {
WITHDRAWALS: {
CRYPTO: '/withdrawals/crypto',
FEE_ESTIMATE: '/withdrawals/fee-estimate',
},
};

Expand Down Expand Up @@ -57,4 +62,19 @@ export class WithdrawAPI {
const response = await this.apiClient.post(resource, withdrawal);
return response.data;
}

/**
* Gets the network fee estimate when sending to the given address.
*
* @param currency - The type of currency
* @param cryptoAddress - A crypto address of the recipient
* @see https://docs.pro.coinbase.com/#fee-estimate
*/
async getFeeEstimate(currency: string, cryptoAddress: string): Promise<WithdrawalFeeEstimate> {
const resource = WithdrawAPI.URL.WITHDRAWALS.FEE_ESTIMATE;
const response = await this.apiClient.get<WithdrawalFeeEstimate>(resource, {
params: {crypto_address: cryptoAddress, currency},
});
return response.data;
}
}