Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ node_modules
dist
coverage

.envrc
.envrc
.history
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16.13.0
3 changes: 1 addition & 2 deletions src/helpers/calculateNumberOfPrizesForIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ export function calculateNumberOfPrizesForIndex(
let bitRangeDecimal = 2 ** bitRangeSize;
let numberOfPrizesForIndex = bitRangeDecimal ** prizeDistributionIndex;

while (prizeDistributionIndex > 0) {
if (prizeDistributionIndex > 0) {
numberOfPrizesForIndex -= bitRangeDecimal ** (prizeDistributionIndex - 1);
prizeDistributionIndex--;
}

return numberOfPrizesForIndex;
Expand Down
26 changes: 26 additions & 0 deletions src/utils/computePrizeFromAbsolutePrizes.ts
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))
);
},
Comment on lines +19 to +23
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tot is the total right? Would be more explicit in full.

Suggested change
(tot: BigNumber, tierPrize: BigNumber, index: number): BigNumber => {
return tot.add(
tierPrize.mul(calculateNumberOfPrizesForIndex(bitRangeSize, index))
);
},
(total: BigNumber, tierPrize: BigNumber, index: number): BigNumber => {
return total.add(
tierPrize.mul(calculateNumberOfPrizesForIndex(bitRangeSize, index))
);
},

ethers.BigNumber.from("0")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed and this way we can also remove the ethers import.

Suggested change
ethers.BigNumber.from("0")

);
}
36 changes: 36 additions & 0 deletions src/utils/computeTiersFromAbsolutePrizes.ts
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)
);
}
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export * from "./isDrawStructSet";
export * from "./isPrizeDistributionStructSet";
export * from "./sanityCheckPrizeDistribution";
export * from "./sumBigNumbers";
export * from "./computePrizeFromAbsolutePrizes";
export * from "./computeTiersFromAbsolutePrizes";
Loading