Skip to content

Commit

Permalink
Merge pull request #112 from peanutprotocol/moreRandomness
Browse files Browse the repository at this point in the history
feat: more randomness
  • Loading branch information
borcherd authored Feb 23, 2024
2 parents 2df17aa + 8294fb3 commit b5b016d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
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, validateUserName } 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 @@ -375,11 +379,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)
})

0 comments on commit b5b016d

Please sign in to comment.