Skip to content

Commit

Permalink
fix: use URLSearchParams
Browse files Browse the repository at this point in the history
  • Loading branch information
micaelae committed Oct 28, 2024
1 parent 3157b10 commit 31e9121
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
10 changes: 7 additions & 3 deletions ui/pages/bridge/bridge.util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import {
jest.mock('../../../shared/lib/fetch-with-cache');

describe('Bridge utils', () => {
beforeEach(() => {
jest.clearAllMocks();
});

describe('fetchBridgeFeatureFlags', () => {
it('should fetch bridge feature flags successfully', async () => {
const mockResponse = {
Expand Down Expand Up @@ -166,7 +170,7 @@ describe('Bridge utils', () => {
});

expect(fetchWithCache).toHaveBeenCalledWith({
url: 'https://bridge.api.cx.metamask.io/getQuote?walletAddress=0x123&srcChainId=1&destChainId=10&srcTokenAddress=0x0000000000000000000000000000000000000000&destTokenAddress=0x0000000000000000000000000000000000000000&srcTokenAmount=20000&slippage=0.5',
url: 'https://bridge.api.cx.metamask.io/getQuote?walletAddress=0x123&srcChainId=1&destChainId=10&srcTokenAddress=0x0000000000000000000000000000000000000000&destTokenAddress=0x0000000000000000000000000000000000000000&srcTokenAmount=20000&slippage=0.5&insufficientBal=false&resetApproval=false',
fetchOptions: {
method: 'GET',
headers: { 'X-Client-Id': 'extension' },
Expand Down Expand Up @@ -194,7 +198,7 @@ describe('Bridge utils', () => {
});

expect(fetchWithCache).toHaveBeenCalledWith({
url: 'https://bridge.api.cx.metamask.io/getQuote?walletAddress=0x123&srcChainId=1&destChainId=10&srcTokenAddress=0x0000000000000000000000000000000000000000&destTokenAddress=0x0000000000000000000000000000000000000000&srcTokenAmount=20000&slippage=0.5',
url: 'https://bridge.api.cx.metamask.io/getQuote?walletAddress=0x123&srcChainId=1&destChainId=10&srcTokenAddress=0x0000000000000000000000000000000000000000&destTokenAddress=0x0000000000000000000000000000000000000000&srcTokenAmount=20000&slippage=0.5&insufficientBal=false&resetApproval=false',
fetchOptions: {
method: 'GET',
headers: { 'X-Client-Id': 'extension' },
Expand Down Expand Up @@ -224,7 +228,7 @@ describe('Bridge utils', () => {
});

expect(fetchWithCache).toHaveBeenCalledWith({
url: 'https://bridge.api.cx.metamask.io/getQuote?walletAddress=0x123&srcChainId=1&destChainId=10&srcTokenAddress=0x0000000000000000000000000000000000000000&destTokenAddress=0x0000000000000000000000000000000000000000&srcTokenAmount=20000&slippage=0.5',
url: 'https://bridge.api.cx.metamask.io/getQuote?walletAddress=0x123&srcChainId=1&destChainId=10&srcTokenAddress=0x0000000000000000000000000000000000000000&destTokenAddress=0x0000000000000000000000000000000000000000&srcTokenAmount=20000&slippage=0.5&insufficientBal=false&resetApproval=false',
fetchOptions: {
method: 'GET',
headers: { 'X-Client-Id': 'extension' },
Expand Down
15 changes: 12 additions & 3 deletions ui/pages/bridge/bridge.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,18 @@ export async function fetchBridgeTokens(
export async function fetchBridgeQuotes(
request: QuoteRequest,
): Promise<QuoteResponse[]> {
const url = `${BRIDGE_API_BASE_URL}/getQuote?${Object.entries(request)
.map(([k, v]) => `${k}=${v}`)
.join('&')}`;
const queryParams = new URLSearchParams({
walletAddress: request.walletAddress,
srcChainId: request.srcChainId.toString(),
destChainId: request.destChainId.toString(),
srcTokenAddress: request.srcTokenAddress,
destTokenAddress: request.destTokenAddress,
srcTokenAmount: request.srcTokenAmount,
slippage: request.slippage.toString(),
insufficientBal: request.insufficientBal ? 'true' : 'false',
resetApproval: request.resetApproval ? 'true' : 'false',
});
const url = `${BRIDGE_API_BASE_URL}/getQuote?${queryParams}`;
const quotes = await fetchWithCache({
url,
fetchOptions: { method: 'GET', headers: CLIENT_ID_HEADER },
Expand Down

0 comments on commit 31e9121

Please sign in to comment.