import { getTrendingConstants } from '@masknet/web3-shared-evm' ; import stringify from 'json-stable-stringify' ; import { chunk , first , flatten } from 'lodash-unified' ; import { currentChainIdSettings } from '../../../Wallet/settings' ; interface Block { number : string ; } async function fetchFromEthereumBlocksSubgraph < T > ( query ) { const subgraphURL = getTrendingConstants ( currentChainIdSettings . value ) . ETHEREUM_BLOCKS_SUBGRAPH_URL ; if ( ! subgraphURL ) return null ; const response = await fetch ( subgraphURL , { method : 'POST' , mode : 'cors' , body : stringify ( { query } ) , } ) ; const { data } = ( await response . json ( ) ) as { data : T ; } ; return data ; } param timestamp export async function fetchBlockNumberByTimestamp ( timestamp ) { const data = await fetchFromEthereumBlocksSubgraph < { blocks : Block [ ] ; } > ( `
    {
        blocks (
            first: 1,
            orderBy: timestamp,
            where: {
                timestamp_gt: ${ timestamp },
                timestamp_lt: ${ timestamp + 600 }
            }
        ) {
            number
        }
    }
    ` ) ; return first ( data ?. blocks ) ?. number ; } param timestamps param skipCount export async function fetchBlockNumbersByTimestamps ( timestamps , skipCount = 100 ) { const chunkTimestamps = chunk ( timestamps , skipCount ) ; const data = await Promise . all ( chunkTimestamps . map ( async ( chunk ) => { const queries = chunk . map ( ( x ) => { return `
                    t${ x }: blocks(
                        first: 1,
                        orderBy: timestamp,
                        orderDirection: desc,
                        where: {
                            timestamp_gt: ${ x },
                            timestamp_lt: ${ x + 600 }
                        }
                    ) {
                        number
                    }
                ` ; } ) ; return fetchFromEthereumBlocksSubgraph < Record < string , Block [ ] > > ( `
                query blocks {
                    ${ queries }
                }
            ` ) ; } ) ) ; return flatten ( data . filter ( Boolean ) . map ( ( result ) => Object . keys ( result ! ) . map ( ( x ) => ( { timestamp : Number ( x . split ( 't' ) [ 1 ] ) , blockNumber : first ( result [ x ] ) ! . number , } ) ) ) ) ; } param timestamps export async function fetchBlockNumbersObjectByTimestamps ( timestamps ) { const queries = timestamps . map ( ( x ) => { return `
            t${ x }: blocks(
                first: 1,
                orderBy: timestamp,
                where: {
                    timestamp_gt: ${ x },
                    timestamp_lt: ${ x + 600 }
                }
            ) {
                number
            }
        ` ; } ) ; const data = await fetchFromEthereumBlocksSubgraph < Record < string , Block [ ] > > ( `
        query blocks {
            ${ queries }
        }
    ` ) ; const result = { } ; if ( ! data ) return result ; Object . keys ( data ) . map ( ( key ) => { result [ key ] = first ( data [ key ] ) ?. number ; } ) ; return result ; }