@exponent-labs/exponent-sdk 0.1.7 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/EventDecoderV2.d.ts +31 -0
- package/build/EventDecoderV2.js +76 -0
- package/build/EventDecoderV2.js.map +1 -0
- package/build/addressLookupTableUtil.d.ts +17 -1
- package/build/addressLookupTableUtil.js +35 -1
- package/build/addressLookupTableUtil.js.map +1 -1
- package/build/clmm/events.d.ts +10 -0
- package/build/clmm/events.js +10 -0
- package/build/clmm/events.js.map +1 -0
- package/build/clmm/index.d.ts +1 -0
- package/build/clmm/index.js +18 -0
- package/build/clmm/index.js.map +1 -0
- package/build/events.d.ts +200 -9
- package/build/events.js +73 -24
- package/build/events.js.map +1 -1
- package/build/eventsV2.d.ts +7 -0
- package/build/eventsV2.js +10 -0
- package/build/eventsV2.js.map +1 -0
- package/build/flavors.d.ts +2 -0
- package/build/flavors.js +81 -27
- package/build/flavors.js.map +1 -1
- package/build/index.d.ts +6 -0
- package/build/index.js +14 -4
- package/build/index.js.map +1 -1
- package/build/lpPosition.js +4 -1
- package/build/lpPosition.js.map +1 -1
- package/build/market.d.ts +14 -2
- package/build/market.js +70 -29
- package/build/market.js.map +1 -1
- package/build/marketThree.d.ts +664 -0
- package/build/marketThree.js +1415 -0
- package/build/marketThree.js.map +1 -0
- package/build/marketThree.test.d.ts +1 -0
- package/build/marketThree.test.js +166 -0
- package/build/marketThree.test.js.map +1 -0
- package/build/orderbook/events.d.ts +7 -0
- package/build/orderbook/events.js +10 -0
- package/build/orderbook/events.js.map +1 -0
- package/build/orderbook/index.d.ts +4 -0
- package/build/orderbook/index.js +41 -0
- package/build/orderbook/index.js.map +1 -0
- package/build/orderbook/math.d.ts +26 -0
- package/build/orderbook/math.js +111 -0
- package/build/orderbook/math.js.map +1 -0
- package/build/orderbook/orderbook.d.ts +175 -0
- package/build/orderbook/orderbook.js +756 -0
- package/build/orderbook/orderbook.js.map +1 -0
- package/build/orderbook/types.d.ts +49 -0
- package/build/orderbook/types.js +27 -0
- package/build/orderbook/types.js.map +1 -0
- package/build/orderbook/utils.d.ts +18 -0
- package/build/orderbook/utils.js +74 -0
- package/build/orderbook/utils.js.map +1 -0
- package/build/router.d.ts +92 -0
- package/build/router.js +214 -0
- package/build/router.js.map +1 -0
- package/build/syPosition.js +6 -0
- package/build/syPosition.js.map +1 -1
- package/build/utils/index.d.ts +3 -2
- package/build/utils/index.js +22 -1
- package/build/utils/index.js.map +1 -1
- package/build/vault.d.ts +3 -1
- package/build/vault.js +98 -62
- package/build/vault.js.map +1 -1
- package/build/ytPosition.d.ts +2 -0
- package/build/ytPosition.js +18 -5
- package/build/ytPosition.js.map +1 -1
- package/package.json +28 -23
- package/src/EventDecoderV2.ts +96 -0
- package/src/addressLookupTableUtil.ts +42 -1
- package/src/clmm/events.ts +17 -0
- package/src/clmm/index.ts +1 -0
- package/src/events.ts +280 -27
- package/src/eventsV2.ts +13 -0
- package/src/flavors.ts +97 -27
- package/src/index.ts +6 -0
- package/src/lpPosition.ts +5 -2
- package/src/market.ts +100 -31
- package/src/marketThree.test.ts +208 -0
- package/src/marketThree.ts +2430 -0
- package/src/orderbook/events.ts +13 -0
- package/src/orderbook/index.ts +12 -0
- package/src/orderbook/math.ts +122 -0
- package/src/orderbook/orderbook.ts +1153 -0
- package/src/orderbook/types.ts +45 -0
- package/src/orderbook/utils.ts +74 -0
- package/src/router.ts +360 -0
- package/src/syPosition.ts +4 -0
- package/src/utils/index.ts +27 -2
- package/src/vault.ts +100 -62
- package/src/ytPosition.ts +28 -7
- package/tsconfig.json +4 -1
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { IdlTypes } from "@coral-xyz/anchor"
|
|
2
|
+
|
|
3
|
+
import { ExponentOrderbook, IDL, PROGRAM_ID } from "@exponent-labs/exponent-orderbook-idl"
|
|
4
|
+
|
|
5
|
+
import { EventDecoderV2 } from "../EventDecoderV2"
|
|
6
|
+
import { createAnchorProgram } from "../utils"
|
|
7
|
+
|
|
8
|
+
export const ORDERBOOK_PROGRAM_ID = PROGRAM_ID
|
|
9
|
+
const ORDERBOOK_PROGRAM = createAnchorProgram(IDL as ExponentOrderbook)
|
|
10
|
+
export const ORDERBOOK_EVENT_DECODER = new EventDecoderV2(ORDERBOOK_PROGRAM)
|
|
11
|
+
|
|
12
|
+
export type OrderbookEventNames = ExponentOrderbook["events"][number]["name"]
|
|
13
|
+
export type OrderbookEvent<Name extends OrderbookEventNames> = IdlTypes<ExponentOrderbook>[Name]
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { Orderbook } from "./orderbook"
|
|
2
|
+
export { OfferTradeDirection, QuoteDirection, OfferTypeAnchor, OfferOptionsAnchor, OrderbookLoadOptions } from "./types"
|
|
3
|
+
export {
|
|
4
|
+
getOfferType,
|
|
5
|
+
getOfferTypeFromTradeDirection,
|
|
6
|
+
convertOfferTypeToAnchorType,
|
|
7
|
+
convertOfferTypeFromAnchorType,
|
|
8
|
+
getOfferDirectionFromQuoteDirection,
|
|
9
|
+
isQuoteInputTokenBase,
|
|
10
|
+
isQuoteOutputTokenBase,
|
|
11
|
+
} from "./utils"
|
|
12
|
+
export * as orderbookEvents from "./events"
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { tradeFee } from "@exponent-labs/market-math"
|
|
2
|
+
|
|
3
|
+
import { Offer, OfferType } from "./types"
|
|
4
|
+
|
|
5
|
+
const SECONDS_IN_DAY = 24 * 60 * 60
|
|
6
|
+
export const PRICE_BASE_POINTS = 1e6
|
|
7
|
+
export const MAX_FILLED_OFFERS = 120 // maximum number of filled offers in a single post_offer or market_offer transaction, to prevent CU overflow
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* How much token offer can get (offer buys)
|
|
11
|
+
*/
|
|
12
|
+
export function getOfferAmountToGet(offer: Offer, syExchangeRate: number, secUntilMaturity: number): number {
|
|
13
|
+
if (offer.isVirtual) {
|
|
14
|
+
const offerAmountToGet = getOfferAmountToGetVirtual(offer, syExchangeRate, secUntilMaturity)
|
|
15
|
+
return Math.floor(offerAmountToGet)
|
|
16
|
+
} else {
|
|
17
|
+
const offerAmountToGet = getOfferAmountToGetNoVirtual(offer, syExchangeRate, secUntilMaturity)
|
|
18
|
+
return Math.floor(offerAmountToGet)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* How much YT/SY offer can give (offer sells)
|
|
24
|
+
*/
|
|
25
|
+
export function getOfferSize(offer: Offer, secUntilMaturity: number, syExchangeRate: number): number {
|
|
26
|
+
const { isVirtual, priceApy, amount, type } = offer
|
|
27
|
+
|
|
28
|
+
if (!isVirtual) return amount
|
|
29
|
+
|
|
30
|
+
if (type === OfferType.BuyYT) {
|
|
31
|
+
return getOfferAmountToGetNoVirtual({ ...offer, type: OfferType.SellYT }, syExchangeRate, secUntilMaturity)
|
|
32
|
+
} else {
|
|
33
|
+
const ptToSyRate = getPtToSyRate(priceApy, syExchangeRate, secUntilMaturity)
|
|
34
|
+
const ytToSell = offer.amount / ptToSyRate
|
|
35
|
+
return ytToSell
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function calcPriceInAssetsByAPYPt(priceApy: number, secUntilMaturity: number): number {
|
|
40
|
+
const factor = -secUntilMaturity / (SECONDS_IN_DAY * 365)
|
|
41
|
+
const ptPriceInAsset = Math.exp(priceApy * factor)
|
|
42
|
+
|
|
43
|
+
return ptPriceInAsset
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function getPtToSyRate(priceApy: number, syExchangeRate: number, secUntilMaturity: number): number {
|
|
47
|
+
const ptPriceAsset = calcPriceInAssetsByAPYPt(priceApy, secUntilMaturity)
|
|
48
|
+
return ptPriceAsset / syExchangeRate
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function getYtToSyRate(priceApy: number, syExchangeRate: number, secUntilMaturity: number): number {
|
|
52
|
+
const ptPriceAsset = calcPriceInAssetsByAPYPt(priceApy, secUntilMaturity)
|
|
53
|
+
|
|
54
|
+
const rate = (1 - ptPriceAsset) / syExchangeRate
|
|
55
|
+
return rate
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function getOfferAmountToGetNoVirtual(offer: Offer, syExchangeRate: number, secUntilMaturity: number): number {
|
|
59
|
+
const ytToSyRate = getYtToSyRate(offer.priceApy, syExchangeRate, secUntilMaturity)
|
|
60
|
+
const amountToGet = offer.type === OfferType.SellYT ? offer.amount * ytToSyRate : offer.amount / ytToSyRate
|
|
61
|
+
return Math.floor(amountToGet)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function getOfferAmountToGetVirtual(offer: Offer, syExchangeRate: number, secUntilMaturity: number): number {
|
|
65
|
+
const ptToSyRate = getPtToSyRate(offer.priceApy, syExchangeRate, secUntilMaturity)
|
|
66
|
+
if (offer.type === OfferType.SellYT) {
|
|
67
|
+
return offer.amount / ptToSyRate / syExchangeRate - offer.amount
|
|
68
|
+
} else {
|
|
69
|
+
return offer.amount
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Rounds priceApy(float) to priceDecimals precision. Returns integer
|
|
75
|
+
*/
|
|
76
|
+
export function roundPriceApy(priceApy: number, priceDecimals: number) {
|
|
77
|
+
const factor = 10 ** priceDecimals
|
|
78
|
+
const roundedPrice = Math.round(priceApy * factor) / factor
|
|
79
|
+
|
|
80
|
+
return Math.round(roundedPrice * PRICE_BASE_POINTS)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function calcRemainingAmountOnCounterOfferFilling(
|
|
84
|
+
offerToGet: number,
|
|
85
|
+
takenAmount: number,
|
|
86
|
+
isQuoteVirtualOffer: boolean,
|
|
87
|
+
quoteOfferType: OfferType,
|
|
88
|
+
syExchangeRate: number,
|
|
89
|
+
) {
|
|
90
|
+
if (!isQuoteVirtualOffer) return offerToGet
|
|
91
|
+
|
|
92
|
+
if (quoteOfferType === OfferType.BuyYT) {
|
|
93
|
+
return takenAmount
|
|
94
|
+
} else {
|
|
95
|
+
return offerToGet / syExchangeRate - takenAmount
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function calcOutAmountOnCounterOfferFilling(
|
|
100
|
+
offerToGet: number,
|
|
101
|
+
takenAmount: number,
|
|
102
|
+
isQuoteVirtualOffer: boolean,
|
|
103
|
+
quoteOfferType: OfferType,
|
|
104
|
+
syExchangeRate: number,
|
|
105
|
+
makerFeeRate: number,
|
|
106
|
+
) {
|
|
107
|
+
let feeBase = !isQuoteVirtualOffer
|
|
108
|
+
? takenAmount
|
|
109
|
+
: quoteOfferType === OfferType.BuyYT
|
|
110
|
+
? takenAmount / syExchangeRate - offerToGet
|
|
111
|
+
: offerToGet
|
|
112
|
+
let fees = tradeFee(feeBase, makerFeeRate)
|
|
113
|
+
return {
|
|
114
|
+
tradeFee: fees,
|
|
115
|
+
outAmount: feeBase - fees,
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function getTradeFeesForTakerOffer(offer: Offer, syExchangeRate: number, secUntilMaturity: number, takerFeeRate: number) {
|
|
120
|
+
let amountToGet = getOfferAmountToGet(offer,syExchangeRate, secUntilMaturity)
|
|
121
|
+
return tradeFee(amountToGet, takerFeeRate)
|
|
122
|
+
}
|