@drift-labs/sdk 0.2.0-master.29 → 0.2.0-master.30
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/README.md +13 -13
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +10 -11
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +4 -4
- package/lib/accounts/types.d.ts +4 -6
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +10 -11
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +6 -8
- package/lib/addresses/marketAddresses.d.ts +1 -3
- package/lib/addresses/pda.d.ts +5 -5
- package/lib/addresses/pda.js +5 -5
- package/lib/admin.d.ts +27 -28
- package/lib/admin.js +12 -37
- package/lib/clearingHouse.d.ts +61 -62
- package/lib/clearingHouse.js +92 -110
- package/lib/clearingHouseConfig.d.ts +2 -4
- package/lib/clearingHouseUser.d.ts +16 -16
- package/lib/clearingHouseUser.js +16 -16
- package/lib/config.d.ts +2 -4
- package/lib/config.js +1 -1
- package/lib/constants/numericConstants.d.ts +7 -8
- package/lib/constants/numericConstants.js +17 -18
- package/lib/constants/perpMarkets.d.ts +2 -3
- package/lib/constants/perpMarkets.js +3 -3
- package/lib/constants/spotMarkets.d.ts +2 -1
- package/lib/constants/spotMarkets.js +6 -4
- package/lib/dlob/DLOB.d.ts +13 -13
- package/lib/dlob/DLOB.js +36 -40
- package/lib/dlob/DLOBNode.js +2 -2
- package/lib/events/sort.js +1 -1
- package/lib/examples/makeTradeExample.js +3 -3
- package/lib/idl/clearing_house.json +359 -310
- package/lib/math/amm.d.ts +2 -2
- package/lib/math/amm.js +10 -10
- package/lib/math/conversion.js +1 -1
- package/lib/math/funding.js +9 -9
- package/lib/math/margin.js +3 -3
- package/lib/math/market.d.ts +4 -4
- package/lib/math/market.js +7 -7
- package/lib/math/oracles.js +6 -8
- package/lib/math/position.d.ts +2 -2
- package/lib/math/position.js +9 -9
- package/lib/math/repeg.js +7 -6
- package/lib/math/spotBalance.js +5 -5
- package/lib/math/spotPosition.js +2 -2
- package/lib/math/trade.d.ts +6 -6
- package/lib/math/trade.js +15 -19
- package/lib/oracles/pythClient.js +1 -1
- package/lib/oracles/quoteAssetOracleClient.js +1 -1
- package/lib/oracles/switchboardClient.js +1 -1
- package/lib/types.d.ts +63 -51
- package/lib/types.js +1 -1
- package/package.json +2 -1
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +15 -15
- package/src/accounts/types.ts +4 -5
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +17 -25
- package/src/addresses/marketAddresses.ts +1 -2
- package/src/addresses/pda.ts +10 -10
- package/src/admin.ts +42 -79
- package/src/clearingHouse.ts +164 -212
- package/src/clearingHouseConfig.ts +2 -3
- package/src/clearingHouseUser.ts +35 -35
- package/src/config.ts +3 -4
- package/src/constants/numericConstants.ts +19 -21
- package/src/constants/perpMarkets.ts +5 -5
- package/src/constants/spotMarkets.ts +8 -5
- package/src/dlob/DLOB.ts +54 -70
- package/src/dlob/DLOBNode.ts +5 -6
- package/src/events/sort.ts +1 -1
- package/src/examples/makeTradeExample.js +2 -2
- package/src/examples/makeTradeExample.ts +5 -8
- package/src/idl/clearing_house.json +359 -310
- package/src/math/amm.ts +14 -11
- package/src/math/conversion.ts +2 -2
- package/src/math/funding.ts +13 -11
- package/src/math/margin.ts +4 -5
- package/src/math/market.ts +5 -5
- package/src/math/oracles.ts +9 -9
- package/src/math/position.ts +11 -19
- package/src/math/repeg.ts +8 -7
- package/src/math/spotBalance.ts +6 -6
- package/src/math/spotPosition.ts +2 -2
- package/src/math/trade.ts +17 -21
- package/src/oracles/pythClient.ts +2 -2
- package/src/oracles/quoteAssetOracleClient.ts +2 -2
- package/src/oracles/switchboardClient.ts +2 -2
- package/src/types.ts +69 -51
- package/tests/dlob/helpers.ts +56 -4
- package/src/addresses/marketAddresses.js +0 -26
- package/src/constants/banks.js +0 -42
- package/src/constants/markets.js +0 -42
- package/src/events/txEventCache.js +0 -71
- package/src/factory/bigNum.js +0 -390
- package/src/factory/oracleClient.js +0 -20
- package/src/math/auction.js +0 -42
- package/src/math/conversion.js +0 -11
- package/src/math/funding.js +0 -248
- package/src/math/repeg.js +0 -128
- package/src/math/trade.js +0 -253
- package/src/math/utils.js +0 -26
- package/src/math/utils.js.map +0 -1
- package/src/oracles/oracleClientCache.js +0 -19
- package/src/oracles/pythClient.js +0 -46
- package/src/oracles/quoteAssetOracleClient.js +0 -32
- package/src/oracles/switchboardClient.js +0 -69
- package/src/oracles/types.js +0 -2
- package/src/userName.js +0 -20
- package/src/wallet.js +0 -35
|
@@ -1,46 +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
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.convertPythPrice = exports.PythClient = void 0;
|
|
13
|
-
const client_1 = require("@pythnetwork/client");
|
|
14
|
-
const anchor_1 = require("@project-serum/anchor");
|
|
15
|
-
const numericConstants_1 = require("../constants/numericConstants");
|
|
16
|
-
class PythClient {
|
|
17
|
-
constructor(connection) {
|
|
18
|
-
this.connection = connection;
|
|
19
|
-
}
|
|
20
|
-
getOraclePriceData(pricePublicKey) {
|
|
21
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
-
const accountInfo = yield this.connection.getAccountInfo(pricePublicKey);
|
|
23
|
-
return this.getOraclePriceDataFromBuffer(accountInfo.data);
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
getOraclePriceDataFromBuffer(buffer) {
|
|
27
|
-
const priceData = client_1.parsePriceData(buffer);
|
|
28
|
-
return {
|
|
29
|
-
price: convertPythPrice(priceData.aggregate.price, priceData.exponent),
|
|
30
|
-
slot: new anchor_1.BN(priceData.lastSlot.toString()),
|
|
31
|
-
confidence: convertPythPrice(priceData.confidence, priceData.exponent),
|
|
32
|
-
twap: convertPythPrice(priceData.twap.value, priceData.exponent),
|
|
33
|
-
twapConfidence: convertPythPrice(priceData.twac.value, priceData.exponent),
|
|
34
|
-
hasSufficientNumberOfDataPoints: true,
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
exports.PythClient = PythClient;
|
|
39
|
-
function convertPythPrice(price, exponent) {
|
|
40
|
-
exponent = Math.abs(exponent);
|
|
41
|
-
const pythPrecision = numericConstants_1.TEN.pow(new anchor_1.BN(exponent).abs());
|
|
42
|
-
return new anchor_1.BN(price * Math.pow(10, exponent))
|
|
43
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
44
|
-
.div(pythPrecision);
|
|
45
|
-
}
|
|
46
|
-
exports.convertPythPrice = convertPythPrice;
|
|
@@ -1,32 +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
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.QuoteAssetOracleClient = exports.QUOTE_ORACLE_PRICE_DATA = void 0;
|
|
13
|
-
const anchor_1 = require("@project-serum/anchor");
|
|
14
|
-
const numericConstants_1 = require("../constants/numericConstants");
|
|
15
|
-
exports.QUOTE_ORACLE_PRICE_DATA = {
|
|
16
|
-
price: numericConstants_1.MARK_PRICE_PRECISION,
|
|
17
|
-
slot: new anchor_1.BN(0),
|
|
18
|
-
confidence: new anchor_1.BN(1),
|
|
19
|
-
hasSufficientNumberOfDataPoints: true,
|
|
20
|
-
};
|
|
21
|
-
class QuoteAssetOracleClient {
|
|
22
|
-
constructor() { }
|
|
23
|
-
getOraclePriceData(_pricePublicKey) {
|
|
24
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
-
return Promise.resolve(exports.QUOTE_ORACLE_PRICE_DATA);
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
getOraclePriceDataFromBuffer(_buffer) {
|
|
29
|
-
return exports.QUOTE_ORACLE_PRICE_DATA;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
exports.QuoteAssetOracleClient = QuoteAssetOracleClient;
|
|
@@ -1,69 +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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.SwitchboardClient = void 0;
|
|
16
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
17
|
-
const anchor_1 = require("@project-serum/anchor");
|
|
18
|
-
const numericConstants_1 = require("../constants/numericConstants");
|
|
19
|
-
const wallet_1 = require("../wallet");
|
|
20
|
-
const switchboard_v2_json_1 = __importDefault(require("../idl/switchboard_v2.json"));
|
|
21
|
-
let program;
|
|
22
|
-
class SwitchboardClient {
|
|
23
|
-
constructor(connection) {
|
|
24
|
-
this.connection = connection;
|
|
25
|
-
}
|
|
26
|
-
getOraclePriceData(pricePublicKey) {
|
|
27
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
28
|
-
const accountInfo = yield this.connection.getAccountInfo(pricePublicKey);
|
|
29
|
-
return this.getOraclePriceDataFromBuffer(accountInfo.data);
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
getOraclePriceDataFromBuffer(buffer) {
|
|
33
|
-
const program = this.getProgram();
|
|
34
|
-
const aggregatorAccountData = program.account.aggregatorAccountData.coder.accounts.decode('AggregatorAccountData', buffer);
|
|
35
|
-
const price = convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound.result);
|
|
36
|
-
const confidence = convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound
|
|
37
|
-
.stdDeviation);
|
|
38
|
-
const hasSufficientNumberOfDataPoints = aggregatorAccountData.latestConfirmedRound.numSuccess >=
|
|
39
|
-
aggregatorAccountData.minOracleResults;
|
|
40
|
-
const slot = aggregatorAccountData.latestConfirmedRound.roundOpenSlot;
|
|
41
|
-
return {
|
|
42
|
-
price,
|
|
43
|
-
slot,
|
|
44
|
-
confidence,
|
|
45
|
-
hasSufficientNumberOfDataPoints,
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
getProgram() {
|
|
49
|
-
if (program) {
|
|
50
|
-
return program;
|
|
51
|
-
}
|
|
52
|
-
program = getSwitchboardProgram(this.connection);
|
|
53
|
-
return program;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
exports.SwitchboardClient = SwitchboardClient;
|
|
57
|
-
function getSwitchboardProgram(connection) {
|
|
58
|
-
const DEFAULT_KEYPAIR = web3_js_1.Keypair.fromSeed(new Uint8Array(32).fill(1));
|
|
59
|
-
const programId = web3_js_1.PublicKey.default;
|
|
60
|
-
const wallet = new wallet_1.Wallet(DEFAULT_KEYPAIR);
|
|
61
|
-
const provider = new anchor_1.AnchorProvider(connection, wallet, {});
|
|
62
|
-
return new anchor_1.Program(switchboard_v2_json_1.default, programId, provider);
|
|
63
|
-
}
|
|
64
|
-
function convertSwitchboardDecimal(switchboardDecimal) {
|
|
65
|
-
const switchboardPrecision = numericConstants_1.TEN.pow(new anchor_1.BN(switchboardDecimal.scale));
|
|
66
|
-
return switchboardDecimal.mantissa
|
|
67
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
68
|
-
.div(switchboardPrecision);
|
|
69
|
-
}
|
package/src/oracles/types.js
DELETED
package/src/userName.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.decodeName = exports.encodeName = exports.DEFAULT_USER_NAME = exports.MAX_NAME_LENGTH = void 0;
|
|
4
|
-
exports.MAX_NAME_LENGTH = 32;
|
|
5
|
-
exports.DEFAULT_USER_NAME = 'Main Account';
|
|
6
|
-
function encodeName(name) {
|
|
7
|
-
if (name.length > exports.MAX_NAME_LENGTH) {
|
|
8
|
-
throw Error(`User name (${name}) longer than 32 characters`);
|
|
9
|
-
}
|
|
10
|
-
const buffer = Buffer.alloc(32);
|
|
11
|
-
buffer.fill(name);
|
|
12
|
-
buffer.fill(' ', name.length);
|
|
13
|
-
return Array(...buffer);
|
|
14
|
-
}
|
|
15
|
-
exports.encodeName = encodeName;
|
|
16
|
-
function decodeName(bytes) {
|
|
17
|
-
const buffer = Buffer.from(bytes);
|
|
18
|
-
return buffer.toString('utf8').trim();
|
|
19
|
-
}
|
|
20
|
-
exports.decodeName = decodeName;
|
package/src/wallet.js
DELETED
|
@@ -1,35 +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
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.Wallet = void 0;
|
|
13
|
-
class Wallet {
|
|
14
|
-
constructor(payer) {
|
|
15
|
-
this.payer = payer;
|
|
16
|
-
}
|
|
17
|
-
signTransaction(tx) {
|
|
18
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
-
tx.partialSign(this.payer);
|
|
20
|
-
return tx;
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
signAllTransactions(txs) {
|
|
24
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
-
return txs.map((t) => {
|
|
26
|
-
t.partialSign(this.payer);
|
|
27
|
-
return t;
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
get publicKey() {
|
|
32
|
-
return this.payer.publicKey;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
exports.Wallet = Wallet;
|