-
Notifications
You must be signed in to change notification settings - Fork 0
Added functions to help calculate tier fractions #7
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
asselstine
wants to merge
5
commits into
master
Choose a base branch
from
pool-1918-make-it-easy-to-calculate-more-human
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
5 commits
Select commit
Hold shift + click to select a range
60e5bb9
Added functions to help calculate tier fractions
asselstine e86b9e5
Fixed linter errors
asselstine 554e31a
Fixed calculateNumberOfPrizesForIndex, lint errors, and removed probl…
asselstine 7d1c3f5
Added tests for number of prizes function
asselstine f2edc45
Fixed linter
asselstine 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,4 +4,5 @@ node_modules | |
| dist | ||
| coverage | ||
|
|
||
| .envrc | ||
| .envrc | ||
| .history | ||
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 @@ | ||
| 16.13.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
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,26 @@ | ||||
| import { BigNumber, ethers } from "ethers"; | ||||
| import { calculateNumberOfPrizesForIndex } from "../helpers/calculateNumberOfPrizesForIndex"; | ||||
|
|
||||
| /** | ||||
| * Calculates the total prize payout for tiers defined absolutely. | ||||
| * | ||||
| * @remarks | ||||
| * Given tier prizes of [2500, 800], that means there is 1 $2500 prize and 3 $800 prizes. This function | ||||
| * will add them all up. | ||||
| * | ||||
| * @param bitRangeSize The bit range to use. Determines the number of prizes per tier | ||||
| * @param tierPrizes The prize tiers defined in terms of absolute prize value. | ||||
| */ | ||||
| export function computePrizeFromAbsolutePrizes( | ||||
| bitRangeSize: number, | ||||
| tierPrizes: BigNumber[] | ||||
| ): BigNumber { | ||||
| return tierPrizes.reduce( | ||||
| (tot: BigNumber, tierPrize: BigNumber, index: number): BigNumber => { | ||||
| return tot.add( | ||||
| tierPrize.mul(calculateNumberOfPrizesForIndex(bitRangeSize, index)) | ||||
| ); | ||||
| }, | ||||
| ethers.BigNumber.from("0") | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not needed and this way we can also remove the
Suggested change
|
||||
| ); | ||||
| } | ||||
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,36 @@ | ||
| import { BigNumber, ethers } from "ethers"; | ||
| import { calculateNumberOfPrizesForIndex } from "../helpers/calculateNumberOfPrizesForIndex"; | ||
| import { computePrizeFromAbsolutePrizes } from "./computePrizeFromAbsolutePrizes"; | ||
|
|
||
| /** | ||
| * Converts a list of absolute prizes to a fraction array. | ||
| * | ||
| * @remarks | ||
| * Given an array of prizes, this function returns a corresponding array of fixed point 9 fractions for the prize tiers. | ||
| * | ||
| * For example, if the tierPrizes array is [2500, 800] then this function will assume: | ||
| * | ||
| * There is 1 $2500 prize, | ||
| * There are 3 $800 prizes | ||
| * | ||
| * The total prize is 2500 + 3 * 800 = 4900. | ||
| * | ||
| * The result will then be `[2500*1e9/4900, (3*800*1e9)/4900]` or [ 510204081, 489795918 ] | ||
| * | ||
| * @param bitRangeSize The bit range that determines the count for each tier of prizes | ||
| * @param tierPrizes The tiers of prizes represented as absolute amounts. | ||
| */ | ||
| export function computeTiersFromAbsolutePrizes( | ||
| bitRangeSize: number, | ||
| tierPrizes: BigNumber[] | ||
| ): BigNumber[] { | ||
| const totalPrizes = computePrizeFromAbsolutePrizes(bitRangeSize, tierPrizes); | ||
|
|
||
| return tierPrizes.map( | ||
| (tierPrize: BigNumber, index: number): BigNumber => | ||
| tierPrize | ||
| .mul(calculateNumberOfPrizesForIndex(bitRangeSize, index)) | ||
| .mul(ethers.utils.parseUnits("1", 9)) | ||
| .div(totalPrizes) | ||
| ); | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
totis thetotalright? Would be more explicit in full.