@drift-labs/sdk 0.2.0-master.27 → 0.2.0-master.28
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/lib/accounts/types.d.ts +1 -0
- package/lib/clearingHouse.d.ts +1 -0
- package/lib/clearingHouse.js +5 -0
- package/lib/clearingHouseUser.js +2 -2
- package/lib/idl/clearing_house.json +1 -1
- package/lib/math/market.d.ts +2 -1
- package/lib/math/market.js +16 -11
- package/lib/math/position.d.ts +3 -3
- package/lib/math/position.js +23 -16
- package/lib/math/repeg.js +8 -0
- package/lib/types.d.ts +1 -1
- package/package.json +1 -1
- package/src/clearingHouse.ts +7 -0
- package/src/clearingHouseUser.ts +2 -2
- package/src/idl/clearing_house.json +1 -1
- package/src/math/market.ts +21 -12
- package/src/math/position.ts +36 -22
- package/src/math/repeg.ts +9 -0
- package/src/types.ts +1 -1
- package/tests/dlob/helpers.ts +1 -1
- package/src/addresses/marketAddresses.js +0 -26
- package/src/assert/assert.js +0 -9
- package/src/constants/banks.js +0 -42
- package/src/constants/markets.js +0 -42
- package/src/events/eventList.js +0 -77
- package/src/events/txEventCache.js +0 -71
- package/src/examples/makeTradeExample.js +0 -157
- package/src/factory/bigNum.js +0 -390
- package/src/factory/oracleClient.js +0 -20
- package/src/math/auction.js +0 -42
- package/src/math/conversion.js +0 -11
- package/src/math/funding.js +0 -248
- package/src/math/repeg.js +0 -128
- package/src/math/trade.js +0 -253
- package/src/math/utils.js +0 -26
- package/src/math/utils.js.map +0 -1
- package/src/oracles/oracleClientCache.js +0 -19
- package/src/oracles/pythClient.js +0 -46
- package/src/oracles/quoteAssetOracleClient.js +0 -32
- package/src/oracles/switchboardClient.js +0 -69
- package/src/oracles/types.js +0 -2
- package/src/token/index.js +0 -38
- package/src/tx/types.js +0 -2
- package/src/tx/utils.js +0 -17
- package/src/userName.js +0 -20
- package/src/util/computeUnits.js +0 -27
- package/src/util/getTokenAddress.js +0 -9
- package/src/util/promiseTimeout.js +0 -14
- package/src/util/tps.js +0 -27
- package/src/wallet.js +0 -35
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getOracleClient = void 0;
|
|
4
|
-
const types_1 = require("../types");
|
|
5
|
-
const pythClient_1 = require("../oracles/pythClient");
|
|
6
|
-
const switchboardClient_1 = require("../oracles/switchboardClient");
|
|
7
|
-
const quoteAssetOracleClient_1 = require("../oracles/quoteAssetOracleClient");
|
|
8
|
-
function getOracleClient(oracleSource, connection) {
|
|
9
|
-
if (types_1.isVariant(oracleSource, 'pyth')) {
|
|
10
|
-
return new pythClient_1.PythClient(connection);
|
|
11
|
-
}
|
|
12
|
-
if (types_1.isVariant(oracleSource, 'switchboard')) {
|
|
13
|
-
return new switchboardClient_1.SwitchboardClient(connection);
|
|
14
|
-
}
|
|
15
|
-
if (types_1.isVariant(oracleSource, 'quoteAsset')) {
|
|
16
|
-
return new quoteAssetOracleClient_1.QuoteAssetOracleClient();
|
|
17
|
-
}
|
|
18
|
-
throw new Error(`Unknown oracle source ${oracleSource}`);
|
|
19
|
-
}
|
|
20
|
-
exports.getOracleClient = getOracleClient;
|
package/src/math/auction.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getAuctionPrice = exports.isAuctionComplete = void 0;
|
|
4
|
-
const types_1 = require("../types");
|
|
5
|
-
const _1 = require("../.");
|
|
6
|
-
function isAuctionComplete(order, slot) {
|
|
7
|
-
if (order.auctionDuration === 0) {
|
|
8
|
-
return true;
|
|
9
|
-
}
|
|
10
|
-
return new _1.BN(slot).sub(order.slot).gt(new _1.BN(order.auctionDuration));
|
|
11
|
-
}
|
|
12
|
-
exports.isAuctionComplete = isAuctionComplete;
|
|
13
|
-
function getAuctionPrice(order, slot) {
|
|
14
|
-
const slotsElapsed = new _1.BN(slot).sub(order.slot);
|
|
15
|
-
const deltaDenominator = new _1.BN(order.auctionDuration);
|
|
16
|
-
const deltaNumerator = _1.BN.min(slotsElapsed, deltaDenominator);
|
|
17
|
-
if (deltaDenominator.eq(_1.ZERO)) {
|
|
18
|
-
return order.auctionEndPrice;
|
|
19
|
-
}
|
|
20
|
-
let priceDelta;
|
|
21
|
-
if (types_1.isVariant(order.direction, 'long')) {
|
|
22
|
-
priceDelta = order.auctionEndPrice
|
|
23
|
-
.sub(order.auctionStartPrice)
|
|
24
|
-
.mul(deltaNumerator)
|
|
25
|
-
.div(deltaDenominator);
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
priceDelta = order.auctionStartPrice
|
|
29
|
-
.sub(order.auctionEndPrice)
|
|
30
|
-
.mul(deltaNumerator)
|
|
31
|
-
.div(deltaDenominator);
|
|
32
|
-
}
|
|
33
|
-
let price;
|
|
34
|
-
if (types_1.isVariant(order.direction, 'long')) {
|
|
35
|
-
price = order.auctionStartPrice.add(priceDelta);
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
price = order.auctionStartPrice.sub(priceDelta);
|
|
39
|
-
}
|
|
40
|
-
return price;
|
|
41
|
-
}
|
|
42
|
-
exports.getAuctionPrice = getAuctionPrice;
|
package/src/math/conversion.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.convertToNumber = void 0;
|
|
4
|
-
const numericConstants_1 = require("../constants/numericConstants");
|
|
5
|
-
const convertToNumber = (bigNumber, precision = numericConstants_1.MARK_PRICE_PRECISION) => {
|
|
6
|
-
if (!bigNumber)
|
|
7
|
-
return 0;
|
|
8
|
-
return (bigNumber.div(precision).toNumber() +
|
|
9
|
-
bigNumber.mod(precision).toNumber() / precision.toNumber());
|
|
10
|
-
};
|
|
11
|
-
exports.convertToNumber = convertToNumber;
|
package/src/math/funding.js
DELETED
|
@@ -1,248 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.calculateFundingPool = exports.calculateLongShortFundingRateAndLiveTwaps = exports.calculateLongShortFundingRate = exports.calculateEstimatedFundingRate = exports.calculateAllEstimatedFundingRate = void 0;
|
|
13
|
-
const anchor_1 = require("@project-serum/anchor");
|
|
14
|
-
const numericConstants_1 = require("../constants/numericConstants");
|
|
15
|
-
const market_1 = require("./market");
|
|
16
|
-
/**
|
|
17
|
-
*
|
|
18
|
-
* @param market
|
|
19
|
-
* @param oraclePriceData
|
|
20
|
-
* @param periodAdjustment
|
|
21
|
-
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
22
|
-
*/
|
|
23
|
-
function calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1)) {
|
|
24
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
-
// periodAdjustment
|
|
26
|
-
// 1: hourly
|
|
27
|
-
// 24: daily
|
|
28
|
-
// 24 * 365.25: annualized
|
|
29
|
-
const secondsInHour = new anchor_1.BN(3600);
|
|
30
|
-
const hoursInDay = new anchor_1.BN(24);
|
|
31
|
-
const ONE = new anchor_1.BN(1);
|
|
32
|
-
if (!market.initialized) {
|
|
33
|
-
return [numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO];
|
|
34
|
-
}
|
|
35
|
-
const payFreq = new anchor_1.BN(market.amm.fundingPeriod);
|
|
36
|
-
// todo: sufficiently differs from blockchain timestamp?
|
|
37
|
-
const now = new anchor_1.BN((Date.now() / 1000).toFixed(0));
|
|
38
|
-
const timeSinceLastUpdate = now.sub(market.amm.lastFundingRateTs);
|
|
39
|
-
// calculate real-time mark twap
|
|
40
|
-
const lastMarkTwapWithMantissa = market.amm.lastMarkPriceTwap;
|
|
41
|
-
const lastMarkPriceTwapTs = market.amm.lastMarkPriceTwapTs;
|
|
42
|
-
const timeSinceLastMarkChange = now.sub(lastMarkPriceTwapTs);
|
|
43
|
-
const markTwapTimeSinceLastUpdate = anchor_1.BN.max(secondsInHour, anchor_1.BN.max(numericConstants_1.ZERO, secondsInHour.sub(timeSinceLastMarkChange)));
|
|
44
|
-
const baseAssetPriceWithMantissa = market_1.calculateMarkPrice(market, oraclePriceData);
|
|
45
|
-
const markTwapWithMantissa = markTwapTimeSinceLastUpdate
|
|
46
|
-
.mul(lastMarkTwapWithMantissa)
|
|
47
|
-
.add(timeSinceLastMarkChange.mul(baseAssetPriceWithMantissa))
|
|
48
|
-
.div(timeSinceLastMarkChange.add(markTwapTimeSinceLastUpdate));
|
|
49
|
-
// calculate real-time (predicted) oracle twap
|
|
50
|
-
// note: oracle twap depends on `when the chord is struck` (market is trade)
|
|
51
|
-
const lastOracleTwapWithMantissa = market.amm.lastOraclePriceTwap;
|
|
52
|
-
const lastOraclePriceTwapTs = market.amm.lastOraclePriceTwapTs;
|
|
53
|
-
const oracleInvalidDuration = anchor_1.BN.max(numericConstants_1.ZERO, lastMarkPriceTwapTs.sub(lastOraclePriceTwapTs));
|
|
54
|
-
const timeSinceLastOracleTwapUpdate = now.sub(lastOraclePriceTwapTs);
|
|
55
|
-
const oracleTwapTimeSinceLastUpdate = anchor_1.BN.max(ONE, anchor_1.BN.min(secondsInHour, anchor_1.BN.max(ONE, secondsInHour.sub(timeSinceLastOracleTwapUpdate))));
|
|
56
|
-
let oracleTwapWithMantissa = lastOracleTwapWithMantissa;
|
|
57
|
-
// if passing live oracle data, improve predicted calc estimate
|
|
58
|
-
if (oraclePriceData) {
|
|
59
|
-
const oraclePrice = oraclePriceData.price;
|
|
60
|
-
const oracleLiveVsTwap = oraclePrice
|
|
61
|
-
.sub(lastOracleTwapWithMantissa)
|
|
62
|
-
.abs()
|
|
63
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
64
|
-
.mul(new anchor_1.BN(100))
|
|
65
|
-
.div(lastOracleTwapWithMantissa);
|
|
66
|
-
// verify pyth live input is within 10% of last twap for live update
|
|
67
|
-
if (oracleLiveVsTwap.lte(numericConstants_1.MARK_PRICE_PRECISION.mul(new anchor_1.BN(10)))) {
|
|
68
|
-
oracleTwapWithMantissa = oracleTwapTimeSinceLastUpdate
|
|
69
|
-
.mul(lastOracleTwapWithMantissa)
|
|
70
|
-
.add(timeSinceLastMarkChange.mul(oraclePrice))
|
|
71
|
-
.div(timeSinceLastMarkChange.add(oracleTwapTimeSinceLastUpdate));
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
const shrunkLastOracleTwapwithMantissa = oracleTwapTimeSinceLastUpdate
|
|
75
|
-
.mul(lastOracleTwapWithMantissa)
|
|
76
|
-
.add(oracleInvalidDuration.mul(lastMarkTwapWithMantissa))
|
|
77
|
-
.div(oracleTwapTimeSinceLastUpdate.add(oracleInvalidDuration));
|
|
78
|
-
const twapSpread = lastMarkTwapWithMantissa.sub(shrunkLastOracleTwapwithMantissa);
|
|
79
|
-
const twapSpreadPct = twapSpread
|
|
80
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
81
|
-
.mul(new anchor_1.BN(100))
|
|
82
|
-
.div(shrunkLastOracleTwapwithMantissa);
|
|
83
|
-
const lowerboundEst = twapSpreadPct
|
|
84
|
-
.mul(payFreq)
|
|
85
|
-
.mul(anchor_1.BN.min(secondsInHour, timeSinceLastUpdate))
|
|
86
|
-
.mul(periodAdjustment)
|
|
87
|
-
.div(secondsInHour)
|
|
88
|
-
.div(secondsInHour)
|
|
89
|
-
.div(hoursInDay);
|
|
90
|
-
const interpEst = twapSpreadPct.mul(periodAdjustment).div(hoursInDay);
|
|
91
|
-
const interpRateQuote = twapSpreadPct
|
|
92
|
-
.mul(periodAdjustment)
|
|
93
|
-
.div(hoursInDay)
|
|
94
|
-
.div(numericConstants_1.MARK_PRICE_PRECISION.div(numericConstants_1.QUOTE_PRECISION));
|
|
95
|
-
let feePoolSize = calculateFundingPool(market);
|
|
96
|
-
if (interpRateQuote.lt(new anchor_1.BN(0))) {
|
|
97
|
-
feePoolSize = feePoolSize.mul(new anchor_1.BN(-1));
|
|
98
|
-
}
|
|
99
|
-
let cappedAltEst;
|
|
100
|
-
let largerSide;
|
|
101
|
-
let smallerSide;
|
|
102
|
-
if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort.abs())) {
|
|
103
|
-
largerSide = market.baseAssetAmountLong.abs();
|
|
104
|
-
smallerSide = market.baseAssetAmountShort.abs();
|
|
105
|
-
if (twapSpread.gt(new anchor_1.BN(0))) {
|
|
106
|
-
return [
|
|
107
|
-
markTwapWithMantissa,
|
|
108
|
-
oracleTwapWithMantissa,
|
|
109
|
-
lowerboundEst,
|
|
110
|
-
interpEst,
|
|
111
|
-
interpEst,
|
|
112
|
-
];
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort.abs())) {
|
|
116
|
-
largerSide = market.baseAssetAmountShort.abs();
|
|
117
|
-
smallerSide = market.baseAssetAmountLong.abs();
|
|
118
|
-
if (twapSpread.lt(new anchor_1.BN(0))) {
|
|
119
|
-
return [
|
|
120
|
-
markTwapWithMantissa,
|
|
121
|
-
oracleTwapWithMantissa,
|
|
122
|
-
lowerboundEst,
|
|
123
|
-
interpEst,
|
|
124
|
-
interpEst,
|
|
125
|
-
];
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
return [
|
|
130
|
-
markTwapWithMantissa,
|
|
131
|
-
oracleTwapWithMantissa,
|
|
132
|
-
lowerboundEst,
|
|
133
|
-
interpEst,
|
|
134
|
-
interpEst,
|
|
135
|
-
];
|
|
136
|
-
}
|
|
137
|
-
if (largerSide.gt(numericConstants_1.ZERO)) {
|
|
138
|
-
// funding smaller flow
|
|
139
|
-
cappedAltEst = smallerSide.mul(twapSpread).div(hoursInDay);
|
|
140
|
-
const feePoolTopOff = feePoolSize
|
|
141
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION.div(numericConstants_1.QUOTE_PRECISION))
|
|
142
|
-
.mul(numericConstants_1.AMM_RESERVE_PRECISION);
|
|
143
|
-
cappedAltEst = cappedAltEst.add(feePoolTopOff).div(largerSide);
|
|
144
|
-
cappedAltEst = cappedAltEst
|
|
145
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
146
|
-
.mul(new anchor_1.BN(100))
|
|
147
|
-
.div(oracleTwapWithMantissa)
|
|
148
|
-
.mul(periodAdjustment);
|
|
149
|
-
if (cappedAltEst.abs().gte(interpEst.abs())) {
|
|
150
|
-
cappedAltEst = interpEst;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
else {
|
|
154
|
-
cappedAltEst = interpEst;
|
|
155
|
-
}
|
|
156
|
-
return [
|
|
157
|
-
markTwapWithMantissa,
|
|
158
|
-
oracleTwapWithMantissa,
|
|
159
|
-
lowerboundEst,
|
|
160
|
-
cappedAltEst,
|
|
161
|
-
interpEst,
|
|
162
|
-
];
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
exports.calculateAllEstimatedFundingRate = calculateAllEstimatedFundingRate;
|
|
166
|
-
/**
|
|
167
|
-
*
|
|
168
|
-
* @param market
|
|
169
|
-
* @param oraclePriceData
|
|
170
|
-
* @param periodAdjustment
|
|
171
|
-
* @param estimationMethod
|
|
172
|
-
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
173
|
-
*/
|
|
174
|
-
function calculateEstimatedFundingRate(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1), estimationMethod) {
|
|
175
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
176
|
-
const [_1, _2, lowerboundEst, cappedAltEst, interpEst] = yield calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment);
|
|
177
|
-
if (estimationMethod == 'lowerbound') {
|
|
178
|
-
//assuming remaining funding period has no gap
|
|
179
|
-
return lowerboundEst;
|
|
180
|
-
}
|
|
181
|
-
else if (estimationMethod == 'capped') {
|
|
182
|
-
return cappedAltEst;
|
|
183
|
-
}
|
|
184
|
-
else {
|
|
185
|
-
return interpEst;
|
|
186
|
-
}
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
exports.calculateEstimatedFundingRate = calculateEstimatedFundingRate;
|
|
190
|
-
/**
|
|
191
|
-
*
|
|
192
|
-
* @param market
|
|
193
|
-
* @param oraclePriceData
|
|
194
|
-
* @param periodAdjustment
|
|
195
|
-
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
196
|
-
*/
|
|
197
|
-
function calculateLongShortFundingRate(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1)) {
|
|
198
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
199
|
-
const [_1, _2, _, cappedAltEst, interpEst] = yield calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment);
|
|
200
|
-
if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort)) {
|
|
201
|
-
return [cappedAltEst, interpEst];
|
|
202
|
-
}
|
|
203
|
-
else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort)) {
|
|
204
|
-
return [interpEst, cappedAltEst];
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
return [interpEst, interpEst];
|
|
208
|
-
}
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
exports.calculateLongShortFundingRate = calculateLongShortFundingRate;
|
|
212
|
-
/**
|
|
213
|
-
*
|
|
214
|
-
* @param market
|
|
215
|
-
* @param oraclePriceData
|
|
216
|
-
* @param periodAdjustment
|
|
217
|
-
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
218
|
-
*/
|
|
219
|
-
function calculateLongShortFundingRateAndLiveTwaps(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1)) {
|
|
220
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
221
|
-
const [markTwapLive, oracleTwapLive, _2, cappedAltEst, interpEst] = yield calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment);
|
|
222
|
-
if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort.abs())) {
|
|
223
|
-
return [markTwapLive, oracleTwapLive, cappedAltEst, interpEst];
|
|
224
|
-
}
|
|
225
|
-
else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort.abs())) {
|
|
226
|
-
return [markTwapLive, oracleTwapLive, interpEst, cappedAltEst];
|
|
227
|
-
}
|
|
228
|
-
else {
|
|
229
|
-
return [markTwapLive, oracleTwapLive, interpEst, interpEst];
|
|
230
|
-
}
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
exports.calculateLongShortFundingRateAndLiveTwaps = calculateLongShortFundingRateAndLiveTwaps;
|
|
234
|
-
/**
|
|
235
|
-
*
|
|
236
|
-
* @param market
|
|
237
|
-
* @returns Estimated fee pool size
|
|
238
|
-
*/
|
|
239
|
-
function calculateFundingPool(market) {
|
|
240
|
-
// todo
|
|
241
|
-
const totalFeeLB = market.amm.totalExchangeFee.div(new anchor_1.BN(2));
|
|
242
|
-
const feePool = anchor_1.BN.max(numericConstants_1.ZERO, market.amm.totalFeeMinusDistributions
|
|
243
|
-
.sub(totalFeeLB)
|
|
244
|
-
.mul(new anchor_1.BN(1))
|
|
245
|
-
.div(new anchor_1.BN(3)));
|
|
246
|
-
return feePool;
|
|
247
|
-
}
|
|
248
|
-
exports.calculateFundingPool = calculateFundingPool;
|
package/src/math/repeg.js
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateBudgetedPeg = exports.calculateBudgetedK = exports.calculateBudgetedKBN = exports.calculateRepegCost = exports.calculateAdjustKCost = void 0;
|
|
4
|
-
const anchor_1 = require("@project-serum/anchor");
|
|
5
|
-
const assert_1 = require("../assert/assert");
|
|
6
|
-
const numericConstants_1 = require("../constants/numericConstants");
|
|
7
|
-
/**
|
|
8
|
-
* Helper function calculating adjust k cost
|
|
9
|
-
* @param amm
|
|
10
|
-
* @param numerator
|
|
11
|
-
* @param denomenator
|
|
12
|
-
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
13
|
-
*/
|
|
14
|
-
function calculateAdjustKCost(amm, numerator, denomenator) {
|
|
15
|
-
// const k = market.amm.sqrtK.mul(market.amm.sqrtK);
|
|
16
|
-
const x = amm.baseAssetReserve;
|
|
17
|
-
const y = amm.quoteAssetReserve;
|
|
18
|
-
const d = amm.netBaseAssetAmount;
|
|
19
|
-
const Q = amm.pegMultiplier;
|
|
20
|
-
const quoteScale = y.mul(d).mul(Q); //.div(AMM_RESERVE_PRECISION);
|
|
21
|
-
const p = numerator.mul(numericConstants_1.MARK_PRICE_PRECISION).div(denomenator);
|
|
22
|
-
const cost = quoteScale
|
|
23
|
-
.div(x.add(d))
|
|
24
|
-
.sub(quoteScale
|
|
25
|
-
.mul(p)
|
|
26
|
-
.div(numericConstants_1.MARK_PRICE_PRECISION)
|
|
27
|
-
.div(x.mul(p).div(numericConstants_1.MARK_PRICE_PRECISION).add(d)))
|
|
28
|
-
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
|
|
29
|
-
.div(numericConstants_1.PEG_PRECISION);
|
|
30
|
-
return cost.mul(new anchor_1.BN(-1));
|
|
31
|
-
}
|
|
32
|
-
exports.calculateAdjustKCost = calculateAdjustKCost;
|
|
33
|
-
/**
|
|
34
|
-
* Helper function calculating adjust pegMultiplier (repeg) cost
|
|
35
|
-
*
|
|
36
|
-
* @param amm
|
|
37
|
-
* @param newPeg
|
|
38
|
-
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
39
|
-
*/
|
|
40
|
-
function calculateRepegCost(amm, newPeg) {
|
|
41
|
-
const dqar = amm.quoteAssetReserve.sub(amm.terminalQuoteAssetReserve);
|
|
42
|
-
const cost = dqar
|
|
43
|
-
.mul(newPeg.sub(amm.pegMultiplier))
|
|
44
|
-
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
|
|
45
|
-
.div(numericConstants_1.PEG_PRECISION);
|
|
46
|
-
return cost;
|
|
47
|
-
}
|
|
48
|
-
exports.calculateRepegCost = calculateRepegCost;
|
|
49
|
-
function calculateBudgetedKBN(x, y, budget, Q, d) {
|
|
50
|
-
assert_1.assert(Q.gt(new anchor_1.BN(0)));
|
|
51
|
-
const C = budget.mul(new anchor_1.BN(-1));
|
|
52
|
-
let dSign = new anchor_1.BN(1);
|
|
53
|
-
if (d.lt(new anchor_1.BN(0))) {
|
|
54
|
-
dSign = new anchor_1.BN(-1);
|
|
55
|
-
}
|
|
56
|
-
const pegged_y_d_d = y
|
|
57
|
-
.mul(d)
|
|
58
|
-
.mul(d)
|
|
59
|
-
.mul(Q)
|
|
60
|
-
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
61
|
-
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
62
|
-
.div(numericConstants_1.PEG_PRECISION);
|
|
63
|
-
const numer1 = pegged_y_d_d;
|
|
64
|
-
const numer2 = C.mul(d)
|
|
65
|
-
.div(numericConstants_1.QUOTE_PRECISION)
|
|
66
|
-
.mul(x.add(d))
|
|
67
|
-
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
68
|
-
.mul(dSign);
|
|
69
|
-
const denom1 = C.mul(x)
|
|
70
|
-
.mul(x.add(d))
|
|
71
|
-
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
72
|
-
.div(numericConstants_1.QUOTE_PRECISION);
|
|
73
|
-
const denom2 = pegged_y_d_d;
|
|
74
|
-
const numerator = numer1.sub(numer2).div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO);
|
|
75
|
-
const denominator = denom1.add(denom2).div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO);
|
|
76
|
-
return [numerator, denominator];
|
|
77
|
-
}
|
|
78
|
-
exports.calculateBudgetedKBN = calculateBudgetedKBN;
|
|
79
|
-
function calculateBudgetedK(amm, cost) {
|
|
80
|
-
// wolframalpha.com
|
|
81
|
-
// (1/(x+d) - p/(x*p+d))*y*d*Q = C solve for p
|
|
82
|
-
// p = (d(y*d*Q - C(x+d))) / (C*x(x+d) + y*d*d*Q)
|
|
83
|
-
// numer
|
|
84
|
-
// = y*d*d*Q - Cxd - Cdd
|
|
85
|
-
// = y/x*Q*d*d - Cd - Cd/x
|
|
86
|
-
// = mark - C/d - C/(x)
|
|
87
|
-
// = mark/C - 1/d - 1/x
|
|
88
|
-
// denom
|
|
89
|
-
// = C*x*x + C*x*d + y*d*d*Q
|
|
90
|
-
// = x/d**2 + 1 / d + mark/C
|
|
91
|
-
// todo: assumes k = x * y
|
|
92
|
-
// otherwise use: (y(1-p) + (kp^2/(x*p+d)) - k/(x+d)) * Q = C solve for p
|
|
93
|
-
const x = amm.baseAssetReserve;
|
|
94
|
-
const y = amm.quoteAssetReserve;
|
|
95
|
-
const d = amm.netBaseAssetAmount;
|
|
96
|
-
const Q = amm.pegMultiplier;
|
|
97
|
-
const [numerator, denominator] = calculateBudgetedKBN(x, y, cost, Q, d);
|
|
98
|
-
return [numerator, denominator];
|
|
99
|
-
}
|
|
100
|
-
exports.calculateBudgetedK = calculateBudgetedK;
|
|
101
|
-
function calculateBudgetedPeg(amm, cost, targetPrice) {
|
|
102
|
-
// wolframalpha.com
|
|
103
|
-
// (1/(x+d) - p/(x*p+d))*y*d*Q = C solve for p
|
|
104
|
-
// p = (d(y*d*Q - C(x+d))) / (C*x(x+d) + y*y*d*Q)
|
|
105
|
-
// todo: assumes k = x * y
|
|
106
|
-
// otherwise use: (y(1-p) + (kp^2/(x*p+d)) - k/(x+d)) * Q = C solve for p
|
|
107
|
-
const targetPeg = targetPrice
|
|
108
|
-
.mul(amm.baseAssetReserve)
|
|
109
|
-
.div(amm.quoteAssetReserve)
|
|
110
|
-
.div(numericConstants_1.PRICE_DIV_PEG);
|
|
111
|
-
const k = amm.sqrtK.mul(amm.sqrtK);
|
|
112
|
-
const x = amm.baseAssetReserve;
|
|
113
|
-
const y = amm.quoteAssetReserve;
|
|
114
|
-
const d = amm.netBaseAssetAmount;
|
|
115
|
-
const Q = amm.pegMultiplier;
|
|
116
|
-
const C = cost.mul(new anchor_1.BN(-1));
|
|
117
|
-
const deltaQuoteAssetReserves = y.sub(k.div(x.add(d)));
|
|
118
|
-
const pegChangeDirection = targetPeg.sub(Q);
|
|
119
|
-
const useTargetPeg = (deltaQuoteAssetReserves.lt(numericConstants_1.ZERO) && pegChangeDirection.gt(numericConstants_1.ZERO)) ||
|
|
120
|
-
(deltaQuoteAssetReserves.gt(numericConstants_1.ZERO) && pegChangeDirection.lt(numericConstants_1.ZERO));
|
|
121
|
-
if (deltaQuoteAssetReserves.eq(numericConstants_1.ZERO) || useTargetPeg) {
|
|
122
|
-
return targetPeg;
|
|
123
|
-
}
|
|
124
|
-
const deltaPegMultiplier = C.mul(numericConstants_1.MARK_PRICE_PRECISION).div(deltaQuoteAssetReserves.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO));
|
|
125
|
-
const newPeg = Q.sub(deltaPegMultiplier.mul(numericConstants_1.PEG_PRECISION).div(numericConstants_1.MARK_PRICE_PRECISION));
|
|
126
|
-
return newPeg;
|
|
127
|
-
}
|
|
128
|
-
exports.calculateBudgetedPeg = calculateBudgetedPeg;
|