@drift-labs/sdk 0.2.0-master.26 → 0.2.0-master.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +1 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +3 -0
- package/lib/accounts/types.d.ts +1 -0
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +1 -0
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +3 -0
- package/lib/admin.d.ts +5 -2
- package/lib/admin.js +30 -4
- package/lib/clearingHouse.d.ts +2 -0
- package/lib/clearingHouse.js +14 -1
- package/lib/clearingHouseUser.d.ts +11 -9
- package/lib/clearingHouseUser.js +182 -106
- package/lib/config.js +1 -1
- package/lib/dlob/DLOB.d.ts +73 -0
- package/lib/dlob/DLOB.js +557 -0
- package/lib/dlob/DLOBNode.d.ts +52 -0
- package/lib/dlob/DLOBNode.js +82 -0
- package/lib/dlob/NodeList.d.ts +26 -0
- package/lib/dlob/NodeList.js +138 -0
- package/lib/idl/clearing_house.json +122 -202
- package/lib/index.d.ts +5 -0
- package/lib/index.js +5 -0
- package/lib/math/market.d.ts +1 -1
- package/lib/math/market.js +1 -1
- package/lib/math/orders.d.ts +3 -3
- package/lib/math/orders.js +31 -16
- package/lib/math/spotBalance.d.ts +3 -0
- package/lib/math/spotBalance.js +18 -1
- package/lib/math/spotPosition.d.ts +5 -1
- package/lib/math/spotPosition.js +16 -1
- package/lib/types.d.ts +24 -23
- package/lib/types.js +9 -3
- package/lib/userMap/userMap.d.ts +25 -0
- package/lib/userMap/userMap.js +73 -0
- package/lib/userMap/userStatsMap.d.ts +19 -0
- package/lib/userMap/userStatsMap.js +68 -0
- package/package.json +6 -3
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +4 -0
- package/src/accounts/types.ts +1 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +6 -0
- package/src/addresses/marketAddresses.js +26 -0
- package/src/admin.ts +50 -8
- package/src/assert/assert.js +1 -1
- package/src/clearingHouse.ts +23 -1
- package/src/clearingHouseUser.ts +364 -165
- package/src/config.ts +1 -1
- package/src/constants/banks.js +42 -0
- package/src/constants/{perpMarkets.js → markets.js} +11 -11
- package/src/dlob/DLOB.ts +884 -0
- package/src/dlob/DLOBNode.ts +163 -0
- package/src/dlob/NodeList.ts +185 -0
- package/src/events/eventList.js +23 -66
- package/src/events/txEventCache.js +19 -22
- package/src/examples/makeTradeExample.js +157 -0
- package/src/factory/bigNum.js +180 -183
- package/src/factory/oracleClient.js +9 -9
- package/src/idl/clearing_house.json +122 -202
- package/src/index.ts +5 -0
- package/src/math/auction.js +10 -10
- package/src/math/conversion.js +3 -4
- package/src/math/funding.js +175 -223
- package/src/math/market.ts +1 -1
- package/src/math/orders.ts +29 -21
- package/src/math/repeg.js +40 -40
- package/src/math/spotBalance.ts +26 -0
- package/src/math/spotPosition.ts +42 -1
- package/src/math/trade.js +74 -81
- package/src/math/utils.js +7 -8
- package/src/math/utils.js.map +1 -0
- package/src/oracles/oracleClientCache.js +9 -10
- package/src/oracles/pythClient.js +17 -52
- package/src/oracles/quoteAssetOracleClient.js +13 -44
- package/src/oracles/switchboardClient.js +37 -69
- package/src/oracles/types.js +1 -1
- package/src/token/index.js +4 -4
- package/src/tx/types.js +1 -1
- package/src/tx/utils.js +6 -7
- package/src/types.ts +25 -23
- package/src/userMap/userMap.ts +100 -0
- package/src/userMap/userStatsMap.ts +110 -0
- package/src/userName.js +5 -5
- package/src/util/computeUnits.js +11 -46
- package/src/util/getTokenAddress.js +9 -0
- package/src/util/promiseTimeout.js +5 -5
- package/src/util/tps.js +12 -46
- package/src/wallet.js +18 -55
- package/tests/bn/test.ts +2 -3
- package/tests/dlob/helpers.ts +322 -0
- package/tests/dlob/test.ts +2865 -0
- package/my-script/.env +0 -7
- package/my-script/getUserStats.ts +0 -106
- package/my-script/multiConnections.ts +0 -119
- package/my-script/test-regex.ts +0 -11
- package/my-script/utils.ts +0 -52
- package/src/accounts/bulkAccountLoader.js +0 -249
- package/src/accounts/bulkUserStatsSubscription.js +0 -75
- package/src/accounts/bulkUserSubscription.js +0 -75
- package/src/accounts/fetch.js +0 -92
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -465
- package/src/accounts/pollingOracleSubscriber.js +0 -156
- package/src/accounts/pollingTokenAccountSubscriber.js +0 -141
- package/src/accounts/pollingUserAccountSubscriber.js +0 -208
- package/src/accounts/pollingUserStatsAccountSubscriber.js +0 -208
- package/src/accounts/types.js +0 -28
- package/src/accounts/utils.js +0 -7
- package/src/accounts/webSocketAccountSubscriber.js +0 -138
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -433
- package/src/accounts/webSocketUserAccountSubscriber.js +0 -113
- package/src/accounts/webSocketUserStatsAccountSubsriber.js +0 -113
- package/src/addresses/pda.js +0 -186
- package/src/admin.js +0 -1284
- package/src/clearingHouse.js +0 -3433
- package/src/clearingHouseConfig.js +0 -2
- package/src/clearingHouseUser.js +0 -874
- package/src/clearingHouseUserConfig.js +0 -2
- package/src/clearingHouseUserStats.js +0 -115
- package/src/clearingHouseUserStatsConfig.js +0 -2
- package/src/config.js +0 -80
- package/src/constants/numericConstants.js +0 -48
- package/src/constants/spotMarkets.js +0 -51
- package/src/events/eventSubscriber.js +0 -202
- package/src/events/fetchLogs.js +0 -117
- package/src/events/pollingLogProvider.js +0 -113
- package/src/events/sort.js +0 -41
- package/src/events/types.js +0 -25
- package/src/events/webSocketLogProvider.js +0 -76
- package/src/index.js +0 -75
- package/src/math/amm.js +0 -422
- package/src/math/insurance.js +0 -27
- package/src/math/margin.js +0 -77
- package/src/math/market.js +0 -105
- package/src/math/oracles.js +0 -56
- package/src/math/orders.js +0 -153
- package/src/math/position.js +0 -172
- package/src/math/spotBalance.js +0 -176
- package/src/math/spotMarket.js +0 -8
- package/src/math/spotPosition.js +0 -8
- package/src/orderParams.js +0 -28
- package/src/serum/serumSubscriber.js +0 -102
- package/src/serum/types.js +0 -2
- package/src/slot/SlotSubscriber.js +0 -86
- package/src/tokenFaucet.js +0 -323
- package/src/tx/retryTxSender.js +0 -280
- package/src/types.js +0 -216
|
@@ -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;
|
package/src/serum/types.js
DELETED
|
@@ -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;
|
package/src/tokenFaucet.js
DELETED
|
@@ -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;
|