Skip to content

Commit

Permalink
Merge pull request #201 from reservoirprotocol/ted/relay-5408-switch-…
Browse files Browse the repository at this point in the history
…from-executeswap-to-quote-api

Switch from execute/swap to quote api
  • Loading branch information
ted-palmer authored Aug 6, 2024
2 parents a690842 + 105d1b8 commit 2c32cbd
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 36 deletions.
7 changes: 7 additions & 0 deletions .changeset/seven-numbers-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@reservoir0x/relay-kit-hooks': minor
'@reservoir0x/relay-sdk': minor
'@reservoir0x/relay-kit-ui': minor
---

Switch from execute/swap to quote api
24 changes: 12 additions & 12 deletions packages/hooks/src/hooks/useQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ import { useCallback, useMemo } from 'react'
import type { WalletClient } from 'viem'
import type { AxiosRequestConfig } from 'axios'

type ExecuteSwapBody =
paths['/execute/swap']['post']['requestBody']['content']['application/json']
type QuoteBody =
paths['/quote']['post']['requestBody']['content']['application/json']

export type ExecuteSwapResponse =
paths['/execute/swap']['post']['responses']['200']['content']['application/json']
export type QuoteResponse =
paths['/quote']['post']['responses']['200']['content']['application/json']

type QueryType = typeof useQuery<
ExecuteSwapResponse,
QuoteResponse,
DefaultError,
ExecuteSwapResponse,
QuoteResponse,
QueryKey
>
type QueryOptions = Parameters<QueryType>['0']

export const queryQuote = function (
baseApiUrl: string = MAINNET_RELAY_API,
options?: ExecuteSwapBody
options?: QuoteBody
): Promise<Execute> {
return new Promise((resolve, reject) => {
const url = new URL(`${baseApiUrl}/execute/swap`)
const url = new URL(`${baseApiUrl}/quote`)
axiosPostFetcher(url.href, options)
.then((response) => {
const request: AxiosRequestConfig = {
Expand All @@ -59,7 +59,7 @@ export type onProgress = (data: ProgressData) => void
export default function (
client?: RelayClient,
wallet?: WalletClient | AdaptedWallet,
options?: ExecuteSwapBody,
options?: QuoteBody,
onRequest?: () => void,
onResponse?: (data: Execute) => void,
queryOptions?: Partial<QueryOptions>
Expand All @@ -68,8 +68,8 @@ export default function (
queryKey: ['useQuote', options],
queryFn: () => {
onRequest?.()
if (options && client?.source && !options.source) {
options.source = client.source
if (options && client?.source && !options.referrer) {
options.referrer = client.source
}
const promise = queryQuote(client?.baseApiUrl, options)
promise.then((response: any) => {
Expand Down Expand Up @@ -114,7 +114,7 @@ export default function (
data: response.error ? undefined : response.data,
executeQuote
} as Omit<ReturnType<QueryType>, 'data'> & {
data?: ExecuteSwapResponse
data?: QuoteResponse
executeQuote: (onProgress: onProgress) => Promise<Execute> | undefined
}),
[
Expand Down
2 changes: 1 addition & 1 deletion packages/hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ export {
//types
export type { CurrencyList, Currency } from './hooks/useTokenList.js'
export type { PriceResponse } from './hooks/usePrice.js'
export type { ExecuteSwapResponse } from './hooks/useQuote.js'
export type { QuoteResponse } from './hooks/useQuote.js'
4 changes: 2 additions & 2 deletions packages/sdk/src/actions/getQuote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('Should test the getQuote action.', () => {

expect(axiosRequestSpy).toHaveBeenCalledWith(
expect.objectContaining({
url: expect.stringContaining('execute/swap'),
url: expect.stringContaining('quote'),
data: expect.objectContaining({
user: '0x0000000000000000000000000000000000000000',
destinationCurrency: '0x0000000000000000000000000000000000000000',
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('Should test the getQuote action.', () => {

expect(axiosRequestSpy).toHaveBeenCalledWith(
expect.objectContaining({
url: expect.stringContaining('execute/swap'),
url: expect.stringContaining('quote'),
data: expect.objectContaining({
user: '0x0000000000000000000000000000000000000000',
destinationCurrency: '0x0000000000000000000000000000000000000000',
Expand Down
22 changes: 11 additions & 11 deletions packages/sdk/src/actions/getQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { isViemWalletClient } from '../utils/viemWallet.js'
import { getClient } from '../client.js'
import type { AdaptedWallet, Execute, paths } from '../types/index.js'

export type ExecuteBody = NonNullable<
paths['/execute/swap']['post']['requestBody']['content']['application/json']
export type QuoteBody = NonNullable<
paths['/quote']['post']['requestBody']['content']['application/json']
>
export type ExecuteBodyOptions = Omit<
ExecuteBody,
export type QuoteBodyOptions = Omit<
QuoteBody,
| 'destinationChainId'
| 'originChainId'
| 'originCurrency'
Expand All @@ -30,12 +30,12 @@ export type GetQuoteParameters = {
currency: string
toChainId: number
toCurrency: string
tradeType: ExecuteBodyOptions['tradeType']
tradeType: QuoteBodyOptions['tradeType']
wallet?: AdaptedWallet | WalletClient
amount?: string
recipient?: Address
options?: Omit<ExecuteBodyOptions, 'user' | 'source' | 'txs' | 'tradeType'>
txs?: (NonNullable<ExecuteBody['txs']>[0] | SimulateContractRequest)[]
options?: Omit<QuoteBodyOptions, 'user' | 'source' | 'txs' | 'tradeType'>
txs?: (NonNullable<QuoteBody['txs']>[0] | SimulateContractRequest)[]
}

/**
Expand Down Expand Up @@ -73,7 +73,7 @@ export async function getQuote(
caller = await adaptedWallet.address()
}

let preparedTransactions: ExecuteBody['txs']
let preparedTransactions: QuoteBody['txs']
if (txs && txs.length > 0) {
preparedTransactions = txs.map((tx) => {
if (isSimulateContractRequest(tx)) {
Expand All @@ -85,7 +85,7 @@ export async function getQuote(
})
}

const query: ExecuteBody = {
const query: QuoteBody = {
user: caller || zeroAddress,
destinationCurrency: toCurrency,
destinationChainId: toChainId,
Expand All @@ -94,13 +94,13 @@ export async function getQuote(
amount,
recipient: recipient ? (recipient as string) : caller ?? zeroAddress,
tradeType,
source: client.source || undefined,
referrer: client.source || undefined,
txs: preparedTransactions,
...options
}

const request: AxiosRequestConfig = {
url: `${client.baseApiUrl}/execute/swap`,
url: `${client.baseApiUrl}/quote`,
method: 'post',
data: query
}
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/types/Execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export type CheckApi = NonNullable<
paths['/execute/call/v2']['post']['responses']['200']['content']['application/json']['steps']
>['0']['items']
>[0]['check']
export type ExecuteDetails = NonNullable<
paths['/execute/swap']['post']['responses']['200']['content']['application/json']['details']
export type QuoteDetails = NonNullable<
paths['/quote']['post']['responses']['200']['content']['application/json']['details']
>

export type TransactionStepState = 'confirming' | 'validating' | 'complete'
Expand All @@ -26,7 +26,7 @@ export type Execute = {
errors?: { message?: string; orderId?: string }[]
fees?: CallFees
breakdown?: CallBreakdown
details?: ExecuteDetails
details?: QuoteDetails
error?: any // Manually added client error

steps: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export const TransactionModalRenderer: FC<Props> = ({
debouncedOutputAmountValue,
toToken.decimals
).toString(),
source: relayClient?.source ?? undefined,
referrer: relayClient?.source ?? undefined,
useExternalLiquidity
}
: undefined,
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/providers/RelayKitProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type CoinGecko = {
}

export type AppFees =
paths['/execute/swap']['post']['requestBody']['content']['application/json']['appFees']
paths['/quote']['post']['requestBody']['content']['application/json']['appFees']

type RelayKitProviderOptions = {
appName?: string
Expand Down
8 changes: 3 additions & 5 deletions packages/ui/src/utils/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { useQuote, PriceResponse } from '@reservoir0x/relay-kit-hooks'
import type { ComponentPropsWithoutRef } from 'react'
import type Text from '../components/primitives/Text.js'

type ExecuteSwapResponse = ReturnType<typeof useQuote>['data']
type QuoteResponse = ReturnType<typeof useQuote>['data']

export const parseFees = (
selectedTo: RelayChain,
Expand Down Expand Up @@ -144,9 +144,7 @@ export const parseFees = (
}
}

export const calculateRelayerFeeProportionUsd = (
quote?: ExecuteSwapResponse
) => {
export const calculateRelayerFeeProportionUsd = (quote?: QuoteResponse) => {
const usdIn = quote?.details?.currencyIn?.amountUsd
? Number(quote.details.currencyIn.amountUsd)
: null
Expand All @@ -173,7 +171,7 @@ export const calculateRelayerFeeProportion = (
return 0n
}

export const isHighRelayerServiceFeeUsd = (quote?: ExecuteSwapResponse) => {
export const isHighRelayerServiceFeeUsd = (quote?: QuoteResponse) => {
const usdIn = quote?.details?.currencyIn?.amountUsd
? Number(quote.details.currencyIn.amountUsd)
: null
Expand Down

0 comments on commit 2c32cbd

Please sign in to comment.