-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: refactor, use and export calcPriceImpact function (#307)
Co-authored-by: Eugene Chybisov <[email protected]>
- Loading branch information
1 parent
b247afb
commit 234844f
Showing
4 changed files
with
43 additions
and
25 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
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,27 @@ | ||
import type { Token } from '@lifi/sdk'; | ||
import { formatTokenAmount, formatTokenPrice } from './format.js'; | ||
|
||
interface GetPriceImpractProps { | ||
fromToken: Token; | ||
toToken: Token; | ||
fromAmount?: bigint; | ||
toAmount?: bigint; | ||
} | ||
|
||
export const getPriceImpact = ({ | ||
fromToken, | ||
toToken, | ||
fromAmount, | ||
toAmount, | ||
}: GetPriceImpractProps) => { | ||
const fromTokenAmount = formatTokenAmount(fromAmount, fromToken.decimals); | ||
const fromTokenPrice = formatTokenPrice(fromTokenAmount, fromToken.priceUSD); | ||
|
||
const toTokenAmount = formatTokenAmount(toAmount, toToken.decimals); | ||
const toTokenPrice = | ||
formatTokenPrice(toTokenAmount, toToken.priceUSD) || 0.01; | ||
|
||
const priceImpact = toTokenPrice / fromTokenPrice - 1; | ||
|
||
return Number(priceImpact); | ||
}; |