@drift-labs/sdk 0.2.0-master.24 → 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.
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +13 -13
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +27 -27
- package/lib/accounts/types.d.ts +8 -9
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +14 -14
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +35 -34
- package/lib/addresses/pda.d.ts +8 -6
- package/lib/addresses/pda.js +35 -27
- package/lib/admin.d.ts +9 -6
- package/lib/admin.js +90 -54
- package/lib/clearingHouse.d.ts +71 -42
- package/lib/clearingHouse.js +765 -282
- package/lib/clearingHouseConfig.d.ts +2 -2
- package/lib/clearingHouseUser.d.ts +19 -17
- package/lib/clearingHouseUser.js +145 -123
- package/lib/config.d.ts +7 -7
- package/lib/config.js +21 -21
- package/lib/constants/numericConstants.d.ts +12 -12
- package/lib/constants/numericConstants.js +13 -13
- package/lib/constants/{markets.d.ts → perpMarkets.d.ts} +5 -5
- package/{src/constants/markets.js → lib/constants/perpMarkets.js} +4 -4
- package/lib/constants/{banks.d.ts → spotMarkets.d.ts} +6 -6
- package/lib/constants/{banks.js → spotMarkets.js} +16 -16
- package/lib/events/eventSubscriber.d.ts +4 -2
- package/lib/events/eventSubscriber.js +16 -9
- package/lib/events/fetchLogs.d.ts +10 -1
- package/lib/events/fetchLogs.js +27 -7
- package/lib/events/pollingLogProvider.d.ts +2 -1
- package/lib/events/pollingLogProvider.js +6 -2
- package/lib/events/types.d.ts +4 -2
- package/lib/events/types.js +2 -0
- package/lib/examples/makeTradeExample.js +18 -6
- package/lib/idl/clearing_house.json +1128 -347
- package/lib/index.d.ts +6 -3
- package/lib/index.js +6 -3
- package/lib/math/amm.d.ts +5 -3
- package/lib/math/amm.js +42 -4
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +2 -1
- package/lib/math/margin.d.ts +4 -4
- package/lib/math/margin.js +18 -11
- package/lib/math/market.d.ts +10 -9
- package/lib/math/market.js +29 -6
- package/lib/math/oracles.d.ts +2 -1
- package/lib/math/oracles.js +11 -1
- package/lib/math/orders.d.ts +5 -5
- package/lib/math/position.d.ts +13 -13
- package/lib/math/position.js +19 -19
- package/lib/math/spotBalance.d.ts +19 -0
- package/lib/math/spotBalance.js +176 -0
- package/lib/math/spotMarket.d.ts +4 -0
- package/lib/math/spotMarket.js +8 -0
- package/lib/math/spotPosition.d.ts +2 -0
- package/lib/math/spotPosition.js +8 -0
- package/lib/math/state.js +2 -2
- package/lib/math/trade.d.ts +4 -4
- package/lib/orderParams.d.ts +4 -4
- package/lib/orderParams.js +12 -4
- package/lib/serum/serumSubscriber.d.ts +23 -0
- package/lib/serum/serumSubscriber.js +41 -0
- package/lib/serum/types.d.ts +11 -0
- package/lib/serum/types.js +2 -0
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/retryTxSender.js +4 -2
- package/lib/tx/types.d.ts +1 -1
- package/lib/types.d.ts +148 -39
- package/lib/types.js +37 -9
- package/my-script/.env +7 -0
- package/my-script/getUserStats.ts +106 -0
- package/my-script/multiConnections.ts +119 -0
- package/my-script/test-regex.ts +11 -0
- package/my-script/utils.ts +52 -0
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.js +249 -0
- package/src/accounts/bulkUserStatsSubscription.js +75 -0
- package/src/accounts/bulkUserSubscription.js +75 -0
- package/src/accounts/fetch.js +92 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +465 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +38 -38
- package/src/accounts/pollingOracleSubscriber.js +156 -0
- package/src/accounts/pollingTokenAccountSubscriber.js +141 -0
- package/src/accounts/pollingUserAccountSubscriber.js +208 -0
- package/src/accounts/pollingUserStatsAccountSubscriber.js +208 -0
- package/src/accounts/types.js +28 -0
- package/src/accounts/types.ts +11 -9
- package/src/accounts/utils.js +7 -0
- package/src/accounts/webSocketAccountSubscriber.js +138 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +433 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +59 -52
- package/src/accounts/webSocketUserAccountSubscriber.js +113 -0
- package/src/accounts/webSocketUserStatsAccountSubsriber.js +113 -0
- package/src/addresses/pda.js +186 -0
- package/src/addresses/pda.ts +56 -42
- package/src/admin.js +1284 -0
- package/src/admin.ts +149 -75
- package/src/assert/assert.js +1 -1
- package/src/clearingHouse.js +3433 -0
- package/src/clearingHouse.ts +1097 -380
- package/src/clearingHouseConfig.js +2 -0
- package/src/clearingHouseConfig.ts +2 -2
- package/src/clearingHouseUser.js +874 -0
- package/src/clearingHouseUser.ts +237 -172
- package/src/clearingHouseUserConfig.js +2 -0
- package/src/clearingHouseUserStats.js +115 -0
- package/src/clearingHouseUserStatsConfig.js +2 -0
- package/src/config.js +80 -0
- package/src/config.ts +30 -30
- package/src/constants/numericConstants.js +18 -11
- package/src/constants/numericConstants.ts +17 -15
- package/{lib/constants/markets.js → src/constants/perpMarkets.js} +11 -11
- package/src/constants/{markets.ts → perpMarkets.ts} +5 -5
- package/src/constants/spotMarkets.js +51 -0
- package/src/constants/{banks.ts → spotMarkets.ts} +19 -19
- package/src/events/eventList.js +66 -23
- package/src/events/eventSubscriber.js +202 -0
- package/src/events/eventSubscriber.ts +20 -12
- package/src/events/fetchLogs.js +117 -0
- package/src/events/fetchLogs.ts +35 -8
- package/src/events/pollingLogProvider.js +113 -0
- package/src/events/pollingLogProvider.ts +10 -2
- package/src/events/sort.js +41 -0
- package/src/events/txEventCache.js +22 -19
- package/src/events/types.js +25 -0
- package/src/events/types.ts +7 -1
- package/src/events/webSocketLogProvider.js +76 -0
- package/src/examples/makeTradeExample.ts +27 -6
- package/src/factory/bigNum.js +183 -180
- package/src/factory/oracleClient.js +9 -9
- package/src/idl/clearing_house.json +1128 -347
- package/src/index.js +75 -0
- package/src/index.ts +6 -3
- package/src/math/amm.js +422 -0
- package/src/math/amm.ts +73 -5
- package/src/math/auction.js +10 -10
- package/src/math/conversion.js +4 -3
- package/src/math/funding.js +223 -175
- package/src/math/funding.ts +7 -7
- package/src/math/insurance.js +27 -0
- package/src/math/margin.js +77 -0
- package/src/math/margin.ts +34 -23
- package/src/math/market.js +105 -0
- package/src/math/market.ts +71 -19
- package/src/math/oracles.js +40 -10
- package/src/math/oracles.ts +18 -1
- package/src/math/orders.js +153 -0
- package/src/math/orders.ts +5 -5
- package/src/math/position.js +172 -0
- package/src/math/position.ts +31 -31
- package/src/math/repeg.js +40 -40
- package/src/math/spotBalance.js +176 -0
- package/src/math/spotBalance.ts +290 -0
- package/src/math/spotMarket.js +8 -0
- package/src/math/spotMarket.ts +9 -0
- package/src/math/spotPosition.js +8 -0
- package/src/math/spotPosition.ts +6 -0
- package/src/math/state.ts +2 -2
- package/src/math/trade.js +81 -74
- package/src/math/trade.ts +4 -4
- package/src/math/utils.js +8 -7
- package/src/oracles/oracleClientCache.js +10 -9
- package/src/oracles/pythClient.js +52 -17
- package/src/oracles/quoteAssetOracleClient.js +44 -13
- package/src/oracles/switchboardClient.js +69 -37
- package/src/oracles/types.js +1 -1
- package/src/orderParams.js +14 -6
- package/src/orderParams.ts +16 -8
- package/src/serum/serumSubscriber.js +102 -0
- package/src/serum/serumSubscriber.ts +80 -0
- package/src/serum/types.js +2 -0
- package/src/serum/types.ts +13 -0
- package/src/slot/SlotSubscriber.js +67 -20
- package/src/token/index.js +4 -4
- package/src/tokenFaucet.js +288 -154
- package/src/tx/retryTxSender.js +280 -0
- package/src/tx/retryTxSender.ts +5 -2
- package/src/tx/types.js +1 -1
- package/src/tx/types.ts +2 -1
- package/src/tx/utils.js +7 -6
- package/src/types.js +216 -0
- package/src/types.ts +131 -39
- package/src/userName.js +5 -5
- package/src/util/computeUnits.js +46 -11
- package/src/util/promiseTimeout.js +5 -5
- package/src/util/tps.js +46 -12
- package/src/wallet.js +55 -18
- package/lib/math/bankBalance.d.ts +0 -15
- package/lib/math/bankBalance.js +0 -150
- package/src/addresses/marketAddresses.js +0 -26
- package/src/constants/banks.js +0 -42
- package/src/examples/makeTradeExample.js +0 -80
- package/src/math/bankBalance.ts +0 -258
- package/src/math/state.js +0 -15
- package/src/math/utils.js.map +0 -1
- package/src/util/getTokenAddress.js +0 -9
package/src/tokenFaucet.js
CHANGED
|
@@ -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
|
|
31
|
-
|
|
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
|
-
|
|
38
|
+
exports.__esModule = true;
|
|
34
39
|
exports.TokenFaucet = void 0;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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 (
|
|
61
|
-
|
|
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
|
+
});
|
|
64
76
|
});
|
|
65
|
-
}
|
|
66
|
-
getFaucetConfigPublicKey() {
|
|
67
|
-
return __awaiter(this, void 0, void 0, function
|
|
68
|
-
return (
|
|
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
|
-
|
|
74
|
-
return
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
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
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
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
|
-
|
|
158
|
-
return
|
|
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
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
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;
|