@drift-labs/sdk 0.1.36-master.7 → 0.2.0-master.10
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 +6 -6
- package/lib/accounts/bulkAccountLoader.js +81 -95
- package/lib/accounts/bulkUserSubscription.js +13 -57
- package/lib/accounts/fetch.d.ts +4 -0
- package/lib/accounts/fetch.js +18 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +34 -38
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +224 -224
- package/lib/accounts/pollingOracleSubscriber.d.ts +3 -3
- package/lib/accounts/pollingOracleSubscriber.js +37 -49
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts +3 -3
- package/lib/accounts/pollingTokenAccountSubscriber.js +35 -50
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +7 -13
- package/lib/accounts/pollingUserAccountSubscriber.js +71 -134
- package/lib/accounts/types.d.ts +34 -41
- package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -4
- package/lib/accounts/webSocketAccountSubscriber.js +39 -35
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +33 -28
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +153 -185
- package/lib/accounts/webSocketUserAccountSubscriber.d.ts +5 -11
- package/lib/accounts/webSocketUserAccountSubscriber.js +22 -67
- package/lib/addresses/marketAddresses.d.ts +4 -0
- package/lib/addresses/marketAddresses.js +15 -0
- package/lib/addresses/pda.d.ts +12 -0
- package/lib/addresses/pda.js +83 -0
- package/lib/admin.d.ts +12 -18
- package/lib/admin.js +398 -558
- package/lib/clearingHouse.d.ts +83 -109
- package/lib/clearingHouse.js +832 -893
- package/lib/clearingHouseConfig.d.ts +34 -0
- package/lib/clearingHouseConfig.js +2 -0
- package/lib/clearingHouseUser.d.ts +19 -22
- package/lib/clearingHouseUser.js +155 -141
- package/lib/clearingHouseUserConfig.d.ts +14 -0
- package/lib/clearingHouseUserConfig.js +2 -0
- package/lib/config.d.ts +12 -0
- package/lib/config.js +35 -4
- package/lib/constants/banks.d.ts +16 -0
- package/lib/constants/banks.js +41 -0
- package/lib/constants/markets.d.ts +8 -3
- package/lib/constants/markets.js +13 -206
- package/lib/constants/numericConstants.d.ts +17 -0
- package/lib/constants/numericConstants.js +23 -6
- package/lib/events/eventList.d.ts +22 -0
- package/lib/events/eventList.js +77 -0
- package/lib/events/eventSubscriber.d.ts +34 -0
- package/lib/events/eventSubscriber.js +126 -0
- package/lib/events/fetchLogs.d.ts +13 -0
- package/lib/events/fetchLogs.js +39 -0
- package/lib/events/pollingLogProvider.d.ts +15 -0
- package/lib/events/pollingLogProvider.js +53 -0
- package/lib/events/sort.d.ts +2 -0
- package/lib/events/sort.js +44 -0
- package/lib/events/txEventCache.d.ts +24 -0
- package/lib/events/txEventCache.js +71 -0
- package/lib/events/types.d.ts +49 -0
- package/lib/events/types.js +20 -0
- package/lib/events/webSocketLogProvider.d.ts +12 -0
- package/lib/events/webSocketLogProvider.js +30 -0
- package/lib/examples/makeTradeExample.js +26 -27
- package/lib/factory/bigNum.d.ts +118 -0
- package/lib/factory/bigNum.js +364 -0
- package/lib/factory/oracleClient.d.ts +1 -2
- package/lib/factory/oracleClient.js +6 -2
- package/lib/idl/clearing_house.json +2258 -2774
- package/lib/index.d.ts +14 -5
- package/lib/index.js +18 -5
- package/lib/math/amm.d.ts +24 -29
- package/lib/math/amm.js +209 -176
- package/lib/math/auction.d.ts +5 -0
- package/lib/math/auction.js +42 -0
- package/lib/math/bankBalance.d.ts +9 -0
- package/lib/math/bankBalance.js +75 -0
- package/lib/math/conversion.d.ts +0 -1
- package/lib/math/conversion.js +1 -5
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +158 -175
- package/lib/math/market.d.ts +6 -6
- package/lib/math/market.js +10 -9
- package/lib/math/orders.d.ts +6 -1
- package/lib/math/orders.js +38 -1
- package/lib/math/position.d.ts +7 -5
- package/lib/math/position.js +29 -27
- package/lib/math/repeg.d.ts +22 -0
- package/lib/math/repeg.js +128 -0
- package/lib/math/trade.d.ts +5 -4
- package/lib/math/trade.js +29 -24
- package/lib/mockUSDCFaucet.js +87 -116
- package/lib/oracles/oracleClientCache.d.ts +8 -0
- package/lib/oracles/oracleClientCache.js +19 -0
- package/lib/oracles/pythClient.d.ts +3 -5
- package/lib/oracles/pythClient.js +12 -31
- package/lib/oracles/quoteAssetOracleClient.d.ts +9 -0
- package/lib/oracles/quoteAssetOracleClient.js +21 -0
- package/lib/oracles/switchboardClient.d.ts +3 -5
- package/lib/oracles/switchboardClient.js +29 -50
- package/lib/oracles/types.d.ts +6 -1
- package/lib/orderParams.d.ts +14 -5
- package/lib/orderParams.js +8 -96
- package/lib/orders.d.ts +6 -7
- package/lib/orders.js +11 -89
- package/lib/slot/SlotSubscriber.d.ts +19 -0
- package/lib/slot/SlotSubscriber.js +26 -0
- package/lib/tx/retryTxSender.d.ts +2 -2
- package/lib/tx/retryTxSender.js +108 -123
- package/lib/tx/types.d.ts +5 -1
- package/lib/tx/utils.d.ts +1 -1
- package/lib/tx/utils.js +11 -2
- package/lib/types.d.ts +180 -110
- package/lib/types.js +54 -1
- package/lib/userName.d.ts +4 -0
- package/lib/userName.js +20 -0
- package/lib/util/computeUnits.js +10 -21
- package/lib/util/tps.js +11 -22
- package/lib/wallet.js +7 -20
- package/package.json +11 -4
- package/src/accounts/bulkAccountLoader.js +197 -0
- package/src/accounts/bulkAccountLoader.ts +21 -15
- package/src/accounts/bulkUserSubscription.js +33 -0
- package/src/accounts/bulkUserSubscription.ts +1 -45
- package/src/accounts/fetch.js +29 -0
- package/src/accounts/fetch.ts +33 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +311 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +249 -214
- package/src/accounts/pollingOracleSubscriber.js +93 -0
- package/src/accounts/pollingOracleSubscriber.ts +16 -8
- package/src/accounts/pollingTokenAccountSubscriber.js +90 -0
- package/src/accounts/pollingTokenAccountSubscriber.ts +11 -8
- package/src/accounts/pollingUserAccountSubscriber.js +132 -0
- package/src/accounts/pollingUserAccountSubscriber.ts +21 -86
- package/src/accounts/types.js +10 -0
- package/src/accounts/types.ts +41 -70
- package/src/accounts/utils.js +7 -0
- package/src/accounts/webSocketAccountSubscriber.js +93 -0
- package/src/accounts/webSocketAccountSubscriber.ts +33 -16
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +233 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +190 -240
- package/src/accounts/webSocketUserAccountSubscriber.js +62 -0
- package/src/accounts/webSocketUserAccountSubscriber.ts +11 -71
- package/src/addresses/marketAddresses.js +26 -0
- package/src/addresses/marketAddresses.ts +18 -0
- package/{lib/addresses.js → src/addresses/pda.js} +45 -34
- package/src/addresses/pda.ts +118 -0
- package/src/admin.ts +247 -336
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +1023 -1026
- package/src/clearingHouseConfig.ts +37 -0
- package/src/clearingHouseUser.ts +275 -185
- package/src/clearingHouseUserConfig.ts +18 -0
- package/src/config.ts +54 -1
- package/src/constants/banks.ts +50 -0
- package/src/constants/markets.ts +16 -207
- package/src/constants/numericConstants.ts +34 -5
- package/src/events/eventList.js +77 -0
- package/src/events/eventList.ts +94 -0
- package/src/events/eventSubscriber.js +139 -0
- package/src/events/eventSubscriber.ts +194 -0
- package/src/events/fetchLogs.js +50 -0
- package/src/events/fetchLogs.ts +80 -0
- package/src/events/pollingLogProvider.js +64 -0
- package/src/events/pollingLogProvider.ts +79 -0
- package/src/events/sort.js +44 -0
- package/src/events/sort.ts +65 -0
- package/src/events/txEventCache.js +71 -0
- package/src/events/txEventCache.ts +74 -0
- package/src/events/types.js +20 -0
- package/src/events/types.ts +98 -0
- package/src/events/webSocketLogProvider.js +41 -0
- package/src/events/webSocketLogProvider.ts +38 -0
- package/src/examples/makeTradeExample.js +80 -0
- package/src/examples/makeTradeExample.ts +20 -11
- package/src/factory/bigNum.js +364 -0
- package/src/factory/bigNum.ts +524 -0
- package/src/factory/oracleClient.js +20 -0
- package/src/factory/oracleClient.ts +7 -4
- package/src/idl/clearing_house.json +2258 -2774
- package/src/index.js +69 -0
- package/src/index.ts +14 -5
- package/src/math/amm.js +369 -0
- package/src/math/amm.ts +382 -243
- package/src/math/auction.js +42 -0
- package/src/math/auction.ts +43 -0
- package/src/math/bankBalance.js +75 -0
- package/src/math/bankBalance.ts +112 -0
- package/src/math/conversion.js +11 -0
- package/src/math/conversion.ts +1 -11
- package/src/math/funding.js +248 -0
- package/src/math/funding.ts +12 -9
- package/src/math/market.js +57 -0
- package/src/math/market.ts +37 -30
- package/src/math/oracles.js +26 -0
- package/src/math/orders.js +110 -0
- package/src/math/orders.ts +43 -1
- package/src/math/position.js +140 -0
- package/src/math/position.ts +52 -35
- package/src/math/repeg.js +128 -0
- package/src/math/repeg.ts +176 -0
- package/src/math/state.js +15 -0
- package/src/math/trade.js +253 -0
- package/src/math/trade.ts +55 -47
- package/src/math/utils.js +26 -0
- package/src/math/utils.js.map +1 -0
- package/src/mockUSDCFaucet.js +171 -0
- package/src/oracles/oracleClientCache.js +19 -0
- package/src/oracles/oracleClientCache.ts +20 -0
- package/src/oracles/pythClient.js +46 -0
- package/src/oracles/pythClient.ts +4 -11
- package/src/oracles/quoteAssetOracleClient.js +32 -0
- package/src/oracles/quoteAssetOracleClient.ts +25 -0
- package/src/oracles/switchboardClient.js +69 -0
- package/src/oracles/switchboardClient.ts +11 -24
- package/src/oracles/types.js +2 -0
- package/src/oracles/types.ts +7 -1
- package/src/orderParams.js +20 -0
- package/src/orderParams.ts +20 -141
- package/src/orders.js +134 -0
- package/src/orders.ts +25 -134
- package/src/slot/SlotSubscriber.js +39 -0
- package/src/slot/SlotSubscriber.ts +42 -0
- package/src/token/index.js +38 -0
- package/src/tx/retryTxSender.js +188 -0
- package/src/tx/retryTxSender.ts +6 -4
- package/src/tx/types.js +2 -0
- package/src/tx/types.ts +6 -1
- package/src/tx/utils.js +17 -0
- package/src/tx/utils.ts +22 -3
- package/src/types.js +114 -0
- package/src/types.ts +176 -124
- package/src/userName.js +20 -0
- package/src/userName.ts +20 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/src/wallet.js +35 -0
- package/tests/bn/test.ts +255 -0
- package/tsconfig.json +12 -12
- package/lib/addresses.d.ts +0 -10
- package/lib/constants/accounts.d.ts +0 -15
- package/lib/constants/accounts.js +0 -18
- package/lib/factory/clearingHouse.d.ts +0 -35
- package/lib/factory/clearingHouse.js +0 -81
- package/lib/factory/clearingHouseUser.d.ts +0 -19
- package/lib/factory/clearingHouseUser.js +0 -34
- package/lib/math/insuranceFund.d.ts +0 -15
- package/lib/math/insuranceFund.js +0 -33
- package/lib/settlement.d.ts +0 -4
- package/lib/settlement.js +0 -10
- package/lib/tx/defaultTxSender.d.ts +0 -8
- package/lib/tx/defaultTxSender.js +0 -12
- package/src/addresses.ts +0 -82
- package/src/constants/accounts.ts +0 -26
- package/src/factory/clearingHouse.ts +0 -173
- package/src/factory/clearingHouseUser.ts +0 -73
- package/src/math/insuranceFund.ts +0 -29
- package/src/settlement.ts +0 -9
- package/src/tx/defaultTxSender.ts +0 -24
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EventMap,
|
|
3
|
+
EventSubscriptionOrderBy,
|
|
4
|
+
EventSubscriptionOrderDirection,
|
|
5
|
+
EventType,
|
|
6
|
+
SortFn,
|
|
7
|
+
Event,
|
|
8
|
+
} from './types';
|
|
9
|
+
import { OrderRecord } from '../types';
|
|
10
|
+
import { PublicKey, ZERO } from '../index';
|
|
11
|
+
|
|
12
|
+
function clientSortAscFn(): 'less than' {
|
|
13
|
+
return 'less than';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function clientSortDescFn(): 'greater than' {
|
|
17
|
+
return 'greater than';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function defaultBlockchainSortFn(
|
|
21
|
+
currentEvent: EventMap[EventType],
|
|
22
|
+
newEvent: EventMap[EventType]
|
|
23
|
+
): 'less than' | 'greater than' {
|
|
24
|
+
return currentEvent.slot <= newEvent.slot ? 'less than' : 'greater than';
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function orderRecordSortFn(
|
|
28
|
+
currentEvent: Event<OrderRecord>,
|
|
29
|
+
newEvent: Event<OrderRecord>
|
|
30
|
+
): 'less than' | 'greater than' {
|
|
31
|
+
const currentEventMarketIndex = !currentEvent.maker.equals(PublicKey.default)
|
|
32
|
+
? currentEvent.makerOrder.marketIndex
|
|
33
|
+
: currentEvent.takerOrder.marketIndex;
|
|
34
|
+
const newEventMarketIndex = !newEvent.maker.equals(PublicKey.default)
|
|
35
|
+
? newEvent.makerOrder.marketIndex
|
|
36
|
+
: newEvent.takerOrder.marketIndex;
|
|
37
|
+
if (!currentEventMarketIndex.eq(newEventMarketIndex)) {
|
|
38
|
+
return currentEvent.ts.lte(newEvent.ts) ? 'less than' : 'greater than';
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (currentEvent.fillRecordId.gt(ZERO) && newEvent.fillRecordId.gt(ZERO)) {
|
|
42
|
+
return currentEvent.fillRecordId.lte(newEvent.fillRecordId)
|
|
43
|
+
? 'less than'
|
|
44
|
+
: 'greater than';
|
|
45
|
+
} else {
|
|
46
|
+
return currentEvent.ts.lte(newEvent.ts) ? 'less than' : 'greater than';
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function getSortFn(
|
|
51
|
+
orderBy: EventSubscriptionOrderBy,
|
|
52
|
+
orderDir: EventSubscriptionOrderDirection,
|
|
53
|
+
eventType: EventType
|
|
54
|
+
): SortFn {
|
|
55
|
+
if (orderBy === 'client') {
|
|
56
|
+
return orderDir === 'asc' ? clientSortAscFn : clientSortDescFn;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
switch (eventType) {
|
|
60
|
+
case 'OrderRecord':
|
|
61
|
+
return orderRecordSortFn;
|
|
62
|
+
default:
|
|
63
|
+
return defaultBlockchainSortFn;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
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;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { WrappedEvent, EventType } from './types';
|
|
2
|
+
|
|
3
|
+
class Node {
|
|
4
|
+
constructor(
|
|
5
|
+
public key: string,
|
|
6
|
+
public value: WrappedEvent<EventType>[],
|
|
7
|
+
public next?: Node,
|
|
8
|
+
public prev?: Node
|
|
9
|
+
) {}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// lru cache
|
|
13
|
+
export class TxEventCache {
|
|
14
|
+
size = 0;
|
|
15
|
+
head?: Node;
|
|
16
|
+
tail?: Node;
|
|
17
|
+
cacheMap: { [key: string]: Node } = {};
|
|
18
|
+
|
|
19
|
+
constructor(public maxTx = 1024) {}
|
|
20
|
+
|
|
21
|
+
public add(key: string, events: WrappedEvent<EventType>[]): void {
|
|
22
|
+
const existingNode = this.cacheMap[key];
|
|
23
|
+
if (existingNode) {
|
|
24
|
+
this.detach(existingNode);
|
|
25
|
+
this.size--;
|
|
26
|
+
} else if (this.size === this.maxTx) {
|
|
27
|
+
delete this.cacheMap[this.tail.key];
|
|
28
|
+
this.detach(this.tail);
|
|
29
|
+
this.size--;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Write to head of LinkedList
|
|
33
|
+
if (!this.head) {
|
|
34
|
+
this.head = this.tail = new Node(key, events);
|
|
35
|
+
} else {
|
|
36
|
+
const node = new Node(key, events, this.head);
|
|
37
|
+
this.head.prev = node;
|
|
38
|
+
this.head = node;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// update cacheMap with LinkedList key and Node reference
|
|
42
|
+
this.cacheMap[key] = this.head;
|
|
43
|
+
this.size++;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public has(key: string): boolean {
|
|
47
|
+
return this.cacheMap.hasOwnProperty(key);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public get(key: string): WrappedEvent<EventType>[] | undefined {
|
|
51
|
+
return this.cacheMap[key]?.value;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
detach(node: Node): void {
|
|
55
|
+
if (node.prev !== undefined) {
|
|
56
|
+
node.prev.next = node.next;
|
|
57
|
+
} else {
|
|
58
|
+
this.head = node.next;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (node.next !== undefined) {
|
|
62
|
+
node.next.prev = node.prev;
|
|
63
|
+
} else {
|
|
64
|
+
this.tail = node.prev;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public clear(): void {
|
|
69
|
+
this.head = undefined;
|
|
70
|
+
this.tail = undefined;
|
|
71
|
+
this.size = 0;
|
|
72
|
+
this.cacheMap = {};
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Commitment, TransactionSignature } from '@solana/web3.js';
|
|
2
|
+
import {
|
|
3
|
+
DepositRecord,
|
|
4
|
+
FundingPaymentRecord,
|
|
5
|
+
FundingRateRecord,
|
|
6
|
+
LiquidationRecord,
|
|
7
|
+
OrderRecord,
|
|
8
|
+
} from '../index';
|
|
9
|
+
|
|
10
|
+
export type EventSubscriptionOptions = {
|
|
11
|
+
eventTypes?: EventType[];
|
|
12
|
+
maxEventsPerType?: number;
|
|
13
|
+
orderBy?: EventSubscriptionOrderBy;
|
|
14
|
+
orderDir?: EventSubscriptionOrderDirection;
|
|
15
|
+
commitment?: Commitment;
|
|
16
|
+
maxTx?: number;
|
|
17
|
+
logProviderConfig?: LogProviderConfig;
|
|
18
|
+
// when the subscription starts, client might want to backtrack and fetch old tx's
|
|
19
|
+
// this specifies how far to backtrack
|
|
20
|
+
untilTx?: TransactionSignature;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const DefaultEventSubscriptionOptions: EventSubscriptionOptions = {
|
|
24
|
+
eventTypes: [
|
|
25
|
+
'DepositRecord',
|
|
26
|
+
'FundingPaymentRecord',
|
|
27
|
+
'LiquidationRecord',
|
|
28
|
+
'OrderRecord',
|
|
29
|
+
'FundingRateRecord',
|
|
30
|
+
],
|
|
31
|
+
maxEventsPerType: 4096,
|
|
32
|
+
orderBy: 'blockchain',
|
|
33
|
+
orderDir: 'asc',
|
|
34
|
+
commitment: 'confirmed',
|
|
35
|
+
maxTx: 4096,
|
|
36
|
+
logProviderConfig: {
|
|
37
|
+
type: 'websocket',
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// Whether we sort events based on order blockchain produced events or client receives events
|
|
42
|
+
export type EventSubscriptionOrderBy = 'blockchain' | 'client';
|
|
43
|
+
export type EventSubscriptionOrderDirection = 'asc' | 'desc';
|
|
44
|
+
|
|
45
|
+
export type Event<T> = T & {
|
|
46
|
+
txSig: TransactionSignature;
|
|
47
|
+
slot: number;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export type WrappedEvent<Type extends EventType> = EventMap[Type] & {
|
|
51
|
+
eventType: Type;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export type WrappedEvents = WrappedEvent<EventType>[];
|
|
55
|
+
|
|
56
|
+
export type EventMap = {
|
|
57
|
+
DepositRecord: Event<DepositRecord>;
|
|
58
|
+
FundingPaymentRecord: Event<FundingPaymentRecord>;
|
|
59
|
+
LiquidationRecord: Event<LiquidationRecord>;
|
|
60
|
+
FundingRateRecord: Event<FundingRateRecord>;
|
|
61
|
+
OrderRecord: Event<OrderRecord>;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export type EventType = keyof EventMap;
|
|
65
|
+
|
|
66
|
+
export interface EventSubscriberEvents {
|
|
67
|
+
newEvent: (event: WrappedEvent<EventType>) => void;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export type SortFn = (
|
|
71
|
+
currentRecord: EventMap[EventType],
|
|
72
|
+
newRecord: EventMap[EventType]
|
|
73
|
+
) => 'less than' | 'greater than';
|
|
74
|
+
|
|
75
|
+
export type logProviderCallback = (
|
|
76
|
+
txSig: TransactionSignature,
|
|
77
|
+
slot: number,
|
|
78
|
+
logs: string[]
|
|
79
|
+
) => void;
|
|
80
|
+
|
|
81
|
+
export interface LogProvider {
|
|
82
|
+
isSubscribed(): boolean;
|
|
83
|
+
subscribe(callback: logProviderCallback): boolean;
|
|
84
|
+
unsubscribe(): Promise<boolean>;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export type WebSocketLogProviderConfig = {
|
|
88
|
+
type: 'websocket';
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export type PollingLogProviderConfig = {
|
|
92
|
+
type: 'polling';
|
|
93
|
+
frequency: number;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export type LogProviderConfig =
|
|
97
|
+
| WebSocketLogProviderConfig
|
|
98
|
+
| PollingLogProviderConfig;
|
|
@@ -0,0 +1,41 @@
|
|
|
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;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { LogProvider, logProviderCallback } from './types';
|
|
2
|
+
import { Commitment, Connection, PublicKey } from '@solana/web3.js';
|
|
3
|
+
|
|
4
|
+
export class WebSocketLogProvider implements LogProvider {
|
|
5
|
+
private subscriptionId: number;
|
|
6
|
+
public constructor(
|
|
7
|
+
private connection: Connection,
|
|
8
|
+
private programId: PublicKey,
|
|
9
|
+
private commitment: Commitment
|
|
10
|
+
) {}
|
|
11
|
+
|
|
12
|
+
public subscribe(callback: logProviderCallback): boolean {
|
|
13
|
+
if (this.subscriptionId) {
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
this.subscriptionId = this.connection.onLogs(
|
|
18
|
+
this.programId,
|
|
19
|
+
(logs, ctx) => {
|
|
20
|
+
callback(logs.signature, ctx.slot, logs.logs);
|
|
21
|
+
},
|
|
22
|
+
this.commitment
|
|
23
|
+
);
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public isSubscribed(): boolean {
|
|
28
|
+
return this.subscriptionId !== undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public async unsubscribe(): Promise<boolean> {
|
|
32
|
+
if (this.subscriptionId !== undefined) {
|
|
33
|
+
await this.connection.removeOnLogsListener(this.subscriptionId);
|
|
34
|
+
this.subscriptionId = undefined;
|
|
35
|
+
}
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
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();
|
|
@@ -7,13 +7,13 @@ import {
|
|
|
7
7
|
ClearingHouse,
|
|
8
8
|
ClearingHouseUser,
|
|
9
9
|
initialize,
|
|
10
|
-
Markets,
|
|
11
10
|
PositionDirection,
|
|
12
11
|
convertToNumber,
|
|
13
12
|
calculateTradeSlippage,
|
|
14
13
|
MARK_PRICE_PRECISION,
|
|
15
14
|
QUOTE_PRECISION,
|
|
16
15
|
} from '..';
|
|
16
|
+
import { Banks } from '../constants/banks';
|
|
17
17
|
|
|
18
18
|
export const getTokenAddress = (
|
|
19
19
|
mintAddress: string,
|
|
@@ -63,15 +63,18 @@ const main = async () => {
|
|
|
63
63
|
const clearingHousePublicKey = new PublicKey(
|
|
64
64
|
sdkConfig.CLEARING_HOUSE_PROGRAM_ID
|
|
65
65
|
);
|
|
66
|
-
const clearingHouse = ClearingHouse
|
|
66
|
+
const clearingHouse = new ClearingHouse({
|
|
67
67
|
connection,
|
|
68
|
-
provider.wallet,
|
|
69
|
-
clearingHousePublicKey
|
|
70
|
-
);
|
|
68
|
+
wallet: provider.wallet,
|
|
69
|
+
programID: clearingHousePublicKey,
|
|
70
|
+
});
|
|
71
71
|
await clearingHouse.subscribe();
|
|
72
72
|
|
|
73
73
|
// Set up Clearing House user client
|
|
74
|
-
const user = ClearingHouseUser
|
|
74
|
+
const user = new ClearingHouseUser({
|
|
75
|
+
clearingHouse,
|
|
76
|
+
userAccountPublicKey: await clearingHouse.getUserAccountPublicKey(),
|
|
77
|
+
});
|
|
75
78
|
|
|
76
79
|
//// Check if clearing house account exists for the current wallet
|
|
77
80
|
const userAccountExists = await user.exists();
|
|
@@ -84,19 +87,21 @@ const main = async () => {
|
|
|
84
87
|
await getTokenAddress(
|
|
85
88
|
usdcTokenAddress.toString(),
|
|
86
89
|
wallet.publicKey.toString()
|
|
87
|
-
)
|
|
90
|
+
),
|
|
91
|
+
Banks['devnet'][0].bankIndex
|
|
88
92
|
);
|
|
89
93
|
}
|
|
90
94
|
|
|
91
95
|
await user.subscribe();
|
|
92
96
|
|
|
93
97
|
// Get current price
|
|
94
|
-
const solMarketInfo =
|
|
98
|
+
const solMarketInfo = sdkConfig.MARKETS.find(
|
|
95
99
|
(market) => market.baseAssetSymbol === 'SOL'
|
|
96
100
|
);
|
|
97
101
|
|
|
98
102
|
const currentMarketPrice = calculateMarkPrice(
|
|
99
|
-
clearingHouse.
|
|
103
|
+
clearingHouse.getMarketAccount(solMarketInfo.marketIndex),
|
|
104
|
+
undefined
|
|
100
105
|
);
|
|
101
106
|
|
|
102
107
|
const formattedPrice = convertToNumber(
|
|
@@ -107,14 +112,18 @@ const main = async () => {
|
|
|
107
112
|
console.log(`Current Market Price is $${formattedPrice}`);
|
|
108
113
|
|
|
109
114
|
// Estimate the slippage for a $5000 LONG trade
|
|
110
|
-
const solMarketAccount = clearingHouse.
|
|
115
|
+
const solMarketAccount = clearingHouse.getMarketAccount(
|
|
116
|
+
solMarketInfo.marketIndex
|
|
117
|
+
);
|
|
111
118
|
|
|
112
119
|
const longAmount = new BN(5000).mul(QUOTE_PRECISION);
|
|
113
120
|
const slippage = convertToNumber(
|
|
114
121
|
calculateTradeSlippage(
|
|
115
122
|
PositionDirection.LONG,
|
|
116
123
|
longAmount,
|
|
117
|
-
solMarketAccount
|
|
124
|
+
solMarketAccount,
|
|
125
|
+
'quote',
|
|
126
|
+
undefined
|
|
118
127
|
)[0],
|
|
119
128
|
MARK_PRICE_PRECISION
|
|
120
129
|
);
|