Commit 5ca6905c authored by Moody Salem's avatar Moody Salem

use sqrt from sdk-core

parent 0bcc2513
import { BigintIsh, ChainId, Price, Token, TokenAmount } from '@uniswap/sdk-core'
import { BigintIsh, ChainId, Price, sqrt, Token, TokenAmount } from '@uniswap/sdk-core'
import invariant from 'tiny-invariant'
import JSBI from 'jsbi'
import { pack, keccak256 } from '@ethersproject/solidity'
import { getCreate2Address } from '@ethersproject/address'
import babylonianSqrt from '../utils/babylonianSqrt'
import { FACTORY_ADDRESS, INIT_CODE_HASH, MINIMUM_LIQUIDITY, FIVE, _997, _1000, ONE, ZERO } from '../constants'
import { InsufficientReservesError, InsufficientInputAmountError } from '../errors'
......@@ -159,10 +158,7 @@ export class Pair {
let liquidity: JSBI
if (JSBI.equal(totalSupply.raw, ZERO)) {
liquidity = JSBI.subtract(
babylonianSqrt(JSBI.multiply(tokenAmounts[0].raw, tokenAmounts[1].raw)),
MINIMUM_LIQUIDITY
)
liquidity = JSBI.subtract(sqrt(JSBI.multiply(tokenAmounts[0].raw, tokenAmounts[1].raw)), MINIMUM_LIQUIDITY)
} else {
const amount0 = JSBI.divide(JSBI.multiply(tokenAmounts[0].raw, totalSupply.raw), this.reserve0.raw)
const amount1 = JSBI.divide(JSBI.multiply(tokenAmounts[1].raw, totalSupply.raw), this.reserve1.raw)
......@@ -193,8 +189,8 @@ export class Pair {
invariant(!!kLast, 'K_LAST')
const kLastParsed = JSBI.BigInt(kLast)
if (!JSBI.equal(kLastParsed, ZERO)) {
const rootK = babylonianSqrt(JSBI.multiply(this.reserve0.raw, this.reserve1.raw))
const rootKLast = babylonianSqrt(kLastParsed)
const rootK = sqrt(JSBI.multiply(this.reserve0.raw, this.reserve1.raw))
const rootKLast = sqrt(kLastParsed)
if (JSBI.greaterThan(rootK, rootKLast)) {
const numerator = JSBI.multiply(totalSupply.raw, JSBI.subtract(rootK, rootKLast))
const denominator = JSBI.add(JSBI.multiply(rootK, FIVE), rootKLast)
......
import JSBI from 'jsbi'
import babylonianSqrt from './babylonianSqrt'
describe('#babylonianSqrt', () => {
it('correct for 0-1000', () => {
for (let i = 0; i < 1000; i++) {
expect(babylonianSqrt(JSBI.BigInt(i))).toEqual(JSBI.BigInt(Math.floor(Math.sqrt(i))))
}
})
})
import JSBI from 'jsbi'
const ZERO = JSBI.BigInt(0)
const ONE = JSBI.BigInt(1)
const TWO = JSBI.BigInt(2)
const THREE = JSBI.BigInt(3)
// computes floor(babylonianSqrt(y)) using the babylonian method (not the fastest way)
export default function babylonianSqrt(y: JSBI): JSBI {
let z: JSBI = ZERO
let x: JSBI
if (JSBI.greaterThan(y, THREE)) {
z = y
x = JSBI.add(JSBI.divide(y, TWO), ONE)
while (JSBI.lessThan(x, z)) {
z = x
x = JSBI.divide(JSBI.add(JSBI.divide(y, x), x), TWO)
}
} else if (JSBI.notEqual(y, ZERO)) {
z = ONE
}
return z
}
......@@ -1706,10 +1706,10 @@
semver "^7.3.2"
tsutils "^3.17.1"
"@uniswap/sdk-core@^1.0.8":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@uniswap/sdk-core/-/sdk-core-1.0.8.tgz#ef71021bd7f2791bdb146b740f876a4f2d89c05b"
integrity sha512-ted1NTEj023A501rVLdYHqvJt+abAJZPyYZKMLgQ0OGsT/rO8NCYR4kZgvwumv+c7ZlUM8jdmnkiPVH+lFc/kw==
"@uniswap/sdk-core@^1.0.9":
version "1.0.9"
resolved "https://registry.yarnpkg.com/@uniswap/sdk-core/-/sdk-core-1.0.9.tgz#67e8d506e2c563f5183245ae9c32c54ac083479b"
integrity sha512-CWvDhN7mU+v786HEqAcyDhRMlLt9r6/Etgcduc36PlmmWk1Bb2XyK/UGMu/MD8cB9NlJ0h17WLx/X+CFg93ivg==
dependencies:
"@ethersproject/address" "^5.0.2"
big.js "^5.2.2"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment