Skip to content

Commit 35a58dc

Browse files
committed
feat: pull overriding strategies from strategies list
1 parent 884d421 commit 35a58dc

File tree

3 files changed

+127
-29
lines changed

3 files changed

+127
-29
lines changed

src/helpers/strategies.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const URI = new URL(
1919
let consecutiveFailsCount = 0;
2020
let shouldStop = false;
2121
let strategies: Record<Strategy['id'], Strategy> = {};
22+
let overridingStrategies: string[] = [];
2223

2324
async function loadStrategies() {
2425
const res = await snapshot.utils.getJSON(URI);
@@ -40,13 +41,19 @@ async function loadStrategies() {
4041
}));
4142

4243
strategies = Object.fromEntries(strategiesList.map(strategy => [strategy.id, strategy]));
44+
45+
overridingStrategies = strategiesList.filter(s => s.override).map(s => s.id);
4346
}
4447

4548
// Using a getter to avoid potential reference initialization issues
4649
export function getStrategies(): Record<Strategy['id'], Strategy> {
4750
return strategies;
4851
}
4952

53+
export function getOverridingStrategies(): string[] {
54+
return overridingStrategies;
55+
}
56+
5057
export async function initialize() {
5158
log.info('[strategies] Initial strategies load');
5259
await loadStrategies();

src/helpers/utils.ts

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { capture } from '@snapshot-labs/snapshot-sentry';
77
import snapshot from '@snapshot-labs/snapshot.js';
88
import { Response } from 'express';
99
import fetch from 'node-fetch';
10+
import { getOverridingStrategies } from './strategies';
1011

1112
const MAINNET_NETWORK_ID_WHITELIST = ['s', 'eth', 'arb1', 'oeth', 'sn', 'base', 'mnt', 'ape'];
1213
const TESTNET_NETWORK_ID_WHITELIST = ['s-tn', 'sep', 'curtis', 'linea-testnet', 'sn-sep'];
@@ -62,36 +63,12 @@ export function rpcError(res, code, e, id) {
6263
});
6364
}
6465

65-
export function hasStrategyOverride(strategies: any[]) {
66-
const keywords = [
67-
'"aura-vlaura-vebal-with-overrides"',
68-
'"balance-of-with-linear-vesting-power"',
69-
'"balancer-delegation"',
70-
'"cyberkongz"',
71-
'"cyberkongz-v2"',
72-
'"delegation"',
73-
'"delegation-with-cap"',
74-
'"delegation-with-overrides"',
75-
'"erc20-balance-of-delegation"',
76-
'"erc20-balance-of-fixed-total"',
77-
'"erc20-balance-of-quadratic-delegation"',
78-
'"erc20-votes-with-override"',
79-
'"esd-delegation"',
80-
'"ocean-dao-brightid"',
81-
'"orbs-network-delegation"',
82-
'"api-v2-override"',
83-
'"rocketpool-node-operator-delegate-v8"',
84-
'"eden-online-override"',
85-
'"split-delegation"',
86-
'"sonic-staked-balance"'
87-
];
66+
export function hasStrategyOverride(strategies: any[]): boolean {
67+
if (!strategies?.length) return false;
68+
8869
const strategiesStr = JSON.stringify(strategies).toLowerCase();
89-
if (keywords.some(keyword => strategiesStr.includes(`"name":${keyword}`))) return true;
90-
// Check for split-delegation with delegationOverride
91-
const splitDelegation = strategies.filter(strategy => strategy.name === 'split-delegation');
92-
return (
93-
splitDelegation.length > 0 &&
94-
splitDelegation.some(strategy => strategy.params?.delegationOverride)
70+
return getOverridingStrategies().some(strategyId =>
71+
strategiesStr.includes(`"name":"${strategyId}"`)
9572
);
9673
}
9774

test/unit/helpers/utils.test.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import * as strategies from '../../../src/helpers/strategies';
2+
import { hasStrategyOverride } from '../../../src/helpers/utils';
3+
4+
const OVERRIDING_STRATEGIES = ['delegation', 'delegation-with-cap'];
5+
6+
// Mock the getOverridingStrategies function
7+
jest.mock('../../../src/helpers/strategies');
8+
const mockGetOverridingStrategies = jest.mocked(strategies.getOverridingStrategies);
9+
10+
describe('Utils', () => {
11+
describe('hasStrategyOverride()', () => {
12+
beforeEach(() => {
13+
jest.clearAllMocks();
14+
mockGetOverridingStrategies.mockReturnValue(OVERRIDING_STRATEGIES);
15+
});
16+
17+
it('should return false when strategies array is empty', () => {
18+
expect(hasStrategyOverride([])).toBe(false);
19+
});
20+
21+
it('should return false when no overriding strategies exist', () => {
22+
const strategies = [
23+
{ name: 'whitelist', network: '1', params: {} },
24+
{ name: 'ticket', network: '1', params: {} }
25+
];
26+
27+
expect(hasStrategyOverride(strategies)).toBe(false);
28+
});
29+
30+
it('should return true when an overriding strategy is used', () => {
31+
const strategies = [
32+
{ name: 'whitelist', network: '1', params: {} },
33+
{ name: 'delegation', network: '1', params: {} }
34+
];
35+
36+
expect(hasStrategyOverride(strategies)).toBe(true);
37+
});
38+
39+
it('should return true when multiple overriding strategies are used', () => {
40+
const strategies = [
41+
{ name: 'delegation', network: '1', params: {} },
42+
{ name: 'delegation-with-cap', network: '1', params: {} }
43+
];
44+
45+
expect(hasStrategyOverride(strategies)).toBe(true);
46+
});
47+
48+
it('should handle mixed case strategy names correctly', () => {
49+
const strategies = [{ name: 'Delegation', network: '1', params: {} }];
50+
51+
expect(hasStrategyOverride(strategies)).toBe(true);
52+
});
53+
54+
it('should handle inner strategies', () => {
55+
const strategies = [
56+
{
57+
name: 'multichain',
58+
network: '1',
59+
params: {
60+
symbol: 'MULTI',
61+
strategies: [
62+
{
63+
name: 'erc20-balance-of',
64+
network: '1',
65+
params: {
66+
address: '0x579cea1889991f68acc35ff5c3dd0621ff29b0c9',
67+
decimals: 18
68+
}
69+
},
70+
{
71+
name: 'delegation',
72+
network: '137',
73+
params: {
74+
delegationSpace: 'test.eth',
75+
strategies: [
76+
{
77+
name: 'erc20-balance-of',
78+
params: {
79+
address: '0xB9638272aD6998708de56BBC0A290a1dE534a578',
80+
decimals: 18
81+
}
82+
}
83+
]
84+
}
85+
}
86+
]
87+
}
88+
}
89+
];
90+
91+
expect(hasStrategyOverride(strategies)).toBe(true);
92+
});
93+
94+
it('should not match when strategy id appears in other fields', () => {
95+
const strategies = [
96+
{
97+
name: 'whitelist',
98+
network: '1',
99+
params: { description: 'delegation' }
100+
}
101+
];
102+
103+
expect(hasStrategyOverride(strategies)).toBe(false);
104+
});
105+
106+
it('should handle empty getOverridingStrategies result', () => {
107+
mockGetOverridingStrategies.mockReturnValue([]);
108+
109+
const strategies = [{ name: 'delegation', network: '1', params: {} }];
110+
111+
expect(hasStrategyOverride(strategies)).toBe(false);
112+
});
113+
});
114+
});

0 commit comments

Comments
 (0)