@drift-labs/sdk 0.2.0-master.9 → 0.2.0-temp.2

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.
Files changed (61) hide show
  1. package/lib/admin.d.ts +3 -3
  2. package/lib/admin.js +6 -6
  3. package/lib/clearingHouse.d.ts +15 -4
  4. package/lib/clearingHouse.js +200 -37
  5. package/lib/clearingHouseUser.d.ts +2 -2
  6. package/lib/clearingHouseUser.js +8 -17
  7. package/lib/config.js +1 -1
  8. package/lib/constants/banks.js +2 -3
  9. package/lib/constants/numericConstants.d.ts +1 -0
  10. package/lib/constants/numericConstants.js +2 -1
  11. package/lib/events/eventList.d.ts +1 -0
  12. package/lib/events/eventList.js +10 -0
  13. package/lib/events/txEventCache.d.ts +2 -0
  14. package/lib/events/txEventCache.js +14 -0
  15. package/lib/idl/clearing_house.json +484 -103
  16. package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
  17. package/lib/index.d.ts +1 -1
  18. package/lib/index.js +1 -1
  19. package/lib/orders.js +1 -1
  20. package/lib/{mockUSDCFaucet.d.ts → tokenFaucet.d.ts} +7 -5
  21. package/lib/{mockUSDCFaucet.js → tokenFaucet.js} +41 -40
  22. package/lib/types.d.ts +62 -13
  23. package/lib/types.js +12 -1
  24. package/lib/util/computeUnits.js +1 -1
  25. package/package.json +1 -1
  26. package/src/admin.js +517 -0
  27. package/src/admin.ts +7 -7
  28. package/src/clearingHouse.ts +335 -47
  29. package/src/clearingHouseConfig.js +2 -0
  30. package/src/clearingHouseUser.ts +12 -23
  31. package/src/clearingHouseUserConfig.js +2 -0
  32. package/src/config.js +67 -0
  33. package/src/config.ts +1 -1
  34. package/src/constants/banks.js +42 -0
  35. package/src/constants/banks.ts +2 -3
  36. package/src/constants/markets.js +42 -0
  37. package/src/constants/numericConstants.js +41 -0
  38. package/src/constants/numericConstants.ts +1 -0
  39. package/src/events/eventList.ts +10 -0
  40. package/src/events/txEventCache.ts +16 -0
  41. package/src/factory/bigNum.js +37 -11
  42. package/src/idl/clearing_house.json +484 -103
  43. package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
  44. package/src/index.js +1 -1
  45. package/src/index.ts +1 -1
  46. package/src/mockUSDCFaucet.js +276 -167
  47. package/src/orders.ts +2 -1
  48. package/src/tokenFaucet.js +189 -0
  49. package/src/{mockUSDCFaucet.ts → tokenFaucet.ts} +48 -59
  50. package/src/types.js +12 -1
  51. package/src/types.ts +63 -13
  52. package/src/{accounts/fetch.js → util/computeUnits.js} +11 -13
  53. package/src/util/computeUnits.ts +1 -1
  54. package/src/util/getTokenAddress.js +9 -0
  55. package/src/addresses/pda.js +0 -104
  56. package/src/math/bankBalance.js +0 -75
  57. package/src/math/market.js +0 -57
  58. package/src/math/orders.js +0 -110
  59. package/src/math/position.js +0 -140
  60. package/src/orders.js +0 -134
  61. package/src/tx/retryTxSender.js +0 -188
@@ -1,188 +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
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.RetryTxSender = void 0;
16
- const assert_1 = __importDefault(require("assert"));
17
- const bs58_1 = __importDefault(require("bs58"));
18
- const DEFAULT_TIMEOUT = 35000;
19
- const DEFAULT_RETRY = 8000;
20
- class RetryTxSender {
21
- constructor(provider, timeout, retrySleep, additionalConnections = new Array()) {
22
- this.provider = provider;
23
- this.timeout = timeout !== null && timeout !== void 0 ? timeout : DEFAULT_TIMEOUT;
24
- this.retrySleep = retrySleep !== null && retrySleep !== void 0 ? retrySleep : DEFAULT_RETRY;
25
- this.additionalConnections = additionalConnections;
26
- }
27
- send(tx, additionalSigners, opts) {
28
- return __awaiter(this, void 0, void 0, function* () {
29
- if (additionalSigners === undefined) {
30
- additionalSigners = [];
31
- }
32
- if (opts === undefined) {
33
- opts = this.provider.opts;
34
- }
35
- yield this.prepareTx(tx, additionalSigners, opts);
36
- const rawTransaction = tx.serialize();
37
- const startTime = this.getTimestamp();
38
- const txid = yield this.provider.connection.sendRawTransaction(rawTransaction, opts);
39
- this.sendToAdditionalConnections(rawTransaction, opts);
40
- let done = false;
41
- const resolveReference = {
42
- resolve: undefined,
43
- };
44
- const stopWaiting = () => {
45
- done = true;
46
- if (resolveReference.resolve) {
47
- resolveReference.resolve();
48
- }
49
- };
50
- (() => __awaiter(this, void 0, void 0, function* () {
51
- while (!done && this.getTimestamp() - startTime < this.timeout) {
52
- yield this.sleep(resolveReference);
53
- if (!done) {
54
- this.provider.connection
55
- .sendRawTransaction(rawTransaction, opts)
56
- .catch((e) => {
57
- console.error(e);
58
- stopWaiting();
59
- });
60
- this.sendToAdditionalConnections(rawTransaction, opts);
61
- }
62
- }
63
- }))();
64
- let slot;
65
- try {
66
- const result = yield this.confirmTransaction(txid, opts.commitment);
67
- slot = result.context.slot;
68
- }
69
- catch (e) {
70
- console.error(e);
71
- throw e;
72
- }
73
- finally {
74
- stopWaiting();
75
- }
76
- return { txSig: txid, slot };
77
- });
78
- }
79
- prepareTx(tx, additionalSigners, opts) {
80
- return __awaiter(this, void 0, void 0, function* () {
81
- tx.feePayer = this.provider.wallet.publicKey;
82
- tx.recentBlockhash = (yield this.provider.connection.getRecentBlockhash(opts.preflightCommitment)).blockhash;
83
- yield this.provider.wallet.signTransaction(tx);
84
- additionalSigners
85
- .filter((s) => s !== undefined)
86
- .forEach((kp) => {
87
- tx.partialSign(kp);
88
- });
89
- return tx;
90
- });
91
- }
92
- confirmTransaction(signature, commitment) {
93
- return __awaiter(this, void 0, void 0, function* () {
94
- let decodedSignature;
95
- try {
96
- decodedSignature = bs58_1.default.decode(signature);
97
- }
98
- catch (err) {
99
- throw new Error('signature must be base58 encoded: ' + signature);
100
- }
101
- assert_1.default(decodedSignature.length === 64, 'signature has invalid length');
102
- const start = Date.now();
103
- const subscriptionCommitment = commitment || this.provider.opts.commitment;
104
- const subscriptionIds = new Array();
105
- const connections = [
106
- this.provider.connection,
107
- ...this.additionalConnections,
108
- ];
109
- let response = null;
110
- const promises = connections.map((connection, i) => {
111
- let subscriptionId;
112
- const confirmPromise = new Promise((resolve, reject) => {
113
- try {
114
- subscriptionId = connection.onSignature(signature, (result, context) => {
115
- subscriptionIds[i] = undefined;
116
- response = {
117
- context,
118
- value: result,
119
- };
120
- resolve(null);
121
- }, subscriptionCommitment);
122
- }
123
- catch (err) {
124
- reject(err);
125
- }
126
- });
127
- subscriptionIds.push(subscriptionId);
128
- return confirmPromise;
129
- });
130
- try {
131
- yield this.promiseTimeout(promises, this.timeout);
132
- }
133
- finally {
134
- for (const [i, subscriptionId] of subscriptionIds.entries()) {
135
- if (subscriptionId) {
136
- connections[i].removeSignatureListener(subscriptionId);
137
- }
138
- }
139
- }
140
- if (response === null) {
141
- const duration = (Date.now() - start) / 1000;
142
- throw new Error(`Transaction was not confirmed in ${duration.toFixed(2)} seconds. It is unknown if it succeeded or failed. Check signature ${signature} using the Solana Explorer or CLI tools.`);
143
- }
144
- return response;
145
- });
146
- }
147
- getTimestamp() {
148
- return new Date().getTime();
149
- }
150
- sleep(reference) {
151
- return __awaiter(this, void 0, void 0, function* () {
152
- return new Promise((resolve) => {
153
- reference.resolve = resolve;
154
- setTimeout(resolve, this.retrySleep);
155
- });
156
- });
157
- }
158
- promiseTimeout(promises, timeoutMs) {
159
- let timeoutId;
160
- const timeoutPromise = new Promise((resolve) => {
161
- timeoutId = setTimeout(() => resolve(null), timeoutMs);
162
- });
163
- return Promise.race([...promises, timeoutPromise]).then((result) => {
164
- clearTimeout(timeoutId);
165
- return result;
166
- });
167
- }
168
- sendToAdditionalConnections(rawTx, opts) {
169
- this.additionalConnections.map((connection) => {
170
- connection.sendRawTransaction(rawTx, opts).catch((e) => {
171
- console.error(
172
- // @ts-ignore
173
- `error sending tx to additional connection ${connection._rpcEndpoint}`);
174
- console.error(e);
175
- });
176
- });
177
- }
178
- addAdditionalConnection(newConnection) {
179
- const alreadyUsingConnection = this.additionalConnections.filter((connection) => {
180
- // @ts-ignore
181
- return connection._rpcEndpoint === newConnection.rpcEndpoint;
182
- }).length > 0;
183
- if (!alreadyUsingConnection) {
184
- this.additionalConnections.push(newConnection);
185
- }
186
- }
187
- }
188
- exports.RetryTxSender = RetryTxSender;