-
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.
Merge pull request #9 from haiko-xyz/feat/test-reversion-solver
feat: reversion solver impl + tests
- Loading branch information
Showing
50 changed files
with
6,089 additions
and
171 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 |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
members = [ | ||
"packages/core", | ||
"packages/replicating", | ||
"packages/reversion", | ||
] |
File renamed without changes.
File renamed without changes.
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,79 @@ | ||
import Decimal from "decimal.js"; | ||
import { PRECISION, ROUNDING } from "../config"; | ||
|
||
export const calcFee = (grossAmount: Decimal.Value, feeRate: Decimal.Value) => { | ||
Decimal.set({ precision: PRECISION, rounding: ROUNDING }); | ||
let grossAmountDec = new Decimal(grossAmount); | ||
let fee = grossAmountDec.mul(feeRate); | ||
return fee.toFixed(); | ||
}; | ||
|
||
export const netToFee = (netAmount: Decimal.Value, feeRate: Decimal.Value) => { | ||
Decimal.set({ precision: PRECISION, rounding: ROUNDING }); | ||
let netAmountBN = new Decimal(netAmount); | ||
let one = new Decimal(1); | ||
let fee = netAmountBN.mul(feeRate).div(one.sub(feeRate)); | ||
return fee.toFixed(); | ||
}; | ||
|
||
export const netToGross = ( | ||
netAmount: Decimal.Value, | ||
feeRate: Decimal.Value | ||
) => { | ||
Decimal.set({ precision: PRECISION, rounding: ROUNDING }); | ||
let netAmountBN = new Decimal(netAmount); | ||
let one = new Decimal(1); | ||
let fee = netAmountBN.div(one.sub(feeRate)); | ||
return fee.toFixed(); | ||
}; | ||
|
||
export const grossToNet = ( | ||
grossAmount: Decimal.Value, | ||
feeRate: Decimal.Value | ||
) => { | ||
Decimal.set({ precision: PRECISION, rounding: ROUNDING }); | ||
let grossAmountDec = new Decimal(grossAmount); | ||
let one = new Decimal(1); | ||
let netAmount = grossAmountDec.mul(one.sub(feeRate)); | ||
return netAmount.toFixed(); | ||
}; | ||
|
||
export const getFeeInside = ( | ||
lowerBaseFeeFactor: Decimal.Value, | ||
lowerQuoteFeeFactor: Decimal.Value, | ||
upperBaseFeeFactor: Decimal.Value, | ||
upperQuoteFeeFactor: Decimal.Value, | ||
lowerLimit: Decimal.Value, | ||
upperLimit: Decimal.Value, | ||
currLimit: Decimal.Value, | ||
marketBaseFeeFactor: Decimal.Value, | ||
marketQuoteFeeFactor: Decimal.Value | ||
) => { | ||
const currLimitDec = new Decimal(currLimit); | ||
const marketBaseFeeFactorDec = new Decimal(marketBaseFeeFactor); | ||
const marketQuoteFeeFactorDec = new Decimal(marketQuoteFeeFactor); | ||
|
||
const baseFeesBelow = currLimitDec.gte(lowerLimit) | ||
? lowerBaseFeeFactor | ||
: marketBaseFeeFactorDec.sub(lowerBaseFeeFactor); | ||
const baseFeesAbove = currLimitDec.lt(upperLimit) | ||
? upperBaseFeeFactor | ||
: marketBaseFeeFactorDec.sub(upperBaseFeeFactor); | ||
const quoteFeesBelow = currLimitDec.gte(lowerLimit) | ||
? lowerQuoteFeeFactor | ||
: marketQuoteFeeFactorDec.sub(lowerQuoteFeeFactor); | ||
const quoteFeesAbove = currLimitDec.lt(upperLimit) | ||
? upperQuoteFeeFactor | ||
: marketQuoteFeeFactorDec.sub(upperQuoteFeeFactor); | ||
|
||
const baseFeeFactor = marketBaseFeeFactorDec | ||
.sub(baseFeesBelow) | ||
.sub(baseFeesAbove) | ||
.toFixed(); | ||
const quoteFeeFactor = marketQuoteFeeFactorDec | ||
.sub(quoteFeesBelow) | ||
.sub(quoteFeesAbove) | ||
.toFixed(); | ||
|
||
return { baseFeeFactor, quoteFeeFactor }; | ||
}; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
9 changes: 6 additions & 3 deletions
9
models/src/libraries/SpreadMath.ts → ...s/src/replicating/libraries/SpreadMath.ts
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
9 changes: 6 additions & 3 deletions
9
models/src/libraries/SwapLib.ts → models/src/replicating/libraries/SwapLib.ts
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
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,150 @@ | ||
import Decimal from "decimal.js"; | ||
import { | ||
baseToLiquidity, | ||
quoteToLiquidity, | ||
} from "../../common/math/liquidityMath"; | ||
import { PRECISION, ROUNDING } from "../../common/config"; | ||
import { limitToSqrtPrice, priceToLimit } from "../../common/math/priceMath"; | ||
import { Trend } from "../types"; | ||
|
||
type PositionInfo = { | ||
lowerSqrtPrice: Decimal.Value; | ||
upperSqrtPrice: Decimal.Value; | ||
liquidity: Decimal.Value; | ||
}; | ||
|
||
type PositionRange = { | ||
bidLower: Decimal.Value; | ||
bidUpper: Decimal.Value; | ||
askLower: Decimal.Value; | ||
askUpper: Decimal.Value; | ||
}; | ||
|
||
export const getVirtualPosition = ( | ||
isBid: boolean, | ||
lowerLimit: Decimal.Value, | ||
upperLimit: Decimal.Value, | ||
amount: Decimal.Value | ||
): PositionInfo => { | ||
Decimal.set({ precision: PRECISION, rounding: ROUNDING }); | ||
|
||
const lowerSqrtPrice = limitToSqrtPrice(lowerLimit, 1); | ||
const upperSqrtPrice = limitToSqrtPrice(upperLimit, 1); | ||
if (isBid) { | ||
const liquidity = quoteToLiquidity( | ||
lowerSqrtPrice, | ||
upperSqrtPrice, | ||
amount | ||
).toFixed(); | ||
return { lowerSqrtPrice, upperSqrtPrice, liquidity }; | ||
} else { | ||
const liquidity = baseToLiquidity( | ||
lowerSqrtPrice, | ||
upperSqrtPrice, | ||
amount | ||
).toFixed(); | ||
return { lowerSqrtPrice, upperSqrtPrice, liquidity }; | ||
} | ||
}; | ||
|
||
export const getVirtualPositionRange = ( | ||
trend: Trend, | ||
range: Decimal.Value, | ||
cachedPrice: Decimal.Value, | ||
oraclePrice: Decimal.Value | ||
): PositionRange => { | ||
Decimal.set({ precision: PRECISION, rounding: ROUNDING }); | ||
|
||
if (new Decimal(oraclePrice).isZero()) { | ||
throw new Error("Oracle price is zero"); | ||
} | ||
|
||
const oracleLimit = priceToLimit(oraclePrice, 1); | ||
const newBidLower = oracleLimit.sub(range); | ||
const newBidUpper = oracleLimit; | ||
const newAskLower = oracleLimit; | ||
const newAskUpper = oracleLimit.add(range); | ||
|
||
let cachedPriceSet = cachedPrice; | ||
if (new Decimal(cachedPrice).isZero()) { | ||
cachedPriceSet = oraclePrice; | ||
} | ||
|
||
const cachedLimit = priceToLimit(cachedPriceSet, 1); | ||
const bidLower = cachedLimit.sub(range); | ||
const bidUpper = cachedLimit; | ||
const askLower = cachedLimit; | ||
const askUpper = cachedLimit.add(range); | ||
|
||
switch (trend) { | ||
case Trend.Up: | ||
if (newBidUpper.gt(bidUpper)) { | ||
return { | ||
bidLower: newBidLower, | ||
bidUpper: newBidUpper, | ||
askLower: 0, | ||
askUpper: 0, | ||
}; | ||
} else if (newAskLower.lte(bidLower)) { | ||
return { | ||
bidLower: 0, | ||
bidUpper: 0, | ||
askLower: bidLower, | ||
askUpper: bidUpper, | ||
}; | ||
} else { | ||
if (newAskLower.gte(bidUpper)) { | ||
return { | ||
bidLower: bidLower, | ||
bidUpper: newBidUpper, | ||
askLower: 0, | ||
askUpper: 0, | ||
}; | ||
} | ||
return { | ||
bidLower: bidLower, | ||
bidUpper: newBidUpper, | ||
askLower: newAskLower, | ||
askUpper: bidUpper, | ||
}; | ||
} | ||
case Trend.Down: | ||
if (newAskLower.lt(askLower)) { | ||
return { | ||
bidLower: 0, | ||
bidUpper: 0, | ||
askLower: newAskLower, | ||
askUpper: newAskUpper, | ||
}; | ||
} else if (newBidUpper.gte(askUpper)) { | ||
return { | ||
bidLower: askLower, | ||
bidUpper: askUpper, | ||
askLower: 0, | ||
askUpper: 0, | ||
}; | ||
} else { | ||
if (newBidUpper.lte(askLower)) { | ||
return { | ||
bidLower: 0, | ||
bidUpper: 0, | ||
askLower: newAskLower, | ||
askUpper: askUpper, | ||
}; | ||
} | ||
return { | ||
bidLower: askLower, | ||
bidUpper: newBidUpper, | ||
askLower: newAskLower, | ||
askUpper: askUpper, | ||
}; | ||
} | ||
default: | ||
return { | ||
bidLower: newBidLower, | ||
bidUpper: newBidUpper, | ||
askLower: newAskLower, | ||
askUpper: newAskUpper, | ||
}; | ||
} | ||
}; |
Oops, something went wrong.