Skip to content

Commit 09ba5a1

Browse files
committed
fix: use getter to avoid reference invalidation
1 parent 83e61c8 commit 09ba5a1

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed

src/helpers/spaceValidation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import snapshot from '@snapshot-labs/snapshot.js';
22
import log from './log';
3-
import { strategies } from './strategies';
3+
import { getStrategies } from './strategies';
44

55
const DEFAULT_SNAPSHOT_ENV: string = 'testnet';
66

@@ -67,7 +67,7 @@ export async function validateSpaceSettings(
6767
}
6868

6969
for (const id of strategiesIds) {
70-
const strategy = strategies[id];
70+
const strategy = getStrategies()[id];
7171

7272
if (!strategy) {
7373
return Promise.reject(`strategy "${id}" is not a valid strategy`);

src/helpers/strategies.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const URI = new URL(
1818

1919
let consecutiveFailsCount = 0;
2020
let shouldStop = false;
21-
export let strategies: Record<Strategy['id'], Strategy> = {};
21+
let strategies: Record<Strategy['id'], Strategy> = {};
2222

2323
async function loadStrategies() {
2424
const res = await snapshot.utils.getJSON(URI);
@@ -33,13 +33,18 @@ async function loadStrategies() {
3333
throw error;
3434
}
3535

36-
const strat = Object.values(res).map((strategy: any) => ({
36+
const strategiesList = Object.values(res).map((strategy: any) => ({
3737
id: strategy.key,
3838
override: strategy.dependOnOtherAddress || false,
3939
disabled: strategy.disabled || false
4040
}));
4141

42-
strategies = Object.fromEntries(strat.map(strategy => [strategy.id, strategy]));
42+
strategies = Object.fromEntries(strategiesList.map(strategy => [strategy.id, strategy]));
43+
}
44+
45+
// Using a getter to avoid potential reference initialization issues
46+
export function getStrategies(): Record<Strategy['id'], Strategy> {
47+
return strategies;
4348
}
4449

4550
export async function initialize() {

test/integration/ingestor.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ jest.mock('@snapshot-labs/pineapple', () => {
113113
});
114114

115115
jest.mock('../../src/helpers/strategies', () => ({
116-
strategies: {
116+
getStrategies: jest.fn(() => ({
117117
'erc20-balance-of': { id: 'erc20-balance-of', override: false, disabled: false },
118118
'contract-call': { id: 'contract-call', override: true, disabled: false },
119119
delegation: { id: 'delegation', override: false, disabled: false },
120120
whitelist: { id: 'whitelist', override: false, disabled: false }
121-
},
121+
})),
122122
initialize: jest.fn().mockResolvedValue(undefined),
123123
run: jest.fn(),
124124
stop: jest.fn()

test/unit/helpers/spaceValidation.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const mockStrategies: Record<string, any> = {
99
};
1010

1111
jest.mock('../../../src/helpers/strategies', () => ({
12-
strategies: mockStrategies
12+
getStrategies: jest.fn(() => mockStrategies)
1313
}));
1414

1515
// Mock the log module to avoid any issues

0 commit comments

Comments
 (0)