@drift-labs/sdk 0.1.29-master.0 → 0.1.29-master.1
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/bulkAccountLoader.js +5 -0
- package/lib/idl/clearing_house.json +12 -2
- package/lib/math/amm.d.ts +0 -18
- package/lib/math/amm.js +1 -88
- package/lib/math/repeg.d.ts +32 -0
- package/lib/math/repeg.js +178 -0
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.ts +4 -0
- package/src/idl/clearing_house.json +12 -2
- package/src/math/amm.ts +2 -127
- package/src/math/repeg.ts +253 -0
- package/src/addresses.js +0 -59
- package/src/addresses.js.map +0 -1
- package/src/admin.js +0 -443
- package/src/admin.js.map +0 -1
- package/src/clearingHouseUser.js +0 -581
- package/src/clearingHouseUser.js.map +0 -1
- package/src/config.js +0 -37
- package/src/config.js.map +0 -1
- package/src/index.js +0 -56
- package/src/index.js.map +0 -1
- package/src/mockUSDCFaucet.js +0 -143
- package/src/mockUSDCFaucet.js.map +0 -1
- package/src/orderParams.js +0 -109
- package/src/orderParams.js.map +0 -1
- package/src/orders.js +0 -172
- package/src/orders.js.map +0 -1
- package/src/types.js +0 -61
- package/src/types.js.map +0 -1
- package/src/wallet.js +0 -23
- package/src/wallet.js.map +0 -1
|
@@ -163,6 +163,11 @@ class BulkAccountLoader {
|
|
|
163
163
|
this.handleAccountCallbacks(accountToLoad, newBuffer);
|
|
164
164
|
this.lastUpdate = Date.now();
|
|
165
165
|
}
|
|
166
|
+
else if (this.loggingEnabled) {
|
|
167
|
+
console.log('unable to update account for newest slot');
|
|
168
|
+
console.log('oldBuffer', oldBuffer);
|
|
169
|
+
console.log('newBuffer', newBuffer);
|
|
170
|
+
}
|
|
166
171
|
}
|
|
167
172
|
});
|
|
168
173
|
}
|
|
@@ -1444,6 +1444,11 @@
|
|
|
1444
1444
|
"isMut": true,
|
|
1445
1445
|
"isSigner": false
|
|
1446
1446
|
},
|
|
1447
|
+
{
|
|
1448
|
+
"name": "userOrders",
|
|
1449
|
+
"isMut": true,
|
|
1450
|
+
"isSigner": false
|
|
1451
|
+
},
|
|
1447
1452
|
{
|
|
1448
1453
|
"name": "authority",
|
|
1449
1454
|
"isMut": false,
|
|
@@ -3236,8 +3241,8 @@
|
|
|
3236
3241
|
"type": "u128"
|
|
3237
3242
|
},
|
|
3238
3243
|
{
|
|
3239
|
-
"name": "
|
|
3240
|
-
"type": "
|
|
3244
|
+
"name": "netRevenueSinceLastFunding",
|
|
3245
|
+
"type": "i64"
|
|
3241
3246
|
},
|
|
3242
3247
|
{
|
|
3243
3248
|
"name": "padding2",
|
|
@@ -4244,6 +4249,11 @@
|
|
|
4244
4249
|
"code": 6060,
|
|
4245
4250
|
"name": "CantExpireOrders",
|
|
4246
4251
|
"msg": "CantExpireOrders"
|
|
4252
|
+
},
|
|
4253
|
+
{
|
|
4254
|
+
"code": 6061,
|
|
4255
|
+
"name": "InvalidRepegPriceImpact",
|
|
4256
|
+
"msg": "AMM repeg mark price impact vs oracle too large"
|
|
4247
4257
|
}
|
|
4248
4258
|
]
|
|
4249
4259
|
}
|
package/lib/math/amm.d.ts
CHANGED
|
@@ -38,24 +38,6 @@ export declare function calculateSwapOutput(inputAssetReserve: BN, swapAmount: B
|
|
|
38
38
|
* @param positionDirection
|
|
39
39
|
*/
|
|
40
40
|
export declare function getSwapDirection(inputAssetType: AssetType, positionDirection: PositionDirection): SwapDirection;
|
|
41
|
-
/**
|
|
42
|
-
* Helper function calculating adjust k cost
|
|
43
|
-
* @param market
|
|
44
|
-
* @param marketIndex
|
|
45
|
-
* @param numerator
|
|
46
|
-
* @param denomenator
|
|
47
|
-
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
48
|
-
*/
|
|
49
|
-
export declare function calculateAdjustKCost(market: Market, marketIndex: BN, numerator: BN, denomenator: BN): BN;
|
|
50
|
-
/**
|
|
51
|
-
* Helper function calculating adjust pegMultiplier (repeg) cost
|
|
52
|
-
*
|
|
53
|
-
* @param market
|
|
54
|
-
* @param marketIndex
|
|
55
|
-
* @param newPeg
|
|
56
|
-
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
57
|
-
*/
|
|
58
|
-
export declare function calculateRepegCost(market: Market, marketIndex: BN, newPeg: BN): BN;
|
|
59
41
|
/**
|
|
60
42
|
* Helper function calculating terminal price of amm
|
|
61
43
|
*
|
package/lib/math/amm.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateBudgetedPeg = exports.calculateBudgetedK = exports.calculateMaxBaseAssetAmountToTrade = exports.calculateTerminalPrice = exports.
|
|
3
|
+
exports.calculateBudgetedPeg = exports.calculateBudgetedK = exports.calculateMaxBaseAssetAmountToTrade = exports.calculateTerminalPrice = exports.getSwapDirection = exports.calculateSwapOutput = exports.calculateAmmReservesAfterSwap = exports.calculatePrice = void 0;
|
|
4
4
|
const anchor_1 = require("@project-serum/anchor");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
|
-
const position_1 = require("./position");
|
|
7
6
|
const types_1 = require("../types");
|
|
8
7
|
const assert_1 = require("../assert/assert");
|
|
9
8
|
const __1 = require("..");
|
|
@@ -88,92 +87,6 @@ function getSwapDirection(inputAssetType, positionDirection) {
|
|
|
88
87
|
return types_1.SwapDirection.ADD;
|
|
89
88
|
}
|
|
90
89
|
exports.getSwapDirection = getSwapDirection;
|
|
91
|
-
/**
|
|
92
|
-
* Helper function calculating adjust k cost
|
|
93
|
-
* @param market
|
|
94
|
-
* @param marketIndex
|
|
95
|
-
* @param numerator
|
|
96
|
-
* @param denomenator
|
|
97
|
-
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
98
|
-
*/
|
|
99
|
-
function calculateAdjustKCost(market, marketIndex, numerator, denomenator) {
|
|
100
|
-
const netUserPosition = {
|
|
101
|
-
baseAssetAmount: market.baseAssetAmount,
|
|
102
|
-
lastCumulativeFundingRate: market.amm.cumulativeFundingRate,
|
|
103
|
-
marketIndex: new anchor_1.BN(marketIndex),
|
|
104
|
-
quoteAssetAmount: new anchor_1.BN(0),
|
|
105
|
-
openOrders: new anchor_1.BN(0),
|
|
106
|
-
};
|
|
107
|
-
const currentValue = (0, position_1.calculateBaseAssetValue)(market, netUserPosition);
|
|
108
|
-
const marketNewK = Object.assign({}, market);
|
|
109
|
-
marketNewK.amm = Object.assign({}, market.amm);
|
|
110
|
-
marketNewK.amm.baseAssetReserve = market.amm.baseAssetReserve
|
|
111
|
-
.mul(numerator)
|
|
112
|
-
.div(denomenator);
|
|
113
|
-
marketNewK.amm.quoteAssetReserve = market.amm.quoteAssetReserve
|
|
114
|
-
.mul(numerator)
|
|
115
|
-
.div(denomenator);
|
|
116
|
-
marketNewK.amm.sqrtK = market.amm.sqrtK.mul(numerator).div(denomenator);
|
|
117
|
-
netUserPosition.quoteAssetAmount = currentValue;
|
|
118
|
-
const cost = (0, __1.calculatePositionPNL)(marketNewK, netUserPosition);
|
|
119
|
-
const p = numericConstants_1.PEG_PRECISION.mul(numerator).div(denomenator);
|
|
120
|
-
const x = market.amm.baseAssetReserve;
|
|
121
|
-
const y = market.amm.quoteAssetReserve;
|
|
122
|
-
const delta = market.baseAssetAmount;
|
|
123
|
-
const k = market.amm.sqrtK.mul(market.amm.sqrtK);
|
|
124
|
-
const numer1 = numericConstants_1.PEG_PRECISION.sub(p).mul(y).div(numericConstants_1.PEG_PRECISION);
|
|
125
|
-
const numer20 = k
|
|
126
|
-
.mul(p)
|
|
127
|
-
.mul(p)
|
|
128
|
-
.div(numericConstants_1.PEG_PRECISION)
|
|
129
|
-
.div(numericConstants_1.PEG_PRECISION)
|
|
130
|
-
.div(x.mul(p).div(numericConstants_1.PEG_PRECISION).add(delta));
|
|
131
|
-
const numer21 = k.div(x.add(delta));
|
|
132
|
-
const formulaCost = numer21
|
|
133
|
-
.sub(numer20)
|
|
134
|
-
.sub(numer1)
|
|
135
|
-
.mul(market.amm.pegMultiplier)
|
|
136
|
-
.div(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
137
|
-
console.log((0, __1.convertToNumber)(formulaCost, numericConstants_1.QUOTE_PRECISION));
|
|
138
|
-
// p.div(p.mul(x).add(delta)).sub()
|
|
139
|
-
return cost;
|
|
140
|
-
}
|
|
141
|
-
exports.calculateAdjustKCost = calculateAdjustKCost;
|
|
142
|
-
/**
|
|
143
|
-
* Helper function calculating adjust pegMultiplier (repeg) cost
|
|
144
|
-
*
|
|
145
|
-
* @param market
|
|
146
|
-
* @param marketIndex
|
|
147
|
-
* @param newPeg
|
|
148
|
-
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
149
|
-
*/
|
|
150
|
-
function calculateRepegCost(market, marketIndex, newPeg) {
|
|
151
|
-
const netUserPosition = {
|
|
152
|
-
baseAssetAmount: market.baseAssetAmount,
|
|
153
|
-
lastCumulativeFundingRate: market.amm.cumulativeFundingRate,
|
|
154
|
-
marketIndex: new anchor_1.BN(marketIndex),
|
|
155
|
-
quoteAssetAmount: new anchor_1.BN(0),
|
|
156
|
-
openOrders: new anchor_1.BN(0),
|
|
157
|
-
};
|
|
158
|
-
const currentValue = (0, position_1.calculateBaseAssetValue)(market, netUserPosition);
|
|
159
|
-
netUserPosition.quoteAssetAmount = currentValue;
|
|
160
|
-
const prevMarketPrice = (0, __1.calculateMarkPrice)(market);
|
|
161
|
-
const marketNewPeg = Object.assign({}, market);
|
|
162
|
-
marketNewPeg.amm = Object.assign({}, market.amm);
|
|
163
|
-
// const marketNewPeg = JSON.parse(JSON.stringify(market));
|
|
164
|
-
marketNewPeg.amm.pegMultiplier = newPeg;
|
|
165
|
-
console.log('Price moves from', (0, __1.convertToNumber)(prevMarketPrice), 'to', (0, __1.convertToNumber)((0, __1.calculateMarkPrice)(marketNewPeg)));
|
|
166
|
-
const cost = (0, __1.calculatePositionPNL)(marketNewPeg, netUserPosition);
|
|
167
|
-
const k = market.amm.sqrtK.mul(market.amm.sqrtK);
|
|
168
|
-
const newQuoteAssetReserve = k.div(market.amm.baseAssetReserve.add(netUserPosition.baseAssetAmount));
|
|
169
|
-
const deltaQuoteAssetReserves = newQuoteAssetReserve.sub(market.amm.quoteAssetReserve);
|
|
170
|
-
const cost2 = deltaQuoteAssetReserves
|
|
171
|
-
.mul(market.amm.pegMultiplier.sub(newPeg))
|
|
172
|
-
.div(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
173
|
-
console.log((0, __1.convertToNumber)(cost2, numericConstants_1.QUOTE_PRECISION));
|
|
174
|
-
return cost;
|
|
175
|
-
}
|
|
176
|
-
exports.calculateRepegCost = calculateRepegCost;
|
|
177
90
|
/**
|
|
178
91
|
* Helper function calculating terminal price of amm
|
|
179
92
|
*
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
2
|
+
import { BN } from '@project-serum/anchor';
|
|
3
|
+
import { Market } from '../types';
|
|
4
|
+
/**
|
|
5
|
+
* Helper function calculating adjust k cost
|
|
6
|
+
* @param market
|
|
7
|
+
* @param marketIndex
|
|
8
|
+
* @param numerator
|
|
9
|
+
* @param denomenator
|
|
10
|
+
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
11
|
+
*/
|
|
12
|
+
export declare function calculateAdjustKCost(market: Market, marketIndex: BN, numerator: BN, denomenator: BN): BN;
|
|
13
|
+
/**
|
|
14
|
+
* Helper function calculating adjust pegMultiplier (repeg) cost
|
|
15
|
+
*
|
|
16
|
+
* @param market
|
|
17
|
+
* @param marketIndex
|
|
18
|
+
* @param newPeg
|
|
19
|
+
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
20
|
+
*/
|
|
21
|
+
export declare function calculateRepegCost(market: Market, marketIndex: BN, newPeg: BN): BN;
|
|
22
|
+
/**
|
|
23
|
+
* Helper function calculating adjust pegMultiplier (repeg) cost
|
|
24
|
+
*
|
|
25
|
+
* @param market
|
|
26
|
+
* @param marketIndex
|
|
27
|
+
* @param newPeg
|
|
28
|
+
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
29
|
+
*/
|
|
30
|
+
export declare function calculateReserveRebalanceCost(market: Market, marketIndex: BN): BN;
|
|
31
|
+
export declare function calculateBudgetedK(market: Market, cost: BN): [BN, BN];
|
|
32
|
+
export declare function calculateBudgetedPeg(market: Market, cost: BN): BN;
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calculateBudgetedPeg = exports.calculateBudgetedK = exports.calculateReserveRebalanceCost = exports.calculateRepegCost = exports.calculateAdjustKCost = void 0;
|
|
4
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
5
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
|
+
const position_1 = require("./position");
|
|
7
|
+
const amm_1 = require("./amm");
|
|
8
|
+
const __1 = require("..");
|
|
9
|
+
/**
|
|
10
|
+
* Helper function calculating adjust k cost
|
|
11
|
+
* @param market
|
|
12
|
+
* @param marketIndex
|
|
13
|
+
* @param numerator
|
|
14
|
+
* @param denomenator
|
|
15
|
+
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
16
|
+
*/
|
|
17
|
+
function calculateAdjustKCost(market, marketIndex, numerator, denomenator) {
|
|
18
|
+
const netUserPosition = {
|
|
19
|
+
baseAssetAmount: market.baseAssetAmount,
|
|
20
|
+
lastCumulativeFundingRate: market.amm.cumulativeFundingRate,
|
|
21
|
+
marketIndex: new anchor_1.BN(marketIndex),
|
|
22
|
+
quoteAssetAmount: numericConstants_1.ZERO,
|
|
23
|
+
openOrders: numericConstants_1.ZERO,
|
|
24
|
+
};
|
|
25
|
+
const currentValue = (0, position_1.calculateBaseAssetValue)(market, netUserPosition);
|
|
26
|
+
const marketNewK = Object.assign({}, market);
|
|
27
|
+
marketNewK.amm = Object.assign({}, market.amm);
|
|
28
|
+
marketNewK.amm.baseAssetReserve = market.amm.baseAssetReserve
|
|
29
|
+
.mul(numerator)
|
|
30
|
+
.div(denomenator);
|
|
31
|
+
marketNewK.amm.quoteAssetReserve = market.amm.quoteAssetReserve
|
|
32
|
+
.mul(numerator)
|
|
33
|
+
.div(denomenator);
|
|
34
|
+
marketNewK.amm.sqrtK = market.amm.sqrtK.mul(numerator).div(denomenator);
|
|
35
|
+
netUserPosition.quoteAssetAmount = currentValue;
|
|
36
|
+
const cost = (0, __1.calculatePositionPNL)(marketNewK, netUserPosition);
|
|
37
|
+
return cost;
|
|
38
|
+
}
|
|
39
|
+
exports.calculateAdjustKCost = calculateAdjustKCost;
|
|
40
|
+
/**
|
|
41
|
+
* Helper function calculating adjust pegMultiplier (repeg) cost
|
|
42
|
+
*
|
|
43
|
+
* @param market
|
|
44
|
+
* @param marketIndex
|
|
45
|
+
* @param newPeg
|
|
46
|
+
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
47
|
+
*/
|
|
48
|
+
function calculateRepegCost(market, marketIndex, newPeg) {
|
|
49
|
+
const netUserPosition = {
|
|
50
|
+
baseAssetAmount: market.baseAssetAmount,
|
|
51
|
+
lastCumulativeFundingRate: market.amm.cumulativeFundingRate,
|
|
52
|
+
marketIndex: new anchor_1.BN(marketIndex),
|
|
53
|
+
quoteAssetAmount: new anchor_1.BN(0),
|
|
54
|
+
openOrders: numericConstants_1.ZERO,
|
|
55
|
+
};
|
|
56
|
+
const currentValue = (0, position_1.calculateBaseAssetValue)(market, netUserPosition);
|
|
57
|
+
netUserPosition.quoteAssetAmount = currentValue;
|
|
58
|
+
const prevMarketPrice = (0, __1.calculateMarkPrice)(market);
|
|
59
|
+
const marketNewPeg = Object.assign({}, market);
|
|
60
|
+
marketNewPeg.amm = Object.assign({}, market.amm);
|
|
61
|
+
// const marketNewPeg = JSON.parse(JSON.stringify(market));
|
|
62
|
+
marketNewPeg.amm.pegMultiplier = newPeg;
|
|
63
|
+
console.log('Price moves from', (0, __1.convertToNumber)(prevMarketPrice), 'to', (0, __1.convertToNumber)((0, __1.calculateMarkPrice)(marketNewPeg)));
|
|
64
|
+
const cost = (0, __1.calculatePositionPNL)(marketNewPeg, netUserPosition);
|
|
65
|
+
return cost;
|
|
66
|
+
}
|
|
67
|
+
exports.calculateRepegCost = calculateRepegCost;
|
|
68
|
+
/**
|
|
69
|
+
* Helper function calculating adjust pegMultiplier (repeg) cost
|
|
70
|
+
*
|
|
71
|
+
* @param market
|
|
72
|
+
* @param marketIndex
|
|
73
|
+
* @param newPeg
|
|
74
|
+
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
75
|
+
*/
|
|
76
|
+
function calculateReserveRebalanceCost(market, marketIndex) {
|
|
77
|
+
const netUserPosition = {
|
|
78
|
+
baseAssetAmount: market.baseAssetAmount,
|
|
79
|
+
lastCumulativeFundingRate: market.amm.cumulativeFundingRate,
|
|
80
|
+
marketIndex: new anchor_1.BN(marketIndex),
|
|
81
|
+
quoteAssetAmount: new anchor_1.BN(0),
|
|
82
|
+
openOrders: numericConstants_1.ZERO,
|
|
83
|
+
};
|
|
84
|
+
const currentValue = (0, position_1.calculateBaseAssetValue)(market, netUserPosition);
|
|
85
|
+
netUserPosition.quoteAssetAmount = currentValue;
|
|
86
|
+
const prevMarketPrice = (0, __1.calculateMarkPrice)(market);
|
|
87
|
+
const marketNewPeg = Object.assign({}, market);
|
|
88
|
+
marketNewPeg.amm = Object.assign({}, market.amm);
|
|
89
|
+
// const marketNewPeg = JSON.parse(JSON.stringify(market));
|
|
90
|
+
const newPeg = (0, amm_1.calculateTerminalPrice)(market)
|
|
91
|
+
.mul(numericConstants_1.PEG_PRECISION)
|
|
92
|
+
.div(numericConstants_1.MARK_PRICE_PRECISION);
|
|
93
|
+
// const newPeg = prevMarketPrice.mul(PEG_PRECISION).div(MARK_PRICE_PRECISION);
|
|
94
|
+
const newBaseReserve = market.amm.baseAssetReserve.add(market.baseAssetAmount);
|
|
95
|
+
const newQuoteReserve = market.amm.sqrtK
|
|
96
|
+
.mul(market.amm.sqrtK)
|
|
97
|
+
.div(newBaseReserve);
|
|
98
|
+
console.log('current reserves on close, quote:', (0, __1.convertToNumber)(newQuoteReserve, numericConstants_1.AMM_RESERVE_PRECISION), 'base:', (0, __1.convertToNumber)(newBaseReserve, numericConstants_1.AMM_RESERVE_PRECISION));
|
|
99
|
+
let newSqrtK;
|
|
100
|
+
if (newPeg.lt(market.amm.pegMultiplier)) {
|
|
101
|
+
newSqrtK = newBaseReserve;
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
newSqrtK = newQuoteReserve;
|
|
105
|
+
}
|
|
106
|
+
marketNewPeg.amm.baseAssetReserve = newSqrtK.sub(market.baseAssetAmount); // newSqrtK.sub(market.baseAssetAmount);
|
|
107
|
+
marketNewPeg.amm.quoteAssetReserve = newSqrtK
|
|
108
|
+
.mul(newSqrtK)
|
|
109
|
+
.div(marketNewPeg.amm.baseAssetReserve);
|
|
110
|
+
marketNewPeg.amm.sqrtK = newSqrtK;
|
|
111
|
+
marketNewPeg.amm.pegMultiplier = newPeg;
|
|
112
|
+
console.log('Price moves from', (0, __1.convertToNumber)(prevMarketPrice), 'to', (0, __1.convertToNumber)((0, __1.calculateMarkPrice)(marketNewPeg)));
|
|
113
|
+
const cost = (0, __1.calculatePositionPNL)(marketNewPeg, netUserPosition);
|
|
114
|
+
return cost;
|
|
115
|
+
}
|
|
116
|
+
exports.calculateReserveRebalanceCost = calculateReserveRebalanceCost;
|
|
117
|
+
function calculateBudgetedK(market, cost) {
|
|
118
|
+
// wolframalpha.com
|
|
119
|
+
// (1/(x+d) - p/(x*p+d))*y*d*Q = C solve for p
|
|
120
|
+
// p = (d(y*d*Q - C(x+d))) / (C*x(x+d) + y*y*d*Q)
|
|
121
|
+
// todo: assumes k = x * y
|
|
122
|
+
// otherwise use: (y(1-p) + (kp^2/(x*p+d)) - k/(x+d)) * Q = C solve for p
|
|
123
|
+
// const k = market.amm.sqrtK.mul(market.amm.sqrtK);
|
|
124
|
+
const x = market.amm.baseAssetReserve;
|
|
125
|
+
const y = market.amm.quoteAssetReserve;
|
|
126
|
+
const d = market.baseAssetAmount;
|
|
127
|
+
const Q = market.amm.pegMultiplier;
|
|
128
|
+
const C = cost.mul(new anchor_1.BN(-1));
|
|
129
|
+
const numer1 = y.mul(d).mul(Q).div(numericConstants_1.AMM_RESERVE_PRECISION).div(numericConstants_1.PEG_PRECISION);
|
|
130
|
+
const numer2 = C.mul(x.add(d)).div(numericConstants_1.QUOTE_PRECISION);
|
|
131
|
+
const denom1 = C.mul(x)
|
|
132
|
+
.mul(x.add(d))
|
|
133
|
+
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
134
|
+
.div(numericConstants_1.QUOTE_PRECISION);
|
|
135
|
+
const denom2 = y
|
|
136
|
+
.mul(d)
|
|
137
|
+
.mul(d)
|
|
138
|
+
.mul(Q)
|
|
139
|
+
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
140
|
+
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
141
|
+
.div(numericConstants_1.PEG_PRECISION);
|
|
142
|
+
const numerator = d
|
|
143
|
+
.mul(numer1.add(numer2))
|
|
144
|
+
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
145
|
+
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
146
|
+
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO);
|
|
147
|
+
const denominator = denom1
|
|
148
|
+
.add(denom2)
|
|
149
|
+
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
150
|
+
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO);
|
|
151
|
+
console.log(numerator, denominator);
|
|
152
|
+
// const p = (numerator).div(denominator);
|
|
153
|
+
// const formulaCost = (numer21.sub(numer20).sub(numer1)).mul(market.amm.pegMultiplier).div(AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO)
|
|
154
|
+
// console.log(convertToNumber(formulaCost, QUOTE_PRECISION))
|
|
155
|
+
return [numerator, denominator];
|
|
156
|
+
}
|
|
157
|
+
exports.calculateBudgetedK = calculateBudgetedK;
|
|
158
|
+
function calculateBudgetedPeg(market, cost) {
|
|
159
|
+
// wolframalpha.com
|
|
160
|
+
// (1/(x+d) - p/(x*p+d))*y*d*Q = C solve for p
|
|
161
|
+
// p = (d(y*d*Q - C(x+d))) / (C*x(x+d) + y*y*d*Q)
|
|
162
|
+
// todo: assumes k = x * y
|
|
163
|
+
// otherwise use: (y(1-p) + (kp^2/(x*p+d)) - k/(x+d)) * Q = C solve for p
|
|
164
|
+
const k = market.amm.sqrtK.mul(market.amm.sqrtK);
|
|
165
|
+
const x = market.amm.baseAssetReserve;
|
|
166
|
+
const y = market.amm.quoteAssetReserve;
|
|
167
|
+
const d = market.baseAssetAmount;
|
|
168
|
+
const Q = market.amm.pegMultiplier;
|
|
169
|
+
const C = cost.mul(new anchor_1.BN(-1));
|
|
170
|
+
const deltaQuoteAssetReserves = y.sub(k.div(x.add(d)));
|
|
171
|
+
const deltaPegMultiplier = C.mul(numericConstants_1.MARK_PRICE_PRECISION).div(deltaQuoteAssetReserves.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO));
|
|
172
|
+
// .mul(PEG_PRECISION)
|
|
173
|
+
// .div(QUOTE_PRECISION);
|
|
174
|
+
console.log(Q.toNumber(), 'change by', deltaPegMultiplier.toNumber() / numericConstants_1.MARK_PRICE_PRECISION.toNumber());
|
|
175
|
+
const newPeg = Q.sub(deltaPegMultiplier.mul(numericConstants_1.PEG_PRECISION).div(numericConstants_1.MARK_PRICE_PRECISION));
|
|
176
|
+
return newPeg;
|
|
177
|
+
}
|
|
178
|
+
exports.calculateBudgetedPeg = calculateBudgetedPeg;
|
package/package.json
CHANGED
|
@@ -205,6 +205,10 @@ export class BulkAccountLoader {
|
|
|
205
205
|
});
|
|
206
206
|
this.handleAccountCallbacks(accountToLoad, newBuffer);
|
|
207
207
|
this.lastUpdate = Date.now();
|
|
208
|
+
} else if (this.loggingEnabled) {
|
|
209
|
+
console.log('unable to update account for newest slot');
|
|
210
|
+
console.log('oldBuffer', oldBuffer);
|
|
211
|
+
console.log('newBuffer', newBuffer);
|
|
208
212
|
}
|
|
209
213
|
}
|
|
210
214
|
}
|
|
@@ -1444,6 +1444,11 @@
|
|
|
1444
1444
|
"isMut": true,
|
|
1445
1445
|
"isSigner": false
|
|
1446
1446
|
},
|
|
1447
|
+
{
|
|
1448
|
+
"name": "userOrders",
|
|
1449
|
+
"isMut": true,
|
|
1450
|
+
"isSigner": false
|
|
1451
|
+
},
|
|
1447
1452
|
{
|
|
1448
1453
|
"name": "authority",
|
|
1449
1454
|
"isMut": false,
|
|
@@ -3236,8 +3241,8 @@
|
|
|
3236
3241
|
"type": "u128"
|
|
3237
3242
|
},
|
|
3238
3243
|
{
|
|
3239
|
-
"name": "
|
|
3240
|
-
"type": "
|
|
3244
|
+
"name": "netRevenueSinceLastFunding",
|
|
3245
|
+
"type": "i64"
|
|
3241
3246
|
},
|
|
3242
3247
|
{
|
|
3243
3248
|
"name": "padding2",
|
|
@@ -4244,6 +4249,11 @@
|
|
|
4244
4249
|
"code": 6060,
|
|
4245
4250
|
"name": "CantExpireOrders",
|
|
4246
4251
|
"msg": "CantExpireOrders"
|
|
4252
|
+
},
|
|
4253
|
+
{
|
|
4254
|
+
"code": 6061,
|
|
4255
|
+
"name": "InvalidRepegPriceImpact",
|
|
4256
|
+
"msg": "AMM repeg mark price impact vs oracle too large"
|
|
4247
4257
|
}
|
|
4248
4258
|
]
|
|
4249
4259
|
}
|
package/src/math/amm.ts
CHANGED
|
@@ -2,13 +2,12 @@ import { BN } from '@project-serum/anchor';
|
|
|
2
2
|
import {
|
|
3
3
|
AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO,
|
|
4
4
|
MARK_PRICE_PRECISION,
|
|
5
|
+
AMM_RESERVE_PRECISION,
|
|
5
6
|
PEG_PRECISION,
|
|
6
7
|
ZERO,
|
|
7
8
|
AMM_TO_QUOTE_PRECISION_RATIO,
|
|
8
9
|
QUOTE_PRECISION,
|
|
9
|
-
AMM_RESERVE_PRECISION,
|
|
10
10
|
} from '../constants/numericConstants';
|
|
11
|
-
import { calculateBaseAssetValue } from './position';
|
|
12
11
|
import {
|
|
13
12
|
AMM,
|
|
14
13
|
PositionDirection,
|
|
@@ -17,12 +16,7 @@ import {
|
|
|
17
16
|
isVariant,
|
|
18
17
|
} from '../types';
|
|
19
18
|
import { assert } from '../assert/assert';
|
|
20
|
-
import {
|
|
21
|
-
calculatePositionPNL,
|
|
22
|
-
calculateMarkPrice,
|
|
23
|
-
convertToNumber,
|
|
24
|
-
squareRootBN,
|
|
25
|
-
} from '..';
|
|
19
|
+
import { squareRootBN } from '..';
|
|
26
20
|
|
|
27
21
|
/**
|
|
28
22
|
* Calculates a price given an arbitrary base and quote amount (they must have the same precision)
|
|
@@ -139,125 +133,6 @@ export function getSwapDirection(
|
|
|
139
133
|
return SwapDirection.ADD;
|
|
140
134
|
}
|
|
141
135
|
|
|
142
|
-
/**
|
|
143
|
-
* Helper function calculating adjust k cost
|
|
144
|
-
* @param market
|
|
145
|
-
* @param marketIndex
|
|
146
|
-
* @param numerator
|
|
147
|
-
* @param denomenator
|
|
148
|
-
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
149
|
-
*/
|
|
150
|
-
export function calculateAdjustKCost(
|
|
151
|
-
market: Market,
|
|
152
|
-
marketIndex: BN,
|
|
153
|
-
numerator: BN,
|
|
154
|
-
denomenator: BN
|
|
155
|
-
): BN {
|
|
156
|
-
const netUserPosition = {
|
|
157
|
-
baseAssetAmount: market.baseAssetAmount,
|
|
158
|
-
lastCumulativeFundingRate: market.amm.cumulativeFundingRate,
|
|
159
|
-
marketIndex: new BN(marketIndex),
|
|
160
|
-
quoteAssetAmount: new BN(0),
|
|
161
|
-
openOrders: new BN(0),
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
const currentValue = calculateBaseAssetValue(market, netUserPosition);
|
|
165
|
-
|
|
166
|
-
const marketNewK = Object.assign({}, market);
|
|
167
|
-
marketNewK.amm = Object.assign({}, market.amm);
|
|
168
|
-
|
|
169
|
-
marketNewK.amm.baseAssetReserve = market.amm.baseAssetReserve
|
|
170
|
-
.mul(numerator)
|
|
171
|
-
.div(denomenator);
|
|
172
|
-
marketNewK.amm.quoteAssetReserve = market.amm.quoteAssetReserve
|
|
173
|
-
.mul(numerator)
|
|
174
|
-
.div(denomenator);
|
|
175
|
-
marketNewK.amm.sqrtK = market.amm.sqrtK.mul(numerator).div(denomenator);
|
|
176
|
-
|
|
177
|
-
netUserPosition.quoteAssetAmount = currentValue;
|
|
178
|
-
|
|
179
|
-
const cost = calculatePositionPNL(marketNewK, netUserPosition);
|
|
180
|
-
|
|
181
|
-
const p = PEG_PRECISION.mul(numerator).div(denomenator);
|
|
182
|
-
const x = market.amm.baseAssetReserve;
|
|
183
|
-
const y = market.amm.quoteAssetReserve;
|
|
184
|
-
const delta = market.baseAssetAmount;
|
|
185
|
-
const k = market.amm.sqrtK.mul(market.amm.sqrtK);
|
|
186
|
-
|
|
187
|
-
const numer1 = PEG_PRECISION.sub(p).mul(y).div(PEG_PRECISION);
|
|
188
|
-
const numer20 = k
|
|
189
|
-
.mul(p)
|
|
190
|
-
.mul(p)
|
|
191
|
-
.div(PEG_PRECISION)
|
|
192
|
-
.div(PEG_PRECISION)
|
|
193
|
-
.div(x.mul(p).div(PEG_PRECISION).add(delta));
|
|
194
|
-
const numer21 = k.div(x.add(delta));
|
|
195
|
-
|
|
196
|
-
const formulaCost = numer21
|
|
197
|
-
.sub(numer20)
|
|
198
|
-
.sub(numer1)
|
|
199
|
-
.mul(market.amm.pegMultiplier)
|
|
200
|
-
.div(AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
201
|
-
console.log(convertToNumber(formulaCost, QUOTE_PRECISION));
|
|
202
|
-
|
|
203
|
-
// p.div(p.mul(x).add(delta)).sub()
|
|
204
|
-
|
|
205
|
-
return cost;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* Helper function calculating adjust pegMultiplier (repeg) cost
|
|
210
|
-
*
|
|
211
|
-
* @param market
|
|
212
|
-
* @param marketIndex
|
|
213
|
-
* @param newPeg
|
|
214
|
-
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
215
|
-
*/
|
|
216
|
-
export function calculateRepegCost(
|
|
217
|
-
market: Market,
|
|
218
|
-
marketIndex: BN,
|
|
219
|
-
newPeg: BN
|
|
220
|
-
): BN {
|
|
221
|
-
const netUserPosition = {
|
|
222
|
-
baseAssetAmount: market.baseAssetAmount,
|
|
223
|
-
lastCumulativeFundingRate: market.amm.cumulativeFundingRate,
|
|
224
|
-
marketIndex: new BN(marketIndex),
|
|
225
|
-
quoteAssetAmount: new BN(0),
|
|
226
|
-
openOrders: new BN(0),
|
|
227
|
-
};
|
|
228
|
-
|
|
229
|
-
const currentValue = calculateBaseAssetValue(market, netUserPosition);
|
|
230
|
-
netUserPosition.quoteAssetAmount = currentValue;
|
|
231
|
-
const prevMarketPrice = calculateMarkPrice(market);
|
|
232
|
-
const marketNewPeg = Object.assign({}, market);
|
|
233
|
-
marketNewPeg.amm = Object.assign({}, market.amm);
|
|
234
|
-
|
|
235
|
-
// const marketNewPeg = JSON.parse(JSON.stringify(market));
|
|
236
|
-
marketNewPeg.amm.pegMultiplier = newPeg;
|
|
237
|
-
|
|
238
|
-
console.log(
|
|
239
|
-
'Price moves from',
|
|
240
|
-
convertToNumber(prevMarketPrice),
|
|
241
|
-
'to',
|
|
242
|
-
convertToNumber(calculateMarkPrice(marketNewPeg))
|
|
243
|
-
);
|
|
244
|
-
|
|
245
|
-
const cost = calculatePositionPNL(marketNewPeg, netUserPosition);
|
|
246
|
-
|
|
247
|
-
const k = market.amm.sqrtK.mul(market.amm.sqrtK);
|
|
248
|
-
const newQuoteAssetReserve = k.div(
|
|
249
|
-
market.amm.baseAssetReserve.add(netUserPosition.baseAssetAmount)
|
|
250
|
-
);
|
|
251
|
-
const deltaQuoteAssetReserves = newQuoteAssetReserve.sub(
|
|
252
|
-
market.amm.quoteAssetReserve
|
|
253
|
-
);
|
|
254
|
-
const cost2 = deltaQuoteAssetReserves
|
|
255
|
-
.mul(market.amm.pegMultiplier.sub(newPeg))
|
|
256
|
-
.div(AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
257
|
-
console.log(convertToNumber(cost2, QUOTE_PRECISION));
|
|
258
|
-
return cost;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
136
|
/**
|
|
262
137
|
* Helper function calculating terminal price of amm
|
|
263
138
|
*
|