-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(limit-order): limit max salt and amounts (#20)
- Loading branch information
Showing
3 changed files
with
40 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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,14 @@ | ||
import {randBigInt} from './rand-bigint' | ||
|
||
describe('randBigint', () => { | ||
it('should generate rand bigint in correct interval', () => { | ||
expect(randBigInt(1)).toBeGreaterThanOrEqual(0n) | ||
expect(randBigInt(1)).toBeLessThanOrEqual(1n) | ||
|
||
expect(randBigInt(10n)).toBeGreaterThanOrEqual(0) | ||
expect(randBigInt(10n)).toBeLessThanOrEqual(10n) | ||
|
||
expect(randBigInt(2n << 96n)).toBeGreaterThanOrEqual(0) | ||
expect(randBigInt(2n << 96n)).toBeLessThanOrEqual(2n << 96n) | ||
}) | ||
}) |
This file contains 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 |
---|---|---|
@@ -1,3 +1,20 @@ | ||
export function randBigInt(max: number): bigint { | ||
return BigInt(Math.floor(Math.random() * max)) | ||
import {randomBytes} from 'ethers' | ||
|
||
export function randBigInt(max: number | bigint): bigint { | ||
let bytesCount = 0 | ||
max = BigInt(max) + 1n | ||
let rest = max | ||
while (rest) { | ||
rest = rest >> 8n | ||
bytesCount += 1 | ||
} | ||
|
||
const bytes = randomBytes(bytesCount) | ||
|
||
const val = bytes.reduce( | ||
(acc, val, i) => acc + (BigInt(val) << BigInt(i * 8)), | ||
0n | ||
) | ||
|
||
return val % max | ||
} |