-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move fetchEthPrice / fetchSpread / fetchSpotPrice to @nftx/api
these methods were previously in @nftx/trade and interacted directly with the AMM router as we now have an API layer for requesting ERC20 quotes, these methods have been moved to @nftx/api BREAKING CHANGE: these methods are no longer available in @nftx/trade
- Loading branch information
1 parent
cbd2bc1
commit 6d71440
Showing
6 changed files
with
93 additions
and
77 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import fetchSpotPrice from './fetchSpotPrice'; | ||
|
||
const fetchEthPrice = async ({ network }: { network?: number }) => { | ||
const { price } = await fetchSpotPrice({ | ||
network, | ||
tokenAddress: 'ETH', | ||
quoteToken: 'USDC', | ||
}); | ||
|
||
return price; | ||
}; | ||
|
||
export default fetchEthPrice; |
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,41 @@ | ||
import type { QuoteToken } from '@nftx/types'; | ||
import fetchPrice from './fetchPrice'; | ||
import { WeiPerEther } from '@nftx/constants'; | ||
|
||
const fetchSpotPrice = async ({ | ||
tokenAddress, | ||
amount = WeiPerEther, | ||
network, | ||
quoteToken, | ||
}: { | ||
tokenAddress: QuoteToken; | ||
network?: number; | ||
quoteToken?: QuoteToken; | ||
amount?: bigint; | ||
}) => { | ||
let fraction = WeiPerEther; | ||
let error: any = new Error('Failed to fetch spot price'); | ||
|
||
do { | ||
try { | ||
const quote = await fetchPrice({ | ||
network, | ||
type: 'erc20', | ||
buyToken: tokenAddress, | ||
sellToken: quoteToken || 'ETH', | ||
buyAmount: fraction, | ||
}); | ||
|
||
quote.price = (quote.price * amount) / fraction; | ||
|
||
return quote; | ||
} catch (e) { | ||
error = e; | ||
fraction /= 100n; | ||
} | ||
} while (fraction > 0n); | ||
|
||
throw error; | ||
}; | ||
|
||
export default fetchSpotPrice; |
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,36 @@ | ||
import { WeiPerEther, Zero } from '@nftx/constants'; | ||
import { QuoteToken } from '@nftx/types'; | ||
import fetchPrice from './fetchPrice'; | ||
|
||
const fetchSpread = async ({ | ||
tokenAddress, | ||
network, | ||
quoteToken, | ||
}: { | ||
tokenAddress: QuoteToken; | ||
network?: number; | ||
quoteToken?: QuoteToken; | ||
}) => { | ||
try { | ||
const { price: buyPrice } = await fetchPrice({ | ||
network, | ||
type: 'erc20', | ||
buyToken: tokenAddress, | ||
sellToken: quoteToken || 'ETH', | ||
buyAmount: WeiPerEther, | ||
}); | ||
const { price: sellPrice } = await fetchPrice({ | ||
network, | ||
type: 'erc20', | ||
sellToken: tokenAddress, | ||
buyToken: quoteToken || 'ETH', | ||
sellAmount: WeiPerEther, | ||
}); | ||
|
||
return buyPrice - sellPrice; | ||
} catch { | ||
return Zero; | ||
} | ||
}; | ||
|
||
export default fetchSpread; |
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,2 +1,5 @@ | ||
export { default as fetchQuote } from './fetchQuote'; | ||
export { default as fetchPrice } from './fetchPrice'; | ||
export { default as fetchEthPrice } from './fetchEthPrice'; | ||
export { default as fetchSpotPrice } from './fetchSpotPrice'; | ||
export { default as fetchSpread } from './fetchSpread'; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.