Skip to content

Commit

Permalink
refactor: move fetchEthPrice / fetchSpread / fetchSpotPrice to @nftx/api
Browse files Browse the repository at this point in the history
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
jackmellis committed May 23, 2024
1 parent cbd2bc1 commit 6d71440
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 77 deletions.
13 changes: 13 additions & 0 deletions packages/api/src/prices/fetchEthPrice.ts
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;
41 changes: 41 additions & 0 deletions packages/api/src/prices/fetchSpotPrice.ts
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;
36 changes: 36 additions & 0 deletions packages/api/src/prices/fetchSpread.ts
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;
3 changes: 3 additions & 0 deletions packages/api/src/prices/index.ts
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';
39 changes: 0 additions & 39 deletions packages/trade/src/price/fetchEthPrice.ts

This file was deleted.

38 changes: 0 additions & 38 deletions packages/trade/src/price/fetchSpread.ts

This file was deleted.

0 comments on commit 6d71440

Please sign in to comment.