Skip to content

Commit 49feeeb

Browse files
feat: WT-1766 Add new isSwapAvailable checkout sdk method (#959)
Co-authored-by: Deepti Luthra <[email protected]>
1 parent e51457e commit 49feeeb

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

packages/checkout/sdk/src/Checkout.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import { FiatRampService } from './fiatRamp';
4545
import { FiatRampParams, ExchangeType } from './types/fiatRamp';
4646
import { getItemRequirementsFromRequirements } from './smartCheckout/itemRequirements';
4747
import { CheckoutErrorType } from './errors';
48+
import { availabilityService } from './availability';
4849

4950
jest.mock('./connect');
5051
jest.mock('./network');
@@ -61,6 +62,7 @@ jest.mock('./smartCheckout/cancel');
6162
jest.mock('./smartCheckout');
6263
jest.mock('./fiatRamp');
6364
jest.mock('./smartCheckout/itemRequirements');
65+
jest.mock('./availability');
6466

6567
describe('Connect', () => {
6668
let providerMock: ExternalProvider;
@@ -886,4 +888,23 @@ describe('Connect', () => {
886888
expect(feeEstimateMock).toBeCalledTimes(1);
887889
});
888890
});
891+
892+
describe('isSwapAvailable', () => {
893+
let checkout: Checkout;
894+
895+
beforeEach(() => {
896+
(availabilityService as jest.Mock).mockReturnValue({
897+
checkDexAvailability: jest.fn().mockResolvedValue(true),
898+
});
899+
checkout = new Checkout({
900+
baseConfig: { environment: Environment.PRODUCTION },
901+
});
902+
});
903+
904+
it('should call availability.checkDexAvailability', async () => {
905+
await checkout.isSwapAvailable();
906+
907+
expect(checkout.availability.checkDexAvailability).toBeCalledTimes(1);
908+
});
909+
});
889910
});

packages/checkout/sdk/src/Checkout.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import { CancelParams } from './types/cancel';
6060
import { FiatRampService, FiatRampWidgetParams } from './fiatRamp';
6161
import { getItemRequirementsFromRequirements } from './smartCheckout/itemRequirements';
6262
import { CheckoutError, CheckoutErrorType } from './errors';
63+
import { AvailabilityService, availabilityService } from './availability';
6364

6465
const SANDBOX_CONFIGURATION = {
6566
baseConfig: {
@@ -74,6 +75,8 @@ export class Checkout {
7475

7576
private readOnlyProviders: Map<ChainId, ethers.providers.JsonRpcProvider>;
7677

78+
readonly availability: AvailabilityService;
79+
7780
/**
7881
* Constructs a new instance of the CheckoutModule class.
7982
* @param {CheckoutModuleConfiguration} [config=SANDBOX_CONFIGURATION] - The configuration object for the CheckoutModule.
@@ -84,6 +87,7 @@ export class Checkout {
8487
this.config = new CheckoutConfiguration(config);
8588
this.fiatRampService = new FiatRampService(this.config);
8689
this.readOnlyProviders = new Map<ChainId, ethers.providers.JsonRpcProvider>();
90+
this.availability = availabilityService(this.config.isDevelopment, this.config.isProduction);
8791
}
8892

8993
/**
@@ -472,4 +476,12 @@ export class Checkout {
472476
public async getExchangeFeeEstimate(): Promise<OnRampProviderFees> {
473477
return await this.fiatRampService.feeEstimate();
474478
}
479+
480+
/**
481+
* Fetches Swap widget availability.
482+
* @returns {Promise<boolean>} - A promise that resolves to a boolean.
483+
*/
484+
public async isSwapAvailable(): Promise<boolean> {
485+
return this.availability.checkDexAvailability();
486+
}
475487
}

0 commit comments

Comments
 (0)