@drift-labs/sdk 0.2.0-master.11 → 0.2.0-master.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.
Files changed (72) hide show
  1. package/lib/clearingHouse.d.ts +7 -2
  2. package/lib/clearingHouse.js +157 -37
  3. package/lib/clearingHouseUser.d.ts +10 -15
  4. package/lib/clearingHouseUser.js +92 -74
  5. package/lib/config.js +1 -1
  6. package/lib/constants/banks.d.ts +2 -2
  7. package/lib/constants/banks.js +4 -3
  8. package/lib/constants/numericConstants.d.ts +2 -0
  9. package/lib/constants/numericConstants.js +3 -1
  10. package/lib/events/eventList.js +3 -0
  11. package/lib/events/types.d.ts +2 -1
  12. package/lib/factory/bigNum.d.ts +1 -0
  13. package/lib/factory/bigNum.js +37 -11
  14. package/lib/idl/clearing_house.json +97 -19
  15. package/lib/index.d.ts +1 -0
  16. package/lib/index.js +1 -0
  17. package/lib/math/bankBalance.d.ts +3 -1
  18. package/lib/math/bankBalance.js +54 -1
  19. package/lib/math/margin.d.ts +11 -0
  20. package/lib/math/margin.js +72 -0
  21. package/lib/math/market.d.ts +4 -1
  22. package/lib/math/market.js +35 -1
  23. package/lib/math/position.d.ts +8 -0
  24. package/lib/math/position.js +42 -12
  25. package/lib/orders.d.ts +1 -2
  26. package/lib/orders.js +2 -77
  27. package/lib/tokenFaucet.d.ts +1 -0
  28. package/lib/tokenFaucet.js +23 -12
  29. package/lib/tx/retryTxSender.js +9 -2
  30. package/lib/types.d.ts +24 -3
  31. package/lib/types.js +6 -0
  32. package/lib/util/getTokenAddress.d.ts +2 -0
  33. package/lib/util/getTokenAddress.js +9 -0
  34. package/package.json +1 -1
  35. package/src/clearingHouse.ts +301 -47
  36. package/src/clearingHouseConfig.js +2 -0
  37. package/src/clearingHouseUser.ts +213 -104
  38. package/src/clearingHouseUserConfig.js +2 -0
  39. package/src/config.ts +1 -1
  40. package/src/constants/banks.js +42 -0
  41. package/src/constants/banks.ts +6 -3
  42. package/src/constants/markets.js +42 -0
  43. package/src/constants/numericConstants.js +41 -0
  44. package/src/constants/numericConstants.ts +3 -0
  45. package/src/events/eventList.ts +3 -0
  46. package/src/events/types.ts +2 -0
  47. package/src/factory/bigNum.js +37 -11
  48. package/src/factory/bigNum.ts +43 -13
  49. package/src/idl/clearing_house.json +97 -19
  50. package/src/index.js +67 -98
  51. package/src/index.ts +1 -0
  52. package/src/math/bankBalance.ts +98 -1
  53. package/src/math/margin.ts +124 -0
  54. package/src/math/market.ts +66 -1
  55. package/src/math/position.ts +59 -9
  56. package/src/orders.ts +4 -157
  57. package/src/tokenFaucet.js +189 -0
  58. package/src/tokenFaucet.ts +38 -15
  59. package/src/tx/retryTxSender.ts +11 -3
  60. package/src/types.js +12 -1
  61. package/src/types.ts +25 -3
  62. package/src/{accounts/fetch.js → util/computeUnits.js} +11 -13
  63. package/src/util/getTokenAddress.js +9 -0
  64. package/src/util/getTokenAddress.ts +18 -0
  65. package/tests/bn/test.ts +2 -0
  66. package/src/addresses/pda.js +0 -104
  67. package/src/math/bankBalance.js +0 -75
  68. package/src/math/market.js +0 -57
  69. package/src/math/orders.js +0 -110
  70. package/src/math/position.js +0 -140
  71. package/src/orders.js +0 -134
  72. 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;