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

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 (184) hide show
  1. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +13 -13
  2. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +27 -27
  3. package/lib/accounts/types.d.ts +8 -9
  4. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +14 -14
  5. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +35 -34
  6. package/lib/addresses/pda.d.ts +7 -6
  7. package/lib/addresses/pda.js +31 -27
  8. package/lib/admin.d.ts +9 -6
  9. package/lib/admin.js +83 -42
  10. package/lib/clearingHouse.d.ts +69 -42
  11. package/lib/clearingHouse.js +753 -277
  12. package/lib/clearingHouseConfig.d.ts +2 -2
  13. package/lib/clearingHouseUser.d.ts +16 -16
  14. package/lib/clearingHouseUser.js +139 -119
  15. package/lib/config.d.ts +7 -7
  16. package/lib/config.js +20 -20
  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/{src/constants/markets.js → lib/constants/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/events/types.d.ts +2 -1
  24. package/lib/events/types.js +1 -0
  25. package/lib/examples/makeTradeExample.js +7 -7
  26. package/lib/idl/clearing_house.json +1008 -279
  27. package/lib/index.d.ts +5 -3
  28. package/lib/index.js +5 -3
  29. package/lib/math/amm.d.ts +2 -2
  30. package/lib/math/amm.js +1 -1
  31. package/lib/math/funding.d.ts +6 -6
  32. package/lib/math/funding.js +2 -1
  33. package/lib/math/margin.d.ts +4 -4
  34. package/lib/math/margin.js +18 -11
  35. package/lib/math/market.d.ts +10 -9
  36. package/lib/math/market.js +29 -6
  37. package/lib/math/oracles.d.ts +2 -1
  38. package/lib/math/oracles.js +11 -1
  39. package/lib/math/orders.d.ts +5 -5
  40. package/lib/math/position.d.ts +13 -13
  41. package/lib/math/position.js +19 -19
  42. package/lib/math/spotBalance.d.ts +19 -0
  43. package/lib/math/spotBalance.js +176 -0
  44. package/lib/math/spotMarket.d.ts +4 -0
  45. package/lib/math/spotMarket.js +8 -0
  46. package/lib/math/spotPosition.d.ts +2 -0
  47. package/lib/math/spotPosition.js +8 -0
  48. package/lib/math/state.js +2 -2
  49. package/lib/math/trade.d.ts +4 -4
  50. package/lib/orderParams.d.ts +4 -4
  51. package/lib/orderParams.js +12 -4
  52. package/lib/serum/serumSubscriber.d.ts +23 -0
  53. package/lib/serum/serumSubscriber.js +41 -0
  54. package/lib/serum/types.d.ts +11 -0
  55. package/lib/serum/types.js +2 -0
  56. package/lib/tx/retryTxSender.d.ts +1 -1
  57. package/lib/tx/retryTxSender.js +4 -2
  58. package/lib/tx/types.d.ts +1 -1
  59. package/lib/types.d.ts +123 -33
  60. package/lib/types.js +31 -9
  61. package/my-script/.env +7 -0
  62. package/my-script/getUserStats.ts +106 -0
  63. package/my-script/multiConnections.ts +119 -0
  64. package/my-script/test-regex.ts +11 -0
  65. package/my-script/utils.ts +52 -0
  66. package/package.json +1 -1
  67. package/src/accounts/bulkAccountLoader.js +249 -0
  68. package/src/accounts/bulkUserStatsSubscription.js +75 -0
  69. package/src/accounts/bulkUserSubscription.js +75 -0
  70. package/src/accounts/fetch.js +92 -0
  71. package/src/accounts/pollingClearingHouseAccountSubscriber.js +465 -0
  72. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +38 -38
  73. package/src/accounts/pollingOracleSubscriber.js +156 -0
  74. package/src/accounts/pollingTokenAccountSubscriber.js +141 -0
  75. package/src/accounts/pollingUserAccountSubscriber.js +208 -0
  76. package/src/accounts/pollingUserStatsAccountSubscriber.js +208 -0
  77. package/src/accounts/types.js +28 -0
  78. package/src/accounts/types.ts +11 -9
  79. package/src/accounts/utils.js +7 -0
  80. package/src/accounts/webSocketAccountSubscriber.js +138 -0
  81. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +433 -0
  82. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +59 -52
  83. package/src/accounts/webSocketUserAccountSubscriber.js +113 -0
  84. package/src/accounts/webSocketUserStatsAccountSubsriber.js +113 -0
  85. package/src/addresses/pda.js +186 -0
  86. package/src/addresses/pda.ts +49 -44
  87. package/src/admin.js +1284 -0
  88. package/src/admin.ts +140 -47
  89. package/src/assert/assert.js +1 -1
  90. package/src/clearingHouse.js +3433 -0
  91. package/src/clearingHouse.ts +1083 -378
  92. package/src/clearingHouseConfig.js +2 -0
  93. package/src/clearingHouseConfig.ts +2 -2
  94. package/src/clearingHouseUser.js +874 -0
  95. package/src/clearingHouseUser.ts +232 -168
  96. package/src/clearingHouseUserConfig.js +2 -0
  97. package/src/clearingHouseUserStats.js +115 -0
  98. package/src/clearingHouseUserStatsConfig.js +2 -0
  99. package/src/config.js +80 -0
  100. package/src/config.ts +29 -29
  101. package/src/constants/numericConstants.js +18 -11
  102. package/src/constants/numericConstants.ts +17 -15
  103. package/{lib/constants/markets.js → src/constants/perpMarkets.js} +11 -11
  104. package/src/constants/{markets.ts → perpMarkets.ts} +5 -5
  105. package/src/constants/spotMarkets.js +51 -0
  106. package/src/constants/{banks.ts → spotMarkets.ts} +19 -19
  107. package/src/events/eventList.js +66 -23
  108. package/src/events/eventSubscriber.js +202 -0
  109. package/src/events/fetchLogs.js +117 -0
  110. package/src/events/pollingLogProvider.js +113 -0
  111. package/src/events/sort.js +41 -0
  112. package/src/events/txEventCache.js +22 -19
  113. package/src/events/types.js +25 -0
  114. package/src/events/types.ts +3 -0
  115. package/src/events/webSocketLogProvider.js +76 -0
  116. package/src/examples/makeTradeExample.ts +10 -8
  117. package/src/factory/bigNum.js +183 -180
  118. package/src/factory/oracleClient.js +9 -9
  119. package/src/idl/clearing_house.json +1008 -279
  120. package/src/index.js +75 -0
  121. package/src/index.ts +5 -3
  122. package/src/math/amm.js +422 -0
  123. package/src/math/amm.ts +6 -3
  124. package/src/math/auction.js +10 -10
  125. package/src/math/conversion.js +4 -3
  126. package/src/math/funding.js +223 -175
  127. package/src/math/funding.ts +7 -7
  128. package/src/math/insurance.js +27 -0
  129. package/src/math/margin.js +77 -0
  130. package/src/math/margin.ts +34 -23
  131. package/src/math/market.js +105 -0
  132. package/src/math/market.ts +71 -19
  133. package/src/math/oracles.js +40 -10
  134. package/src/math/oracles.ts +18 -1
  135. package/src/math/orders.js +153 -0
  136. package/src/math/orders.ts +5 -5
  137. package/src/math/position.js +172 -0
  138. package/src/math/position.ts +31 -31
  139. package/src/math/repeg.js +40 -40
  140. package/src/math/spotBalance.js +176 -0
  141. package/src/math/spotBalance.ts +290 -0
  142. package/src/math/spotMarket.js +8 -0
  143. package/src/math/spotMarket.ts +9 -0
  144. package/src/math/spotPosition.js +8 -0
  145. package/src/math/spotPosition.ts +6 -0
  146. package/src/math/state.ts +2 -2
  147. package/src/math/trade.js +81 -74
  148. package/src/math/trade.ts +4 -4
  149. package/src/math/utils.js +8 -7
  150. package/src/oracles/oracleClientCache.js +10 -9
  151. package/src/oracles/pythClient.js +52 -17
  152. package/src/oracles/quoteAssetOracleClient.js +44 -13
  153. package/src/oracles/switchboardClient.js +69 -37
  154. package/src/oracles/types.js +1 -1
  155. package/src/orderParams.js +14 -6
  156. package/src/orderParams.ts +16 -8
  157. package/src/serum/serumSubscriber.js +102 -0
  158. package/src/serum/serumSubscriber.ts +80 -0
  159. package/src/serum/types.js +2 -0
  160. package/src/serum/types.ts +13 -0
  161. package/src/slot/SlotSubscriber.js +67 -20
  162. package/src/token/index.js +4 -4
  163. package/src/tokenFaucet.js +288 -154
  164. package/src/tx/retryTxSender.js +280 -0
  165. package/src/tx/retryTxSender.ts +5 -2
  166. package/src/tx/types.js +1 -1
  167. package/src/tx/types.ts +2 -1
  168. package/src/tx/utils.js +7 -6
  169. package/src/types.js +216 -0
  170. package/src/types.ts +110 -33
  171. package/src/userName.js +5 -5
  172. package/src/util/computeUnits.js +46 -11
  173. package/src/util/promiseTimeout.js +5 -5
  174. package/src/util/tps.js +46 -12
  175. package/src/wallet.js +55 -18
  176. package/lib/math/bankBalance.d.ts +0 -15
  177. package/lib/math/bankBalance.js +0 -150
  178. package/src/addresses/marketAddresses.js +0 -26
  179. package/src/constants/banks.js +0 -42
  180. package/src/examples/makeTradeExample.js +0 -80
  181. package/src/math/bankBalance.ts +0 -258
  182. package/src/math/state.js +0 -15
  183. package/src/math/utils.js.map +0 -1
  184. package/src/util/getTokenAddress.js +0 -9
@@ -8,32 +8,79 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- Object.defineProperty(exports, "__esModule", { value: true });
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ exports.__esModule = true;
12
39
  exports.SlotSubscriber = void 0;
13
- const events_1 = require("events");
14
- class SlotSubscriber {
15
- constructor(connection, _config) {
40
+ var events_1 = require("events");
41
+ var SlotSubscriber = /** @class */ (function () {
42
+ function SlotSubscriber(connection, _config) {
16
43
  this.connection = connection;
17
44
  this.eventEmitter = new events_1.EventEmitter();
18
45
  }
19
- subscribe() {
20
- return __awaiter(this, void 0, void 0, function* () {
21
- this.currentSlot = yield this.connection.getSlot('confirmed');
22
- this.subscriptionId = this.connection.onSlotChange((slotInfo) => {
23
- this.currentSlot = slotInfo.slot;
24
- this.eventEmitter.emit('newSlot', slotInfo.slot);
46
+ SlotSubscriber.prototype.subscribe = function () {
47
+ return __awaiter(this, void 0, void 0, function () {
48
+ var _a;
49
+ var _this = this;
50
+ return __generator(this, function (_b) {
51
+ switch (_b.label) {
52
+ case 0:
53
+ _a = this;
54
+ return [4 /*yield*/, this.connection.getSlot('confirmed')];
55
+ case 1:
56
+ _a.currentSlot = _b.sent();
57
+ this.subscriptionId = this.connection.onSlotChange(function (slotInfo) {
58
+ _this.currentSlot = slotInfo.slot;
59
+ _this.eventEmitter.emit('newSlot', slotInfo.slot);
60
+ });
61
+ return [2 /*return*/];
62
+ }
25
63
  });
26
64
  });
27
- }
28
- getSlot() {
65
+ };
66
+ SlotSubscriber.prototype.getSlot = function () {
29
67
  return this.currentSlot;
30
- }
31
- unsubscribe() {
32
- return __awaiter(this, void 0, void 0, function* () {
33
- if (this.subscriptionId) {
34
- yield this.connection.removeSlotChangeListener(this.subscriptionId);
35
- }
68
+ };
69
+ SlotSubscriber.prototype.unsubscribe = function () {
70
+ return __awaiter(this, void 0, void 0, function () {
71
+ return __generator(this, function (_a) {
72
+ switch (_a.label) {
73
+ case 0:
74
+ if (!this.subscriptionId) return [3 /*break*/, 2];
75
+ return [4 /*yield*/, this.connection.removeSlotChangeListener(this.subscriptionId)];
76
+ case 1:
77
+ _a.sent();
78
+ _a.label = 2;
79
+ case 2: return [2 /*return*/];
80
+ }
81
+ });
36
82
  });
37
- }
38
- }
83
+ };
84
+ return SlotSubscriber;
85
+ }());
39
86
  exports.SlotSubscriber = SlotSubscriber;
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
2
+ exports.__esModule = true;
3
3
  exports.parseTokenAccount = void 0;
4
- const spl_token_1 = require("@solana/spl-token");
5
- const web3_js_1 = require("@solana/web3.js");
4
+ var spl_token_1 = require("@solana/spl-token");
5
+ var web3_js_1 = require("@solana/web3.js");
6
6
  function parseTokenAccount(data) {
7
- const accountInfo = spl_token_1.AccountLayout.decode(data);
7
+ var accountInfo = spl_token_1.AccountLayout.decode(data);
8
8
  accountInfo.mint = new web3_js_1.PublicKey(accountInfo.mint);
9
9
  accountInfo.owner = new web3_js_1.PublicKey(accountInfo.owner);
10
10
  accountInfo.amount = spl_token_1.u64.fromBuffer(accountInfo.amount);
@@ -1,23 +1,4 @@
1
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
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -27,163 +8,316 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
27
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
28
9
  });
29
10
  };
30
- var __importDefault = (this && this.__importDefault) || function (mod) {
31
- return (mod && mod.__esModule) ? mod : { "default": mod };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
32
37
  };
33
- Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.__esModule = true;
34
39
  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) {
40
+ var anchor = require("@project-serum/anchor");
41
+ var anchor_1 = require("@project-serum/anchor");
42
+ var spl_token_1 = require("@solana/spl-token");
43
+ var web3_js_1 = require("@solana/web3.js");
44
+ var token_faucet_json_1 = require("./idl/token_faucet.json");
45
+ var TokenFaucet = /** @class */ (function () {
46
+ function TokenFaucet(connection, wallet, programId, mint, opts) {
42
47
  this.connection = connection;
43
48
  this.wallet = wallet;
44
49
  this.opts = opts || anchor_1.AnchorProvider.defaultOptions();
45
- const provider = new anchor_1.AnchorProvider(connection, wallet, this.opts);
50
+ var provider = new anchor_1.AnchorProvider(connection, wallet, this.opts);
46
51
  this.provider = provider;
47
- this.program = new anchor_1.Program(token_faucet_json_1.default, programId, provider);
52
+ this.program = new anchor_1.Program(token_faucet_json_1["default"], programId, provider);
48
53
  this.mint = mint;
49
54
  }
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);
55
+ TokenFaucet.prototype.getFaucetConfigPublicKeyAndNonce = function () {
56
+ return __awaiter(this, void 0, void 0, function () {
57
+ return __generator(this, function (_a) {
58
+ return [2 /*return*/, anchor.web3.PublicKey.findProgramAddress([
59
+ Buffer.from(anchor.utils.bytes.utf8.encode('faucet_config')),
60
+ this.mint.toBuffer(),
61
+ ], this.program.programId)];
62
+ });
56
63
  });
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
+ TokenFaucet.prototype.getMintAuthority = function () {
66
+ return __awaiter(this, void 0, void 0, function () {
67
+ return __generator(this, function (_a) {
68
+ switch (_a.label) {
69
+ case 0: return [4 /*yield*/, anchor.web3.PublicKey.findProgramAddress([
70
+ Buffer.from(anchor.utils.bytes.utf8.encode('mint_authority')),
71
+ this.mint.toBuffer(),
72
+ ], this.program.programId)];
73
+ case 1: return [2 /*return*/, (_a.sent())[0]];
74
+ }
75
+ });
64
76
  });
65
- }
66
- getFaucetConfigPublicKey() {
67
- return __awaiter(this, void 0, void 0, function* () {
68
- return (yield this.getFaucetConfigPublicKeyAndNonce())[0];
77
+ };
78
+ TokenFaucet.prototype.getFaucetConfigPublicKey = function () {
79
+ return __awaiter(this, void 0, void 0, function () {
80
+ return __generator(this, function (_a) {
81
+ switch (_a.label) {
82
+ case 0: return [4 /*yield*/, this.getFaucetConfigPublicKeyAndNonce()];
83
+ case 1: return [2 /*return*/, (_a.sent())[0]];
84
+ }
85
+ });
69
86
  });
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
- },
87
+ };
88
+ TokenFaucet.prototype.initialize = function () {
89
+ return __awaiter(this, void 0, void 0, function () {
90
+ var faucetConfigPublicKey;
91
+ return __generator(this, function (_a) {
92
+ switch (_a.label) {
93
+ case 0: return [4 /*yield*/, this.getFaucetConfigPublicKeyAndNonce()];
94
+ case 1:
95
+ faucetConfigPublicKey = (_a.sent())[0];
96
+ return [4 /*yield*/, this.program.rpc.initialize({
97
+ accounts: {
98
+ faucetConfig: faucetConfigPublicKey,
99
+ admin: this.wallet.publicKey,
100
+ mintAccount: this.mint,
101
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
102
+ systemProgram: anchor.web3.SystemProgram.programId,
103
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID
104
+ }
105
+ })];
106
+ case 2: return [2 /*return*/, _a.sent()];
107
+ }
83
108
  });
84
109
  });
85
- }
86
- fetchState() {
87
- return __awaiter(this, void 0, void 0, function* () {
88
- return yield this.program.account.faucetConfig.fetch(yield this.getFaucetConfigPublicKey());
110
+ };
111
+ TokenFaucet.prototype.fetchState = function () {
112
+ return __awaiter(this, void 0, void 0, function () {
113
+ var _a, _b;
114
+ return __generator(this, function (_c) {
115
+ switch (_c.label) {
116
+ case 0:
117
+ _b = (_a = this.program.account.faucetConfig).fetch;
118
+ return [4 /*yield*/, this.getFaucetConfigPublicKey()];
119
+ case 1: return [4 /*yield*/, _b.apply(_a, [_c.sent()])];
120
+ case 2: return [2 /*return*/, _c.sent()];
121
+ }
122
+ });
89
123
  });
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
- },
124
+ };
125
+ TokenFaucet.prototype.mintToUserIx = function (userTokenAccount, amount) {
126
+ return __awaiter(this, void 0, void 0, function () {
127
+ var _a, _b, _c;
128
+ var _d, _e;
129
+ return __generator(this, function (_f) {
130
+ switch (_f.label) {
131
+ case 0:
132
+ _b = (_a = this.program.instruction).mintToUser;
133
+ _c = [amount];
134
+ _d = {};
135
+ _e = {};
136
+ return [4 /*yield*/, this.getFaucetConfigPublicKey()];
137
+ case 1:
138
+ _e.faucetConfig = _f.sent(),
139
+ _e.mintAccount = this.mint,
140
+ _e.userTokenAccount = userTokenAccount;
141
+ return [4 /*yield*/, this.getMintAuthority()];
142
+ case 2: return [2 /*return*/, _b.apply(_a, _c.concat([(_d.accounts = (_e.mintAuthority = _f.sent(),
143
+ _e.tokenProgram = spl_token_1.TOKEN_PROGRAM_ID,
144
+ _e),
145
+ _d)]))];
146
+ }
101
147
  });
102
148
  });
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;
149
+ };
150
+ TokenFaucet.prototype.mintToUser = function (userTokenAccount, amount) {
151
+ return __awaiter(this, void 0, void 0, function () {
152
+ var mintIx, tx, txSig;
153
+ return __generator(this, function (_a) {
154
+ switch (_a.label) {
155
+ case 0: return [4 /*yield*/, this.mintToUserIx(userTokenAccount, amount)];
156
+ case 1:
157
+ mintIx = _a.sent();
158
+ tx = new web3_js_1.Transaction().add(mintIx);
159
+ return [4 /*yield*/, this.program.provider.sendAndConfirm(tx, [], this.opts)];
160
+ case 2:
161
+ txSig = _a.sent();
162
+ return [2 /*return*/, txSig];
163
+ }
164
+ });
110
165
  });
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
- },
166
+ };
167
+ TokenFaucet.prototype.transferMintAuthority = function () {
168
+ return __awaiter(this, void 0, void 0, function () {
169
+ var _a, _b;
170
+ var _c, _d;
171
+ return __generator(this, function (_e) {
172
+ switch (_e.label) {
173
+ case 0:
174
+ _b = (_a = this.program.rpc).transferMintAuthority;
175
+ _c = {};
176
+ _d = {};
177
+ return [4 /*yield*/, this.getFaucetConfigPublicKey()];
178
+ case 1:
179
+ _d.faucetConfig = _e.sent(),
180
+ _d.mintAccount = this.mint;
181
+ return [4 /*yield*/, this.getMintAuthority()];
182
+ case 2: return [4 /*yield*/, _b.apply(_a, [(_c.accounts = (_d.mintAuthority = _e.sent(),
183
+ _d.tokenProgram = spl_token_1.TOKEN_PROGRAM_ID,
184
+ _d.admin = this.wallet.publicKey,
185
+ _d),
186
+ _c)])];
187
+ case 3: return [2 /*return*/, _e.sent()];
188
+ }
122
189
  });
123
190
  });
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];
191
+ };
192
+ TokenFaucet.prototype.createAssociatedTokenAccountAndMintTo = function (userPublicKey, amount) {
193
+ return __awaiter(this, void 0, void 0, function () {
194
+ var tx, _a, associatedTokenPublicKey, createAssociatedAccountIx, mintToTx, associatedTokenAccountExists, assosciatedTokenAccount, e_1, skipAccountCreation, txSig;
195
+ return __generator(this, function (_b) {
196
+ switch (_b.label) {
197
+ case 0:
198
+ tx = new web3_js_1.Transaction();
199
+ return [4 /*yield*/, this.createAssociatedTokenAccountAndMintToInstructions(userPublicKey, amount)];
200
+ case 1:
201
+ _a = _b.sent(), associatedTokenPublicKey = _a[0], createAssociatedAccountIx = _a[1], mintToTx = _a[2];
202
+ associatedTokenAccountExists = false;
203
+ _b.label = 2;
204
+ case 2:
205
+ _b.trys.push([2, 4, , 5]);
206
+ return [4 /*yield*/, this.connection.getAccountInfo(associatedTokenPublicKey)];
207
+ case 3:
208
+ assosciatedTokenAccount = _b.sent();
209
+ associatedTokenAccountExists = !!assosciatedTokenAccount;
210
+ return [3 /*break*/, 5];
211
+ case 4:
212
+ e_1 = _b.sent();
213
+ // token account doesn't exist
214
+ associatedTokenAccountExists = false;
215
+ return [3 /*break*/, 5];
216
+ case 5:
217
+ skipAccountCreation = associatedTokenAccountExists;
218
+ if (!skipAccountCreation)
219
+ tx.add(createAssociatedAccountIx);
220
+ tx.add(mintToTx);
221
+ return [4 /*yield*/, this.program.provider.sendAndConfirm(tx, [], this.opts)];
222
+ case 6:
223
+ txSig = _b.sent();
224
+ return [2 /*return*/, [associatedTokenPublicKey, txSig]];
225
+ }
226
+ });
144
227
  });
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];
228
+ };
229
+ TokenFaucet.prototype.createAssociatedTokenAccountAndMintToInstructions = function (userPublicKey, amount) {
230
+ return __awaiter(this, void 0, void 0, function () {
231
+ var state, associateTokenPublicKey, createAssociatedAccountIx, mintToIx;
232
+ return __generator(this, function (_a) {
233
+ switch (_a.label) {
234
+ case 0: return [4 /*yield*/, this.fetchState()];
235
+ case 1:
236
+ state = _a.sent();
237
+ return [4 /*yield*/, this.getAssosciatedMockUSDMintAddress({ userPubKey: userPublicKey })];
238
+ case 2:
239
+ associateTokenPublicKey = _a.sent();
240
+ 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);
241
+ return [4 /*yield*/, this.mintToUserIx(associateTokenPublicKey, amount)];
242
+ case 3:
243
+ mintToIx = _a.sent();
244
+ return [2 /*return*/, [associateTokenPublicKey, createAssociatedAccountIx, mintToIx]];
245
+ }
246
+ });
153
247
  });
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);
248
+ };
249
+ TokenFaucet.prototype.getAssosciatedMockUSDMintAddress = function (props) {
250
+ return __awaiter(this, void 0, void 0, function () {
251
+ var state;
252
+ return __generator(this, function (_a) {
253
+ switch (_a.label) {
254
+ case 0: return [4 /*yield*/, this.fetchState()];
255
+ case 1:
256
+ state = _a.sent();
257
+ return [2 /*return*/, spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, state.mint, props.userPubKey)];
258
+ }
259
+ });
159
260
  });
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);
261
+ };
262
+ TokenFaucet.prototype.getTokenAccountInfo = function (props) {
263
+ return __awaiter(this, void 0, void 0, function () {
264
+ var assosciatedKey, state, token;
265
+ return __generator(this, function (_a) {
266
+ switch (_a.label) {
267
+ case 0: return [4 /*yield*/, this.getAssosciatedMockUSDMintAddress(props)];
268
+ case 1:
269
+ assosciatedKey = _a.sent();
270
+ return [4 /*yield*/, this.fetchState()];
271
+ case 2:
272
+ state = _a.sent();
273
+ token = new spl_token_1.Token(this.connection, state.mint, spl_token_1.TOKEN_PROGRAM_ID,
274
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
275
+ // @ts-ignore
276
+ this.provider.payer);
277
+ return [4 /*yield*/, token.getAccountInfo(assosciatedKey)];
278
+ case 3: return [2 /*return*/, _a.sent()];
279
+ }
280
+ });
170
281
  });
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
- }
282
+ };
283
+ TokenFaucet.prototype.subscribeToTokenAccount = function (props) {
284
+ return __awaiter(this, void 0, void 0, function () {
285
+ var tokenAccountKey, _a, _b, e_2;
286
+ var _this = this;
287
+ return __generator(this, function (_c) {
288
+ switch (_c.label) {
289
+ case 0:
290
+ _c.trys.push([0, 3, , 4]);
291
+ return [4 /*yield*/, this.getAssosciatedMockUSDMintAddress(props)];
292
+ case 1:
293
+ tokenAccountKey = _c.sent();
294
+ _b = (_a = props).callback;
295
+ return [4 /*yield*/, this.getTokenAccountInfo(props)];
296
+ case 2:
297
+ _b.apply(_a, [_c.sent()]);
298
+ // Couldn't find a way to do it using anchor framework subscription, someone on serum discord recommended this way
299
+ this.connection.onAccountChange(tokenAccountKey, function (_accountInfo /* accountInfo is a buffer which we don't know how to deserialize */) { return __awaiter(_this, void 0, void 0, function () {
300
+ var _a, _b;
301
+ return __generator(this, function (_c) {
302
+ switch (_c.label) {
303
+ case 0:
304
+ _b = (_a = props).callback;
305
+ return [4 /*yield*/, this.getTokenAccountInfo(props)];
306
+ case 1:
307
+ _b.apply(_a, [_c.sent()]);
308
+ return [2 /*return*/];
309
+ }
310
+ });
311
+ }); });
312
+ return [2 /*return*/, true];
313
+ case 3:
314
+ e_2 = _c.sent();
315
+ return [2 /*return*/, false];
316
+ case 4: return [2 /*return*/];
317
+ }
318
+ });
186
319
  });
187
- }
188
- }
320
+ };
321
+ return TokenFaucet;
322
+ }());
189
323
  exports.TokenFaucet = TokenFaucet;