Skip to content

Commit 798be2f

Browse files
committed
feat(api): support quotes to amount
1 parent b66dc5c commit 798be2f

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

src/services/api.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
type GasRecommendationResponse,
1212
type GetStatusRequest,
1313
type LiFiStep,
14-
type QuoteRequest,
14+
type QuoteRequest as QuoteRequestFromAmount,
1515
type RelayRequest,
1616
type RelayResponse,
1717
type RelayResponseData,
@@ -42,6 +42,13 @@ import { request } from '../request.js'
4242
import { isRoutesRequest, isStep } from '../typeguards.js'
4343
import { withDedupe } from '../utils/withDedupe.js'
4444

45+
type QuoteRequest =
46+
| QuoteRequestFromAmount
47+
| (Omit<QuoteRequestFromAmount, 'fromAmount'> & {
48+
toAmount: string
49+
fromAmount?: never
50+
})
51+
4552
/**
4653
* Get a quote for a token transfer
4754
* @param params - The configuration of the requested quote
@@ -57,7 +64,6 @@ export const getQuote = async (
5764
'fromChain',
5865
'fromToken',
5966
'fromAddress',
60-
'fromAmount',
6167
'toChain',
6268
'toToken',
6369
]
@@ -70,6 +76,29 @@ export const getQuote = async (
7076
)
7177
}
7278
}
79+
if (
80+
(!('fromAmount' in params) || !params.fromAmount) &&
81+
(!('toAmount' in params) || !params.toAmount)
82+
) {
83+
throw new SDKError(
84+
new ValidationError(
85+
'Required parameter "fromAmount" or "toAmount" is missing.'
86+
)
87+
)
88+
}
89+
if (
90+
'fromAmount' in params &&
91+
'toAmount' in params &&
92+
params.fromAmount &&
93+
// @ts-expect-error type-agnostic runtime check
94+
params.toAmount
95+
) {
96+
throw new SDKError(
97+
new ValidationError(
98+
'Cannot provide both "fromAmount" and "toAmount" parameters.'
99+
)
100+
)
101+
}
73102
const _config = config.get()
74103
// apply defaults
75104
params.integrator ??= _config.integrator
@@ -91,7 +120,7 @@ export const getQuote = async (
91120
}
92121

93122
return await request<LiFiStep>(
94-
`${_config.apiUrl}/quote?${new URLSearchParams(
123+
`${_config.apiUrl}/${'fromAmount' in params ? 'quote' : 'quote/toAmount'}?${new URLSearchParams(
95124
params as unknown as Record<string, string>
96125
)}`,
97126
{
@@ -259,10 +288,10 @@ export const getStatus = async (
259288
* @returns Relayer quote for a token transfer
260289
*/
261290
export const getRelayerQuote = async (
262-
params: QuoteRequest,
291+
params: QuoteRequestFromAmount,
263292
options?: RequestOptions
264293
): Promise<LiFiStep> => {
265-
const requiredParameters: Array<keyof QuoteRequest> = [
294+
const requiredParameters: Array<keyof QuoteRequestFromAmount> = [
266295
'fromChain',
267296
'fromToken',
268297
'fromAddress',

src/services/api.unit.spec.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ describe('ApiService', () => {
195195
const fromAmount = '1000'
196196
const toChain = ChainId.POL
197197
const toToken = 'MATIC'
198+
const toAmount = '1000'
198199

199200
describe('user input is invalid', () => {
200201
it('throw an error', async () => {
@@ -254,7 +255,28 @@ describe('ApiService', () => {
254255
})
255256
).rejects.toThrowError(
256257
new SDKError(
257-
new ValidationError('Required parameter "fromAmount" is missing.')
258+
new ValidationError(
259+
'Required parameter "fromAmount" or "toAmount" is missing.'
260+
)
261+
)
262+
)
263+
264+
await expect(
265+
ApiService.getQuote({
266+
fromChain,
267+
fromToken,
268+
fromAddress,
269+
fromAmount,
270+
toChain,
271+
toToken,
272+
// @ts-expect-error test runtime check
273+
toAmount,
274+
})
275+
).rejects.toThrowError(
276+
new SDKError(
277+
new ValidationError(
278+
'Cannot provide both "fromAmount" and "toAmount" parameters.'
279+
)
258280
)
259281
)
260282

0 commit comments

Comments
 (0)