-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15d0437
commit 3c68f41
Showing
9 changed files
with
105 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,10 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
import axios from 'axios' | ||
|
||
import type { ChainflipQuote, ChainflipQuoteParams } from './types' | ||
|
||
const CHAINFLIP_API_URL = import.meta.env.VITE_CHAINFLIP_API_URL | ||
const CHAINFLIP_API_KEY = import.meta.env.VITE_CHAINFLIP_API_KEY | ||
import { reactQueries } from '../react-queries' | ||
import type { ChainflipQuoteParams } from './types' | ||
|
||
export const useChainflipQuoteQuery = (params: ChainflipQuoteParams) => { | ||
const { sourceAsset, destinationAsset, amount, commissionBps } = params | ||
|
||
return useQuery({ | ||
queryKey: ['chainflip', 'quote', params], | ||
queryFn: async () => { | ||
const { data } = await axios.get<ChainflipQuote[]>(`${CHAINFLIP_API_URL}/quotes-native`, { | ||
params: { | ||
apiKey: CHAINFLIP_API_KEY, | ||
sourceAsset, | ||
destinationAsset, | ||
amount, | ||
...(commissionBps && { commissionBps }), | ||
}, | ||
}) | ||
|
||
// TODO(gomes): select best quote based on output amount, since o.g doesn't support multiple quotes | ||
return data[0] | ||
}, | ||
...reactQueries.chainflip.quote(params), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,10 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
import axios from 'axios' | ||
|
||
import type { ChainflipSwapParams, ChainflipSwapResponse } from './types' | ||
|
||
const CHAINFLIP_API_URL = import.meta.env.VITE_CHAINFLIP_API_URL | ||
const CHAINFLIP_API_KEY = import.meta.env.VITE_CHAINFLIP_API_KEY | ||
import { reactQueries } from '../react-queries' | ||
import type { ChainflipSwapParams } from './types' | ||
|
||
export const useChainflipSwapQuery = (params: ChainflipSwapParams) => { | ||
return useQuery({ | ||
queryKey: ['chainflip', 'swap', params], | ||
queryFn: async () => { | ||
const { data } = await axios.get<ChainflipSwapResponse>(`${CHAINFLIP_API_URL}/swap`, { | ||
params: { | ||
apiKey: CHAINFLIP_API_KEY, | ||
...params, | ||
boostFee: params.maxBoostFee ?? 0, | ||
retryDurationInBlocks: params.retryDurationInBlocks ?? 150, | ||
}, | ||
}) | ||
return data | ||
}, | ||
...reactQueries.chainflip.swap(params), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import type { inferQueryKeyStore } from '@lukemorales/query-key-factory' | ||
import { createQueryKeyStore } from '@lukemorales/query-key-factory' | ||
import axios from 'axios' | ||
|
||
import type { | ||
ChainflipAssetsResponse, | ||
ChainflipQuote, | ||
ChainflipQuoteParams, | ||
ChainflipStatusResponse, | ||
ChainflipSwapParams, | ||
ChainflipSwapResponse, | ||
} from './chainflip/types' | ||
import { findByAssetId } from './marketData' | ||
|
||
const CHAINFLIP_API_URL = import.meta.env.VITE_CHAINFLIP_API_URL | ||
const CHAINFLIP_API_KEY = import.meta.env.VITE_CHAINFLIP_API_KEY | ||
|
||
export const reactQueries = createQueryKeyStore({ | ||
chainflip: { | ||
assets: { | ||
queryKey: null, | ||
queryFn: async () => { | ||
const { data } = await axios.get<ChainflipAssetsResponse>(`${CHAINFLIP_API_URL}/assets`) | ||
return data | ||
}, | ||
}, | ||
quote: (params: ChainflipQuoteParams) => ({ | ||
queryKey: [params], | ||
queryFn: async () => { | ||
const { data } = await axios.get<ChainflipQuote[]>(`${CHAINFLIP_API_URL}/quotes-native`, { | ||
params: { | ||
apiKey: CHAINFLIP_API_KEY, | ||
sourceAsset: params.sourceAsset, | ||
destinationAsset: params.destinationAsset, | ||
amount: params.amount, | ||
...(params.commissionBps && { commissionBps: params.commissionBps }), | ||
}, | ||
}) | ||
return data[0] | ||
}, | ||
}), | ||
swap: (params: ChainflipSwapParams) => ({ | ||
queryKey: [params], | ||
queryFn: async () => { | ||
const { data } = await axios.get<ChainflipSwapResponse>(`${CHAINFLIP_API_URL}/swap`, { | ||
params: { | ||
apiKey: CHAINFLIP_API_KEY, | ||
...params, | ||
boostFee: params.maxBoostFee ?? 0, | ||
retryDurationInBlocks: params.retryDurationInBlocks ?? 150, | ||
}, | ||
}) | ||
return data | ||
}, | ||
}), | ||
status: (swapId: number) => ({ | ||
queryKey: [swapId], | ||
queryFn: async () => { | ||
const { data } = await axios.get<ChainflipStatusResponse>( | ||
`${CHAINFLIP_API_URL}/status-by-id`, | ||
{ | ||
params: { | ||
apiKey: CHAINFLIP_API_KEY, | ||
swapId, | ||
}, | ||
}, | ||
) | ||
return data | ||
}, | ||
}), | ||
}, | ||
marketData: { | ||
byAssetId: (assetId: string) => ({ | ||
queryKey: [assetId], | ||
queryFn: () => findByAssetId(assetId), | ||
}), | ||
}, | ||
}) | ||
|
||
export type QueryKeys = inferQueryKeyStore<typeof reactQueries> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters