@drift-labs/sdk 0.2.0-master.25 → 0.2.0-master.27

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 (124) hide show
  1. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +14 -13
  2. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +30 -27
  3. package/lib/accounts/types.d.ts +9 -9
  4. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +15 -14
  5. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +38 -34
  6. package/lib/addresses/pda.d.ts +7 -6
  7. package/lib/addresses/pda.js +31 -27
  8. package/lib/admin.d.ts +13 -7
  9. package/lib/admin.js +111 -44
  10. package/lib/clearingHouse.d.ts +71 -42
  11. package/lib/clearingHouse.js +767 -278
  12. package/lib/clearingHouseConfig.d.ts +2 -2
  13. package/lib/clearingHouseUser.d.ts +24 -22
  14. package/lib/clearingHouseUser.js +273 -177
  15. package/lib/config.d.ts +7 -7
  16. package/lib/config.js +21 -21
  17. package/lib/constants/numericConstants.d.ts +12 -12
  18. package/lib/constants/numericConstants.js +13 -13
  19. package/lib/constants/{markets.d.ts → perpMarkets.d.ts} +5 -5
  20. package/lib/constants/{markets.js → perpMarkets.js} +4 -4
  21. package/lib/constants/{banks.d.ts → spotMarkets.d.ts} +6 -6
  22. package/lib/constants/{banks.js → spotMarkets.js} +16 -16
  23. package/lib/dlob/DLOB.d.ts +73 -0
  24. package/lib/dlob/DLOB.js +557 -0
  25. package/lib/dlob/DLOBNode.d.ts +52 -0
  26. package/lib/dlob/DLOBNode.js +82 -0
  27. package/lib/dlob/NodeList.d.ts +26 -0
  28. package/lib/dlob/NodeList.js +138 -0
  29. package/lib/events/types.d.ts +2 -1
  30. package/lib/events/types.js +1 -0
  31. package/lib/examples/makeTradeExample.js +7 -7
  32. package/lib/idl/clearing_house.json +1152 -503
  33. package/lib/index.d.ts +10 -3
  34. package/lib/index.js +10 -3
  35. package/lib/math/amm.d.ts +2 -2
  36. package/lib/math/amm.js +1 -1
  37. package/lib/math/funding.d.ts +6 -6
  38. package/lib/math/funding.js +2 -1
  39. package/lib/math/margin.d.ts +4 -4
  40. package/lib/math/margin.js +18 -11
  41. package/lib/math/market.d.ts +11 -10
  42. package/lib/math/market.js +30 -7
  43. package/lib/math/oracles.d.ts +2 -1
  44. package/lib/math/oracles.js +11 -1
  45. package/lib/math/orders.d.ts +6 -6
  46. package/lib/math/orders.js +31 -16
  47. package/lib/math/position.d.ts +13 -13
  48. package/lib/math/position.js +19 -19
  49. package/lib/math/spotBalance.d.ts +22 -0
  50. package/lib/math/spotBalance.js +193 -0
  51. package/lib/math/spotMarket.d.ts +4 -0
  52. package/lib/math/spotMarket.js +8 -0
  53. package/lib/math/spotPosition.d.ts +6 -0
  54. package/lib/math/spotPosition.js +23 -0
  55. package/lib/math/state.js +2 -2
  56. package/lib/math/trade.d.ts +4 -4
  57. package/lib/orderParams.d.ts +4 -4
  58. package/lib/orderParams.js +12 -4
  59. package/lib/serum/serumSubscriber.d.ts +23 -0
  60. package/lib/serum/serumSubscriber.js +41 -0
  61. package/lib/serum/types.d.ts +11 -0
  62. package/lib/serum/types.js +2 -0
  63. package/lib/tx/retryTxSender.d.ts +1 -1
  64. package/lib/tx/retryTxSender.js +4 -2
  65. package/lib/tx/types.d.ts +1 -1
  66. package/lib/types.d.ts +148 -57
  67. package/lib/types.js +39 -11
  68. package/lib/userMap/userMap.d.ts +25 -0
  69. package/lib/userMap/userMap.js +73 -0
  70. package/lib/userMap/userStatsMap.d.ts +19 -0
  71. package/lib/userMap/userStatsMap.js +68 -0
  72. package/package.json +6 -3
  73. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +42 -38
  74. package/src/accounts/types.ts +12 -9
  75. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +65 -52
  76. package/src/addresses/pda.ts +49 -44
  77. package/src/admin.ts +190 -55
  78. package/src/clearingHouse.ts +1092 -365
  79. package/src/clearingHouseConfig.ts +2 -2
  80. package/src/clearingHouseUser.ts +518 -255
  81. package/src/config.ts +30 -30
  82. package/src/constants/numericConstants.ts +17 -15
  83. package/src/constants/{markets.ts → perpMarkets.ts} +5 -5
  84. package/src/constants/{banks.ts → spotMarkets.ts} +19 -19
  85. package/src/dlob/DLOB.ts +884 -0
  86. package/src/dlob/DLOBNode.ts +163 -0
  87. package/src/dlob/NodeList.ts +185 -0
  88. package/src/events/types.ts +3 -0
  89. package/src/examples/makeTradeExample.js +152 -75
  90. package/src/examples/makeTradeExample.ts +10 -8
  91. package/src/idl/clearing_house.json +1152 -503
  92. package/src/index.ts +10 -3
  93. package/src/math/amm.ts +6 -3
  94. package/src/math/funding.ts +7 -7
  95. package/src/math/margin.ts +34 -23
  96. package/src/math/market.ts +72 -20
  97. package/src/math/oracles.ts +18 -1
  98. package/src/math/orders.ts +33 -25
  99. package/src/math/position.ts +31 -31
  100. package/src/math/spotBalance.ts +316 -0
  101. package/src/math/spotMarket.ts +9 -0
  102. package/src/math/spotPosition.ts +47 -0
  103. package/src/math/state.ts +2 -2
  104. package/src/math/trade.ts +4 -4
  105. package/src/orderParams.ts +16 -8
  106. package/src/serum/serumSubscriber.ts +80 -0
  107. package/src/serum/types.ts +13 -0
  108. package/src/tx/retryTxSender.ts +5 -2
  109. package/src/tx/types.ts +2 -1
  110. package/src/types.ts +135 -56
  111. package/src/userMap/userMap.ts +100 -0
  112. package/src/userMap/userStatsMap.ts +110 -0
  113. package/tests/bn/test.ts +2 -3
  114. package/tests/dlob/helpers.ts +322 -0
  115. package/tests/dlob/test.ts +2865 -0
  116. package/lib/math/bankBalance.d.ts +0 -15
  117. package/lib/math/bankBalance.js +0 -150
  118. package/src/constants/numericConstants.js +0 -41
  119. package/src/math/bankBalance.ts +0 -258
  120. package/src/math/oracles.js +0 -26
  121. package/src/math/state.js +0 -15
  122. package/src/orderParams.js +0 -20
  123. package/src/slot/SlotSubscriber.js +0 -39
  124. package/src/tokenFaucet.js +0 -189
@@ -1,189 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
- Object.defineProperty(o, "default", { enumerable: true, value: v });
11
- }) : function(o, v) {
12
- o["default"] = v;
13
- });
14
- var __importStar = (this && this.__importStar) || function (mod) {
15
- if (mod && mod.__esModule) return mod;
16
- var result = {};
17
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
- __setModuleDefault(result, mod);
19
- return result;
20
- };
21
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
- return new (P || (P = Promise))(function (resolve, reject) {
24
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
- step((generator = generator.apply(thisArg, _arguments || [])).next());
28
- });
29
- };
30
- var __importDefault = (this && this.__importDefault) || function (mod) {
31
- return (mod && mod.__esModule) ? mod : { "default": mod };
32
- };
33
- Object.defineProperty(exports, "__esModule", { value: true });
34
- exports.TokenFaucet = void 0;
35
- const anchor = __importStar(require("@project-serum/anchor"));
36
- const anchor_1 = require("@project-serum/anchor");
37
- const spl_token_1 = require("@solana/spl-token");
38
- const web3_js_1 = require("@solana/web3.js");
39
- const token_faucet_json_1 = __importDefault(require("./idl/token_faucet.json"));
40
- class TokenFaucet {
41
- constructor(connection, wallet, programId, mint, opts) {
42
- this.connection = connection;
43
- this.wallet = wallet;
44
- this.opts = opts || anchor_1.AnchorProvider.defaultOptions();
45
- const provider = new anchor_1.AnchorProvider(connection, wallet, this.opts);
46
- this.provider = provider;
47
- this.program = new anchor_1.Program(token_faucet_json_1.default, programId, provider);
48
- this.mint = mint;
49
- }
50
- getFaucetConfigPublicKeyAndNonce() {
51
- return __awaiter(this, void 0, void 0, function* () {
52
- return anchor.web3.PublicKey.findProgramAddress([
53
- Buffer.from(anchor.utils.bytes.utf8.encode('faucet_config')),
54
- this.mint.toBuffer(),
55
- ], this.program.programId);
56
- });
57
- }
58
- getMintAuthority() {
59
- return __awaiter(this, void 0, void 0, function* () {
60
- return (yield anchor.web3.PublicKey.findProgramAddress([
61
- Buffer.from(anchor.utils.bytes.utf8.encode('mint_authority')),
62
- this.mint.toBuffer(),
63
- ], this.program.programId))[0];
64
- });
65
- }
66
- getFaucetConfigPublicKey() {
67
- return __awaiter(this, void 0, void 0, function* () {
68
- return (yield this.getFaucetConfigPublicKeyAndNonce())[0];
69
- });
70
- }
71
- initialize() {
72
- return __awaiter(this, void 0, void 0, function* () {
73
- const [faucetConfigPublicKey] = yield this.getFaucetConfigPublicKeyAndNonce();
74
- return yield this.program.rpc.initialize({
75
- accounts: {
76
- faucetConfig: faucetConfigPublicKey,
77
- admin: this.wallet.publicKey,
78
- mintAccount: this.mint,
79
- rent: web3_js_1.SYSVAR_RENT_PUBKEY,
80
- systemProgram: anchor.web3.SystemProgram.programId,
81
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
82
- },
83
- });
84
- });
85
- }
86
- fetchState() {
87
- return __awaiter(this, void 0, void 0, function* () {
88
- return yield this.program.account.faucetConfig.fetch(yield this.getFaucetConfigPublicKey());
89
- });
90
- }
91
- mintToUserIx(userTokenAccount, amount) {
92
- return __awaiter(this, void 0, void 0, function* () {
93
- return this.program.instruction.mintToUser(amount, {
94
- accounts: {
95
- faucetConfig: yield this.getFaucetConfigPublicKey(),
96
- mintAccount: this.mint,
97
- userTokenAccount,
98
- mintAuthority: yield this.getMintAuthority(),
99
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
100
- },
101
- });
102
- });
103
- }
104
- mintToUser(userTokenAccount, amount) {
105
- return __awaiter(this, void 0, void 0, function* () {
106
- const mintIx = yield this.mintToUserIx(userTokenAccount, amount);
107
- const tx = new web3_js_1.Transaction().add(mintIx);
108
- const txSig = yield this.program.provider.sendAndConfirm(tx, [], this.opts);
109
- return txSig;
110
- });
111
- }
112
- transferMintAuthority() {
113
- return __awaiter(this, void 0, void 0, function* () {
114
- return yield this.program.rpc.transferMintAuthority({
115
- accounts: {
116
- faucetConfig: yield this.getFaucetConfigPublicKey(),
117
- mintAccount: this.mint,
118
- mintAuthority: yield this.getMintAuthority(),
119
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
120
- admin: this.wallet.publicKey,
121
- },
122
- });
123
- });
124
- }
125
- createAssociatedTokenAccountAndMintTo(userPublicKey, amount) {
126
- return __awaiter(this, void 0, void 0, function* () {
127
- const tx = new web3_js_1.Transaction();
128
- const [associatedTokenPublicKey, createAssociatedAccountIx, mintToTx] = yield this.createAssociatedTokenAccountAndMintToInstructions(userPublicKey, amount);
129
- let associatedTokenAccountExists = false;
130
- try {
131
- const assosciatedTokenAccount = yield this.connection.getAccountInfo(associatedTokenPublicKey);
132
- associatedTokenAccountExists = !!assosciatedTokenAccount;
133
- }
134
- catch (e) {
135
- // token account doesn't exist
136
- associatedTokenAccountExists = false;
137
- }
138
- const skipAccountCreation = associatedTokenAccountExists;
139
- if (!skipAccountCreation)
140
- tx.add(createAssociatedAccountIx);
141
- tx.add(mintToTx);
142
- const txSig = yield this.program.provider.sendAndConfirm(tx, [], this.opts);
143
- return [associatedTokenPublicKey, txSig];
144
- });
145
- }
146
- createAssociatedTokenAccountAndMintToInstructions(userPublicKey, amount) {
147
- return __awaiter(this, void 0, void 0, function* () {
148
- const state = yield this.fetchState();
149
- const associateTokenPublicKey = yield this.getAssosciatedMockUSDMintAddress({ userPubKey: userPublicKey });
150
- const createAssociatedAccountIx = spl_token_1.Token.createAssociatedTokenAccountInstruction(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, state.mint, associateTokenPublicKey, userPublicKey, this.wallet.publicKey);
151
- const mintToIx = yield this.mintToUserIx(associateTokenPublicKey, amount);
152
- return [associateTokenPublicKey, createAssociatedAccountIx, mintToIx];
153
- });
154
- }
155
- getAssosciatedMockUSDMintAddress(props) {
156
- return __awaiter(this, void 0, void 0, function* () {
157
- const state = yield this.fetchState();
158
- return spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, state.mint, props.userPubKey);
159
- });
160
- }
161
- getTokenAccountInfo(props) {
162
- return __awaiter(this, void 0, void 0, function* () {
163
- const assosciatedKey = yield this.getAssosciatedMockUSDMintAddress(props);
164
- const state = yield this.fetchState();
165
- const token = new spl_token_1.Token(this.connection, state.mint, spl_token_1.TOKEN_PROGRAM_ID,
166
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
167
- // @ts-ignore
168
- this.provider.payer);
169
- return yield token.getAccountInfo(assosciatedKey);
170
- });
171
- }
172
- subscribeToTokenAccount(props) {
173
- return __awaiter(this, void 0, void 0, function* () {
174
- try {
175
- const tokenAccountKey = yield this.getAssosciatedMockUSDMintAddress(props);
176
- props.callback(yield this.getTokenAccountInfo(props));
177
- // Couldn't find a way to do it using anchor framework subscription, someone on serum discord recommended this way
178
- this.connection.onAccountChange(tokenAccountKey, (_accountInfo /* accountInfo is a buffer which we don't know how to deserialize */) => __awaiter(this, void 0, void 0, function* () {
179
- props.callback(yield this.getTokenAccountInfo(props));
180
- }));
181
- return true;
182
- }
183
- catch (e) {
184
- return false;
185
- }
186
- });
187
- }
188
- }
189
- exports.TokenFaucet = TokenFaucet;