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

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 (143) hide show
  1. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +1 -0
  2. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +3 -0
  3. package/lib/accounts/types.d.ts +2 -0
  4. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +1 -0
  5. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +3 -0
  6. package/lib/admin.d.ts +5 -2
  7. package/lib/admin.js +30 -4
  8. package/lib/clearingHouse.d.ts +3 -0
  9. package/lib/clearingHouse.js +19 -1
  10. package/lib/clearingHouseUser.d.ts +11 -9
  11. package/lib/clearingHouseUser.js +184 -108
  12. package/lib/config.js +1 -1
  13. package/lib/dlob/DLOB.d.ts +73 -0
  14. package/lib/dlob/DLOB.js +557 -0
  15. package/lib/dlob/DLOBNode.d.ts +52 -0
  16. package/lib/dlob/DLOBNode.js +82 -0
  17. package/lib/dlob/NodeList.d.ts +26 -0
  18. package/lib/dlob/NodeList.js +138 -0
  19. package/lib/idl/clearing_house.json +123 -203
  20. package/lib/index.d.ts +5 -0
  21. package/lib/index.js +5 -0
  22. package/lib/math/market.d.ts +3 -2
  23. package/lib/math/market.js +17 -12
  24. package/lib/math/orders.d.ts +3 -3
  25. package/lib/math/orders.js +31 -16
  26. package/lib/math/position.d.ts +3 -3
  27. package/lib/math/position.js +23 -16
  28. package/lib/math/repeg.js +8 -0
  29. package/lib/math/spotBalance.d.ts +3 -0
  30. package/lib/math/spotBalance.js +18 -1
  31. package/lib/math/spotPosition.d.ts +5 -1
  32. package/lib/math/spotPosition.js +16 -1
  33. package/lib/types.d.ts +25 -24
  34. package/lib/types.js +9 -3
  35. package/lib/userMap/userMap.d.ts +25 -0
  36. package/lib/userMap/userMap.js +73 -0
  37. package/lib/userMap/userStatsMap.d.ts +19 -0
  38. package/lib/userMap/userStatsMap.js +68 -0
  39. package/package.json +6 -3
  40. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +4 -0
  41. package/src/accounts/types.ts +1 -0
  42. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +6 -0
  43. package/src/admin.ts +50 -8
  44. package/src/clearingHouse.ts +30 -1
  45. package/src/clearingHouseUser.ts +366 -167
  46. package/src/config.ts +1 -1
  47. package/src/dlob/DLOB.ts +884 -0
  48. package/src/dlob/DLOBNode.ts +163 -0
  49. package/src/dlob/NodeList.ts +185 -0
  50. package/src/idl/clearing_house.json +123 -203
  51. package/src/index.ts +5 -0
  52. package/src/math/market.ts +22 -13
  53. package/src/math/orders.ts +29 -21
  54. package/src/math/position.ts +36 -22
  55. package/src/math/repeg.ts +9 -0
  56. package/src/math/spotBalance.ts +26 -0
  57. package/src/math/spotPosition.ts +42 -1
  58. package/src/types.ts +26 -24
  59. package/src/userMap/userMap.ts +100 -0
  60. package/src/userMap/userStatsMap.ts +110 -0
  61. package/tests/bn/test.ts +2 -3
  62. package/tests/dlob/helpers.ts +322 -0
  63. package/tests/dlob/test.ts +2865 -0
  64. package/my-script/.env +0 -7
  65. package/my-script/getUserStats.ts +0 -106
  66. package/my-script/multiConnections.ts +0 -119
  67. package/my-script/test-regex.ts +0 -11
  68. package/my-script/utils.ts +0 -52
  69. package/src/accounts/bulkAccountLoader.js +0 -249
  70. package/src/accounts/bulkUserStatsSubscription.js +0 -75
  71. package/src/accounts/bulkUserSubscription.js +0 -75
  72. package/src/accounts/fetch.js +0 -92
  73. package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -465
  74. package/src/accounts/pollingOracleSubscriber.js +0 -156
  75. package/src/accounts/pollingTokenAccountSubscriber.js +0 -141
  76. package/src/accounts/pollingUserAccountSubscriber.js +0 -208
  77. package/src/accounts/pollingUserStatsAccountSubscriber.js +0 -208
  78. package/src/accounts/types.js +0 -28
  79. package/src/accounts/utils.js +0 -7
  80. package/src/accounts/webSocketAccountSubscriber.js +0 -138
  81. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -433
  82. package/src/accounts/webSocketUserAccountSubscriber.js +0 -113
  83. package/src/accounts/webSocketUserStatsAccountSubsriber.js +0 -113
  84. package/src/addresses/pda.js +0 -186
  85. package/src/admin.js +0 -1284
  86. package/src/assert/assert.js +0 -9
  87. package/src/clearingHouse.js +0 -3433
  88. package/src/clearingHouseConfig.js +0 -2
  89. package/src/clearingHouseUser.js +0 -874
  90. package/src/clearingHouseUserConfig.js +0 -2
  91. package/src/clearingHouseUserStats.js +0 -115
  92. package/src/clearingHouseUserStatsConfig.js +0 -2
  93. package/src/config.js +0 -80
  94. package/src/constants/numericConstants.js +0 -48
  95. package/src/constants/perpMarkets.js +0 -42
  96. package/src/constants/spotMarkets.js +0 -51
  97. package/src/events/eventList.js +0 -120
  98. package/src/events/eventSubscriber.js +0 -202
  99. package/src/events/fetchLogs.js +0 -117
  100. package/src/events/pollingLogProvider.js +0 -113
  101. package/src/events/sort.js +0 -41
  102. package/src/events/txEventCache.js +0 -74
  103. package/src/events/types.js +0 -25
  104. package/src/events/webSocketLogProvider.js +0 -76
  105. package/src/factory/bigNum.js +0 -393
  106. package/src/factory/oracleClient.js +0 -20
  107. package/src/index.js +0 -75
  108. package/src/math/amm.js +0 -422
  109. package/src/math/auction.js +0 -42
  110. package/src/math/conversion.js +0 -12
  111. package/src/math/funding.js +0 -296
  112. package/src/math/insurance.js +0 -27
  113. package/src/math/margin.js +0 -77
  114. package/src/math/market.js +0 -105
  115. package/src/math/oracles.js +0 -56
  116. package/src/math/orders.js +0 -153
  117. package/src/math/position.js +0 -172
  118. package/src/math/repeg.js +0 -128
  119. package/src/math/spotBalance.js +0 -176
  120. package/src/math/spotMarket.js +0 -8
  121. package/src/math/spotPosition.js +0 -8
  122. package/src/math/trade.js +0 -260
  123. package/src/math/utils.js +0 -27
  124. package/src/oracles/oracleClientCache.js +0 -20
  125. package/src/oracles/pythClient.js +0 -81
  126. package/src/oracles/quoteAssetOracleClient.js +0 -63
  127. package/src/oracles/switchboardClient.js +0 -101
  128. package/src/oracles/types.js +0 -2
  129. package/src/orderParams.js +0 -28
  130. package/src/serum/serumSubscriber.js +0 -102
  131. package/src/serum/types.js +0 -2
  132. package/src/slot/SlotSubscriber.js +0 -86
  133. package/src/token/index.js +0 -38
  134. package/src/tokenFaucet.js +0 -323
  135. package/src/tx/retryTxSender.js +0 -280
  136. package/src/tx/types.js +0 -2
  137. package/src/tx/utils.js +0 -18
  138. package/src/types.js +0 -216
  139. package/src/userName.js +0 -20
  140. package/src/util/computeUnits.js +0 -62
  141. package/src/util/promiseTimeout.js +0 -14
  142. package/src/util/tps.js +0 -61
  143. package/src/wallet.js +0 -72
@@ -1,102 +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 __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;
39
- exports.SerumSubscriber = void 0;
40
- var serum_1 = require("@project-serum/serum");
41
- var SerumSubscriber = /** @class */ (function () {
42
- function SerumSubscriber(config) {
43
- this.connection = config.connection;
44
- this.programId = config.programId;
45
- this.marketAddress = config.marketAddress;
46
- this.accountLoader = config.accountSubscription.accountLoader;
47
- }
48
- SerumSubscriber.prototype.subscribe = function () {
49
- return __awaiter(this, void 0, void 0, function () {
50
- var _a, _b, _c;
51
- var _this = this;
52
- return __generator(this, function (_d) {
53
- switch (_d.label) {
54
- case 0:
55
- if (this.subscribed) {
56
- return [2 /*return*/];
57
- }
58
- _a = this;
59
- return [4 /*yield*/, serum_1.Market.load(this.connection, this.marketAddress, undefined, this.programId)];
60
- case 1:
61
- _a.market = _d.sent();
62
- this.asksAddress = this.market.asksAddress;
63
- _b = this;
64
- return [4 /*yield*/, this.market.loadAsks(this.connection)];
65
- case 2:
66
- _b.asks = _d.sent();
67
- this.asksCallbackId = this.accountLoader.addAccount(this.asksAddress, function (buffer, slot) {
68
- _this.lastAsksSlot = slot;
69
- _this.asks = serum_1.Orderbook.decode(_this.market, buffer);
70
- console.log(_this.asks.getL2(3));
71
- });
72
- this.bidsAddress = this.market.bidsAddress;
73
- _c = this;
74
- return [4 /*yield*/, this.market.loadBids(this.connection)];
75
- case 3:
76
- _c.bids = _d.sent();
77
- this.bidsCallbackId = this.accountLoader.addAccount(this.bidsAddress, function (buffer, slot) {
78
- _this.lastBidsSlot = slot;
79
- _this.bids = serum_1.Orderbook.decode(_this.market, buffer);
80
- });
81
- this.subscribed = true;
82
- return [2 /*return*/];
83
- }
84
- });
85
- });
86
- };
87
- SerumSubscriber.prototype.unsubscribe = function () {
88
- return __awaiter(this, void 0, void 0, function () {
89
- return __generator(this, function (_a) {
90
- if (!this.subscribed) {
91
- return [2 /*return*/];
92
- }
93
- this.accountLoader.removeAccount(this.asksAddress, this.asksCallbackId);
94
- this.accountLoader.removeAccount(this.bidsAddress, this.bidsCallbackId);
95
- this.subscribed = false;
96
- return [2 /*return*/];
97
- });
98
- });
99
- };
100
- return SerumSubscriber;
101
- }());
102
- exports.SerumSubscriber = SerumSubscriber;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- exports.__esModule = true;
@@ -1,86 +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 __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;
39
- exports.SlotSubscriber = void 0;
40
- var events_1 = require("events");
41
- var SlotSubscriber = /** @class */ (function () {
42
- function SlotSubscriber(connection, _config) {
43
- this.connection = connection;
44
- this.eventEmitter = new events_1.EventEmitter();
45
- }
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
- }
63
- });
64
- });
65
- };
66
- SlotSubscriber.prototype.getSlot = function () {
67
- return this.currentSlot;
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
- });
82
- });
83
- };
84
- return SlotSubscriber;
85
- }());
86
- exports.SlotSubscriber = SlotSubscriber;
@@ -1,38 +0,0 @@
1
- "use strict";
2
- exports.__esModule = true;
3
- exports.parseTokenAccount = void 0;
4
- var spl_token_1 = require("@solana/spl-token");
5
- var web3_js_1 = require("@solana/web3.js");
6
- function parseTokenAccount(data) {
7
- var accountInfo = spl_token_1.AccountLayout.decode(data);
8
- accountInfo.mint = new web3_js_1.PublicKey(accountInfo.mint);
9
- accountInfo.owner = new web3_js_1.PublicKey(accountInfo.owner);
10
- accountInfo.amount = spl_token_1.u64.fromBuffer(accountInfo.amount);
11
- if (accountInfo.delegateOption === 0) {
12
- accountInfo.delegate = null;
13
- // eslint-disable-next-line new-cap
14
- accountInfo.delegatedAmount = new spl_token_1.u64(0);
15
- }
16
- else {
17
- accountInfo.delegate = new web3_js_1.PublicKey(accountInfo.delegate);
18
- accountInfo.delegatedAmount = spl_token_1.u64.fromBuffer(accountInfo.delegatedAmount);
19
- }
20
- accountInfo.isInitialized = accountInfo.state !== 0;
21
- accountInfo.isFrozen = accountInfo.state === 2;
22
- if (accountInfo.isNativeOption === 1) {
23
- accountInfo.rentExemptReserve = spl_token_1.u64.fromBuffer(accountInfo.isNative);
24
- accountInfo.isNative = true;
25
- }
26
- else {
27
- accountInfo.rentExemptReserve = null;
28
- accountInfo.isNative = false;
29
- }
30
- if (accountInfo.closeAuthorityOption === 0) {
31
- accountInfo.closeAuthority = null;
32
- }
33
- else {
34
- accountInfo.closeAuthority = new web3_js_1.PublicKey(accountInfo.closeAuthority);
35
- }
36
- return accountInfo;
37
- }
38
- exports.parseTokenAccount = parseTokenAccount;
@@ -1,323 +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 __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;
39
- exports.TokenFaucet = void 0;
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) {
47
- this.connection = connection;
48
- this.wallet = wallet;
49
- this.opts = opts || anchor_1.AnchorProvider.defaultOptions();
50
- var provider = new anchor_1.AnchorProvider(connection, wallet, this.opts);
51
- this.provider = provider;
52
- this.program = new anchor_1.Program(token_faucet_json_1["default"], programId, provider);
53
- this.mint = mint;
54
- }
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
- });
63
- });
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
- });
76
- });
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
- });
86
- });
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
- }
108
- });
109
- });
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
- });
123
- });
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
- }
147
- });
148
- });
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
- });
165
- });
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
- }
189
- });
190
- });
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
- });
227
- });
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
- });
247
- });
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
- });
260
- });
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
- });
281
- });
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
- });
319
- });
320
- };
321
- return TokenFaucet;
322
- }());
323
- exports.TokenFaucet = TokenFaucet;