Skip to content

Commit 0706ce6

Browse files
committed
refactor: improve readability and robustness
refactor:
1 parent 716f434 commit 0706ce6

File tree

2 files changed

+89
-7
lines changed

2 files changed

+89
-7
lines changed

src/helpers/entityValue.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,43 @@ type Proposal = {
44
};
55

66
/**
7-
* Calculates the total proposal value based on all votes' total voting power and the proposal's value per strategy.
7+
* Calculates the proposal total value based on all votes' total voting power and the proposal's value per strategy.
88
* @returns The total value of the given proposal's votes, in the currency unit specified by the proposal's vp_value_by_strategy values
99
*/
1010
export function getProposalValue(proposal: Proposal): number {
11-
return (
12-
proposal.scores_by_strategy[0]
13-
?.map((_, index) => proposal.scores_by_strategy.reduce((sum, array) => sum + array[index], 0))
14-
?.map((value, index) => value * proposal.vp_value_by_strategy[index])
15-
?.reduce((sum, value) => sum + value, 0) || 0
16-
);
11+
const { scores_by_strategy, vp_value_by_strategy } = proposal;
12+
13+
if (
14+
!scores_by_strategy.length ||
15+
!scores_by_strategy[0]?.length ||
16+
!vp_value_by_strategy.length
17+
) {
18+
return 0;
19+
}
20+
21+
let totalValue = 0;
22+
for (let strategyIndex = 0; strategyIndex < vp_value_by_strategy.length; strategyIndex++) {
23+
const strategyTotal = scores_by_strategy.reduce((sum, voteScores) => {
24+
if (voteScores.length !== vp_value_by_strategy.length) {
25+
throw new Error(
26+
'Array size mismatch: voteScores length does not match vp_value_by_strategy length'
27+
);
28+
}
29+
const score = voteScores[strategyIndex];
30+
if (typeof score !== 'number') {
31+
throw new Error(`Invalid score value: expected number, got ${typeof score}`);
32+
}
33+
return sum + score;
34+
}, 0);
35+
36+
if (typeof vp_value_by_strategy[strategyIndex] !== 'number') {
37+
throw new Error(
38+
`Invalid vp_value: expected number, got ${typeof vp_value_by_strategy[strategyIndex]}`
39+
);
40+
}
41+
42+
totalValue += strategyTotal * vp_value_by_strategy[strategyIndex];
43+
}
44+
45+
return totalValue;
1746
}

test/unit/helpers/entityValue.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ describe('getProposalValue', () => {
4949
expect(result).toBe(0);
5050
});
5151

52+
it('should return 0 when vp_value_by_strategy is empty', () => {
53+
const proposal = {
54+
scores_by_strategy: [[100], [200]],
55+
vp_value_by_strategy: []
56+
};
57+
58+
const result = getProposalValue(proposal);
59+
60+
expect(result).toBe(0);
61+
});
62+
5263
it('should handle zero values correctly', () => {
5364
const proposal = {
5465
scores_by_strategy: [
@@ -101,4 +112,46 @@ describe('getProposalValue', () => {
101112

102113
expect(result).toBe(200); // 100 * 2.0 = 200
103114
});
115+
116+
it('should throw on array size mismatch', () => {
117+
const proposal = {
118+
scores_by_strategy: [
119+
[100, 50], // 2 strategies
120+
[200, 75] // 2 strategies
121+
],
122+
vp_value_by_strategy: [1.5] // Only 1 strategy value
123+
};
124+
125+
expect(() => getProposalValue(proposal)).toThrow(
126+
'Array size mismatch: voteScores length does not match vp_value_by_strategy length'
127+
);
128+
});
129+
130+
it('should throw on invalid score value', () => {
131+
const proposal = {
132+
scores_by_strategy: [
133+
[100, 'invalid'], // Invalid string value
134+
[200, 75]
135+
],
136+
vp_value_by_strategy: [1.5, 2.0]
137+
} as any;
138+
139+
expect(() => getProposalValue(proposal)).toThrow(
140+
'Invalid score value: expected number, got string'
141+
);
142+
});
143+
144+
it('should throw on invalid vp_value', () => {
145+
const proposal = {
146+
scores_by_strategy: [
147+
[100, 50],
148+
[200, 75]
149+
],
150+
vp_value_by_strategy: [1.5, 'invalid'] // Invalid string value
151+
} as any;
152+
153+
expect(() => getProposalValue(proposal)).toThrow(
154+
'Invalid vp_value: expected number, got string'
155+
);
156+
});
104157
});

0 commit comments

Comments
 (0)