11import { formatBytes32String } from '@ethersproject/strings' ;
2- import { getAddress } from '@ethersproject/address' ;
2+ import { getAddress , isAddress } 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 ,
@@ -38,8 +45,10 @@ export async function getVp(
3845 networks
3946 ) ;
4047
48+ // TODO: Delegation support for Starknet
49+ const delegationSupported = delegation && isAddress ( address ) ;
4150 const delegations = { } ;
42- if ( delegation ) {
51+ if ( delegationSupported ) {
4352 const ds = await Promise . all (
4453 networks . map ( ( n ) => getDelegations ( address , n , snapshots [ n ] , space ) )
4554 ) ;
@@ -49,19 +58,20 @@ export async function getVp(
4958 const p : Score [ ] = strategies . map ( ( strategy ) => {
5059 const n = strategy . network || network ;
5160 let addresses = [ address ] ;
61+ const supportedProtocols = _strategies [ strategy . name ] . supportedProtocols ;
5262
53- if ( delegation ) {
63+ if ( delegationSupported && supportedProtocols . includes ( 'evm' ) ) {
5464 addresses = delegations [ n ] . in ;
5565 if ( ! delegations [ n ] . out ) addresses . push ( address ) ;
5666 addresses = [ ...new Set ( addresses ) ] ;
5767 if ( addresses . length === 0 ) return { } ;
5868 }
5969
60- addresses = getFormattedAddressesByProtocol (
61- addresses ,
62- _strategies [ strategy . name ] . supportedProtocols ??
63- DEFAULT_SUPPORTED_PROTOCOLS
64- ) ;
70+ addresses = getAddressesByProtocol ( addresses , supportedProtocols ) ;
71+ addresses = getFormattedAddressesByProtocol ( addresses , supportedProtocols ) ;
72+
73+ if ( addresses . length === 0 ) return { } ;
74+
6575 return _strategies [ strategy . name ] . strategy (
6676 space ,
6777 n ,
@@ -76,18 +86,17 @@ export async function getVp(
7686 const vpByStrategy = scores . map ( ( score , i ) => {
7787 const n = strategies [ i ] . network || network ;
7888 let addresses = [ address ] ;
89+ const supportedProtocols =
90+ _strategies [ strategies [ i ] . name ] . supportedProtocols ;
7991
80- if ( delegation ) {
92+ if ( delegationSupported && supportedProtocols . includes ( 'evm' ) ) {
8193 addresses = delegations [ n ] . in ;
8294 if ( ! delegations [ n ] . out ) addresses . push ( address ) ;
8395 addresses = [ ...new Set ( addresses ) ] ;
8496 }
8597
86- addresses = getFormattedAddressesByProtocol (
87- addresses ,
88- _strategies [ strategies [ i ] . name ] . supportedProtocols ??
89- DEFAULT_SUPPORTED_PROTOCOLS
90- ) ;
98+ addresses = getAddressesByProtocol ( addresses , supportedProtocols ) ;
99+ addresses = getFormattedAddressesByProtocol ( addresses , supportedProtocols ) ;
91100 return addresses . reduce ( ( a , b ) => a + ( score [ b ] || 0 ) , 0 ) ;
92101 } ) ;
93102 const vp = vpByStrategy . reduce ( ( a , b ) => a + b , 0 ) ;
0 commit comments