-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Charles Lanier <[email protected]>
- Loading branch information
Showing
8 changed files
with
93 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module.exports = { | ||
root: true, | ||
extends: ['../../.eslintrc.js', '@uniswap/eslint-config/react'], | ||
extends: ['@uniswap/eslint-config/react', '../../.eslintrc.js'], | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* eslint-disable import/no-unused-modules */ | ||
import { fetchBuildExecuteTransaction, fetchQuotes, Quote, QuoteRequest } from '@avnu/avnu-sdk' | ||
import { useAccount, useBlockNumber } from '@starknet-react/core' | ||
import { Percent } from '@uniswap/sdk-core' | ||
import { useCallback, useEffect, useState } from 'react' | ||
import { SLIPPAGE_PRECISION, STARKNET_POLLING } from 'src/constants/misc' | ||
import { getAvnuOptions } from 'src/utils/avnu' | ||
import { Call } from 'starknet' | ||
|
||
export function useGetAvnuQuotes( | ||
tokenAddressFrom: string, | ||
tokenAddressTo: string, | ||
amount: string | number, | ||
): Quote | null { | ||
const account = useAccount() | ||
|
||
const [quote, setQuote] = useState<Quote | null>(null) | ||
|
||
const { data: blockNumber } = useBlockNumber({ refetchInterval: STARKNET_POLLING }) | ||
|
||
useEffect(() => { | ||
if (!blockNumber) return | ||
|
||
const AVNU_OPTIONS = getAvnuOptions(account.chainId) | ||
|
||
const abortController = new AbortController() | ||
|
||
const params: QuoteRequest = { | ||
sellTokenAddress: tokenAddressFrom, | ||
buyTokenAddress: tokenAddressTo, | ||
sellAmount: BigInt(amount), | ||
} | ||
|
||
fetchQuotes(params, { ...AVNU_OPTIONS, abortSignal: abortController.signal }) | ||
.then((quotes) => { | ||
setQuote(quotes.length > 0 ? quotes[0] : null) | ||
}) | ||
.catch((error) => { | ||
if (!abortController.signal.aborted) { | ||
// TODO: handle error | ||
console.log(error) | ||
} | ||
}) | ||
|
||
return () => abortController.abort() | ||
}, [account.chainId, tokenAddressFrom, tokenAddressTo, amount, blockNumber]) | ||
|
||
return quote | ||
} | ||
|
||
export function useAvnuSwapBuilder(slippage: Percent): (quote: Quote) => Promise<Call[] | undefined> { | ||
const account = useAccount() | ||
|
||
return useCallback( | ||
async (quote: Quote) => { | ||
if (!account.address) return | ||
|
||
const AVNU_OPTIONS = getAvnuOptions(account.chainId) | ||
|
||
const { calls } = await fetchBuildExecuteTransaction( | ||
quote.quoteId, | ||
account.address, | ||
+slippage.toFixed(SLIPPAGE_PRECISION), | ||
true, | ||
AVNU_OPTIONS, | ||
) | ||
return calls | ||
}, | ||
[account.address, account.chainId, slippage], | ||
) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { BASE_URL, SEPOLIA_BASE_URL } from '@avnu/avnu-sdk' | ||
import { starknetChainId } from '@starknet-react/core' | ||
import { constants } from 'starknet' | ||
|
||
export function getAvnuOptions(chainId: bigint | undefined): { baseUrl: string } { | ||
const actualChainId = chainId ?? BigInt(constants.StarknetChainId.SN_MAIN) | ||
|
||
return { | ||
baseUrl: starknetChainId(actualChainId) === constants.StarknetChainId.SN_MAIN ? BASE_URL : SEPOLIA_BASE_URL, | ||
} | ||
} |