param balances export function selectWeightedPstHolder ( balances ) { let totalTokens = 0 ; for ( const address of Object . keys ( balances ) ) { totalTokens += balances [ address ] ; } const weighted = { } ; for ( const address of Object . keys ( balances ) ) { weighted [ address ] = balances [ address ] / totalTokens ; } let sum = 0 ; const r = Math . random ( ) ; for ( const address of Object . keys ( weighted ) ) { sum += weighted [ address ] ; if ( r <= sum && weighted [ address ] > 0 ) { return address ; } } throw new Error ( 'Unable to select token holder' ) ; }