-
Notifications
You must be signed in to change notification settings - Fork 12
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
7 changed files
with
146 additions
and
3 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,81 @@ | ||
import { CHAINS, TOKENS, getTokenAddress } from '@lido-sdk/constants'; | ||
import { BigNumber } from 'ethers'; | ||
import { standardFetcher } from './standardFetcher'; | ||
import { ESTIMATE_ACCOUNT } from 'config'; | ||
import { getAddress } from 'ethers/lib/utils.js'; | ||
|
||
type BebopGetQuotePartial = { | ||
routes: { | ||
quote: { | ||
buyTokens: Record< | ||
string, | ||
{ | ||
amount: string; | ||
amountBeforeFee: string; | ||
} | ||
>; | ||
sellTokens: Record< | ||
string, | ||
{ | ||
amount: string; | ||
priceBeforeFee: number; | ||
} | ||
>; | ||
}; | ||
}[]; | ||
}; | ||
|
||
type RateToken = TOKENS.STETH | TOKENS.WSTETH | 'ETH'; | ||
|
||
type RateCalculationResult = { rate: number; toReceive: BigNumber }; | ||
|
||
const getRateTokenAddress = (token: RateToken) => | ||
token === 'ETH' | ||
? '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE' | ||
: getTokenAddress(CHAINS.Mainnet, token); | ||
|
||
export const getBebopRate = async ( | ||
amount: BigNumber, | ||
fromToken: RateToken, | ||
toToken: RateToken, | ||
): Promise<RateCalculationResult> => { | ||
const basePath = 'https://api.bebop.xyz/router/ethereum/v1/quote'; | ||
|
||
const sell_tokens = getAddress(getRateTokenAddress(fromToken)); | ||
const buy_tokens = getAddress(getRateTokenAddress(toToken)); | ||
|
||
const params = new URLSearchParams({ | ||
sell_tokens, | ||
buy_tokens, | ||
taker_address: ESTIMATE_ACCOUNT, | ||
sell_amounts: amount.toString(), | ||
approval_type: 'Standard', | ||
}); | ||
|
||
const data = await standardFetcher<BebopGetQuotePartial>( | ||
`${basePath}/?${params.toString()}`, | ||
); | ||
|
||
const bestRoute = data.routes.toSorted( | ||
(r1, r2) => | ||
r2.quote.sellTokens[sell_tokens].priceBeforeFee - | ||
r1.quote.sellTokens[sell_tokens].priceBeforeFee, | ||
)[0]; | ||
|
||
if ( | ||
bestRoute && | ||
bestRoute.quote.sellTokens[sell_tokens] && | ||
bestRoute.quote.buyTokens[buy_tokens] | ||
) { | ||
const rate = data.routes[0].quote.sellTokens[sell_tokens].priceBeforeFee; | ||
|
||
const toAmount = BigNumber.from( | ||
data.routes[0].quote.buyTokens[buy_tokens].amountBeforeFee, | ||
); | ||
return { | ||
rate, | ||
toReceive: toAmount, | ||
}; | ||
} | ||
throw new Error('[getBebopRate] Could not get quote, invalid response body'); | ||
}; |