forked from 1inch/solidity-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update typechain, migrate to ethers + new hardhat
- Loading branch information
Showing
18 changed files
with
1,175 additions
and
982 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
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,32 +1,22 @@ | ||
import { expect, toBN } from './prelude'; | ||
import BN from 'bn.js'; | ||
import { expect } from './prelude'; | ||
|
||
export function toBNExtended (value: string | number | BN): BN { | ||
if (typeof value === 'string' || typeof value === 'number') { | ||
return toBN(value); | ||
} | ||
return value; | ||
} | ||
|
||
export function assertRoughlyEqualValues (expected: string | number | BN, actual: string | number | BN, relativeDiff: number) { | ||
let expectedBN = toBNExtended(expected); | ||
let actualBN = toBNExtended(actual); | ||
if (expectedBN.isNeg() !== actualBN.isNeg()) { | ||
expect(actualBN).to.be.bignumber.equal(expectedBN, 'Values are of different sign'); | ||
} | ||
export function assertRoughlyEqualValues (expected: string | number | bigint, actual: string | number | bigint, relativeDiff: number) { | ||
let expectedBN = BigInt(expected); | ||
let actualBN = BigInt(actual); | ||
expect(expectedBN * actualBN).to.be.gte(0, 'Values are of different sign'); | ||
|
||
expectedBN = expectedBN.abs(); | ||
actualBN = actualBN.abs(); | ||
if (expectedBN < 0) expectedBN = -expectedBN; | ||
if (actualBN < 0) actualBN = -actualBN; | ||
|
||
let multiplerNumerator = relativeDiff; | ||
let multiplerDenominator = toBN('1'); | ||
let multiplerDenominator = 1n; | ||
while (!Number.isInteger(multiplerNumerator)) { | ||
multiplerDenominator = multiplerDenominator.mul(toBN('10')); | ||
multiplerDenominator = multiplerDenominator * 10n; | ||
multiplerNumerator *= 10; | ||
} | ||
const diff = expectedBN.sub(actualBN).abs(); | ||
const treshold = expectedBN.mul(toBN(multiplerNumerator.toString())).div(multiplerDenominator); | ||
if (!diff.lte(treshold)) { | ||
expect(actualBN).to.be.bignumber.equal(expectedBN, `${actual} != ${expected} with ${relativeDiff} precision`); | ||
const diff = expectedBN > actualBN ? expectedBN - actualBN : actualBN - expectedBN; | ||
const treshold = expectedBN * BigInt(multiplerNumerator) / multiplerDenominator; | ||
if (diff > treshold) { | ||
expect(actualBN).to.be.equal(expectedBN, `${actual} != ${expected} with ${relativeDiff} precision`); | ||
} | ||
} |
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
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
Oops, something went wrong.