@drift-labs/sdk 0.1.35 → 0.1.36-master.0
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.d.ts +1 -2
- package/lib/accounts/bulkAccountLoader.js +3 -30
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.ts +3 -36
- package/src/assert/assert.js +0 -10
- package/src/assert/assert.js.map +0 -1
- package/src/math/conversion.js +0 -16
- package/src/math/conversion.js.map +0 -1
- package/src/math/funding.js +0 -223
- package/src/math/funding.js.map +0 -1
- package/src/math/insuranceFund.js +0 -23
- package/src/math/insuranceFund.js.map +0 -1
- package/src/math/position.js +0 -121
- package/src/math/position.js.map +0 -1
- package/src/math/utils.js +0 -27
- package/src/math/utils.js.map +0 -1
- package/src/oracles/switchboardClient.js +0 -60
- package/src/oracles/switchboardClient.js.map +0 -1
- package/src/token/index.js +0 -39
- package/src/token/index.js.map +0 -1
- package/src/tx/defaultTxSender.js +0 -13
- package/src/tx/defaultTxSender.js.map +0 -1
- package/src/tx/types.js +0 -3
- package/src/tx/types.js.map +0 -1
- package/src/tx/utils.js +0 -9
- package/src/tx/utils.js.map +0 -1
- package/src/util/computeUnits.js +0 -17
- package/src/util/computeUnits.js.map +0 -1
- package/src/util/tps.js +0 -17
- package/src/util/tps.js.map +0 -1
|
@@ -15,7 +15,6 @@ export declare class BulkAccountLoader {
|
|
|
15
15
|
intervalId?: NodeJS.Timer;
|
|
16
16
|
loadPromise?: Promise<void>;
|
|
17
17
|
loadPromiseResolver: () => void;
|
|
18
|
-
loggingEnabled: boolean;
|
|
19
18
|
lastTimeLoadingPromiseCleared: number;
|
|
20
19
|
constructor(connection: Connection, commitment: Commitment, pollingFrequency: number);
|
|
21
20
|
addAccount(publicKey: PublicKey, callback: (buffer: Buffer) => void): string;
|
|
@@ -29,7 +28,7 @@ export declare class BulkAccountLoader {
|
|
|
29
28
|
getAccountData(publicKey: PublicKey): Buffer | undefined;
|
|
30
29
|
startPolling(): void;
|
|
31
30
|
stopPolling(): void;
|
|
32
|
-
log(msg: string
|
|
31
|
+
log(msg: string): void;
|
|
33
32
|
updatePollingFrequency(pollingFrequency: number): void;
|
|
34
33
|
}
|
|
35
34
|
export {};
|
|
@@ -19,7 +19,6 @@ class BulkAccountLoader {
|
|
|
19
19
|
this.accountsToLoad = new Map();
|
|
20
20
|
this.accountData = new Map();
|
|
21
21
|
this.errorCallbacks = new Map();
|
|
22
|
-
this.loggingEnabled = false;
|
|
23
22
|
this.lastTimeLoadingPromiseCleared = Date.now();
|
|
24
23
|
this.connection = connection;
|
|
25
24
|
this.commitment = commitment;
|
|
@@ -28,14 +27,11 @@ class BulkAccountLoader {
|
|
|
28
27
|
addAccount(publicKey, callback) {
|
|
29
28
|
const existingSize = this.accountsToLoad.size;
|
|
30
29
|
const callbackId = (0, uuid_1.v4)();
|
|
31
|
-
this.log(`Adding account ${publicKey.toString()} callback id ${callbackId}`);
|
|
32
30
|
const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
|
|
33
31
|
if (existingAccountToLoad) {
|
|
34
|
-
this.log(`account already exists`);
|
|
35
32
|
existingAccountToLoad.callbacks.set(callbackId, callback);
|
|
36
33
|
}
|
|
37
34
|
else {
|
|
38
|
-
this.log(`account doesn't already exist. creating new callback map`);
|
|
39
35
|
const callbacks = new Map();
|
|
40
36
|
callbacks.set(callbackId, callback);
|
|
41
37
|
const newAccountToLoad = {
|
|
@@ -52,7 +48,6 @@ class BulkAccountLoader {
|
|
|
52
48
|
return callbackId;
|
|
53
49
|
}
|
|
54
50
|
removeAccount(publicKey, callbackId) {
|
|
55
|
-
this.log(`Removing account ${publicKey.toString()} callback id ${callbackId}`);
|
|
56
51
|
const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
|
|
57
52
|
if (existingAccountToLoad) {
|
|
58
53
|
existingAccountToLoad.callbacks.delete(callbackId);
|
|
@@ -83,11 +78,9 @@ class BulkAccountLoader {
|
|
|
83
78
|
if (this.loadPromise) {
|
|
84
79
|
const now = Date.now();
|
|
85
80
|
if (now - this.lastTimeLoadingPromiseCleared > oneMinute) {
|
|
86
|
-
this.log(`Load promise hasnt been clearing for one minute. Clearing.`);
|
|
87
81
|
this.loadPromise = undefined;
|
|
88
82
|
}
|
|
89
83
|
else {
|
|
90
|
-
this.log(`Load promise exists. Returning early`);
|
|
91
84
|
return this.loadPromise;
|
|
92
85
|
}
|
|
93
86
|
}
|
|
@@ -95,10 +88,8 @@ class BulkAccountLoader {
|
|
|
95
88
|
this.loadPromiseResolver = resolver;
|
|
96
89
|
});
|
|
97
90
|
this.lastTimeLoadingPromiseCleared = Date.now();
|
|
98
|
-
this.log(`Loading`);
|
|
99
91
|
try {
|
|
100
92
|
const chunks = this.chunks(Array.from(this.accountsToLoad.values()), GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE);
|
|
101
|
-
this.log(`${chunks.length} chunks`);
|
|
102
93
|
yield Promise.all(chunks.map((chunk) => {
|
|
103
94
|
return this.loadChunk(chunk);
|
|
104
95
|
}));
|
|
@@ -109,10 +100,8 @@ class BulkAccountLoader {
|
|
|
109
100
|
for (const [_, callback] of this.errorCallbacks) {
|
|
110
101
|
callback(e);
|
|
111
102
|
}
|
|
112
|
-
this.log('finished error callbacks');
|
|
113
103
|
}
|
|
114
104
|
finally {
|
|
115
|
-
this.log(`resetting load promise`);
|
|
116
105
|
this.loadPromiseResolver();
|
|
117
106
|
this.loadPromise = undefined;
|
|
118
107
|
}
|
|
@@ -121,7 +110,6 @@ class BulkAccountLoader {
|
|
|
121
110
|
loadChunk(accountsToLoad) {
|
|
122
111
|
return __awaiter(this, void 0, void 0, function* () {
|
|
123
112
|
if (accountsToLoad.length === 0) {
|
|
124
|
-
this.log(`no accounts in chunk`);
|
|
125
113
|
return;
|
|
126
114
|
}
|
|
127
115
|
const args = [
|
|
@@ -135,7 +123,7 @@ class BulkAccountLoader {
|
|
|
135
123
|
this.connection._rpcRequest('getMultipleAccounts', args), 10 * 1000 // 30 second timeout
|
|
136
124
|
);
|
|
137
125
|
if (rpcResponse === null) {
|
|
138
|
-
this.log('request to rpc timed out'
|
|
126
|
+
this.log('request to rpc timed out');
|
|
139
127
|
return;
|
|
140
128
|
}
|
|
141
129
|
const newSlot = rpcResponse.result.context.slot;
|
|
@@ -150,7 +138,6 @@ class BulkAccountLoader {
|
|
|
150
138
|
newBuffer = Buffer.from(raw, dataType);
|
|
151
139
|
}
|
|
152
140
|
if (!oldRPCResponse) {
|
|
153
|
-
this.log('No old rpc response, updating account data');
|
|
154
141
|
this.accountData.set(key, {
|
|
155
142
|
slot: newSlot,
|
|
156
143
|
buffer: newBuffer,
|
|
@@ -159,33 +146,23 @@ class BulkAccountLoader {
|
|
|
159
146
|
continue;
|
|
160
147
|
}
|
|
161
148
|
if (newSlot <= oldRPCResponse.slot) {
|
|
162
|
-
this.log(`new slot ${newSlot} old slot ${oldRPCResponse.slot}`);
|
|
163
149
|
continue;
|
|
164
150
|
}
|
|
165
151
|
const oldBuffer = oldRPCResponse.buffer;
|
|
166
152
|
if (newBuffer && (!oldBuffer || !newBuffer.equals(oldBuffer))) {
|
|
167
|
-
this.log('new buffer, updating account data');
|
|
168
153
|
this.accountData.set(key, {
|
|
169
154
|
slot: newSlot,
|
|
170
155
|
buffer: newBuffer,
|
|
171
156
|
});
|
|
172
157
|
this.handleAccountCallbacks(accountToLoad, newBuffer);
|
|
173
158
|
}
|
|
174
|
-
else {
|
|
175
|
-
this.log('unable to update account for newest slot');
|
|
176
|
-
this.log('oldBuffer ' + oldBuffer);
|
|
177
|
-
this.log('newBuffer ' + newBuffer);
|
|
178
|
-
this.log('buffers equal ' + (newBuffer === null || newBuffer === void 0 ? void 0 : newBuffer.equals(oldBuffer)));
|
|
179
|
-
}
|
|
180
159
|
}
|
|
181
160
|
});
|
|
182
161
|
}
|
|
183
162
|
handleAccountCallbacks(accountToLoad, buffer) {
|
|
184
|
-
this.log('handling account callbacks');
|
|
185
163
|
for (const [_, callback] of accountToLoad.callbacks) {
|
|
186
164
|
callback(buffer);
|
|
187
165
|
}
|
|
188
|
-
this.log('finished account callbacks');
|
|
189
166
|
}
|
|
190
167
|
getAccountData(publicKey) {
|
|
191
168
|
const accountData = this.accountData.get(publicKey.toString());
|
|
@@ -195,20 +172,16 @@ class BulkAccountLoader {
|
|
|
195
172
|
if (this.intervalId) {
|
|
196
173
|
return;
|
|
197
174
|
}
|
|
198
|
-
this.log('startPolling');
|
|
199
175
|
this.intervalId = setInterval(this.load.bind(this), this.pollingFrequency);
|
|
200
176
|
}
|
|
201
177
|
stopPolling() {
|
|
202
178
|
if (this.intervalId) {
|
|
203
179
|
clearInterval(this.intervalId);
|
|
204
180
|
this.intervalId = undefined;
|
|
205
|
-
this.log('stopPolling');
|
|
206
181
|
}
|
|
207
182
|
}
|
|
208
|
-
log(msg
|
|
209
|
-
|
|
210
|
-
console.log(msg);
|
|
211
|
-
}
|
|
183
|
+
log(msg) {
|
|
184
|
+
console.log(msg);
|
|
212
185
|
}
|
|
213
186
|
updatePollingFrequency(pollingFrequency) {
|
|
214
187
|
this.stopPolling();
|
package/package.json
CHANGED
|
@@ -23,7 +23,6 @@ export class BulkAccountLoader {
|
|
|
23
23
|
// to handle clients spamming load
|
|
24
24
|
loadPromise?: Promise<void>;
|
|
25
25
|
loadPromiseResolver: () => void;
|
|
26
|
-
loggingEnabled = false;
|
|
27
26
|
lastTimeLoadingPromiseCleared = Date.now();
|
|
28
27
|
|
|
29
28
|
public constructor(
|
|
@@ -43,15 +42,10 @@ export class BulkAccountLoader {
|
|
|
43
42
|
const existingSize = this.accountsToLoad.size;
|
|
44
43
|
|
|
45
44
|
const callbackId = uuidv4();
|
|
46
|
-
this.log(
|
|
47
|
-
`Adding account ${publicKey.toString()} callback id ${callbackId}`
|
|
48
|
-
);
|
|
49
45
|
const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
|
|
50
46
|
if (existingAccountToLoad) {
|
|
51
|
-
this.log(`account already exists`);
|
|
52
47
|
existingAccountToLoad.callbacks.set(callbackId, callback);
|
|
53
48
|
} else {
|
|
54
|
-
this.log(`account doesn't already exist. creating new callback map`);
|
|
55
49
|
const callbacks = new Map<string, (buffer: Buffer) => void>();
|
|
56
50
|
callbacks.set(callbackId, callback);
|
|
57
51
|
const newAccountToLoad = {
|
|
@@ -72,9 +66,6 @@ export class BulkAccountLoader {
|
|
|
72
66
|
}
|
|
73
67
|
|
|
74
68
|
public removeAccount(publicKey: PublicKey, callbackId: string): void {
|
|
75
|
-
this.log(
|
|
76
|
-
`Removing account ${publicKey.toString()} callback id ${callbackId}`
|
|
77
|
-
);
|
|
78
69
|
const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
|
|
79
70
|
if (existingAccountToLoad) {
|
|
80
71
|
existingAccountToLoad.callbacks.delete(callbackId);
|
|
@@ -109,10 +100,8 @@ export class BulkAccountLoader {
|
|
|
109
100
|
if (this.loadPromise) {
|
|
110
101
|
const now = Date.now();
|
|
111
102
|
if (now - this.lastTimeLoadingPromiseCleared > oneMinute) {
|
|
112
|
-
this.log(`Load promise hasnt been clearing for one minute. Clearing.`);
|
|
113
103
|
this.loadPromise = undefined;
|
|
114
104
|
} else {
|
|
115
|
-
this.log(`Load promise exists. Returning early`);
|
|
116
105
|
return this.loadPromise;
|
|
117
106
|
}
|
|
118
107
|
}
|
|
@@ -122,14 +111,11 @@ export class BulkAccountLoader {
|
|
|
122
111
|
});
|
|
123
112
|
this.lastTimeLoadingPromiseCleared = Date.now();
|
|
124
113
|
|
|
125
|
-
this.log(`Loading`);
|
|
126
|
-
|
|
127
114
|
try {
|
|
128
115
|
const chunks = this.chunks(
|
|
129
116
|
Array.from(this.accountsToLoad.values()),
|
|
130
117
|
GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE
|
|
131
118
|
);
|
|
132
|
-
this.log(`${chunks.length} chunks`);
|
|
133
119
|
|
|
134
120
|
await Promise.all(
|
|
135
121
|
chunks.map((chunk) => {
|
|
@@ -142,9 +128,7 @@ export class BulkAccountLoader {
|
|
|
142
128
|
for (const [_, callback] of this.errorCallbacks) {
|
|
143
129
|
callback(e);
|
|
144
130
|
}
|
|
145
|
-
this.log('finished error callbacks');
|
|
146
131
|
} finally {
|
|
147
|
-
this.log(`resetting load promise`);
|
|
148
132
|
this.loadPromiseResolver();
|
|
149
133
|
this.loadPromise = undefined;
|
|
150
134
|
}
|
|
@@ -152,7 +136,6 @@ export class BulkAccountLoader {
|
|
|
152
136
|
|
|
153
137
|
async loadChunk(accountsToLoad: AccountToLoad[]): Promise<void> {
|
|
154
138
|
if (accountsToLoad.length === 0) {
|
|
155
|
-
this.log(`no accounts in chunk`);
|
|
156
139
|
return;
|
|
157
140
|
}
|
|
158
141
|
|
|
@@ -170,7 +153,7 @@ export class BulkAccountLoader {
|
|
|
170
153
|
);
|
|
171
154
|
|
|
172
155
|
if (rpcResponse === null) {
|
|
173
|
-
this.log('request to rpc timed out'
|
|
156
|
+
this.log('request to rpc timed out');
|
|
174
157
|
return;
|
|
175
158
|
}
|
|
176
159
|
|
|
@@ -189,7 +172,6 @@ export class BulkAccountLoader {
|
|
|
189
172
|
}
|
|
190
173
|
|
|
191
174
|
if (!oldRPCResponse) {
|
|
192
|
-
this.log('No old rpc response, updating account data');
|
|
193
175
|
this.accountData.set(key, {
|
|
194
176
|
slot: newSlot,
|
|
195
177
|
buffer: newBuffer,
|
|
@@ -199,33 +181,24 @@ export class BulkAccountLoader {
|
|
|
199
181
|
}
|
|
200
182
|
|
|
201
183
|
if (newSlot <= oldRPCResponse.slot) {
|
|
202
|
-
this.log(`new slot ${newSlot} old slot ${oldRPCResponse.slot}`);
|
|
203
184
|
continue;
|
|
204
185
|
}
|
|
205
186
|
|
|
206
187
|
const oldBuffer = oldRPCResponse.buffer;
|
|
207
188
|
if (newBuffer && (!oldBuffer || !newBuffer.equals(oldBuffer))) {
|
|
208
|
-
this.log('new buffer, updating account data');
|
|
209
189
|
this.accountData.set(key, {
|
|
210
190
|
slot: newSlot,
|
|
211
191
|
buffer: newBuffer,
|
|
212
192
|
});
|
|
213
193
|
this.handleAccountCallbacks(accountToLoad, newBuffer);
|
|
214
|
-
} else {
|
|
215
|
-
this.log('unable to update account for newest slot');
|
|
216
|
-
this.log('oldBuffer ' + oldBuffer);
|
|
217
|
-
this.log('newBuffer ' + newBuffer);
|
|
218
|
-
this.log('buffers equal ' + newBuffer?.equals(oldBuffer));
|
|
219
194
|
}
|
|
220
195
|
}
|
|
221
196
|
}
|
|
222
197
|
|
|
223
198
|
handleAccountCallbacks(accountToLoad: AccountToLoad, buffer: Buffer): void {
|
|
224
|
-
this.log('handling account callbacks');
|
|
225
199
|
for (const [_, callback] of accountToLoad.callbacks) {
|
|
226
200
|
callback(buffer);
|
|
227
201
|
}
|
|
228
|
-
this.log('finished account callbacks');
|
|
229
202
|
}
|
|
230
203
|
|
|
231
204
|
public getAccountData(publicKey: PublicKey): Buffer | undefined {
|
|
@@ -238,8 +211,6 @@ export class BulkAccountLoader {
|
|
|
238
211
|
return;
|
|
239
212
|
}
|
|
240
213
|
|
|
241
|
-
this.log('startPolling');
|
|
242
|
-
|
|
243
214
|
this.intervalId = setInterval(this.load.bind(this), this.pollingFrequency);
|
|
244
215
|
}
|
|
245
216
|
|
|
@@ -247,15 +218,11 @@ export class BulkAccountLoader {
|
|
|
247
218
|
if (this.intervalId) {
|
|
248
219
|
clearInterval(this.intervalId);
|
|
249
220
|
this.intervalId = undefined;
|
|
250
|
-
|
|
251
|
-
this.log('stopPolling');
|
|
252
221
|
}
|
|
253
222
|
}
|
|
254
223
|
|
|
255
|
-
public log(msg: string
|
|
256
|
-
|
|
257
|
-
console.log(msg);
|
|
258
|
-
}
|
|
224
|
+
public log(msg: string): void {
|
|
225
|
+
console.log(msg);
|
|
259
226
|
}
|
|
260
227
|
|
|
261
228
|
public updatePollingFrequency(pollingFrequency: number): void {
|
package/src/assert/assert.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.assert = void 0;
|
|
4
|
-
function assert(condition, error) {
|
|
5
|
-
if (!condition) {
|
|
6
|
-
throw new Error(error || 'Unspecified AssertionError');
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
exports.assert = assert;
|
|
10
|
-
//# sourceMappingURL=assert.js.map
|
package/src/assert/assert.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"assert.js","sourceRoot":"","sources":["assert.ts"],"names":[],"mappings":";;;AAAA,SAAgB,MAAM,CAAC,SAAkB,EAAE,KAAc;IACxD,IAAI,CAAC,SAAS,EAAE;QACf,MAAM,IAAI,KAAK,CAAC,KAAK,IAAI,4BAA4B,CAAC,CAAC;KACvD;AACF,CAAC;AAJD,wBAIC"}
|
package/src/math/conversion.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.convertBaseAssetAmountToNumber = 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;
|
|
12
|
-
const convertBaseAssetAmountToNumber = (baseAssetAmount) => {
|
|
13
|
-
return (0, exports.convertToNumber)(baseAssetAmount, numericConstants_1.MARK_PRICE_PRECISION.mul(numericConstants_1.PEG_PRECISION));
|
|
14
|
-
};
|
|
15
|
-
exports.convertBaseAssetAmountToNumber = convertBaseAssetAmountToNumber;
|
|
16
|
-
//# sourceMappingURL=conversion.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"conversion.js","sourceRoot":"","sources":["conversion.ts"],"names":[],"mappings":";;;AACA,oEAGuC;AAEhC,MAAM,eAAe,GAAG,CAC9B,SAAa,EACb,YAAgB,uCAAoB,EACnC,EAAE;IACH,IAAI,CAAC,SAAS;QAAE,OAAO,CAAC,CAAC;IACzB,OAAO,CACN,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,QAAQ,EAAE;QACnC,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,QAAQ,EAAE,GAAG,SAAS,CAAC,QAAQ,EAAE,CAC1D,CAAC;AACH,CAAC,CAAC;AATW,QAAA,eAAe,mBAS1B;AAEK,MAAM,8BAA8B,GAAG,CAAC,eAAmB,EAAE,EAAE;IACrE,OAAO,IAAA,uBAAe,EACrB,eAAe,EACf,uCAAoB,CAAC,GAAG,CAAC,gCAAa,CAAC,CACvC,CAAC;AACH,CAAC,CAAC;AALW,QAAA,8BAA8B,kCAKzC"}
|
package/src/math/funding.js
DELETED
|
@@ -1,223 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateFundingPool = exports.calculateLongShortFundingRateAndLiveTwaps = exports.calculateLongShortFundingRate = exports.calculateEstimatedFundingRate = exports.calculateAllEstimatedFundingRate = void 0;
|
|
4
|
-
const anchor_1 = require("@project-serum/anchor");
|
|
5
|
-
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
|
-
const market_1 = require("./market");
|
|
7
|
-
/**
|
|
8
|
-
*
|
|
9
|
-
* @param market
|
|
10
|
-
* @param oraclePriceData
|
|
11
|
-
* @param periodAdjustment
|
|
12
|
-
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
13
|
-
*/
|
|
14
|
-
async function calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1)) {
|
|
15
|
-
// periodAdjustment
|
|
16
|
-
// 1: hourly
|
|
17
|
-
// 24: daily
|
|
18
|
-
// 24 * 365.25: annualized
|
|
19
|
-
const secondsInHour = new anchor_1.BN(3600);
|
|
20
|
-
const hoursInDay = new anchor_1.BN(24);
|
|
21
|
-
if (!market.initialized) {
|
|
22
|
-
return [numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO];
|
|
23
|
-
}
|
|
24
|
-
const payFreq = new anchor_1.BN(market.amm.fundingPeriod);
|
|
25
|
-
// todo: sufficiently differs from blockchain timestamp?
|
|
26
|
-
const now = new anchor_1.BN((Date.now() / 1000).toFixed(0));
|
|
27
|
-
const timeSinceLastUpdate = now.sub(market.amm.lastFundingRateTs);
|
|
28
|
-
// calculate real-time mark twap
|
|
29
|
-
const lastMarkTwapWithMantissa = market.amm.lastMarkPriceTwap;
|
|
30
|
-
const lastMarkPriceTwapTs = market.amm.lastMarkPriceTwapTs;
|
|
31
|
-
const timeSinceLastMarkChange = now.sub(lastMarkPriceTwapTs);
|
|
32
|
-
const markTwapTimeSinceLastUpdate = anchor_1.BN.max(secondsInHour, secondsInHour.sub(timeSinceLastMarkChange));
|
|
33
|
-
const baseAssetPriceWithMantissa = (0, market_1.calculateMarkPrice)(market);
|
|
34
|
-
const markTwapWithMantissa = markTwapTimeSinceLastUpdate
|
|
35
|
-
.mul(lastMarkTwapWithMantissa)
|
|
36
|
-
.add(timeSinceLastMarkChange.mul(baseAssetPriceWithMantissa))
|
|
37
|
-
.div(timeSinceLastMarkChange.add(markTwapTimeSinceLastUpdate));
|
|
38
|
-
// calculate real-time (predicted) oracle twap
|
|
39
|
-
// note: oracle twap depends on `when the chord is struck` (market is trade)
|
|
40
|
-
const lastOracleTwapWithMantissa = market.amm.lastOraclePriceTwap;
|
|
41
|
-
const lastOraclePriceTwapTs = market.amm.lastOraclePriceTwapTs;
|
|
42
|
-
const timeSinceLastOracleTwapUpdate = now.sub(lastOraclePriceTwapTs);
|
|
43
|
-
const oracleTwapTimeSinceLastUpdate = anchor_1.BN.max(secondsInHour, secondsInHour.sub(timeSinceLastOracleTwapUpdate));
|
|
44
|
-
const oraclePrice = oraclePriceData.price;
|
|
45
|
-
let oracleTwapWithMantissa = lastOracleTwapWithMantissa;
|
|
46
|
-
const oracleLiveVsTwap = oraclePrice
|
|
47
|
-
.sub(lastOracleTwapWithMantissa)
|
|
48
|
-
.abs()
|
|
49
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
50
|
-
.mul(new anchor_1.BN(100))
|
|
51
|
-
.div(lastOracleTwapWithMantissa);
|
|
52
|
-
// verify pyth live input is within 10% of last twap for live update
|
|
53
|
-
if (oracleLiveVsTwap.lte(numericConstants_1.MARK_PRICE_PRECISION.mul(new anchor_1.BN(10)))) {
|
|
54
|
-
oracleTwapWithMantissa = oracleTwapTimeSinceLastUpdate
|
|
55
|
-
.mul(lastOracleTwapWithMantissa)
|
|
56
|
-
.add(timeSinceLastMarkChange.mul(oraclePrice))
|
|
57
|
-
.div(timeSinceLastOracleTwapUpdate.add(oracleTwapTimeSinceLastUpdate));
|
|
58
|
-
}
|
|
59
|
-
const twapSpread = lastMarkTwapWithMantissa.sub(lastOracleTwapWithMantissa);
|
|
60
|
-
const twapSpreadPct = twapSpread
|
|
61
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
62
|
-
.mul(new anchor_1.BN(100))
|
|
63
|
-
.div(oracleTwapWithMantissa);
|
|
64
|
-
const lowerboundEst = twapSpreadPct
|
|
65
|
-
.mul(payFreq)
|
|
66
|
-
.mul(anchor_1.BN.min(secondsInHour, timeSinceLastUpdate))
|
|
67
|
-
.mul(periodAdjustment)
|
|
68
|
-
.div(secondsInHour)
|
|
69
|
-
.div(secondsInHour)
|
|
70
|
-
.div(hoursInDay);
|
|
71
|
-
const interpEst = twapSpreadPct.mul(periodAdjustment).div(hoursInDay);
|
|
72
|
-
const interpRateQuote = twapSpreadPct
|
|
73
|
-
.mul(periodAdjustment)
|
|
74
|
-
.div(hoursInDay)
|
|
75
|
-
.div(numericConstants_1.MARK_PRICE_PRECISION.div(numericConstants_1.QUOTE_PRECISION));
|
|
76
|
-
let feePoolSize = calculateFundingPool(market);
|
|
77
|
-
if (interpRateQuote.lt(new anchor_1.BN(0))) {
|
|
78
|
-
feePoolSize = feePoolSize.mul(new anchor_1.BN(-1));
|
|
79
|
-
}
|
|
80
|
-
let cappedAltEst;
|
|
81
|
-
let largerSide;
|
|
82
|
-
let smallerSide;
|
|
83
|
-
if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort.abs())) {
|
|
84
|
-
largerSide = market.baseAssetAmountLong.abs();
|
|
85
|
-
smallerSide = market.baseAssetAmountShort.abs();
|
|
86
|
-
if (twapSpread.gt(new anchor_1.BN(0))) {
|
|
87
|
-
return [
|
|
88
|
-
markTwapWithMantissa,
|
|
89
|
-
oracleTwapWithMantissa,
|
|
90
|
-
lowerboundEst,
|
|
91
|
-
interpEst,
|
|
92
|
-
interpEst,
|
|
93
|
-
];
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort.abs())) {
|
|
97
|
-
largerSide = market.baseAssetAmountShort.abs();
|
|
98
|
-
smallerSide = market.baseAssetAmountLong.abs();
|
|
99
|
-
if (twapSpread.lt(new anchor_1.BN(0))) {
|
|
100
|
-
return [
|
|
101
|
-
markTwapWithMantissa,
|
|
102
|
-
oracleTwapWithMantissa,
|
|
103
|
-
lowerboundEst,
|
|
104
|
-
interpEst,
|
|
105
|
-
interpEst,
|
|
106
|
-
];
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
return [
|
|
111
|
-
markTwapWithMantissa,
|
|
112
|
-
oracleTwapWithMantissa,
|
|
113
|
-
lowerboundEst,
|
|
114
|
-
interpEst,
|
|
115
|
-
interpEst,
|
|
116
|
-
];
|
|
117
|
-
}
|
|
118
|
-
if (largerSide.gt(numericConstants_1.ZERO)) {
|
|
119
|
-
// funding smaller flow
|
|
120
|
-
cappedAltEst = smallerSide.mul(twapSpread).div(hoursInDay);
|
|
121
|
-
const feePoolTopOff = feePoolSize
|
|
122
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION.div(numericConstants_1.QUOTE_PRECISION))
|
|
123
|
-
.mul(numericConstants_1.AMM_RESERVE_PRECISION);
|
|
124
|
-
cappedAltEst = cappedAltEst.add(feePoolTopOff).div(largerSide);
|
|
125
|
-
cappedAltEst = cappedAltEst
|
|
126
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
127
|
-
.mul(new anchor_1.BN(100))
|
|
128
|
-
.div(oracleTwapWithMantissa)
|
|
129
|
-
.mul(periodAdjustment);
|
|
130
|
-
if (cappedAltEst.abs().gte(interpEst.abs())) {
|
|
131
|
-
cappedAltEst = interpEst;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
else {
|
|
135
|
-
cappedAltEst = interpEst;
|
|
136
|
-
}
|
|
137
|
-
return [
|
|
138
|
-
markTwapWithMantissa,
|
|
139
|
-
oracleTwapWithMantissa,
|
|
140
|
-
lowerboundEst,
|
|
141
|
-
cappedAltEst,
|
|
142
|
-
interpEst,
|
|
143
|
-
];
|
|
144
|
-
}
|
|
145
|
-
exports.calculateAllEstimatedFundingRate = calculateAllEstimatedFundingRate;
|
|
146
|
-
/**
|
|
147
|
-
*
|
|
148
|
-
* @param market
|
|
149
|
-
* @param oraclePriceData
|
|
150
|
-
* @param periodAdjustment
|
|
151
|
-
* @param estimationMethod
|
|
152
|
-
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
153
|
-
*/
|
|
154
|
-
async function calculateEstimatedFundingRate(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1), estimationMethod) {
|
|
155
|
-
const [_1, _2, lowerboundEst, cappedAltEst, interpEst] = await calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment);
|
|
156
|
-
if (estimationMethod == 'lowerbound') {
|
|
157
|
-
//assuming remaining funding period has no gap
|
|
158
|
-
return lowerboundEst;
|
|
159
|
-
}
|
|
160
|
-
else if (estimationMethod == 'capped') {
|
|
161
|
-
return cappedAltEst;
|
|
162
|
-
}
|
|
163
|
-
else {
|
|
164
|
-
return interpEst;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
exports.calculateEstimatedFundingRate = calculateEstimatedFundingRate;
|
|
168
|
-
/**
|
|
169
|
-
*
|
|
170
|
-
* @param market
|
|
171
|
-
* @param oraclePriceData
|
|
172
|
-
* @param periodAdjustment
|
|
173
|
-
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
174
|
-
*/
|
|
175
|
-
async function calculateLongShortFundingRate(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1)) {
|
|
176
|
-
const [_1, _2, _, cappedAltEst, interpEst] = await calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment);
|
|
177
|
-
if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort)) {
|
|
178
|
-
return [cappedAltEst, interpEst];
|
|
179
|
-
}
|
|
180
|
-
else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort)) {
|
|
181
|
-
return [interpEst, cappedAltEst];
|
|
182
|
-
}
|
|
183
|
-
else {
|
|
184
|
-
return [interpEst, interpEst];
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
exports.calculateLongShortFundingRate = calculateLongShortFundingRate;
|
|
188
|
-
/**
|
|
189
|
-
*
|
|
190
|
-
* @param market
|
|
191
|
-
* @param oraclePriceData
|
|
192
|
-
* @param periodAdjustment
|
|
193
|
-
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
194
|
-
*/
|
|
195
|
-
async function calculateLongShortFundingRateAndLiveTwaps(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1)) {
|
|
196
|
-
const [markTwapLive, oracleTwapLive, _2, cappedAltEst, interpEst] = await calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment);
|
|
197
|
-
if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort.abs())) {
|
|
198
|
-
return [markTwapLive, oracleTwapLive, cappedAltEst, interpEst];
|
|
199
|
-
}
|
|
200
|
-
else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort.abs())) {
|
|
201
|
-
return [markTwapLive, oracleTwapLive, interpEst, cappedAltEst];
|
|
202
|
-
}
|
|
203
|
-
else {
|
|
204
|
-
return [markTwapLive, oracleTwapLive, interpEst, interpEst];
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
exports.calculateLongShortFundingRateAndLiveTwaps = calculateLongShortFundingRateAndLiveTwaps;
|
|
208
|
-
/**
|
|
209
|
-
*
|
|
210
|
-
* @param market
|
|
211
|
-
* @returns Estimated fee pool size
|
|
212
|
-
*/
|
|
213
|
-
function calculateFundingPool(market) {
|
|
214
|
-
// todo
|
|
215
|
-
const totalFeeLB = market.amm.totalFee.div(new anchor_1.BN(2));
|
|
216
|
-
const feePool = anchor_1.BN.max(numericConstants_1.ZERO, market.amm.totalFeeMinusDistributions
|
|
217
|
-
.sub(totalFeeLB)
|
|
218
|
-
.mul(new anchor_1.BN(2))
|
|
219
|
-
.div(new anchor_1.BN(3)));
|
|
220
|
-
return feePool;
|
|
221
|
-
}
|
|
222
|
-
exports.calculateFundingPool = calculateFundingPool;
|
|
223
|
-
//# sourceMappingURL=funding.js.map
|
package/src/math/funding.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"funding.js","sourceRoot":"","sources":["funding.ts"],"names":[],"mappings":";;;AAAA,kDAA2C;AAC3C,oEAKuC;AAEvC,qCAA8C;AAG9C;;;;;;GAMG;AACI,KAAK,UAAU,gCAAgC,CACrD,MAAc,EACd,eAAgC,EAChC,mBAAuB,IAAI,WAAE,CAAC,CAAC,CAAC;IAEhC,mBAAmB;IACnB,aAAa;IACb,aAAa;IACb,2BAA2B;IAC3B,MAAM,aAAa,GAAG,IAAI,WAAE,CAAC,IAAI,CAAC,CAAC;IACnC,MAAM,UAAU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,EAAE,cAAc,EAAE,EAAE,EAAE,YAAY,EAAE,SAAS,CAAC,GAChE,MAAM,gCAAgC,CACrC,MAAM,EACN,eAAe,EACf,gBAAgB,CAChB,CAAC;IAEH,IAAI,MAAM,CAAC,mBAAmB,CAAC,EAAE,CAAC,MAAM,CAAC,oBAAoB,CAAC,GAAG,EAAE,CAAC,EAAE;QACrE,OAAO,CAAC,YAAY,EAAE,cAAc,EAAE,YAAY,EAAE,SAAS,CAAC,CAAC;KAC/D;SAAM,IAAI,MAAM,CAAC,mBAAmB,CAAC,EAAE,CAAC,MAAM,CAAC,oBAAoB,CAAC,GAAG,EAAE,CAAC,EAAE;QAC5E,OAAO,CAAC,YAAY,EAAE,cAAc,EAAE,SAAS,EAAE,YAAY,CAAC,CAAC;KAC/D;SAAM;QACN,OAAO,CAAC,YAAY,EAAE,cAAc,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC;KAC5D;AACF,CAAC;AAnBD,8FAmBC;AAED;;;;GAIG;AACH,SAAgB,oBAAoB,CAAC,MAAc;IAClD,OAAO;IACP,MAAM,UAAU,GAAG,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,WAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IACtD,MAAM,OAAO,GAAG,WAAE,CAAC,GAAG,CACrB,uBAAI,EACJ,MAAM,CAAC,GAAG,CAAC,0BAA0B;SACnC,GAAG,CAAC,UAAU,CAAC;SACf,GAAG,CAAC,IAAI,WAAE,CAAC,CAAC,CAAC,CAAC;SACd,GAAG,CAAC,IAAI,WAAE,CAAC,CAAC,CAAC,CAAC,CAChB,CAAC;IACF,OAAO,OAAO,CAAC;AAChB,CAAC;AAXD,oDAWC"}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateInsuranceFundSize = void 0;
|
|
4
|
-
const __1 = require("../");
|
|
5
|
-
/**
|
|
6
|
-
* In the case of a levered loss, the exchange first pays out undistributed fees and then the insurance fund.
|
|
7
|
-
* Thus the de facto size of the insurance fund is the amount in the insurance vault plus the sum of each markets
|
|
8
|
-
* undistributed fees.
|
|
9
|
-
*
|
|
10
|
-
* @param connection
|
|
11
|
-
* @param state
|
|
12
|
-
* @param marketsAccount
|
|
13
|
-
* @returns Precision : QUOTE_ASSET_PRECISION
|
|
14
|
-
*/
|
|
15
|
-
async function calculateInsuranceFundSize(connection, state, marketsAccount) {
|
|
16
|
-
const insuranceVaultPublicKey = state.insuranceVault;
|
|
17
|
-
const insuranceVaultAmount = new __1.BN((await connection.getTokenAccountBalance(insuranceVaultPublicKey)).value.amount);
|
|
18
|
-
return marketsAccount.markets.reduce((insuranceVaultAmount, market) => {
|
|
19
|
-
return insuranceVaultAmount.add(market.amm.totalFee.div(new __1.BN(2)));
|
|
20
|
-
}, insuranceVaultAmount);
|
|
21
|
-
}
|
|
22
|
-
exports.calculateInsuranceFundSize = calculateInsuranceFundSize;
|
|
23
|
-
//# sourceMappingURL=insuranceFund.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"insuranceFund.js","sourceRoot":"","sources":["insuranceFund.ts"],"names":[],"mappings":";;;AACA,2BAAyB;AAGzB;;;;;;;;;GASG;AACI,KAAK,UAAU,0BAA0B,CAC/C,UAAsB,EACtB,KAAmB,EACnB,cAA8B;IAE9B,MAAM,uBAAuB,GAAG,KAAK,CAAC,cAAc,CAAC;IACrD,MAAM,oBAAoB,GAAG,IAAI,MAAE,CAClC,CACC,MAAM,UAAU,CAAC,sBAAsB,CAAC,uBAAuB,CAAC,CAChE,CAAC,KAAK,CAAC,MAAM,CACd,CAAC;IACF,OAAO,cAAc,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,oBAAoB,EAAE,MAAM,EAAE,EAAE;QACrE,OAAO,oBAAoB,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,MAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACrE,CAAC,EAAE,oBAAoB,CAAC,CAAC;AAC1B,CAAC;AAdD,gEAcC"}
|
package/src/math/position.js
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isEmptyPosition = exports.positionCurrentDirection = exports.findDirectionToClose = exports.calculateEntryPrice = exports.calculatePositionFundingPNL = exports.calculatePositionPNL = exports.calculateBaseAssetValue = void 0;
|
|
4
|
-
const __1 = require("../");
|
|
5
|
-
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
|
-
const types_1 = require("../types");
|
|
7
|
-
const amm_1 = require("./amm");
|
|
8
|
-
/**
|
|
9
|
-
* calculateBaseAssetValue
|
|
10
|
-
* = market value of closing entire position
|
|
11
|
-
* @param market
|
|
12
|
-
* @param userPosition
|
|
13
|
-
* @returns Base Asset Value. : Precision QUOTE_PRECISION
|
|
14
|
-
*/
|
|
15
|
-
function calculateBaseAssetValue(market, userPosition) {
|
|
16
|
-
if (userPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
17
|
-
return numericConstants_1.ZERO;
|
|
18
|
-
}
|
|
19
|
-
const directionToClose = findDirectionToClose(userPosition);
|
|
20
|
-
const [newQuoteAssetReserve, _] = (0, amm_1.calculateAmmReservesAfterSwap)(market.amm, 'base', userPosition.baseAssetAmount.abs(), (0, amm_1.getSwapDirection)('base', directionToClose));
|
|
21
|
-
switch (directionToClose) {
|
|
22
|
-
case types_1.PositionDirection.SHORT:
|
|
23
|
-
return market.amm.quoteAssetReserve
|
|
24
|
-
.sub(newQuoteAssetReserve)
|
|
25
|
-
.mul(market.amm.pegMultiplier)
|
|
26
|
-
.div(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
27
|
-
case types_1.PositionDirection.LONG:
|
|
28
|
-
return newQuoteAssetReserve
|
|
29
|
-
.sub(market.amm.quoteAssetReserve)
|
|
30
|
-
.mul(market.amm.pegMultiplier)
|
|
31
|
-
.div(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO)
|
|
32
|
-
.add(numericConstants_1.ONE);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
exports.calculateBaseAssetValue = calculateBaseAssetValue;
|
|
36
|
-
/**
|
|
37
|
-
* calculatePositionPNL
|
|
38
|
-
* = BaseAssetAmount * (Avg Exit Price - Avg Entry Price)
|
|
39
|
-
* @param market
|
|
40
|
-
* @param marketPosition
|
|
41
|
-
* @param withFunding (adds unrealized funding payment pnl to result)
|
|
42
|
-
* @returns BaseAssetAmount : Precision QUOTE_PRECISION
|
|
43
|
-
*/
|
|
44
|
-
function calculatePositionPNL(market, marketPosition, withFunding = false) {
|
|
45
|
-
if (marketPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
46
|
-
return numericConstants_1.ZERO;
|
|
47
|
-
}
|
|
48
|
-
const baseAssetValue = calculateBaseAssetValue(market, marketPosition);
|
|
49
|
-
let pnl;
|
|
50
|
-
if (marketPosition.baseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
51
|
-
pnl = baseAssetValue.sub(marketPosition.quoteAssetAmount);
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
pnl = marketPosition.quoteAssetAmount.sub(baseAssetValue);
|
|
55
|
-
}
|
|
56
|
-
if (withFunding) {
|
|
57
|
-
const fundingRatePnL = calculatePositionFundingPNL(market, marketPosition).div(numericConstants_1.PRICE_TO_QUOTE_PRECISION);
|
|
58
|
-
pnl = pnl.add(fundingRatePnL);
|
|
59
|
-
}
|
|
60
|
-
return pnl;
|
|
61
|
-
}
|
|
62
|
-
exports.calculatePositionPNL = calculatePositionPNL;
|
|
63
|
-
/**
|
|
64
|
-
*
|
|
65
|
-
* @param market
|
|
66
|
-
* @param marketPosition
|
|
67
|
-
* @returns // TODO-PRECISION
|
|
68
|
-
*/
|
|
69
|
-
function calculatePositionFundingPNL(market, marketPosition) {
|
|
70
|
-
if (marketPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
71
|
-
return numericConstants_1.ZERO;
|
|
72
|
-
}
|
|
73
|
-
let ammCumulativeFundingRate;
|
|
74
|
-
if (marketPosition.baseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
75
|
-
ammCumulativeFundingRate = market.amm.cumulativeFundingRateLong;
|
|
76
|
-
}
|
|
77
|
-
else {
|
|
78
|
-
ammCumulativeFundingRate = market.amm.cumulativeFundingRateShort;
|
|
79
|
-
}
|
|
80
|
-
const perPositionFundingRate = ammCumulativeFundingRate
|
|
81
|
-
.sub(marketPosition.lastCumulativeFundingRate)
|
|
82
|
-
.mul(marketPosition.baseAssetAmount)
|
|
83
|
-
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
84
|
-
.div(numericConstants_1.FUNDING_PAYMENT_PRECISION)
|
|
85
|
-
.mul(new __1.BN(-1));
|
|
86
|
-
return perPositionFundingRate;
|
|
87
|
-
}
|
|
88
|
-
exports.calculatePositionFundingPNL = calculatePositionFundingPNL;
|
|
89
|
-
/**
|
|
90
|
-
*
|
|
91
|
-
* @param userPosition
|
|
92
|
-
* @returns Precision: MARK_PRICE_PRECISION (10^10)
|
|
93
|
-
*/
|
|
94
|
-
function calculateEntryPrice(userPosition) {
|
|
95
|
-
if (userPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
96
|
-
return numericConstants_1.ZERO;
|
|
97
|
-
}
|
|
98
|
-
return userPosition.quoteAssetAmount
|
|
99
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
100
|
-
.mul(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
|
|
101
|
-
.div(userPosition.baseAssetAmount)
|
|
102
|
-
.abs();
|
|
103
|
-
}
|
|
104
|
-
exports.calculateEntryPrice = calculateEntryPrice;
|
|
105
|
-
function findDirectionToClose(userPosition) {
|
|
106
|
-
return userPosition.baseAssetAmount.gt(numericConstants_1.ZERO)
|
|
107
|
-
? types_1.PositionDirection.SHORT
|
|
108
|
-
: types_1.PositionDirection.LONG;
|
|
109
|
-
}
|
|
110
|
-
exports.findDirectionToClose = findDirectionToClose;
|
|
111
|
-
function positionCurrentDirection(userPosition) {
|
|
112
|
-
return userPosition.baseAssetAmount.gte(numericConstants_1.ZERO)
|
|
113
|
-
? types_1.PositionDirection.LONG
|
|
114
|
-
: types_1.PositionDirection.SHORT;
|
|
115
|
-
}
|
|
116
|
-
exports.positionCurrentDirection = positionCurrentDirection;
|
|
117
|
-
function isEmptyPosition(userPosition) {
|
|
118
|
-
return (userPosition.baseAssetAmount.eq(numericConstants_1.ZERO) && userPosition.openOrders.eq(numericConstants_1.ZERO));
|
|
119
|
-
}
|
|
120
|
-
exports.isEmptyPosition = isEmptyPosition;
|
|
121
|
-
//# sourceMappingURL=position.js.map
|
package/src/math/position.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"position.js","sourceRoot":"","sources":["position.ts"],"names":[],"mappings":";;;AAAA,2BAAyB;AACzB,oEASuC;AACvC,oCAAmE;AACnE,+BAAwE;AAExE;;;;;;GAMG;AACH,SAAgB,uBAAuB,CACtC,MAAc,EACd,YAA0B;IAE1B,IAAI,YAAY,CAAC,eAAe,CAAC,EAAE,CAAC,uBAAI,CAAC,EAAE;QAC1C,OAAO,uBAAI,CAAC;KACZ;IAED,MAAM,gBAAgB,GAAG,oBAAoB,CAAC,YAAY,CAAC,CAAC;IAE5D,MAAM,CAAC,oBAAoB,EAAE,CAAC,CAAC,GAAG,IAAA,mCAA6B,EAC9D,MAAM,CAAC,GAAG,EACV,MAAM,EACN,YAAY,CAAC,eAAe,CAAC,GAAG,EAAE,EAClC,IAAA,sBAAgB,EAAC,MAAM,EAAE,gBAAgB,CAAC,CAC1C,CAAC;IAEF,QAAQ,gBAAgB,EAAE;QACzB,KAAK,yBAAiB,CAAC,KAAK;YAC3B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B,CACvC,YAA0B;IAE1B,OAAO,YAAY,CAAC,eAAe,CAAC,GAAG,CAAC,uBAAI,CAAC;QAC5C,CAAC,CAAC,yBAAiB,CAAC,IAAI;QACxB,CAAC,CAAC,yBAAiB,CAAC,KAAK,CAAC;AAC5B,CAAC;AAND,4DAMC;AAED,SAAgB,eAAe,CAAC,YAA0B;IACzD,OAAO,CACN,YAAY,CAAC,eAAe,CAAC,EAAE,CAAC,uBAAI,CAAC,IAAI,YAAY,CAAC,UAAU,CAAC,EAAE,CAAC,uBAAI,CAAC,CACzE,CAAC;AACH,CAAC;AAJD,0CAIC"}
|
package/src/math/utils.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.squareRootBN = void 0;
|
|
4
|
-
const __1 = require("../");
|
|
5
|
-
const squareRootBN = (n, closeness = new __1.BN(1)) => {
|
|
6
|
-
// Assuming the sqrt of n as n only
|
|
7
|
-
let x = n;
|
|
8
|
-
// The closed guess will be stored in the root
|
|
9
|
-
let root;
|
|
10
|
-
// To count the number of iterations
|
|
11
|
-
let count = 0;
|
|
12
|
-
const TWO = new __1.BN(2);
|
|
13
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
14
|
-
while (count < Number.MAX_SAFE_INTEGER) {
|
|
15
|
-
count++;
|
|
16
|
-
// Calculate more closed x
|
|
17
|
-
root = x.add(n.div(x)).div(TWO);
|
|
18
|
-
// Check for closeness
|
|
19
|
-
if (x.sub(root).abs().lte(closeness))
|
|
20
|
-
break;
|
|
21
|
-
// Update root
|
|
22
|
-
x = root;
|
|
23
|
-
}
|
|
24
|
-
return root;
|
|
25
|
-
};
|
|
26
|
-
exports.squareRootBN = squareRootBN;
|
|
27
|
-
//# sourceMappingURL=utils.js.map
|
package/src/math/utils.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["utils.ts"],"names":[],"mappings":";;;AAAA,2BAAyB;AAElB,MAAM,YAAY,GAAG,CAAC,CAAC,EAAE,SAAS,GAAG,IAAI,MAAE,CAAC,CAAC,CAAC,EAAE,EAAE;IACxD,mCAAmC;IACnC,IAAI,CAAC,GAAG,CAAC,CAAC;IAEV,8CAA8C;IAC9C,IAAI,IAAI,CAAC;IAET,oCAAoC;IACpC,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,MAAM,GAAG,GAAG,IAAI,MAAE,CAAC,CAAC,CAAC,CAAC;IAEtB,6DAA6D;IAC7D,OAAO,KAAK,GAAG,MAAM,CAAC,gBAAgB,EAAE;QACvC,KAAK,EAAE,CAAC;QAER,0BAA0B;QAC1B,IAAI,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAEhC,sBAAsB;QACtB,IAAI,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,SAAS,CAAC;YAAE,MAAM;QAE5C,cAAc;QACd,CAAC,GAAG,IAAI,CAAC;KACT;IAED,OAAO,IAAI,CAAC;AACb,CAAC,CAAC;AA1BW,QAAA,YAAY,gBA0BvB"}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.SwitchboardClient = void 0;
|
|
7
|
-
const switchboard_v2_1 = require("@switchboard-xyz/switchboard-v2");
|
|
8
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
9
|
-
const anchor_1 = require("@project-serum/anchor");
|
|
10
|
-
const numericConstants_1 = require("../constants/numericConstants");
|
|
11
|
-
const wallet_1 = require("../wallet");
|
|
12
|
-
const switchboard_v2_json_1 = __importDefault(require("../idl/switchboard_v2.json"));
|
|
13
|
-
// cache switchboard program for every client object since itll always be the same
|
|
14
|
-
const programMap = new Map();
|
|
15
|
-
class SwitchboardClient {
|
|
16
|
-
constructor(connection, env) {
|
|
17
|
-
this.connection = connection;
|
|
18
|
-
this.env = env;
|
|
19
|
-
}
|
|
20
|
-
async getOraclePriceData(pricePublicKey) {
|
|
21
|
-
const accountInfo = await this.connection.getAccountInfo(pricePublicKey);
|
|
22
|
-
return this.getOraclePriceDataFromBuffer(accountInfo.data);
|
|
23
|
-
}
|
|
24
|
-
async getOraclePriceDataFromBuffer(buffer) {
|
|
25
|
-
const program = await this.getProgram();
|
|
26
|
-
const aggregatorAccountData = program.account.aggregatorAccountData.coder.accounts.decode('AggregatorAccountData', buffer);
|
|
27
|
-
const price = convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound.result);
|
|
28
|
-
const confidence = convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound
|
|
29
|
-
.stdDeviation);
|
|
30
|
-
const slot = aggregatorAccountData.latestConfirmedRound.roundOpenSlot;
|
|
31
|
-
return {
|
|
32
|
-
price,
|
|
33
|
-
slot,
|
|
34
|
-
confidence,
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
async getProgram() {
|
|
38
|
-
if (programMap.has(this.env)) {
|
|
39
|
-
return programMap.get(this.env);
|
|
40
|
-
}
|
|
41
|
-
const program = await getSwitchboardProgram(this.env, this.connection);
|
|
42
|
-
programMap.set(this.env, program);
|
|
43
|
-
return program;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
exports.SwitchboardClient = SwitchboardClient;
|
|
47
|
-
async function getSwitchboardProgram(env, connection) {
|
|
48
|
-
const DEFAULT_KEYPAIR = web3_js_1.Keypair.fromSeed(new Uint8Array(32).fill(1));
|
|
49
|
-
const programId = (0, switchboard_v2_1.getSwitchboardPid)(env);
|
|
50
|
-
const wallet = new wallet_1.Wallet(DEFAULT_KEYPAIR);
|
|
51
|
-
const provider = new anchor_1.Provider(connection, wallet, {});
|
|
52
|
-
return new anchor_1.Program(switchboard_v2_json_1.default, programId, provider);
|
|
53
|
-
}
|
|
54
|
-
function convertSwitchboardDecimal(switchboardDecimal) {
|
|
55
|
-
const switchboardPrecision = numericConstants_1.TEN.pow(new anchor_1.BN(switchboardDecimal.scale));
|
|
56
|
-
return switchboardDecimal.mantissa
|
|
57
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
58
|
-
.div(switchboardPrecision);
|
|
59
|
-
}
|
|
60
|
-
//# sourceMappingURL=switchboardClient.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"switchboardClient.js","sourceRoot":"","sources":["switchboardClient.ts"],"names":[],"mappings":";;;;;;AAAA,oEAGyC;AACzC,6CAAiE;AAEjE,kDAAmE;AACnE,oEAA0E;AAE1E,sCAAmC;AACnC,qFAA0D;AAE1D,kFAAkF;AAClF,MAAM,UAAU,GAAG,IAAI,GAAG,EAAmB,CAAC;AAE9C,MAAa,iBAAiB;IAI7B,YAAmB,UAAsB,EAAE,GAAa;QACvD,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IAChB,CAAC;IAEM,KAAK,CAAC,kBAAkB,CAC9B,cAAyB;QAEzB,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC,cAAc,CAAC,CAAC;QACzE,OAAO,IAAI,CAAC,4BAA4B,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IAC5D,CAAC;IAEM,KAAK,CAAC,4BAA4B,CACxC,MAAc;QAEd,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,UAAU,EAAE,CAAC;QAExC,MAAM,qBAAqB,GAC1B,OAAO,CAAC,OAAO,CAAC,qBAAqB,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,CAC1D,uBAAuB,EACvB,MAAM,C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B,GAAG,sBAAG,CAAC,GAAG,CAAC,IAAI,WAAE,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC,CAAC;IACvE,OAAO,kBAAkB,CAAC,QAAQ;SAChC,GAAG,CAAC,uCAAoB,CAAC;SACzB,GAAG,CAAC,oBAAoB,CAAC,CAAC;AAC7B,CAAC"}
|
package/src/token/index.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseTokenAccount = void 0;
|
|
4
|
-
const spl_token_1 = require("@solana/spl-token");
|
|
5
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
-
function parseTokenAccount(data) {
|
|
7
|
-
const accountInfo = spl_token_1.AccountLayout.decode(data);
|
|
8
|
-
accountInfo.mint = new web3_js_1.PublicKey(accountInfo.mint);
|
|
9
|
-
accountInfo.owner = new web3_js_1.PublicKey(accountInfo.owner);
|
|
10
|
-
accountInfo.amount = spl_token_1.u64.fromBuffer(accountInfo.amount);
|
|
11
|
-
if (accountInfo.delegateOption === 0) {
|
|
12
|
-
accountInfo.delegate = null;
|
|
13
|
-
// eslint-disable-next-line new-cap
|
|
14
|
-
accountInfo.delegatedAmount = new spl_token_1.u64(0);
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
accountInfo.delegate = new web3_js_1.PublicKey(accountInfo.delegate);
|
|
18
|
-
accountInfo.delegatedAmount = spl_token_1.u64.fromBuffer(accountInfo.delegatedAmount);
|
|
19
|
-
}
|
|
20
|
-
accountInfo.isInitialized = accountInfo.state !== 0;
|
|
21
|
-
accountInfo.isFrozen = accountInfo.state === 2;
|
|
22
|
-
if (accountInfo.isNativeOption === 1) {
|
|
23
|
-
accountInfo.rentExemptReserve = spl_token_1.u64.fromBuffer(accountInfo.isNative);
|
|
24
|
-
accountInfo.isNative = true;
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
accountInfo.rentExemptReserve = null;
|
|
28
|
-
accountInfo.isNative = false;
|
|
29
|
-
}
|
|
30
|
-
if (accountInfo.closeAuthorityOption === 0) {
|
|
31
|
-
accountInfo.closeAuthority = null;
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
accountInfo.closeAuthority = new web3_js_1.PublicKey(accountInfo.closeAuthority);
|
|
35
|
-
}
|
|
36
|
-
return accountInfo;
|
|
37
|
-
}
|
|
38
|
-
exports.parseTokenAccount = parseTokenAccount;
|
|
39
|
-
//# sourceMappingURL=index.js.map
|
package/src/token/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;AAAA,iDAAoE;AACpE,6CAA4C;AAE5C,SAAgB,iBAAiB,CAAC,IAAY;IAC7C,MAAM,WAAW,GAAG,yBAAa,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC/C,WAAW,CAAC,IAAI,GAAG,IAAI,mBAAS,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IACnD,WAAW,CAAC,KAAK,GAAG,IAAI,mBAAS,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;IACrD,WAAW,CAAC,MAAM,GAAG,eAAG,CAAC,UAAU,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;IAExD,IAAI,WAAW,CAAC,cAAc,KAAK,CAAC,EAAE;QACrC,WAAW,CAAC,QAAQ,GAAG,IAAI,CAAC;QAC5B,mCAAmC;QACnC,WAAW,CAAC,eAAe,GAAG,IAAI,eAAG,CAAC,CAAC,CAAC,CAAC;KACzC;SAAM;QACN,WAAW,CAAC,QAAQ,GAAG,IAAI,mBAAS,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;QAC3D,WAAW,CAAC,eAAe,GAAG,eAAG,CAAC,UAAU,CAAC,WAAW,CAAC,eAAe,CAAC,CAAC;KAC1E;IAED,WAAW,CAAC,aAAa,GAAG,WAAW,CAAC,KAAK,KAAK,CAAC,CAAC;IACpD,WAAW,CAAC,QAAQ,GAAG,WAAW,CAAC,KAAK,KAAK,CAAC,CAAC;IAE/C,IAAI,WAAW,CAAC,cAAc,KAAK,CAAC,EAAE;QACrC,WAAW,CAAC,iBAAiB,GAAG,eAAG,CAAC,UAAU,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;QACrE,WAAW,CAAC,QAAQ,GAAG,IAAI,CAAC;KAC5B;SAAM;QACN,WAAW,CAAC,iBAAiB,GAAG,IAAI,CAAC;QACrC,WAAW,CAAC,QAAQ,GAAG,KAAK,CAAC;KAC7B;IAED,IAAI,WAAW,CAAC,oBAAoB,KAAK,CAAC,EAAE;QAC3C,WAAW,CAAC,cAAc,GAAG,IAAI,CAAC;KAClC;SAAM;QACN,WAAW,CAAC,cAAc,GAAG,IAAI,mBAAS,CAAC,WAAW,CAAC,cAAc,CAAC,CAAC;KACvE;IAED,OAAO,WAAW,CAAC;AACpB,CAAC;AAjCD,8CAiCC"}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DefaultTxSender = void 0;
|
|
4
|
-
class DefaultTxSender {
|
|
5
|
-
constructor(provider) {
|
|
6
|
-
this.provider = provider;
|
|
7
|
-
}
|
|
8
|
-
send(tx, additionalSigners, opts) {
|
|
9
|
-
return this.provider.send(tx, additionalSigners, opts);
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
exports.DefaultTxSender = DefaultTxSender;
|
|
13
|
-
//# sourceMappingURL=defaultTxSender.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"defaultTxSender.js","sourceRoot":"","sources":["defaultTxSender.ts"],"names":[],"mappings":";;;AASA,MAAa,eAAe;IAG3B,YAAmB,QAAkB;QACpC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC1B,CAAC;IAED,IAAI,CACH,EAAe,EACf,iBAAiC,EACjC,IAAqB;QAErB,OAAO,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,EAAE,iBAAiB,EAAE,IAAI,CAAC,CAAC;IACxD,CAAC;CACD;AAdD,0CAcC"}
|
package/src/tx/types.js
DELETED
package/src/tx/types.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["types.ts"],"names":[],"mappings":""}
|
package/src/tx/utils.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.wrapInTx = void 0;
|
|
4
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
5
|
-
function wrapInTx(instruction) {
|
|
6
|
-
return new web3_js_1.Transaction().add(instruction);
|
|
7
|
-
}
|
|
8
|
-
exports.wrapInTx = wrapInTx;
|
|
9
|
-
//# sourceMappingURL=utils.js.map
|
package/src/tx/utils.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["utils.ts"],"names":[],"mappings":";;;AAAA,6CAAsE;AAEtE,SAAgB,QAAQ,CAAC,WAAmC;IAC3D,OAAO,IAAI,qBAAW,EAAE,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;AAC3C,CAAC;AAFD,4BAEC"}
|
package/src/util/computeUnits.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.findComputeUnitConsumption = void 0;
|
|
4
|
-
async function findComputeUnitConsumption(programId, connection, txSignature, commitment = 'confirmed') {
|
|
5
|
-
const tx = await connection.getTransaction(txSignature, { commitment });
|
|
6
|
-
const computeUnits = [];
|
|
7
|
-
const regex = new RegExp(`Program ${programId.toString()} consumed ([0-9]{0,6}) of 200000 compute units`);
|
|
8
|
-
tx.meta.logMessages.forEach((logMessage) => {
|
|
9
|
-
const match = logMessage.match(regex);
|
|
10
|
-
if (match && match[1]) {
|
|
11
|
-
computeUnits.push(match[1]);
|
|
12
|
-
}
|
|
13
|
-
});
|
|
14
|
-
return computeUnits;
|
|
15
|
-
}
|
|
16
|
-
exports.findComputeUnitConsumption = findComputeUnitConsumption;
|
|
17
|
-
//# sourceMappingURL=computeUnits.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"computeUnits.js","sourceRoot":"","sources":["computeUnits.ts"],"names":[],"mappings":";;;AAEO,KAAK,UAAU,0BAA0B,CAC/C,SAAoB,EACpB,UAAsB,EACtB,WAAmB,EACnB,aAAuB,WAAW;IAElC,MAAM,EAAE,GAAG,MAAM,UAAU,CAAC,cAAc,CAAC,WAAW,EAAE,EAAE,UAAU,EAAE,CAAC,CAAC;IACxE,MAAM,YAAY,GAAG,EAAE,CAAC;IACxB,MAAM,KAAK,GAAG,IAAI,MAAM,CACvB,WAAW,SAAS,CAAC,QAAQ,EAAE,gDAAgD,CAC/E,CAAC;IACF,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE;QAC1C,MAAM,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACtC,IAAI,KAAK,IAAI,KAAK,CAAC,CAAC,CAAC,EAAE;YACtB,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;SAC5B;IACF,CAAC,CAAC,CAAC;IACH,OAAO,YAAY,CAAC;AACrB,CAAC;AAlBD,gEAkBC"}
|
package/src/util/tps.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.estimateTps = void 0;
|
|
4
|
-
async function estimateTps(programId, connection, failed) {
|
|
5
|
-
let signatures = await connection.getSignaturesForAddress(programId, undefined, 'finalized');
|
|
6
|
-
if (failed) {
|
|
7
|
-
signatures = signatures.filter((signature) => signature.err);
|
|
8
|
-
}
|
|
9
|
-
const numberOfSignatures = signatures.length;
|
|
10
|
-
if (numberOfSignatures === 0) {
|
|
11
|
-
return 0;
|
|
12
|
-
}
|
|
13
|
-
return (numberOfSignatures /
|
|
14
|
-
(signatures[0].blockTime - signatures[numberOfSignatures - 1].blockTime));
|
|
15
|
-
}
|
|
16
|
-
exports.estimateTps = estimateTps;
|
|
17
|
-
//# sourceMappingURL=tps.js.map
|
package/src/util/tps.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"tps.js","sourceRoot":"","sources":["tps.ts"],"names":[],"mappings":";;;AAEO,KAAK,UAAU,WAAW,CAChC,SAAoB,EACpB,UAAsB,EACtB,MAAe;IAEf,IAAI,UAAU,GAAG,MAAM,UAAU,CAAC,uBAAuB,CACxD,SAAS,EACT,SAAS,EACT,WAAW,CACX,CAAC;IACF,IAAI,MAAM,EAAE;QACX,UAAU,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;KAC7D;IAED,MAAM,kBAAkB,GAAG,UAAU,CAAC,MAAM,CAAC;IAE7C,IAAI,kBAAkB,KAAK,CAAC,EAAE;QAC7B,OAAO,CAAC,CAAC;KACT;IAED,OAAO,CACN,kBAAkB;QAClB,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,GAAG,UAAU,CAAC,kBAAkB,GAAG,CAAC,CAAC,CAAC,SAAS,CAAC,CACxE,CAAC;AACH,CAAC;AAxBD,kCAwBC"}
|