Skip to content
Open
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
39 changes: 34 additions & 5 deletions src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
type GasRecommendationResponse,
type GetStatusRequest,
type LiFiStep,
type QuoteRequest,
type QuoteRequest as QuoteRequestFromAmount,
type RelayRequest,
type RelayResponse,
type RelayResponseData,
Expand Down Expand Up @@ -42,6 +42,13 @@ import { request } from '../request.js'
import { isRoutesRequest, isStep } from '../typeguards.js'
import { withDedupe } from '../utils/withDedupe.js'

type QuoteRequest =
| QuoteRequestFromAmount
| (Omit<QuoteRequestFromAmount, 'fromAmount'> & {
toAmount: string
fromAmount?: never
})

/**
* Get a quote for a token transfer
* @param params - The configuration of the requested quote
Expand All @@ -57,7 +64,6 @@ export const getQuote = async (
'fromChain',
'fromToken',
'fromAddress',
'fromAmount',
'toChain',
'toToken',
]
Expand All @@ -70,6 +76,29 @@ export const getQuote = async (
)
}
}
if (
(!('fromAmount' in params) || !params.fromAmount) &&
(!('toAmount' in params) || !params.toAmount)
) {
throw new SDKError(
new ValidationError(
'Required parameter "fromAmount" or "toAmount" is missing.'
)
)
}
if (
'fromAmount' in params &&
'toAmount' in params &&
params.fromAmount &&
// @ts-expect-error type-agnostic runtime check
params.toAmount
) {
throw new SDKError(
new ValidationError(
'Cannot provide both "fromAmount" and "toAmount" parameters.'
)
)
}
const _config = config.get()
// apply defaults
params.integrator ??= _config.integrator
Expand All @@ -91,7 +120,7 @@ export const getQuote = async (
}

return await request<LiFiStep>(
`${_config.apiUrl}/quote?${new URLSearchParams(
`${_config.apiUrl}/${params.fromAmount ? 'quote' : 'quote/toAmount'}?${new URLSearchParams(
params as unknown as Record<string, string>
)}`,
{
Expand Down Expand Up @@ -259,10 +288,10 @@ export const getStatus = async (
* @returns Relayer quote for a token transfer
*/
export const getRelayerQuote = async (
params: QuoteRequest,
params: QuoteRequestFromAmount,
options?: RequestOptions
): Promise<LiFiStep> => {
const requiredParameters: Array<keyof QuoteRequest> = [
const requiredParameters: Array<keyof QuoteRequestFromAmount> = [
'fromChain',
'fromToken',
'fromAddress',
Expand Down
24 changes: 23 additions & 1 deletion src/services/api.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ describe('ApiService', () => {
const fromAmount = '1000'
const toChain = ChainId.POL
const toToken = 'MATIC'
const toAmount = '1000'

describe('user input is invalid', () => {
it('throw an error', async () => {
Expand Down Expand Up @@ -254,7 +255,28 @@ describe('ApiService', () => {
})
).rejects.toThrowError(
new SDKError(
new ValidationError('Required parameter "fromAmount" is missing.')
new ValidationError(
'Required parameter "fromAmount" or "toAmount" is missing.'
)
)
)

await expect(
ApiService.getQuote({
fromChain,
fromToken,
fromAddress,
fromAmount,
toChain,
toToken,
// @ts-expect-error test runtime check
toAmount,
})
).rejects.toThrowError(
new SDKError(
new ValidationError(
'Cannot provide both "fromAmount" and "toAmount" parameters.'
)
)
)

Expand Down