Skip to content

Commit a13c0a4

Browse files
committed
refactor: centralize fiat value logic in dedicated helper
1 parent cac0b30 commit a13c0a4

File tree

3 files changed

+36
-46
lines changed

3 files changed

+36
-46
lines changed

src/helpers/entityValue.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
type Proposal = {
2-
scores_by_strategy: string;
3-
vp_value_by_strategy: string;
2+
scores_by_strategy: number[][];
3+
vp_value_by_strategy: number[];
44
};
55

66
/**
77
* Calculates the total proposal value based on the vote's 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-
const scoresByStrategy: number[][] = JSON.parse(proposal.scores_by_strategy);
12-
const vpValueByStrategy: number[] = JSON.parse(proposal.vp_value_by_strategy);
13-
1411
return (
15-
scoresByStrategy[0]
16-
?.map((_, index) => scoresByStrategy.reduce((sum, array) => sum + array[index], 0))
17-
?.map((value, index) => value * vpValueByStrategy[index])
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])
1815
?.reduce((sum, value) => sum + value, 0) || 0
1916
);
2017
}

src/scores.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ async function getProposal(id: string): Promise<any | undefined> {
1717
proposal.choices = JSON.parse(proposal.choices);
1818
proposal.scores = JSON.parse(proposal.scores);
1919
proposal.scores_by_strategy = JSON.parse(proposal.scores_by_strategy);
20+
proposal.vp_value_by_strategy = JSON.parse(proposal.vp_value_by_strategy);
2021
let proposalState = 'pending';
2122
const ts = parseInt((Date.now() / 1e3).toFixed());
2223
if (ts > proposal.start) proposalState = 'active';
@@ -99,10 +100,7 @@ async function updateProposalScores(proposalId: string, scores: any, votes: numb
99100
}
100101

101102
async function updateProposalScoresValue(proposalId: string) {
102-
const [proposal] = await db.queryAsync(
103-
'SELECT vp_value_by_strategy, scores_by_strategy FROM proposals WHERE id = ? LIMIT 1;',
104-
[proposalId]
105-
);
103+
const proposal = await getProposal(proposalId);
106104
const query = 'UPDATE proposals SET scores_total_value = ? WHERE id = ? LIMIT 1;';
107105
await db.queryAsync(query, [getProposalValue(proposal), proposalId]);
108106
}

test/unit/helpers/entityValue.test.ts

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { getProposalValue } from '../../../src/helpers/entityValue';
33
describe('getProposalValue', () => {
44
it('should calculate correct proposal value with single strategy', () => {
55
const proposal = {
6-
scores_by_strategy: '[[100], [200]]',
7-
vp_value_by_strategy: '[2.5]'
6+
scores_by_strategy: [[100], [200]],
7+
vp_value_by_strategy: [2.5]
88
};
99

1010
const result = getProposalValue(proposal);
@@ -14,8 +14,12 @@ describe('getProposalValue', () => {
1414

1515
it('should calculate correct proposal value with multiple strategies', () => {
1616
const proposal = {
17-
scores_by_strategy: '[[100, 50], [200, 75], [300, 25]]',
18-
vp_value_by_strategy: '[1.5, 3.0]'
17+
scores_by_strategy: [
18+
[100, 50],
19+
[200, 75],
20+
[300, 25]
21+
],
22+
vp_value_by_strategy: [1.5, 3.0]
1923
};
2024

2125
const result = getProposalValue(proposal);
@@ -25,8 +29,8 @@ describe('getProposalValue', () => {
2529

2630
it('should return 0 when scores_by_strategy is empty', () => {
2731
const proposal = {
28-
scores_by_strategy: '[]',
29-
vp_value_by_strategy: '[2.0]'
32+
scores_by_strategy: [],
33+
vp_value_by_strategy: [2.0]
3034
};
3135

3236
const result = getProposalValue(proposal);
@@ -36,8 +40,8 @@ describe('getProposalValue', () => {
3640

3741
it('should return 0 when first strategy array is empty', () => {
3842
const proposal = {
39-
scores_by_strategy: '[[]]',
40-
vp_value_by_strategy: '[2.0]'
43+
scores_by_strategy: [[]],
44+
vp_value_by_strategy: [2.0]
4145
};
4246

4347
const result = getProposalValue(proposal);
@@ -47,8 +51,11 @@ describe('getProposalValue', () => {
4751

4852
it('should handle zero values correctly', () => {
4953
const proposal = {
50-
scores_by_strategy: '[[0, 0], [0, 0]]',
51-
vp_value_by_strategy: '[2.0, 1.5]'
54+
scores_by_strategy: [
55+
[0, 0],
56+
[0, 0]
57+
],
58+
vp_value_by_strategy: [2.0, 1.5]
5259
};
5360

5461
const result = getProposalValue(proposal);
@@ -58,8 +65,11 @@ describe('getProposalValue', () => {
5865

5966
it('should handle zero vp_value_by_strategy correctly', () => {
6067
const proposal = {
61-
scores_by_strategy: '[[100, 50], [200, 75]]',
62-
vp_value_by_strategy: '[0, 0]'
68+
scores_by_strategy: [
69+
[100, 50],
70+
[200, 75]
71+
],
72+
vp_value_by_strategy: [0, 0]
6373
};
6474

6575
const result = getProposalValue(proposal);
@@ -69,8 +79,11 @@ describe('getProposalValue', () => {
6979

7080
it('should handle decimal values correctly', () => {
7181
const proposal = {
72-
scores_by_strategy: '[[10.5, 20.5], [15.5, 25.5]]',
73-
vp_value_by_strategy: '[0.1, 0.2]'
82+
scores_by_strategy: [
83+
[10.5, 20.5],
84+
[15.5, 25.5]
85+
],
86+
vp_value_by_strategy: [0.1, 0.2]
7487
};
7588

7689
const result = getProposalValue(proposal);
@@ -80,30 +93,12 @@ describe('getProposalValue', () => {
8093

8194
it('should handle single vote scenario', () => {
8295
const proposal = {
83-
scores_by_strategy: '[[100]]',
84-
vp_value_by_strategy: '[2.0]'
96+
scores_by_strategy: [[100]],
97+
vp_value_by_strategy: [2.0]
8598
};
8699

87100
const result = getProposalValue(proposal);
88101

89102
expect(result).toBe(200); // 100 * 2.0 = 200
90103
});
91-
92-
it('should throw error for invalid JSON in scores_by_strategy', () => {
93-
const proposal = {
94-
scores_by_strategy: 'invalid json',
95-
vp_value_by_strategy: '[2.0]'
96-
};
97-
98-
expect(() => getProposalValue(proposal)).toThrow();
99-
});
100-
101-
it('should throw error for invalid JSON in vp_value_by_strategy', () => {
102-
const proposal = {
103-
scores_by_strategy: '[[100]]',
104-
vp_value_by_strategy: 'invalid json'
105-
};
106-
107-
expect(() => getProposalValue(proposal)).toThrow();
108-
});
109104
});

0 commit comments

Comments
 (0)