@drift-labs/sdk 0.2.0-master.14 → 0.2.0-master.17
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/bulkUserStatsSubscription.d.ts +7 -0
- package/lib/accounts/bulkUserStatsSubscription.js +21 -0
- package/lib/accounts/bulkUserSubscription.js +0 -1
- package/lib/accounts/types.d.ts +0 -1
- package/lib/factory/bigNum.js +7 -9
- package/lib/index.d.ts +5 -0
- package/lib/index.js +5 -0
- package/lib/math/bankBalance.js +2 -2
- package/lib/types.d.ts +4 -1
- package/package.json +1 -1
- package/src/accounts/bulkUserStatsSubscription.ts +33 -0
- package/src/accounts/bulkUserSubscription.ts +0 -1
- package/src/factory/bigNum.ts +6 -7
- package/src/index.ts +5 -0
- package/src/math/bankBalance.ts +2 -2
- package/src/types.ts +4 -1
- package/tests/bn/test.ts +8 -0
- 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/constants/numericConstants.js +0 -41
- package/src/events/eventList.js +0 -77
- package/src/events/eventSubscriber.js +0 -139
- package/src/events/fetchLogs.js +0 -50
- package/src/events/pollingLogProvider.js +0 -64
- package/src/events/sort.js +0 -44
- package/src/events/txEventCache.js +0 -71
- package/src/events/types.js +0 -20
- package/src/events/webSocketLogProvider.js +0 -41
- package/src/examples/makeTradeExample.js +0 -80
- package/src/factory/bigNum.js +0 -390
- package/src/factory/oracleClient.js +0 -20
- package/src/math/amm.js +0 -369
- package/src/math/auction.js +0 -42
- package/src/math/conversion.js +0 -11
- package/src/math/funding.js +0 -248
- package/src/math/oracles.js +0 -26
- package/src/math/repeg.js +0 -128
- package/src/math/state.js +0 -15
- 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/orderParams.js +0 -20
- package/src/slot/SlotSubscriber.js +0 -39
- package/src/token/index.js +0 -38
- package/src/tokenFaucet.js +0 -189
- package/src/tx/types.js +0 -2
- package/src/tx/utils.js +0 -17
- package/src/types.js +0 -125
- 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,139 +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.EventSubscriber = void 0;
|
|
13
|
-
const types_1 = require("./types");
|
|
14
|
-
const txEventCache_1 = require("./txEventCache");
|
|
15
|
-
const eventList_1 = require("./eventList");
|
|
16
|
-
const pollingLogProvider_1 = require("./pollingLogProvider");
|
|
17
|
-
const fetchLogs_1 = require("./fetchLogs");
|
|
18
|
-
const webSocketLogProvider_1 = require("./webSocketLogProvider");
|
|
19
|
-
const events_1 = require("events");
|
|
20
|
-
const sort_1 = require("./sort");
|
|
21
|
-
class EventSubscriber {
|
|
22
|
-
constructor(connection, program, options = types_1.DefaultEventSubscriptionOptions) {
|
|
23
|
-
this.connection = connection;
|
|
24
|
-
this.program = program;
|
|
25
|
-
this.options = options;
|
|
26
|
-
this.awaitTxPromises = new Map();
|
|
27
|
-
this.awaitTxResolver = new Map();
|
|
28
|
-
this.options = Object.assign({}, types_1.DefaultEventSubscriptionOptions, options);
|
|
29
|
-
this.txEventCache = new txEventCache_1.TxEventCache(this.options.maxTx);
|
|
30
|
-
this.eventListMap = new Map();
|
|
31
|
-
for (const eventType of this.options.eventTypes) {
|
|
32
|
-
this.eventListMap.set(eventType, new eventList_1.EventList(eventType, this.options.maxEventsPerType, sort_1.getSortFn(this.options.orderBy, this.options.orderDir, eventType), this.options.orderDir));
|
|
33
|
-
}
|
|
34
|
-
this.eventEmitter = new events_1.EventEmitter();
|
|
35
|
-
if (this.options.logProviderConfig.type === 'websocket') {
|
|
36
|
-
this.logProvider = new webSocketLogProvider_1.WebSocketLogProvider(this.connection, this.program.programId, this.options.commitment);
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
this.logProvider = new pollingLogProvider_1.PollingLogProvider(this.connection, this.program.programId, options.commitment, this.options.logProviderConfig.frequency);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
subscribe() {
|
|
43
|
-
if (this.logProvider.isSubscribed()) {
|
|
44
|
-
return true;
|
|
45
|
-
}
|
|
46
|
-
this.fetchPreviousTx().catch((e) => {
|
|
47
|
-
console.error('Error fetching previous txs in event subscriber');
|
|
48
|
-
console.error(e);
|
|
49
|
-
});
|
|
50
|
-
return this.logProvider.subscribe((txSig, slot, logs) => {
|
|
51
|
-
this.handleTxLogs(txSig, slot, logs);
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
handleTxLogs(txSig, slot, logs) {
|
|
55
|
-
if (this.txEventCache.has(txSig)) {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
const wrappedEvents = this.parseEventsFromLogs(txSig, slot, logs);
|
|
59
|
-
for (const wrappedEvent of wrappedEvents) {
|
|
60
|
-
this.eventListMap.get(wrappedEvent.eventType).insert(wrappedEvent);
|
|
61
|
-
this.eventEmitter.emit('newEvent', wrappedEvent);
|
|
62
|
-
}
|
|
63
|
-
if (this.awaitTxPromises.has(txSig)) {
|
|
64
|
-
this.awaitTxPromises.delete(txSig);
|
|
65
|
-
this.awaitTxResolver.get(txSig)();
|
|
66
|
-
this.awaitTxResolver.delete(txSig);
|
|
67
|
-
}
|
|
68
|
-
this.txEventCache.add(txSig, wrappedEvents);
|
|
69
|
-
}
|
|
70
|
-
fetchPreviousTx() {
|
|
71
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
72
|
-
if (!this.options.untilTx) {
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
let txFetched = 0;
|
|
76
|
-
let beforeTx = undefined;
|
|
77
|
-
const untilTx = this.options.untilTx;
|
|
78
|
-
while (txFetched < this.options.maxTx) {
|
|
79
|
-
const response = yield fetchLogs_1.fetchLogs(this.connection, this.program.programId, this.options.commitment === 'finalized' ? 'finalized' : 'confirmed', beforeTx, untilTx);
|
|
80
|
-
if (response === undefined) {
|
|
81
|
-
break;
|
|
82
|
-
}
|
|
83
|
-
txFetched += response.transactionLogs.length;
|
|
84
|
-
beforeTx = response.earliestTx;
|
|
85
|
-
for (const { txSig, slot, logs } of response.transactionLogs) {
|
|
86
|
-
this.handleTxLogs(txSig, slot, logs);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
unsubscribe() {
|
|
92
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
93
|
-
return yield this.logProvider.unsubscribe();
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
parseEventsFromLogs(txSig, slot, logs) {
|
|
97
|
-
const records = [];
|
|
98
|
-
// @ts-ignore
|
|
99
|
-
this.program._events._eventParser.parseLogs(logs, (event) => {
|
|
100
|
-
const expectRecordType = this.eventListMap.has(event.name);
|
|
101
|
-
if (expectRecordType) {
|
|
102
|
-
event.data.txSig = txSig;
|
|
103
|
-
event.data.slot = slot;
|
|
104
|
-
event.data.eventType = event.name;
|
|
105
|
-
records.push(event.data);
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
return records;
|
|
109
|
-
}
|
|
110
|
-
awaitTx(txSig) {
|
|
111
|
-
if (this.awaitTxPromises.has(txSig)) {
|
|
112
|
-
return this.awaitTxPromises.get(txSig);
|
|
113
|
-
}
|
|
114
|
-
if (this.txEventCache.has(txSig)) {
|
|
115
|
-
return Promise.resolve();
|
|
116
|
-
}
|
|
117
|
-
const promise = new Promise((resolve) => {
|
|
118
|
-
this.awaitTxResolver.set(txSig, resolve);
|
|
119
|
-
});
|
|
120
|
-
this.awaitTxPromises.set(txSig, promise);
|
|
121
|
-
return promise;
|
|
122
|
-
}
|
|
123
|
-
getEventList(eventType) {
|
|
124
|
-
return this.eventListMap.get(eventType);
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* This requires the EventList be cast to an array, which requires reallocation of memory.
|
|
128
|
-
* Would bias to using getEventList over getEvents
|
|
129
|
-
*
|
|
130
|
-
* @param eventType
|
|
131
|
-
*/
|
|
132
|
-
getEventsArray(eventType) {
|
|
133
|
-
return this.eventListMap.get(eventType).toArray();
|
|
134
|
-
}
|
|
135
|
-
getEventsByTx(txSig) {
|
|
136
|
-
return this.txEventCache.get(txSig);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
exports.EventSubscriber = EventSubscriber;
|
package/src/events/fetchLogs.js
DELETED
|
@@ -1,50 +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.fetchLogs = void 0;
|
|
13
|
-
function fetchLogs(connection, programId, finality, beforeTx, untilTx) {
|
|
14
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
-
const signatures = yield connection.getSignaturesForAddress(programId, {
|
|
16
|
-
before: beforeTx,
|
|
17
|
-
until: untilTx,
|
|
18
|
-
}, finality);
|
|
19
|
-
const sortedSignatures = signatures.sort((a, b) => a.slot < b.slot ? -1 : 1);
|
|
20
|
-
const filteredSignatures = sortedSignatures.filter((signature) => !signature.err);
|
|
21
|
-
if (filteredSignatures.length === 0) {
|
|
22
|
-
return undefined;
|
|
23
|
-
}
|
|
24
|
-
const chunkedSignatures = chunk(filteredSignatures, 100);
|
|
25
|
-
const transactionLogs = (yield Promise.all(chunkedSignatures.map((chunk) => __awaiter(this, void 0, void 0, function* () {
|
|
26
|
-
const transactions = yield connection.getTransactions(chunk.map((confirmedSignature) => confirmedSignature.signature), finality);
|
|
27
|
-
return transactions.map((transaction) => {
|
|
28
|
-
return {
|
|
29
|
-
txSig: transaction.transaction.signatures[0],
|
|
30
|
-
slot: transaction.slot,
|
|
31
|
-
logs: transaction.meta.logMessages,
|
|
32
|
-
};
|
|
33
|
-
});
|
|
34
|
-
})))).flat();
|
|
35
|
-
const earliestTx = filteredSignatures[0].signature;
|
|
36
|
-
const mostRecentTx = filteredSignatures[filteredSignatures.length - 1].signature;
|
|
37
|
-
return {
|
|
38
|
-
transactionLogs: transactionLogs,
|
|
39
|
-
earliestTx: earliestTx,
|
|
40
|
-
mostRecentTx: mostRecentTx,
|
|
41
|
-
};
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
exports.fetchLogs = fetchLogs;
|
|
45
|
-
function chunk(array, size) {
|
|
46
|
-
return new Array(Math.ceil(array.length / size))
|
|
47
|
-
.fill(null)
|
|
48
|
-
.map((_, index) => index * size)
|
|
49
|
-
.map((begin) => array.slice(begin, begin + size));
|
|
50
|
-
}
|
|
@@ -1,64 +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.PollingLogProvider = void 0;
|
|
13
|
-
const fetchLogs_1 = require("./fetchLogs");
|
|
14
|
-
class PollingLogProvider {
|
|
15
|
-
constructor(connection, programId, commitment, frequency = 15 * 1000) {
|
|
16
|
-
this.connection = connection;
|
|
17
|
-
this.programId = programId;
|
|
18
|
-
this.frequency = frequency;
|
|
19
|
-
this.finality = commitment === 'finalized' ? 'finalized' : 'confirmed';
|
|
20
|
-
}
|
|
21
|
-
subscribe(callback) {
|
|
22
|
-
if (this.intervalId) {
|
|
23
|
-
return true;
|
|
24
|
-
}
|
|
25
|
-
this.intervalId = setInterval(() => __awaiter(this, void 0, void 0, function* () {
|
|
26
|
-
if (this.mutex === 1) {
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
this.mutex = 1;
|
|
30
|
-
try {
|
|
31
|
-
const response = yield fetchLogs_1.fetchLogs(this.connection, this.programId, this.finality, undefined, this.mostRecentSeenTx);
|
|
32
|
-
if (response === undefined) {
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
const { mostRecentTx, transactionLogs } = response;
|
|
36
|
-
for (const { txSig, slot, logs } of transactionLogs) {
|
|
37
|
-
callback(txSig, slot, logs);
|
|
38
|
-
}
|
|
39
|
-
this.mostRecentSeenTx = mostRecentTx;
|
|
40
|
-
}
|
|
41
|
-
catch (e) {
|
|
42
|
-
console.error('PollingLogProvider threw an Error');
|
|
43
|
-
console.error(e);
|
|
44
|
-
}
|
|
45
|
-
finally {
|
|
46
|
-
this.mutex = 0;
|
|
47
|
-
}
|
|
48
|
-
}), this.frequency);
|
|
49
|
-
return true;
|
|
50
|
-
}
|
|
51
|
-
isSubscribed() {
|
|
52
|
-
return this.intervalId !== undefined;
|
|
53
|
-
}
|
|
54
|
-
unsubscribe() {
|
|
55
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
-
if (this.intervalId !== undefined) {
|
|
57
|
-
clearInterval(this.intervalId);
|
|
58
|
-
this.intervalId = undefined;
|
|
59
|
-
}
|
|
60
|
-
return true;
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
exports.PollingLogProvider = PollingLogProvider;
|
package/src/events/sort.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getSortFn = void 0;
|
|
4
|
-
const index_1 = require("../index");
|
|
5
|
-
function clientSortAscFn() {
|
|
6
|
-
return 'less than';
|
|
7
|
-
}
|
|
8
|
-
function clientSortDescFn() {
|
|
9
|
-
return 'greater than';
|
|
10
|
-
}
|
|
11
|
-
function defaultBlockchainSortFn(currentEvent, newEvent) {
|
|
12
|
-
return currentEvent.slot <= newEvent.slot ? 'less than' : 'greater than';
|
|
13
|
-
}
|
|
14
|
-
function orderRecordSortFn(currentEvent, newEvent) {
|
|
15
|
-
const currentEventMarketIndex = !currentEvent.maker.equals(index_1.PublicKey.default)
|
|
16
|
-
? currentEvent.makerOrder.marketIndex
|
|
17
|
-
: currentEvent.takerOrder.marketIndex;
|
|
18
|
-
const newEventMarketIndex = !newEvent.maker.equals(index_1.PublicKey.default)
|
|
19
|
-
? newEvent.makerOrder.marketIndex
|
|
20
|
-
: newEvent.takerOrder.marketIndex;
|
|
21
|
-
if (!currentEventMarketIndex.eq(newEventMarketIndex)) {
|
|
22
|
-
return currentEvent.ts.lte(newEvent.ts) ? 'less than' : 'greater than';
|
|
23
|
-
}
|
|
24
|
-
if (currentEvent.fillRecordId.gt(index_1.ZERO) && newEvent.fillRecordId.gt(index_1.ZERO)) {
|
|
25
|
-
return currentEvent.fillRecordId.lte(newEvent.fillRecordId)
|
|
26
|
-
? 'less than'
|
|
27
|
-
: 'greater than';
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
return currentEvent.ts.lte(newEvent.ts) ? 'less than' : 'greater than';
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
function getSortFn(orderBy, orderDir, eventType) {
|
|
34
|
-
if (orderBy === 'client') {
|
|
35
|
-
return orderDir === 'asc' ? clientSortAscFn : clientSortDescFn;
|
|
36
|
-
}
|
|
37
|
-
switch (eventType) {
|
|
38
|
-
case 'OrderRecord':
|
|
39
|
-
return orderRecordSortFn;
|
|
40
|
-
default:
|
|
41
|
-
return defaultBlockchainSortFn;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
exports.getSortFn = getSortFn;
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TxEventCache = void 0;
|
|
4
|
-
class Node {
|
|
5
|
-
constructor(key, value, next, prev) {
|
|
6
|
-
this.key = key;
|
|
7
|
-
this.value = value;
|
|
8
|
-
this.next = next;
|
|
9
|
-
this.prev = prev;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
// lru cache
|
|
13
|
-
class TxEventCache {
|
|
14
|
-
constructor(maxTx = 1024) {
|
|
15
|
-
this.maxTx = maxTx;
|
|
16
|
-
this.size = 0;
|
|
17
|
-
this.cacheMap = {};
|
|
18
|
-
}
|
|
19
|
-
add(key, events) {
|
|
20
|
-
const existingNode = this.cacheMap[key];
|
|
21
|
-
if (existingNode) {
|
|
22
|
-
this.detach(existingNode);
|
|
23
|
-
this.size--;
|
|
24
|
-
}
|
|
25
|
-
else if (this.size === this.maxTx) {
|
|
26
|
-
delete this.cacheMap[this.tail.key];
|
|
27
|
-
this.detach(this.tail);
|
|
28
|
-
this.size--;
|
|
29
|
-
}
|
|
30
|
-
// Write to head of LinkedList
|
|
31
|
-
if (!this.head) {
|
|
32
|
-
this.head = this.tail = new Node(key, events);
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
const node = new Node(key, events, this.head);
|
|
36
|
-
this.head.prev = node;
|
|
37
|
-
this.head = node;
|
|
38
|
-
}
|
|
39
|
-
// update cacheMap with LinkedList key and Node reference
|
|
40
|
-
this.cacheMap[key] = this.head;
|
|
41
|
-
this.size++;
|
|
42
|
-
}
|
|
43
|
-
has(key) {
|
|
44
|
-
return this.cacheMap.hasOwnProperty(key);
|
|
45
|
-
}
|
|
46
|
-
get(key) {
|
|
47
|
-
var _a;
|
|
48
|
-
return (_a = this.cacheMap[key]) === null || _a === void 0 ? void 0 : _a.value;
|
|
49
|
-
}
|
|
50
|
-
detach(node) {
|
|
51
|
-
if (node.prev !== undefined) {
|
|
52
|
-
node.prev.next = node.next;
|
|
53
|
-
}
|
|
54
|
-
else {
|
|
55
|
-
this.head = node.next;
|
|
56
|
-
}
|
|
57
|
-
if (node.next !== undefined) {
|
|
58
|
-
node.next.prev = node.prev;
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
this.tail = node.prev;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
clear() {
|
|
65
|
-
this.head = undefined;
|
|
66
|
-
this.tail = undefined;
|
|
67
|
-
this.size = 0;
|
|
68
|
-
this.cacheMap = {};
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
exports.TxEventCache = TxEventCache;
|
package/src/events/types.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DefaultEventSubscriptionOptions = void 0;
|
|
4
|
-
exports.DefaultEventSubscriptionOptions = {
|
|
5
|
-
eventTypes: [
|
|
6
|
-
'DepositRecord',
|
|
7
|
-
'FundingPaymentRecord',
|
|
8
|
-
'LiquidationRecord',
|
|
9
|
-
'OrderRecord',
|
|
10
|
-
'FundingRateRecord',
|
|
11
|
-
],
|
|
12
|
-
maxEventsPerType: 4096,
|
|
13
|
-
orderBy: 'blockchain',
|
|
14
|
-
orderDir: 'asc',
|
|
15
|
-
commitment: 'confirmed',
|
|
16
|
-
maxTx: 4096,
|
|
17
|
-
logProviderConfig: {
|
|
18
|
-
type: 'websocket',
|
|
19
|
-
},
|
|
20
|
-
};
|
|
@@ -1,41 +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.WebSocketLogProvider = void 0;
|
|
13
|
-
class WebSocketLogProvider {
|
|
14
|
-
constructor(connection, programId, commitment) {
|
|
15
|
-
this.connection = connection;
|
|
16
|
-
this.programId = programId;
|
|
17
|
-
this.commitment = commitment;
|
|
18
|
-
}
|
|
19
|
-
subscribe(callback) {
|
|
20
|
-
if (this.subscriptionId) {
|
|
21
|
-
return true;
|
|
22
|
-
}
|
|
23
|
-
this.subscriptionId = this.connection.onLogs(this.programId, (logs, ctx) => {
|
|
24
|
-
callback(logs.signature, ctx.slot, logs.logs);
|
|
25
|
-
}, this.commitment);
|
|
26
|
-
return true;
|
|
27
|
-
}
|
|
28
|
-
isSubscribed() {
|
|
29
|
-
return this.subscriptionId !== undefined;
|
|
30
|
-
}
|
|
31
|
-
unsubscribe() {
|
|
32
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
-
if (this.subscriptionId !== undefined) {
|
|
34
|
-
yield this.connection.removeOnLogsListener(this.subscriptionId);
|
|
35
|
-
this.subscriptionId = undefined;
|
|
36
|
-
}
|
|
37
|
-
return true;
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
exports.WebSocketLogProvider = WebSocketLogProvider;
|
|
@@ -1,80 +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.getTokenAddress = void 0;
|
|
13
|
-
const anchor_1 = require("@project-serum/anchor");
|
|
14
|
-
const __1 = require("..");
|
|
15
|
-
const spl_token_1 = require("@solana/spl-token");
|
|
16
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
17
|
-
const __2 = require("..");
|
|
18
|
-
const banks_1 = require("../constants/banks");
|
|
19
|
-
const getTokenAddress = (mintAddress, userPubKey) => {
|
|
20
|
-
return spl_token_1.Token.getAssociatedTokenAddress(new web3_js_1.PublicKey(`ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL`), spl_token_1.TOKEN_PROGRAM_ID, new web3_js_1.PublicKey(mintAddress), new web3_js_1.PublicKey(userPubKey));
|
|
21
|
-
};
|
|
22
|
-
exports.getTokenAddress = getTokenAddress;
|
|
23
|
-
const main = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
24
|
-
// Initialize Drift SDK
|
|
25
|
-
const sdkConfig = __2.initialize({ env: 'devnet' });
|
|
26
|
-
// Set up the Wallet and Provider
|
|
27
|
-
const privateKey = process.env.BOT_PRIVATE_KEY; // stored as an array string
|
|
28
|
-
const keypair = web3_js_1.Keypair.fromSecretKey(Uint8Array.from(JSON.parse(privateKey)));
|
|
29
|
-
const wallet = new __1.Wallet(keypair);
|
|
30
|
-
// Set up the Connection
|
|
31
|
-
const rpcAddress = process.env.RPC_ADDRESS; // can use: https://api.devnet.solana.com for devnet; https://api.mainnet-beta.solana.com for mainnet;
|
|
32
|
-
const connection = new web3_js_1.Connection(rpcAddress);
|
|
33
|
-
// Set up the Provider
|
|
34
|
-
const provider = new anchor_1.AnchorProvider(connection, wallet, anchor_1.AnchorProvider.defaultOptions());
|
|
35
|
-
// Check SOL Balance
|
|
36
|
-
const lamportsBalance = yield connection.getBalance(wallet.publicKey);
|
|
37
|
-
console.log('SOL balance:', lamportsBalance / Math.pow(10, 9));
|
|
38
|
-
// Misc. other things to set up
|
|
39
|
-
const usdcTokenAddress = yield exports.getTokenAddress(sdkConfig.USDC_MINT_ADDRESS, wallet.publicKey.toString());
|
|
40
|
-
// Set up the Drift Clearing House
|
|
41
|
-
const clearingHousePublicKey = new web3_js_1.PublicKey(sdkConfig.CLEARING_HOUSE_PROGRAM_ID);
|
|
42
|
-
const clearingHouse = new __2.ClearingHouse({
|
|
43
|
-
connection,
|
|
44
|
-
wallet: provider.wallet,
|
|
45
|
-
programID: clearingHousePublicKey,
|
|
46
|
-
});
|
|
47
|
-
yield clearingHouse.subscribe();
|
|
48
|
-
// Set up Clearing House user client
|
|
49
|
-
const user = new __2.ClearingHouseUser({
|
|
50
|
-
clearingHouse,
|
|
51
|
-
userAccountPublicKey: yield clearingHouse.getUserAccountPublicKey(),
|
|
52
|
-
});
|
|
53
|
-
//// Check if clearing house account exists for the current wallet
|
|
54
|
-
const userAccountExists = yield user.exists();
|
|
55
|
-
if (!userAccountExists) {
|
|
56
|
-
//// Create a Clearing House account by Depositing some USDC ($10,000 in this case)
|
|
57
|
-
const depositAmount = new anchor_1.BN(10000).mul(__2.QUOTE_PRECISION);
|
|
58
|
-
yield clearingHouse.initializeUserAccountAndDepositCollateral(depositAmount, yield exports.getTokenAddress(usdcTokenAddress.toString(), wallet.publicKey.toString()), banks_1.Banks['devnet'][0].bankIndex);
|
|
59
|
-
}
|
|
60
|
-
yield user.subscribe();
|
|
61
|
-
// Get current price
|
|
62
|
-
const solMarketInfo = sdkConfig.MARKETS.find((market) => market.baseAssetSymbol === 'SOL');
|
|
63
|
-
const currentMarketPrice = __2.calculateMarkPrice(clearingHouse.getMarketAccount(solMarketInfo.marketIndex), undefined);
|
|
64
|
-
const formattedPrice = __2.convertToNumber(currentMarketPrice, __2.MARK_PRICE_PRECISION);
|
|
65
|
-
console.log(`Current Market Price is $${formattedPrice}`);
|
|
66
|
-
// Estimate the slippage for a $5000 LONG trade
|
|
67
|
-
const solMarketAccount = clearingHouse.getMarketAccount(solMarketInfo.marketIndex);
|
|
68
|
-
const longAmount = new anchor_1.BN(5000).mul(__2.QUOTE_PRECISION);
|
|
69
|
-
const slippage = __2.convertToNumber(__2.calculateTradeSlippage(__2.PositionDirection.LONG, longAmount, solMarketAccount, 'quote', undefined)[0], __2.MARK_PRICE_PRECISION);
|
|
70
|
-
console.log(`Slippage for a $5000 LONG on the SOL market would be $${slippage}`);
|
|
71
|
-
// Make a $5000 LONG trade
|
|
72
|
-
yield clearingHouse.openPosition(__2.PositionDirection.LONG, longAmount, solMarketInfo.marketIndex);
|
|
73
|
-
console.log(`LONGED $5000 SOL`);
|
|
74
|
-
// Reduce the position by $2000
|
|
75
|
-
const reduceAmount = new anchor_1.BN(2000).mul(__2.QUOTE_PRECISION);
|
|
76
|
-
yield clearingHouse.openPosition(__2.PositionDirection.SHORT, reduceAmount, solMarketInfo.marketIndex);
|
|
77
|
-
// Close the rest of the position
|
|
78
|
-
yield clearingHouse.closePosition(solMarketInfo.marketIndex);
|
|
79
|
-
});
|
|
80
|
-
main();
|