-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from clober-dex/fix/cache
fix: clean cache for next call
- Loading branch information
Showing
29 changed files
with
1,398 additions
and
1,345 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { PublicClient } from 'viem' | ||
|
||
import { CHAIN_IDS } from '../constants/chain' | ||
import { Market } from '../model/market' | ||
import { Book } from '../model/book' | ||
|
@@ -6,14 +8,13 @@ import { toBookId } from '../utils/book-id' | |
import { calculateUnitSize } from '../utils/unit-size' | ||
import type { Currency } from '../model/currency' | ||
import { CONTRACT_ADDRESSES } from '../constants/addresses' | ||
import { cachedPublicClients } from '../constants/client' | ||
import { BOOK_VIEWER_ABI } from '../abis/core/book-viewer-abi' | ||
import { fetchIsOpened } from '../utils/open' | ||
import { fetchCurrency } from '../utils/currency' | ||
import { cachedSubgraph } from '../constants/subgraph' | ||
import { Subgraph } from '../constants/subgraph' | ||
|
||
const fetchBook = async (chainId: CHAIN_IDS, bookId: string) => { | ||
return cachedSubgraph[chainId]!.get<{ | ||
const fetchBookFromSubgraph = async (chainId: CHAIN_IDS, bookId: string) => { | ||
return Subgraph.get<{ | ||
data: { | ||
book: { | ||
depths: { | ||
|
@@ -24,6 +25,7 @@ const fetchBook = async (chainId: CHAIN_IDS, bookId: string) => { | |
} | null | ||
} | ||
}>( | ||
chainId, | ||
'getBook', | ||
'query getBook($bookId: ID!) { book(id: $bookId){ depths { tick unitAmount } } }', | ||
{ | ||
|
@@ -33,22 +35,24 @@ const fetchBook = async (chainId: CHAIN_IDS, bookId: string) => { | |
} | ||
|
||
const getBook = async ( | ||
publicClient: PublicClient, | ||
chainId: CHAIN_IDS, | ||
quoteCurrency: Currency, | ||
baseCurrency: Currency, | ||
useSubgraph: boolean, | ||
n: number, | ||
): Promise<Book> => { | ||
const unitSize = await calculateUnitSize(chainId, quoteCurrency) | ||
const unitSize = await calculateUnitSize(publicClient, chainId, quoteCurrency) | ||
const bookId = toBookId( | ||
chainId, | ||
quoteCurrency.address, | ||
baseCurrency.address, | ||
unitSize, | ||
) | ||
if (cachedSubgraph[chainId]) { | ||
if (useSubgraph) { | ||
const { | ||
data: { book }, | ||
} = await fetchBook(chainId, bookId.toString()) | ||
} = await fetchBookFromSubgraph(chainId, bookId.toString()) | ||
new Book({ | ||
chainId, | ||
id: bookId, | ||
|
@@ -68,13 +72,13 @@ const getBook = async ( | |
} | ||
|
||
const [depths, isOpened] = await Promise.all([ | ||
Check failure on line 74 in src/apis/market.ts GitHub Actions / NPM Test (18.10)limit-order.test.ts > make ask order
|
||
cachedPublicClients[chainId].readContract({ | ||
publicClient.readContract({ | ||
address: CONTRACT_ADDRESSES[chainId]!.BookViewer, | ||
abi: BOOK_VIEWER_ABI, | ||
functionName: 'getLiquidity', | ||
args: [bookId, Number(2n ** 19n - 1n), BigInt(n)], | ||
}), | ||
fetchIsOpened(chainId, bookId), | ||
fetchIsOpened(publicClient, chainId, bookId), | ||
]) | ||
|
||
return new Book({ | ||
|
@@ -92,8 +96,10 @@ const getBook = async ( | |
} | ||
|
||
export async function fetchMarket( | ||
publicClient: PublicClient, | ||
chainId: CHAIN_IDS, | ||
tokenAddresses: `0x${string}`[], | ||
useSubgraph: boolean, | ||
n = 100, | ||
): Promise<Market> { | ||
if (tokenAddresses.length !== 2) { | ||
|
@@ -106,12 +112,12 @@ export async function fetchMarket( | |
]) | ||
|
||
const [quoteCurrency, baseCurrency] = await Promise.all([ | ||
fetchCurrency(chainId, quoteTokenAddress), | ||
fetchCurrency(chainId, baseTokenAddress), | ||
fetchCurrency(publicClient, chainId, quoteTokenAddress), | ||
fetchCurrency(publicClient, chainId, baseTokenAddress), | ||
]) | ||
const [bidBook, askBook] = await Promise.all([ | ||
getBook(chainId, quoteCurrency, baseCurrency, n), | ||
getBook(chainId, baseCurrency, quoteCurrency, n), | ||
getBook(publicClient, chainId, quoteCurrency, baseCurrency, useSubgraph, n), | ||
getBook(publicClient, chainId, baseCurrency, quoteCurrency, useSubgraph, n), | ||
]) | ||
|
||
return new Market({ | ||
|
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
Oops, something went wrong.