From 8294fb3f5adcd50e0e4f6992e5bfc25fe0678b58 Mon Sep 17 00:00:00 2001 From: Hugo Montenegro Date: Wed, 14 Feb 2024 02:53:31 +0000 Subject: [PATCH] feat: more randomness --- src/raffle.ts | 16 ++++----- .../basic/generateAmountsDistribution.test.ts | 33 +++++++++++++++++++ 2 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 test/basic/generateAmountsDistribution.test.ts diff --git a/src/raffle.ts b/src/raffle.ts index b61dd16..cee8749 100644 --- a/src/raffle.ts +++ b/src/raffle.ts @@ -18,12 +18,16 @@ import { import { TransactionRequest } from '@ethersproject/abstract-provider' import { getRawParamsFromLink } from './util' -export function generateAmountsDistribution(totalAmount: BigNumber, numberOfLinks: number): BigNumber[] { +export function generateAmountsDistribution( + totalAmount: BigNumber, + numberOfLinks: number, + exponent: number = 4 +): BigNumber[] { const randoms: number[] = [] let randomsSum = 0 for (let i = 0; i < numberOfLinks; i++) { - let value = Math.random() - value += 0.05 // communism - make sure that everyone gets a reasonable amount + let value = Math.random() ** exponent // Squaring to make distribution more spikeyt + value += 1 / numberOfLinks // communism - make sure that everyone gets a minimal amount randoms.push(value) randomsSum += value } @@ -374,11 +378,7 @@ export async function getRaffleAuthorisation({ ) } if (error.includes('captacha is required')) { - throw new interfaces.SDKStatus( - interfaces.ERaffleErrorCodes.CAPTCHA_REQUIRED, - 'Captcha is required', - error - ) + throw new interfaces.SDKStatus(interfaces.ERaffleErrorCodes.CAPTCHA_REQUIRED, 'Captcha is required', error) } throw new Error(error) } diff --git a/test/basic/generateAmountsDistribution.test.ts b/test/basic/generateAmountsDistribution.test.ts new file mode 100644 index 0000000..c3bc296 --- /dev/null +++ b/test/basic/generateAmountsDistribution.test.ts @@ -0,0 +1,33 @@ +import { generateAmountsDistribution } from '../../src/raffle' +import { BigNumber } from 'ethersv5' + +describe('Amounts Distribution Functionality', () => { + test('should generate a distribution for 250 slots with a total of 125 Amount', async () => { + const totalAmount = BigNumber.from(125000) + const numberOfSlots = 250 + const distribution = generateAmountsDistribution(totalAmount, numberOfSlots) + + // Print out the amounts distribution + console.log( + 'Amounts Distribution:', + distribution.map((amount) => amount.toString()) + ) + + // Rank by size + const rankedDistribution = distribution.sort((a, b) => b.sub(a).toNumber()) + console.log( + 'Ranked Distribution:', + rankedDistribution.map((amount) => amount.toString()) + ) + + // Expectations + expect(distribution.length).toBe(numberOfSlots) + + // Assert that the sum of the distribution equals the total amount + const sumOfDistribution = distribution.reduce((acc, val) => acc.add(val), BigNumber.from(0)) + expect(sumOfDistribution.toString()).toBe(totalAmount.toString()) + + // Optionally, print the sum for verification + console.log('Sum of Distribution:', sumOfDistribution.toString()) + }, 30000) +})