Skip to content

Commit

Permalink
feat(approx-math): uses new solidity approximations for sdk swap amou…
Browse files Browse the repository at this point in the history
…nt outs
  • Loading branch information
Alexangelj committed Oct 10, 2021
1 parent 12f0db0 commit f205743
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
46 changes: 33 additions & 13 deletions src/entities/pool.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Wei, Time, FixedPointX64, parseFixedPointX64, parseWei, toBN } from 'web3-units'
import { quantilePrime, std_n_pdf, inverse_std_n_cdf, nonNegative } from '@primitivefinance/v2-math'
import { getStableGivenRisky, getRiskyGivenStable, calcInvariant } from '@primitivefinance/v2-math'
import {
getStableGivenRiskyApproximation,
getRiskyGivenStableApproximation,
getInvariantApproximation
} from '@primitivefinance/v2-math'
import { Engine } from './engine'
import { Calibration } from './calibration'
import { Token } from '@uniswap/sdk-core'
Expand Down Expand Up @@ -106,17 +110,16 @@ export class VirtualPool {
* @param reserveRisky Amount of risky tokens in reserve
* @return reserveStable Expected amount of stable token reserves
*/
getStableGivenRisky(reserveRisky: Wei, noInvariant?: boolean): Wei {
getStableGivenRisky(reserveRisky: Wei, useInvariant?: boolean): Wei {
const decimals = this.reserveStable.decimals
let invariant = this.invariant.parsed
invariant = Math.abs(invariant) >= 1e-8 ? invariant : 0

let stable = getStableGivenRisky(
let stable = getStableGivenRiskyApproximation(
reserveRisky.float,
this.cal.strike.float,
this.cal.sigma.float,
this.tau.years,
noInvariant ? 0 : invariant
useInvariant ? invariant : 0
)

if (isNaN(stable)) return parseWei(0, decimals)
Expand All @@ -128,17 +131,16 @@ export class VirtualPool {
* @param reserveStable Amount of stable tokens in reserve
* @return reserveRisky Expected amount of risky token reserves
*/
getRiskyGivenStable(reserveStable: Wei, noInvariant?: boolean): Wei {
getRiskyGivenStable(reserveStable: Wei, useInvariant?: boolean): Wei {
const decimals = this.reserveRisky.decimals
let invariant = this.invariant.parsed
invariant = Math.abs(invariant) >= 1e-8 ? invariant : 0

let risky = getRiskyGivenStable(
let risky = getRiskyGivenStableApproximation(
reserveStable.float,
this.cal.strike.float,
this.cal.sigma.float,
this.tau.years,
noInvariant ? 0 : invariant
useInvariant ? invariant : 0
)

if (isNaN(risky)) return parseWei(0, decimals)
Expand All @@ -159,7 +161,13 @@ export class VirtualPool {
calcInvariant(): FixedPointX64 {
const risky = this.reserveRisky.float / this.liquidity.float
const stable = this.reserveStable.float / this.liquidity.float
let invariant = calcInvariant(risky, stable, this.cal.strike.float, this.cal.sigma.float, this.tau.years)
let invariant = getInvariantApproximation(
risky,
stable,
this.cal.strike.float,
this.cal.sigma.float,
this.tau.years
)
invariant = Math.floor(invariant * Math.pow(10, 18))
this.invariant = new FixedPointX64(
toBN(invariant === NaN ? 0 : invariant)
Expand Down Expand Up @@ -238,7 +246,13 @@ export class VirtualPool {

const risky = reserveRiskyLast.add(deltaIn).float / this.liquidity.float
const stable = reserveStableLast.sub(deltaOut).float / this.liquidity.float
let nextInvariant: any = calcInvariant(risky, stable, this.cal.strike.float, this.cal.sigma.float, this.tau.years)
let nextInvariant: any = getInvariantApproximation(
risky,
stable,
this.cal.strike.float,
this.cal.sigma.float,
this.tau.years
)
nextInvariant = Math.floor(nextInvariant * Math.pow(10, 18))
nextInvariant = new FixedPointX64(
toBN(nextInvariant)
Expand Down Expand Up @@ -318,7 +332,13 @@ export class VirtualPool {
const risky = reserveRiskyLast.sub(deltaOut).float / this.liquidity.float
const stable = reserveStableLast.add(deltaIn).float / this.liquidity.float

let nextInvariant: any = calcInvariant(risky, stable, this.cal.strike.float, this.cal.sigma.float, this.tau.years)
let nextInvariant: any = getInvariantApproximation(
risky,
stable,
this.cal.strike.float,
this.cal.sigma.float,
this.tau.years
)
nextInvariant = Math.floor(nextInvariant * Math.pow(10, 18))
nextInvariant = new FixedPointX64(
toBN(nextInvariant)
Expand All @@ -338,7 +358,7 @@ export class VirtualPool {
const strike = this.cal.strike.float
const sigma = this.cal.sigma.float
const tau = this.tau.years
const spot = getStableGivenRisky(risky, strike, sigma, tau) * quantilePrime(1 - risky)
const spot = getStableGivenRiskyApproximation(risky, strike, sigma, tau) * quantilePrime(1 - risky)
return parseWei(spot)
}

Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1473,9 +1473,9 @@
mocha "^9.0.3"

"@primitivefinance/v2-math@^1.3.0":
version "1.3.0"
resolved "https://npm.pkg.github.com/download/@primitivefinance/v2-math/1.3.0/d17086c777d688a097021f1bf6d9466cc50923b4f47a1b7ea507698cc5d467c9#b9885deedc7923a5ae7a948f063d8974023490c6"
integrity sha512-A48cuGax4drUhiqfgZtrtE9Ec8ArZC6JfElasl+OBBm+zX9pVypW+MZ3y5vf2atPTe9NwvixWKMOxLAD4vEEpw==
version "1.4.1"
resolved "https://npm.pkg.github.com/download/@primitivefinance/v2-math/1.4.1/90f425aefcbe4bfee9195c70b0ace643d249a332be77cc55a8799aa9187ac931#386e6620a29578d7d40aaacedacecd2a33d0ac2e"
integrity sha512-z9XfDL88BgsJYbc1wSy5z1SP2ixb/ahrnFeuBvKcpkiaAnmH1H04Hg0OSeeW1RvRUtiLhBweqnn4JNUG2YWeWw==
dependencies:
gaussian "^1.2.0"
numeric "^1.2.6"
Expand Down

0 comments on commit f205743

Please sign in to comment.