Skip to content
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

feat: more randomness #112

Merged
merged 1 commit into from
Feb 23, 2024
Merged
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
16 changes: 8 additions & 8 deletions src/raffle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down
33 changes: 33 additions & 0 deletions test/basic/generateAmountsDistribution.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})