-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
3 changed files
with
112 additions
and
0 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,6 +1,7 @@ | ||
export * from './wrappedAsset'; | ||
export * from './types'; | ||
export * from './quotes'; | ||
export * from './slippage'; | ||
export * from './utils/constants'; | ||
export * from './utils/permit'; | ||
export * from './utils/sources'; |
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,92 @@ | ||
import { BigNumberish } from '@ethersproject/bignumber'; | ||
import { | ||
ChainId, | ||
EthereumAddress, | ||
Slippage, | ||
SlippageError, | ||
SlippageParams, | ||
} from './types'; | ||
import { API_BASE_URL } from './utils/constants'; | ||
|
||
/** | ||
* Function to get a slippage formatted quote url to use with backend | ||
* | ||
* @param {ChainId} params.chainId | ||
* @param {ChainId} params.toChainId | ||
* @param {EthereumAddress} params.sellTokenAddress | ||
* @param {EthereumAddress} params.buyTokenAddress | ||
* @param {BigNumberish} params.buyAmount | ||
* @param {BigNumberish} params.sellAmount | ||
* @returns {string} | ||
*/ | ||
const buildRainbowSlippageUrl = ({ | ||
chainId, | ||
toChainId, | ||
sellTokenAddress, | ||
buyTokenAddress, | ||
buyAmount, | ||
sellAmount, | ||
}: { | ||
chainId: number; | ||
toChainId?: number; | ||
sellTokenAddress: EthereumAddress; | ||
buyTokenAddress: EthereumAddress; | ||
buyAmount?: BigNumberish; | ||
sellAmount?: BigNumberish; | ||
}) => { | ||
const searchParams = new URLSearchParams({ | ||
buyToken: buyTokenAddress, | ||
chainId: String(chainId), | ||
sellToken: sellTokenAddress, | ||
toChainId: String(toChainId), | ||
...(sellAmount | ||
? { sellAmount: String(sellAmount) } | ||
: { buyAmount: String(buyAmount) }), | ||
}); | ||
return `${API_BASE_URL}/v1/slippage?` + searchParams.toString(); | ||
}; | ||
|
||
/** | ||
* Function to get slippage from rainbow's swap aggregator backend | ||
* | ||
* @param {SlippageParams} params | ||
* @param {ChainId} params.chainId | ||
* @param {ChainId} params.toChainId | ||
* @param {EthereumAddress} params.sellTokenAddress | ||
* @param {EthereumAddress} params.buyTokenAddress | ||
* @param {BigNumberish} params.sellAmount | ||
* @param {BigNumberish} params.buyAmount | ||
* @returns {Promise<Slippage | SlippageError | null>} | ||
*/ | ||
export const getSlippage = async ( | ||
params: SlippageParams | ||
): Promise<Slippage | SlippageError | null> => { | ||
const { | ||
chainId = ChainId.mainnet, | ||
toChainId, | ||
sellTokenAddress, | ||
buyTokenAddress, | ||
sellAmount, | ||
buyAmount, | ||
} = params; | ||
|
||
if (isNaN(Number(sellAmount)) && isNaN(Number(buyAmount))) { | ||
return null; | ||
} | ||
|
||
const url = buildRainbowSlippageUrl({ | ||
buyAmount, | ||
buyTokenAddress, | ||
chainId, | ||
sellAmount, | ||
sellTokenAddress, | ||
toChainId, | ||
}); | ||
|
||
const response = await fetch(url); | ||
const slippage = await response.json(); | ||
if (slippage.error) { | ||
return slippage as SlippageError; | ||
} | ||
return slippage?.data as 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