@0x/swap-ts-sdk
Version:
0x Swap API TypeScript Client
61 lines • 2.56 kB
JavaScript
import { createClientV2 } from '../main';
describe('main', () => {
beforeAll(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.resetAllMocks();
});
afterAll(() => {
jest.useRealTimers();
});
describe('create0xV2Client', () => {
it('should create a client', async () => {
const fetchSpy = jest.spyOn(global, 'fetch').mockImplementation(async (_, _options) => {
return Promise.resolve(new Response(JSON.stringify({
ok: true,
result: {
allowanceTarget: null,
buyAmount: '100000',
buyToken: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
fees: {
integratorFee: null,
zeroExFee: null,
gasFee: null,
},
gas: null,
gasPrice: '42069',
issues: {
allowance: null,
balance: null,
slippage: null,
swapFailed: null,
},
minBuyAmount: '248428248024',
route: {
fills: [],
tokens: [],
},
sellAmount: '289434835',
sellToken: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
totalTransactionFee: null,
},
})));
});
const client = createClientV2({ apiKey: 'my-free-api-key' });
const _price = await client.swap.permit2.getPrice.query({
buyToken: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
chainId: 1,
sellAmount: '1000000000000000000',
sellToken: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
taker: '0x60B4f0e1DF30c8c0f0b0c8BEc8787E7564647a80',
txOrigin: '0x60B4f0e1DF30c8c0f0b0c8BEc8787E7564647a80',
});
await jest.runAllTimersAsync();
expect(fetchSpy).toHaveBeenCalledTimes(1);
expect(fetchSpy.mock.calls[0][0]).toContain('https://api.0x.org/trpc');
expect(fetchSpy.mock.calls[0][1]?.headers?.['0x-api-key']).toEqual('my-free-api-key');
});
});
});
//# sourceMappingURL=main.test.js.map