Skip to content

Commit fac6311

Browse files
committed
feat: validate space strategies
1 parent 1a0903b commit fac6311

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/writer/settings.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import { addOrUpdateSpace, getSpace } from '../helpers/actions';
55
import log from '../helpers/log';
66
import db from '../helpers/mysql';
77
import { getLimit, getSpaceType } from '../helpers/options';
8-
import { clearStampCache, DEFAULT_NETWORK, jsonParse } from '../helpers/utils';
8+
import { clearStampCache, DEFAULT_NETWORK, fetchWithKeepAlive, jsonParse } from '../helpers/utils';
99

1010
const SNAPSHOT_ENV = process.env.NETWORK || 'testnet';
1111
const broviderUrl = process.env.BROVIDER_URL || 'https://rpc.snapshot.org';
12+
const scoreAPIUrl = process.env.SCORE_API_URL || 'https://score.snapshot.org';
1213

1314
export async function validateSpaceSettings(originalSpace: any) {
1415
const spaceType = originalSpace.turbo ? 'turbo' : 'default';
@@ -80,6 +81,26 @@ export async function verify(body): Promise<any> {
8081
return Promise.reject(`max number of strategies is ${strategiesLimit}`);
8182
}
8283

84+
try {
85+
const strategiesList = await (await fetchWithKeepAlive(`${scoreAPIUrl}/api/strategies`)).json();
86+
87+
msg.payload.strategies
88+
.map(strategy => strategy.name)
89+
.forEach(strategyName => {
90+
const strategy = strategiesList[strategyName];
91+
92+
if (!strategy) {
93+
return Promise.reject(`strategy "${strategyName}" is not a valid strategy`);
94+
}
95+
96+
if (strategy.disabled) {
97+
return Promise.reject(`strategy "${strategyName}" has been deprecated`);
98+
}
99+
});
100+
} catch (e) {
101+
return Promise.reject('failed to validate strategies');
102+
}
103+
83104
const controller = await snapshot.utils.getSpaceController(msg.space, DEFAULT_NETWORK, {
84105
broviderUrl
85106
});

test/unit/writer/settings.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ function randomStrategies(count = 1) {
1313
return Array(count)
1414
.fill(0)
1515
.map(() => ({
16-
name: `strategy-${Math.floor(Math.random() * 1000)}`
16+
name: 'whitelist',
17+
params: {
18+
addresses: [`0x${Math.floor(Math.random() * 1000)}`]
19+
}
1720
}));
1821
}
1922

@@ -134,7 +137,7 @@ describe('writer/settings', () => {
134137
it.todo('rejects if the submitter does not have permission to change admin');
135138
const maxStrategiesForNormalSpace = LIMITS['space.default.strategies_limit'];
136139
const maxStrategiesForTurboSpace = LIMITS['space.turbo.strategies_limit'];
137-
it(`rejects if passing more than ${maxStrategiesForNormalSpace} strategies for normal space`, async () => {
140+
it.only(`rejects if passing more than ${maxStrategiesForNormalSpace} strategies for normal space`, async () => {
138141
return expect(
139142
verify(
140143
editedInput({

0 commit comments

Comments
 (0)