import { Provider } from '@ethersproject/providers' ; import { getAddress } from '@ethersproject/address' ; import { getTokenLockWallets } from './tokenLockWallets' ; import { GraphAccountScores , GraphStrategyOptions , splitArray , StrategyFunction , verifyResults } from './graphUtils' ; const DEFAULT_PAGE_SIZE = 1000 ; const VALID_STRATEGIES = [ 'balance' , 'indexing' , 'delegation' ] ; export param { string } _space param { string } network param { Provider } _provider param { string [ ] } addresses param { GraphStrategyOptions } options param { ( string | number ) } snapshot param { StrategyFunction } graphStrategy return { Promise < GraphAccountScores > } export async function getScoresPage ( _space , network , _provider , addresses , options , snapshot , graphStrategy ) { const tokenLockWallets = await getTokenLockWallets ( _space , network , _provider , addresses , options , snapshot ) ; const allAccounts = [ ... addresses ] ; for ( const beneficiary in tokenLockWallets ) { tokenLockWallets [ beneficiary ] . forEach ( ( tw ) => { allAccounts . push ( tw ) ; } ) ; } const scores = await graphStrategy ( _space , network , _provider , allAccounts , options , snapshot ) ; if ( options . expectedResults && snapshot !== 'latest' ) { verifyResults ( JSON . stringify ( scores ) , JSON . stringify ( options . expectedResults . scores ) , 'Scores' ) ; } const combinedScores = { } ; for ( const account of addresses ) { let accountScore = scores [ account ] ; if ( tokenLockWallets [ account ] != null ) { tokenLockWallets [ account ] . forEach ( ( tw ) => { accountScore = accountScore + scores [ tw ] ; } ) ; } combinedScores [ account ] = accountScore ; } return combinedScores ; } export async function baseStrategy ( _space , network , _provider , _addresses , options , snapshot , graphStrategy ) { const addresses = _addresses . map ( ( address ) => address . toLowerCase ( ) ) ; let combinedScores = { } ; if ( VALID_STRATEGIES . includes ( options . strategyType ) ) { const pageSize = options . pageSize || DEFAULT_PAGE_SIZE ; const pages = splitArray ( addresses , pageSize ) ; let pageNum = 1 ; for ( const addressesPage of pages ) { console . info ( `Processing page ${ pageNum } of ${ pages . length }` ) ; const pageScores = await getScoresPage ( _space , network , _provider , addressesPage , { ... options , pageSize } , snapshot , graphStrategy ) ; combinedScores = { ... combinedScores , ... pageScores } ; pageNum += 1 ; } } else { console . error ( 'ERROR: Strategy does not exist' ) ; return combinedScores ; } if ( options . expectedResults && snapshot !== 'latest' ) { verifyResults ( JSON . stringify ( combinedScores ) , JSON . stringify ( options . expectedResults . combinedScores ) , 'Combined scores' ) ; } return Object . fromEntries ( Object . entries ( combinedScores ) . map ( ( score ) => [ getAddress ( score [ 0 ] ) , score [ 1 ] ] ) ) ; }