@drift-labs/sdk 2.0.11 → 2.0.12
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/adminClient.d.ts +2 -1
- package/lib/adminClient.js +9 -0
- package/lib/config.js +1 -1
- package/lib/constants/spotMarkets.js +1 -1
- package/lib/events/pollingLogProvider.js +1 -1
- package/lib/idl/drift.json +40 -2
- package/lib/types.d.ts +8 -0
- package/lib/types.js +6 -1
- package/lib/user.d.ts +18 -4
- package/lib/user.js +93 -11
- package/lib/userMap/userMap.d.ts +1 -2
- package/lib/userMap/userMap.js +0 -40
- package/lib/userMap/userStatsMap.d.ts +1 -2
- package/lib/userMap/userStatsMap.js +0 -65
- package/package.json +1 -1
- package/src/adminClient.ts +14 -0
- package/src/assert/assert.js +9 -0
- package/src/config.ts +1 -1
- package/src/constants/spotMarkets.ts +1 -1
- package/src/events/eventList.js +77 -0
- package/src/events/pollingLogProvider.ts +2 -2
- package/src/examples/makeTradeExample.js +157 -0
- package/src/idl/drift.json +40 -2
- package/src/token/index.js +38 -0
- package/src/tx/types.js +2 -0
- package/src/tx/utils.js +17 -0
- package/src/types.ts +5 -0
- package/src/user.ts +133 -15
- package/src/userMap/userMap.ts +0 -44
- package/src/userMap/userStatsMap.ts +0 -76
- package/src/util/computeUnits.js +27 -0
- package/src/util/getTokenAddress.js +9 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
|
@@ -6,15 +6,6 @@ import {
|
|
|
6
6
|
UserStats,
|
|
7
7
|
UserStatsSubscriptionConfig,
|
|
8
8
|
bulkPollingUserStatsSubscribe,
|
|
9
|
-
WrappedEvent,
|
|
10
|
-
DepositRecord,
|
|
11
|
-
FundingPaymentRecord,
|
|
12
|
-
LiquidationRecord,
|
|
13
|
-
OrderActionRecord,
|
|
14
|
-
SettlePnlRecord,
|
|
15
|
-
NewUserRecord,
|
|
16
|
-
LPRecord,
|
|
17
|
-
InsuranceFundStakeRecord,
|
|
18
9
|
} from '..';
|
|
19
10
|
import { ProgramAccount } from '@project-serum/anchor';
|
|
20
11
|
import { PublicKey } from '@solana/web3.js';
|
|
@@ -96,73 +87,6 @@ export class UserStatsMap {
|
|
|
96
87
|
}
|
|
97
88
|
}
|
|
98
89
|
|
|
99
|
-
public async updateWithEventRecord(
|
|
100
|
-
record: WrappedEvent<any>,
|
|
101
|
-
userMap?: UserMap
|
|
102
|
-
) {
|
|
103
|
-
if (record.eventType === 'DepositRecord') {
|
|
104
|
-
const depositRecord = record as DepositRecord;
|
|
105
|
-
await this.mustGet(depositRecord.userAuthority.toString());
|
|
106
|
-
} else if (record.eventType === 'FundingPaymentRecord') {
|
|
107
|
-
const fundingPaymentRecord = record as FundingPaymentRecord;
|
|
108
|
-
await this.mustGet(fundingPaymentRecord.userAuthority.toString());
|
|
109
|
-
} else if (record.eventType === 'LiquidationRecord') {
|
|
110
|
-
if (!userMap) {
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const liqRecord = record as LiquidationRecord;
|
|
115
|
-
|
|
116
|
-
const user = await userMap.mustGet(liqRecord.user.toString());
|
|
117
|
-
await this.mustGet(user.getUserAccount().authority.toString());
|
|
118
|
-
|
|
119
|
-
const liquidatorUser = await userMap.mustGet(
|
|
120
|
-
liqRecord.liquidator.toString()
|
|
121
|
-
);
|
|
122
|
-
await this.mustGet(liquidatorUser.getUserAccount().authority.toString());
|
|
123
|
-
} else if (record.eventType === 'OrderRecord') {
|
|
124
|
-
if (!userMap) {
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
const orderRecord = record as OrderRecord;
|
|
128
|
-
await userMap.updateWithOrderRecord(orderRecord);
|
|
129
|
-
} else if (record.eventType === 'OrderActionRecord') {
|
|
130
|
-
if (!userMap) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
const actionRecord = record as OrderActionRecord;
|
|
134
|
-
|
|
135
|
-
if (actionRecord.taker) {
|
|
136
|
-
const taker = await userMap.mustGet(actionRecord.taker.toString());
|
|
137
|
-
await this.mustGet(taker.getUserAccount().authority.toString());
|
|
138
|
-
}
|
|
139
|
-
if (actionRecord.maker) {
|
|
140
|
-
const maker = await userMap.mustGet(actionRecord.maker.toString());
|
|
141
|
-
await this.mustGet(maker.getUserAccount().authority.toString());
|
|
142
|
-
}
|
|
143
|
-
} else if (record.eventType === 'SettlePnlRecord') {
|
|
144
|
-
if (!userMap) {
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
const settlePnlRecord = record as SettlePnlRecord;
|
|
148
|
-
const user = await userMap.mustGet(settlePnlRecord.user.toString());
|
|
149
|
-
await this.mustGet(user.getUserAccount().authority.toString());
|
|
150
|
-
} else if (record.eventType === 'NewUserRecord') {
|
|
151
|
-
const newUserRecord = record as NewUserRecord;
|
|
152
|
-
await this.mustGet(newUserRecord.userAuthority.toString());
|
|
153
|
-
} else if (record.eventType === 'LPRecord') {
|
|
154
|
-
if (!userMap) {
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
const lpRecord = record as LPRecord;
|
|
158
|
-
const user = await userMap.mustGet(lpRecord.user.toString());
|
|
159
|
-
await this.mustGet(user.getUserAccount().authority.toString());
|
|
160
|
-
} else if (record.eventType === 'InsuranceFundStakeRecord') {
|
|
161
|
-
const ifStakeRecord = record as InsuranceFundStakeRecord;
|
|
162
|
-
await this.mustGet(ifStakeRecord.userAuthority.toString());
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
90
|
public has(authorityPublicKey: string): boolean {
|
|
167
91
|
return this.userStatsMap.has(authorityPublicKey);
|
|
168
92
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
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.findComputeUnitConsumption = void 0;
|
|
13
|
+
function findComputeUnitConsumption(programId, connection, txSignature, commitment = 'confirmed') {
|
|
14
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
const tx = yield connection.getTransaction(txSignature, { commitment });
|
|
16
|
+
const computeUnits = [];
|
|
17
|
+
const regex = new RegExp(`Program ${programId.toString()} consumed ([0-9]{0,6}) of ([0-9]{0,7}) compute units`);
|
|
18
|
+
tx.meta.logMessages.forEach((logMessage) => {
|
|
19
|
+
const match = logMessage.match(regex);
|
|
20
|
+
if (match && match[1]) {
|
|
21
|
+
computeUnits.push(match[1]);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
return computeUnits;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
exports.findComputeUnitConsumption = findComputeUnitConsumption;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getTokenAddress = void 0;
|
|
4
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
5
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
+
const getTokenAddress = (mintAddress, userPubKey) => {
|
|
7
|
+
return spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, new web3_js_1.PublicKey(mintAddress), new web3_js_1.PublicKey(userPubKey));
|
|
8
|
+
};
|
|
9
|
+
exports.getTokenAddress = getTokenAddress;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.promiseTimeout = void 0;
|
|
4
|
+
function promiseTimeout(promise, timeoutMs) {
|
|
5
|
+
let timeoutId;
|
|
6
|
+
const timeoutPromise = new Promise((resolve) => {
|
|
7
|
+
timeoutId = setTimeout(() => resolve(null), timeoutMs);
|
|
8
|
+
});
|
|
9
|
+
return Promise.race([promise, timeoutPromise]).then((result) => {
|
|
10
|
+
clearTimeout(timeoutId);
|
|
11
|
+
return result;
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
exports.promiseTimeout = promiseTimeout;
|
package/src/util/tps.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
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.estimateTps = void 0;
|
|
13
|
+
function estimateTps(programId, connection, failed) {
|
|
14
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
let signatures = yield connection.getSignaturesForAddress(programId, undefined, 'finalized');
|
|
16
|
+
if (failed) {
|
|
17
|
+
signatures = signatures.filter((signature) => signature.err);
|
|
18
|
+
}
|
|
19
|
+
const numberOfSignatures = signatures.length;
|
|
20
|
+
if (numberOfSignatures === 0) {
|
|
21
|
+
return 0;
|
|
22
|
+
}
|
|
23
|
+
return (numberOfSignatures /
|
|
24
|
+
(signatures[0].blockTime - signatures[numberOfSignatures - 1].blockTime));
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
exports.estimateTps = estimateTps;
|