@@ -2,15 +2,16 @@ import { formatBytes32String } from '@ethersproject/strings';
22import { getAddress } from '@ethersproject/address' ;
33import subgraphs from '@snapshot-labs/snapshot.js/src/delegationSubgraphs.json' ;
44import {
5+ getAddressesByProtocol ,
56 getFormattedAddressesByProtocol ,
67 getProvider ,
78 getSnapshots ,
89 Multicaller ,
9- subgraphRequest
10+ subgraphRequest ,
11+ validateAddress
1012} from '../utils' ;
1113import _strategies from '../strategies' ;
1214import { Score , Snapshot , VotingPower } from '../types' ;
13- import { DEFAULT_SUPPORTED_PROTOCOLS } from '../constants' ;
1415
1516const DELEGATION_CONTRACT = '0x469788fE6E9E9681C6ebF3bF78e7Fd26Fc015446' ;
1617const EMPTY_ADDRESS = '0x0000000000000000000000000000000000000000' ;
@@ -30,6 +31,12 @@ export async function getVp(
3031 space : string ,
3132 delegation ?: boolean
3233) : Promise < VotingPower > {
34+ if ( strategies . some ( ( s ) => ! _strategies [ s . name ] ) ) {
35+ throw new Error ( 'invalid strategies' ) ;
36+ }
37+ // Ensure that the address is either a valid EVM address or Starknet address
38+ validateAddress ( address ) ;
39+
3340 const networks = [ ...new Set ( strategies . map ( ( s ) => s . network || network ) ) ] ;
3441 const snapshots = await getSnapshots (
3542 network ,
@@ -49,6 +56,7 @@ export async function getVp(
4956 const p : Score [ ] = strategies . map ( ( strategy ) => {
5057 const n = strategy . network || network ;
5158 let addresses = [ address ] ;
59+ const supportedProtocols = _strategies [ strategy . name ] . supportedProtocols ;
5260
5361 if ( delegation ) {
5462 addresses = delegations [ n ] . in ;
@@ -57,11 +65,11 @@ export async function getVp(
5765 if ( addresses . length === 0 ) return { } ;
5866 }
5967
60- addresses = getFormattedAddressesByProtocol (
61- addresses ,
62- _strategies [ strategy . name ] . supportedProtocols ??
63- DEFAULT_SUPPORTED_PROTOCOLS
64- ) ;
68+ addresses = getAddressesByProtocol ( addresses , supportedProtocols ) ;
69+ addresses = getFormattedAddressesByProtocol ( addresses , supportedProtocols ) ;
70+
71+ if ( addresses . length === 0 ) return { } ;
72+
6573 return _strategies [ strategy . name ] . strategy (
6674 space ,
6775 n ,
@@ -76,18 +84,17 @@ export async function getVp(
7684 const vpByStrategy = scores . map ( ( score , i ) => {
7785 const n = strategies [ i ] . network || network ;
7886 let addresses = [ address ] ;
87+ const supportedProtocols =
88+ _strategies [ strategies [ i ] . name ] . supportedProtocols ;
7989
8090 if ( delegation ) {
8191 addresses = delegations [ n ] . in ;
8292 if ( ! delegations [ n ] . out ) addresses . push ( address ) ;
8393 addresses = [ ...new Set ( addresses ) ] ;
8494 }
8595
86- addresses = getFormattedAddressesByProtocol (
87- addresses ,
88- _strategies [ strategies [ i ] . name ] . supportedProtocols ??
89- DEFAULT_SUPPORTED_PROTOCOLS
90- ) ;
96+ addresses = getAddressesByProtocol ( addresses , supportedProtocols ) ;
97+ addresses = getFormattedAddressesByProtocol ( addresses , supportedProtocols ) ;
9198 return addresses . reduce ( ( a , b ) => a + ( score [ b ] || 0 ) , 0 ) ;
9299 } ) ;
93100 const vp = vpByStrategy . reduce ( ( a , b ) => a + b , 0 ) ;
0 commit comments