-
Notifications
You must be signed in to change notification settings - Fork 13
feat: add vp value to leaderboard #571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wa0x6e
wants to merge
18
commits into
master
Choose a base branch
from
feat-add-vp-value-to-leaderboard
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
4383ded
feat: compute vote fiat value
wa0x6e 9961824
Merge branch 'master' into feat-assign-fiat-value-to-vote
wa0x6e 4715de8
test: fix test for new thrown error
wa0x6e fcfb9df
fix: get last_cb from env var
wa0x6e 433f4fe
refactor: more generic array computation function
wa0x6e 52a9017
fix: remove arrayoperation
wa0x6e d7e85ab
fix: use simple computation as both array are only number
wa0x6e 35e4f26
refactor: move all values logic to a helper
wa0x6e 08cff39
test: add tests
wa0x6e 960d8cb
Update src/helpers/entityValue.ts
wa0x6e 7a94dd6
chore: remove duplicate doc
wa0x6e 43ef1bb
feat: increase leaderboard vp_value on vote creation
wa0x6e 2172f47
feat: decrease leaderboard vp_value on proposal deletion
wa0x6e d6381a8
fix: update leaderboard vp_value on new vote
wa0x6e 01a782b
refactor: code improvement
wa0x6e 207810e
refactor: code improvement
wa0x6e 0e74c74
Merge branch 'master' into feat-assign-fiat-value-to-vote
wa0x6e fe1e039
Merge branch 'feat-assign-fiat-value-to-vote' into feat-add-vp-value-…
wa0x6e File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| type Vote = { | ||
| vp_by_strategy: number[]; | ||
| }; | ||
|
|
||
| type Proposal = { | ||
| vp_value_by_strategy: number[]; | ||
| }; | ||
|
|
||
| /** | ||
| * Calculates the total vote value based on the voting power and the proposal's value per strategy. | ||
| * @returns The total vote value, in the currency unit specified by the proposal's vp_value_by_strategy values | ||
| **/ | ||
| export function getVoteValue(proposal: Proposal, vote: Vote): number { | ||
| if (proposal.vp_value_by_strategy.length !== vote.vp_by_strategy.length) { | ||
| throw new Error('invalid data to compute vote value'); | ||
| } | ||
|
|
||
| return proposal.vp_value_by_strategy.reduce( | ||
| (sum, value, index) => sum + value * vote.vp_by_strategy[index], | ||
| 0 | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { getVoteValue } from '../../../src/helpers/entityValue'; | ||
|
|
||
| describe('getVoteValue', () => { | ||
| it('should calculate correct vote value with single strategy', () => { | ||
| const proposal = { vp_value_by_strategy: [2.5] }; | ||
| const vote = { vp_by_strategy: [100] }; | ||
|
|
||
| const result = getVoteValue(proposal, vote); | ||
|
|
||
| expect(result).toBe(250); | ||
| }); | ||
|
|
||
| it('should calculate correct vote value with multiple strategies', () => { | ||
| const proposal = { vp_value_by_strategy: [1.5, 3.0, 0.5] }; | ||
| const vote = { vp_by_strategy: [100, 50, 200] }; | ||
|
|
||
| const result = getVoteValue(proposal, vote); | ||
|
|
||
| expect(result).toBe(400); // (1.5 * 100) + (3.0 * 50) + (0.5 * 200) = 150 + 150 + 100 = 400 | ||
| }); | ||
|
|
||
| it('should return 0 when vote has no voting power', () => { | ||
| const proposal = { vp_value_by_strategy: [2.0, 1.5] }; | ||
| const vote = { vp_by_strategy: [0, 0] }; | ||
|
|
||
| const result = getVoteValue(proposal, vote); | ||
|
|
||
| expect(result).toBe(0); | ||
| }); | ||
|
|
||
| it('should return 0 when proposal has no value per strategy', () => { | ||
| const proposal = { vp_value_by_strategy: [0, 0] }; | ||
| const vote = { vp_by_strategy: [100, 50] }; | ||
|
|
||
| const result = getVoteValue(proposal, vote); | ||
|
|
||
| expect(result).toBe(0); | ||
| }); | ||
|
|
||
| it('should handle decimal values correctly', () => { | ||
| const proposal = { vp_value_by_strategy: [0.1, 0.25] }; | ||
| const vote = { vp_by_strategy: [10, 20] }; | ||
|
|
||
| const result = getVoteValue(proposal, vote); | ||
|
|
||
| expect(result).toBe(6); // (0.1 * 10) + (0.25 * 20) = 1 + 5 = 6 | ||
| }); | ||
|
|
||
| it('should throw error when strategy arrays have different lengths', () => { | ||
| const proposal = { vp_value_by_strategy: [1.0, 2.0] }; | ||
| const vote = { vp_by_strategy: [100] }; | ||
|
|
||
| expect(() => getVoteValue(proposal, vote)).toThrow('invalid data to compute vote value'); | ||
| }); | ||
|
|
||
| it('should throw error when vote has more strategies than proposal', () => { | ||
| const proposal = { vp_value_by_strategy: [1.0] }; | ||
| const vote = { vp_by_strategy: [100, 50] }; | ||
|
|
||
| expect(() => getVoteValue(proposal, vote)).toThrow('invalid data to compute vote value'); | ||
| }); | ||
|
|
||
| it('should handle empty arrays', () => { | ||
| const proposal = { vp_value_by_strategy: [] }; | ||
| const vote = { vp_by_strategy: [] }; | ||
|
|
||
| const result = getVoteValue(proposal, vote); | ||
|
|
||
| expect(result).toBe(0); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.