@drift-labs/sdk 0.2.0-master.5 → 0.2.0-master.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/accounts/types.d.ts +1 -0
- package/lib/clearingHouse.js +87 -55
- package/lib/constants/banks.js +9 -1
- package/lib/slot/SlotSubscriber.d.ts +7 -0
- package/lib/slot/SlotSubscriber.js +3 -0
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.js +197 -0
- package/src/accounts/bulkUserSubscription.js +33 -0
- package/src/accounts/fetch.js +29 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +311 -0
- package/src/accounts/pollingOracleSubscriber.js +93 -0
- package/src/accounts/pollingTokenAccountSubscriber.js +90 -0
- package/src/accounts/pollingUserAccountSubscriber.js +132 -0
- package/src/accounts/types.js +10 -0
- package/src/accounts/utils.js +7 -0
- package/src/accounts/webSocketAccountSubscriber.js +93 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +233 -0
- package/src/accounts/webSocketUserAccountSubscriber.js +62 -0
- package/src/addresses/marketAddresses.js +26 -0
- package/src/addresses/pda.js +104 -0
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +99 -61
- package/src/constants/banks.ts +9 -1
- package/src/events/eventList.js +77 -0
- package/src/events/eventSubscriber.js +139 -0
- package/src/events/fetchLogs.js +50 -0
- package/src/events/pollingLogProvider.js +64 -0
- package/src/events/sort.js +44 -0
- package/src/events/txEventCache.js +71 -0
- package/src/events/types.js +20 -0
- package/src/events/webSocketLogProvider.js +41 -0
- package/src/examples/makeTradeExample.js +80 -0
- package/src/factory/bigNum.js +364 -0
- package/src/factory/oracleClient.js +20 -0
- package/src/index.js +69 -0
- package/src/math/amm.js +369 -0
- package/src/math/auction.js +42 -0
- package/src/math/bankBalance.js +75 -0
- package/src/math/conversion.js +11 -0
- package/src/math/funding.js +248 -0
- package/src/math/market.js +57 -0
- package/src/math/oracles.js +26 -0
- package/src/math/orders.js +110 -0
- package/src/math/position.js +140 -0
- package/src/math/repeg.js +128 -0
- package/src/math/state.js +15 -0
- package/src/math/trade.js +253 -0
- package/src/math/utils.js +0 -1
- package/src/mockUSDCFaucet.js +171 -0
- package/src/oracles/oracleClientCache.js +19 -0
- package/src/oracles/pythClient.js +46 -0
- package/src/oracles/quoteAssetOracleClient.js +32 -0
- package/src/oracles/switchboardClient.js +69 -0
- package/src/oracles/types.js +2 -0
- package/src/orderParams.js +20 -0
- package/src/orders.js +134 -0
- package/src/slot/SlotSubscriber.js +39 -0
- package/src/slot/SlotSubscriber.ts +11 -1
- package/src/token/index.js +38 -0
- package/src/tx/retryTxSender.js +188 -0
- package/src/tx/types.js +2 -0
- package/src/tx/utils.js +17 -0
- package/src/types.js +114 -0
- package/src/userName.js +20 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/src/wallet.js +35 -0
- package/src/util/computeUnits.js +0 -17
- package/src/util/computeUnits.js.map +0 -1
package/src/clearingHouse.ts
CHANGED
|
@@ -564,13 +564,19 @@ export class ClearingHouse {
|
|
|
564
564
|
writableBankIndex: bankIndex,
|
|
565
565
|
});
|
|
566
566
|
} else {
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
567
|
+
const bankAccount = this.getBankAccount(bankIndex);
|
|
568
|
+
if (!bankAccount.oracle.equals(PublicKey.default)) {
|
|
569
|
+
remainingAccounts.push({
|
|
570
|
+
pubkey: bankAccount.oracle,
|
|
570
571
|
isSigner: false,
|
|
571
|
-
isWritable:
|
|
572
|
-
}
|
|
573
|
-
|
|
572
|
+
isWritable: false,
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
remainingAccounts.push({
|
|
576
|
+
pubkey: bankAccount.pubkey,
|
|
577
|
+
isSigner: false,
|
|
578
|
+
isWritable: true,
|
|
579
|
+
});
|
|
574
580
|
}
|
|
575
581
|
|
|
576
582
|
const bank = this.getBankAccount(bankIndex);
|
|
@@ -972,48 +978,64 @@ export class ClearingHouse {
|
|
|
972
978
|
const marketIndex = order.marketIndex;
|
|
973
979
|
const marketAccount = this.getMarketAccount(marketIndex);
|
|
974
980
|
|
|
975
|
-
const
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
981
|
+
const oracleAccountMap = new Map<string, AccountMeta>();
|
|
982
|
+
const bankAccountMap = new Map<number, AccountMeta>();
|
|
983
|
+
const marketAccountMap = new Map<number, AccountMeta>();
|
|
984
|
+
|
|
985
|
+
marketAccountMap.set(marketIndex.toNumber(), {
|
|
986
|
+
pubkey: marketAccount.pubkey,
|
|
987
|
+
isWritable: true,
|
|
988
|
+
isSigner: false,
|
|
989
|
+
});
|
|
990
|
+
oracleAccountMap.set(marketAccount.amm.oracle.toString(), {
|
|
991
|
+
pubkey: marketAccount.amm.oracle,
|
|
992
|
+
isWritable: false,
|
|
993
|
+
isSigner: false,
|
|
994
|
+
});
|
|
995
|
+
|
|
996
|
+
for (const bankBalance of userAccount.bankBalances) {
|
|
997
|
+
if (!bankBalance.balance.eq(ZERO)) {
|
|
998
|
+
const bankAccount = this.getBankAccount(bankBalance.bankIndex);
|
|
999
|
+
bankAccountMap.set(bankBalance.bankIndex.toNumber(), {
|
|
1000
|
+
pubkey: bankAccount.pubkey,
|
|
1001
|
+
isSigner: false,
|
|
1002
|
+
isWritable: true,
|
|
1003
|
+
});
|
|
1004
|
+
|
|
1005
|
+
if (!bankAccount.oracle.equals(PublicKey.default)) {
|
|
1006
|
+
oracleAccountMap.set(bankAccount.oracle.toString(), {
|
|
1007
|
+
pubkey: bankAccount.oracle,
|
|
1008
|
+
isSigner: false,
|
|
1009
|
+
isWritable: false,
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
|
|
996
1015
|
for (const position of userAccount.positions) {
|
|
997
1016
|
if (
|
|
998
1017
|
!positionIsAvailable(position) &&
|
|
999
1018
|
!position.marketIndex.eq(order.marketIndex)
|
|
1000
1019
|
) {
|
|
1001
1020
|
const market = this.getMarketAccount(position.marketIndex);
|
|
1002
|
-
|
|
1021
|
+
marketAccountMap.set(position.marketIndex.toNumber(), {
|
|
1003
1022
|
pubkey: market.pubkey,
|
|
1004
1023
|
isWritable: true,
|
|
1005
1024
|
isSigner: false,
|
|
1006
1025
|
});
|
|
1007
|
-
|
|
1026
|
+
oracleAccountMap.set(market.amm.oracle.toString(), {
|
|
1008
1027
|
pubkey: market.amm.oracle,
|
|
1009
1028
|
isWritable: false,
|
|
1010
1029
|
isSigner: false,
|
|
1011
1030
|
});
|
|
1012
1031
|
}
|
|
1013
1032
|
}
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1033
|
+
|
|
1034
|
+
const remainingAccounts = [
|
|
1035
|
+
...oracleAccountMap.values(),
|
|
1036
|
+
...bankAccountMap.values(),
|
|
1037
|
+
...marketAccountMap.values(),
|
|
1038
|
+
];
|
|
1017
1039
|
|
|
1018
1040
|
if (makerInfo) {
|
|
1019
1041
|
remainingAccounts.push({
|
|
@@ -1060,48 +1082,64 @@ export class ClearingHouse {
|
|
|
1060
1082
|
const marketIndex = order.marketIndex;
|
|
1061
1083
|
const marketAccount = this.getMarketAccount(marketIndex);
|
|
1062
1084
|
|
|
1063
|
-
const
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1085
|
+
const oracleAccountMap = new Map<string, AccountMeta>();
|
|
1086
|
+
const bankAccountMap = new Map<number, AccountMeta>();
|
|
1087
|
+
const marketAccountMap = new Map<number, AccountMeta>();
|
|
1088
|
+
|
|
1089
|
+
marketAccountMap.set(marketIndex.toNumber(), {
|
|
1090
|
+
pubkey: marketAccount.pubkey,
|
|
1091
|
+
isWritable: true,
|
|
1092
|
+
isSigner: false,
|
|
1093
|
+
});
|
|
1094
|
+
oracleAccountMap.set(marketAccount.amm.oracle.toString(), {
|
|
1095
|
+
pubkey: marketAccount.amm.oracle,
|
|
1096
|
+
isWritable: false,
|
|
1097
|
+
isSigner: false,
|
|
1098
|
+
});
|
|
1099
|
+
|
|
1100
|
+
for (const bankBalance of userAccount.bankBalances) {
|
|
1101
|
+
if (!bankBalance.balance.eq(ZERO)) {
|
|
1102
|
+
const bankAccount = this.getBankAccount(bankBalance.bankIndex);
|
|
1103
|
+
bankAccountMap.set(bankBalance.bankIndex.toNumber(), {
|
|
1104
|
+
pubkey: bankAccount.pubkey,
|
|
1105
|
+
isSigner: false,
|
|
1106
|
+
isWritable: true,
|
|
1107
|
+
});
|
|
1108
|
+
|
|
1109
|
+
if (!bankAccount.oracle.equals(PublicKey.default)) {
|
|
1110
|
+
oracleAccountMap.set(bankAccount.oracle.toString(), {
|
|
1111
|
+
pubkey: bankAccount.oracle,
|
|
1112
|
+
isSigner: false,
|
|
1113
|
+
isWritable: false,
|
|
1114
|
+
});
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1084
1119
|
for (const position of userAccount.positions) {
|
|
1085
1120
|
if (
|
|
1086
1121
|
!positionIsAvailable(position) &&
|
|
1087
1122
|
!position.marketIndex.eq(order.marketIndex)
|
|
1088
1123
|
) {
|
|
1089
1124
|
const market = this.getMarketAccount(position.marketIndex);
|
|
1090
|
-
|
|
1125
|
+
marketAccountMap.set(position.marketIndex.toNumber(), {
|
|
1091
1126
|
pubkey: market.pubkey,
|
|
1092
|
-
isWritable:
|
|
1127
|
+
isWritable: true,
|
|
1093
1128
|
isSigner: false,
|
|
1094
1129
|
});
|
|
1095
|
-
|
|
1130
|
+
oracleAccountMap.set(market.amm.oracle.toString(), {
|
|
1096
1131
|
pubkey: market.amm.oracle,
|
|
1097
1132
|
isWritable: false,
|
|
1098
1133
|
isSigner: false,
|
|
1099
1134
|
});
|
|
1100
1135
|
}
|
|
1101
1136
|
}
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1137
|
+
|
|
1138
|
+
const remainingAccounts = [
|
|
1139
|
+
...oracleAccountMap.values(),
|
|
1140
|
+
...bankAccountMap.values(),
|
|
1141
|
+
...marketAccountMap.values(),
|
|
1142
|
+
];
|
|
1105
1143
|
|
|
1106
1144
|
const orderId = order.orderId;
|
|
1107
1145
|
return await this.program.instruction.triggerOrder(orderId, {
|
|
@@ -1166,7 +1204,7 @@ export class ClearingHouse {
|
|
|
1166
1204
|
|
|
1167
1205
|
public async placeAndMake(
|
|
1168
1206
|
orderParams: OptionalOrderParams,
|
|
1169
|
-
takerInfo: TakerInfo
|
|
1207
|
+
takerInfo: TakerInfo
|
|
1170
1208
|
): Promise<TransactionSignature> {
|
|
1171
1209
|
const { txSig, slot } = await this.txSender.send(
|
|
1172
1210
|
wrapInTx(await this.getPlaceAndMakeIx(orderParams, takerInfo)),
|
|
@@ -1181,7 +1219,7 @@ export class ClearingHouse {
|
|
|
1181
1219
|
|
|
1182
1220
|
public async getPlaceAndMakeIx(
|
|
1183
1221
|
orderParams: OptionalOrderParams,
|
|
1184
|
-
takerInfo: TakerInfo
|
|
1222
|
+
takerInfo: TakerInfo
|
|
1185
1223
|
): Promise<TransactionInstruction> {
|
|
1186
1224
|
orderParams = this.getOrderParams(orderParams);
|
|
1187
1225
|
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
package/src/constants/banks.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { BN, OracleSource } from '../';
|
|
2
2
|
import { DriftEnv } from '../';
|
|
3
3
|
import { PublicKey } from '@solana/web3.js';
|
|
4
|
+
import { WRAPPED_SOL_MINT } from '@project-serum/serum/lib/token-instructions';
|
|
4
5
|
|
|
5
6
|
export type BankConfig = {
|
|
6
7
|
symbol: string;
|
|
@@ -19,8 +20,15 @@ export const DevnetBanks: BankConfig[] = [
|
|
|
19
20
|
mint: new PublicKey('8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2'),
|
|
20
21
|
},
|
|
21
22
|
{
|
|
22
|
-
symbol: '
|
|
23
|
+
symbol: 'SOL',
|
|
23
24
|
bankIndex: new BN(1),
|
|
25
|
+
oracle: new PublicKey('J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix'),
|
|
26
|
+
oracleSource: OracleSource.PYTH,
|
|
27
|
+
mint: WRAPPED_SOL_MINT,
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
symbol: 'BTC',
|
|
31
|
+
bankIndex: new BN(2),
|
|
24
32
|
oracle: new PublicKey('HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J'),
|
|
25
33
|
oracleSource: OracleSource.PYTH,
|
|
26
34
|
mint: new PublicKey('Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr'),
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EventList = void 0;
|
|
4
|
+
class Node {
|
|
5
|
+
constructor(event, next, prev) {
|
|
6
|
+
this.event = event;
|
|
7
|
+
this.next = next;
|
|
8
|
+
this.prev = prev;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
class EventList {
|
|
12
|
+
constructor(eventType, maxSize, sortFn, orderDirection) {
|
|
13
|
+
this.eventType = eventType;
|
|
14
|
+
this.maxSize = maxSize;
|
|
15
|
+
this.sortFn = sortFn;
|
|
16
|
+
this.orderDirection = orderDirection;
|
|
17
|
+
this.size = 0;
|
|
18
|
+
}
|
|
19
|
+
insert(event) {
|
|
20
|
+
this.size++;
|
|
21
|
+
const newNode = new Node(event);
|
|
22
|
+
if (this.head === undefined) {
|
|
23
|
+
this.head = this.tail = newNode;
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (this.sortFn(this.head.event, newNode.event) ===
|
|
27
|
+
(this.orderDirection === 'asc' ? 'less than' : 'greater than')) {
|
|
28
|
+
this.head.prev = newNode;
|
|
29
|
+
newNode.next = this.head;
|
|
30
|
+
this.head = newNode;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
let currentNode = this.head;
|
|
34
|
+
while (currentNode.next !== undefined &&
|
|
35
|
+
this.sortFn(currentNode.next.event, newNode.event) !==
|
|
36
|
+
(this.orderDirection === 'asc' ? 'less than' : 'greater than')) {
|
|
37
|
+
currentNode = currentNode.next;
|
|
38
|
+
}
|
|
39
|
+
newNode.next = currentNode.next;
|
|
40
|
+
if (currentNode.next !== undefined) {
|
|
41
|
+
newNode.next.prev = newNode;
|
|
42
|
+
}
|
|
43
|
+
currentNode.next = newNode;
|
|
44
|
+
newNode.prev = currentNode;
|
|
45
|
+
}
|
|
46
|
+
if (this.size > this.maxSize) {
|
|
47
|
+
this.detach();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
detach() {
|
|
51
|
+
const node = this.tail;
|
|
52
|
+
if (node.prev !== undefined) {
|
|
53
|
+
node.prev.next = node.next;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
this.head = node.next;
|
|
57
|
+
}
|
|
58
|
+
if (node.next !== undefined) {
|
|
59
|
+
node.next.prev = node.prev;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
this.tail = node.prev;
|
|
63
|
+
}
|
|
64
|
+
this.size--;
|
|
65
|
+
}
|
|
66
|
+
toArray() {
|
|
67
|
+
return Array.from(this);
|
|
68
|
+
}
|
|
69
|
+
*[Symbol.iterator]() {
|
|
70
|
+
let node = this.head;
|
|
71
|
+
while (node) {
|
|
72
|
+
yield node.event;
|
|
73
|
+
node = node.next;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.EventList = EventList;
|
|
@@ -0,0 +1,139 @@
|
|
|
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;
|
|
@@ -0,0 +1,50 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
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;
|
|
@@ -0,0 +1,44 @@
|
|
|
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;
|