@drift-labs/sdk 0.2.0-master.28 → 0.2.0-master.29
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/types.d.ts +0 -1
- package/lib/admin.d.ts +3 -3
- package/lib/admin.js +5 -5
- package/lib/clearingHouse.d.ts +5 -0
- package/lib/clearingHouse.js +46 -13
- package/lib/clearingHouseConfig.d.ts +1 -0
- package/lib/clearingHouseUser.js +3 -3
- package/lib/config.js +1 -1
- package/lib/idl/clearing_house.json +122 -94
- package/lib/math/insurance.js +1 -1
- package/lib/types.d.ts +21 -38
- package/package.json +1 -1
- package/src/addresses/marketAddresses.js +26 -0
- package/src/admin.ts +14 -20
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +74 -8
- package/src/clearingHouseConfig.ts +1 -0
- package/src/clearingHouseUser.ts +4 -4
- package/src/config.ts +1 -1
- package/src/constants/banks.js +42 -0
- package/src/constants/markets.js +42 -0
- package/src/events/eventList.js +77 -0
- package/src/events/txEventCache.js +71 -0
- package/src/examples/makeTradeExample.js +157 -0
- package/src/factory/bigNum.js +390 -0
- package/src/factory/oracleClient.js +20 -0
- package/src/idl/clearing_house.json +122 -94
- package/src/math/auction.js +42 -0
- package/src/math/conversion.js +11 -0
- package/src/math/funding.js +248 -0
- package/src/math/insurance.ts +2 -2
- package/src/math/repeg.js +128 -0
- package/src/math/trade.js +253 -0
- package/src/math/utils.js +26 -0
- package/src/math/utils.js.map +1 -0
- package/src/oracles/oracleClientCache.js +19 -0
- package/src/oracles/pythClient.js +46 -0
- package/src/oracles/quoteAssetOracleClient.js +32 -0
- package/src/oracles/switchboardClient.js +69 -0
- package/src/oracles/types.js +2 -0
- package/src/token/index.js +38 -0
- package/src/tx/types.js +2 -0
- package/src/tx/utils.js +17 -0
- package/src/types.ts +23 -39
- package/src/userName.js +20 -0
- package/src/util/computeUnits.js +27 -0
- package/src/util/getTokenAddress.js +9 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/src/wallet.js +35 -0
- package/lib/math/state.d.ts +0 -8
- package/lib/math/state.js +0 -15
- package/src/math/state.ts +0 -14
package/src/types.ts
CHANGED
|
@@ -385,8 +385,6 @@ export type StateAccount = {
|
|
|
385
385
|
exchangePaused: boolean;
|
|
386
386
|
adminControlsPrices: boolean;
|
|
387
387
|
insuranceVault: PublicKey;
|
|
388
|
-
perpFeeStructure: FeeStructure;
|
|
389
|
-
spotFeeStructure: FeeStructure;
|
|
390
388
|
totalFee: BN;
|
|
391
389
|
totalFeeWithdrawn: BN;
|
|
392
390
|
whitelistMint: PublicKey;
|
|
@@ -402,6 +400,8 @@ export type StateAccount = {
|
|
|
402
400
|
minPerpAuctionDuration: number;
|
|
403
401
|
defaultSpotAuctionDuration: number;
|
|
404
402
|
liquidationMarginBufferRatio: number;
|
|
403
|
+
perpFeeStructure: FeeStructure;
|
|
404
|
+
spotFeeStructure: FeeStructure;
|
|
405
405
|
};
|
|
406
406
|
|
|
407
407
|
export type PerpMarketAccount = {
|
|
@@ -584,11 +584,12 @@ export type UserStatsAccount = {
|
|
|
584
584
|
isReferrer: boolean;
|
|
585
585
|
totalReferrerReward: BN;
|
|
586
586
|
authority: PublicKey;
|
|
587
|
-
|
|
587
|
+
stakedQuoteAssetAmount: BN;
|
|
588
588
|
};
|
|
589
589
|
|
|
590
590
|
export type UserAccount = {
|
|
591
591
|
authority: PublicKey;
|
|
592
|
+
delegate: PublicKey;
|
|
592
593
|
name: number[];
|
|
593
594
|
userId: number;
|
|
594
595
|
spotPositions: SpotPosition[];
|
|
@@ -717,40 +718,29 @@ export interface IWallet {
|
|
|
717
718
|
}
|
|
718
719
|
|
|
719
720
|
export type FeeStructure = {
|
|
720
|
-
|
|
721
|
-
feeDenominator: BN;
|
|
722
|
-
discountTokenTiers: {
|
|
723
|
-
firstTier: {
|
|
724
|
-
minimumBalance: BN;
|
|
725
|
-
discountNumerator: BN;
|
|
726
|
-
discountDenominator: BN;
|
|
727
|
-
};
|
|
728
|
-
secondTier: {
|
|
729
|
-
minimumBalance: BN;
|
|
730
|
-
discountNumerator: BN;
|
|
731
|
-
discountDenominator: BN;
|
|
732
|
-
};
|
|
733
|
-
thirdTier: {
|
|
734
|
-
minimumBalance: BN;
|
|
735
|
-
discountNumerator: BN;
|
|
736
|
-
discountDenominator: BN;
|
|
737
|
-
};
|
|
738
|
-
fourthTier: {
|
|
739
|
-
minimumBalance: BN;
|
|
740
|
-
discountNumerator: BN;
|
|
741
|
-
discountDenominator: BN;
|
|
742
|
-
};
|
|
743
|
-
};
|
|
744
|
-
referralDiscount: {
|
|
745
|
-
referrerRewardNumerator: BN;
|
|
746
|
-
referrerRewardDenominator: BN;
|
|
747
|
-
refereeDiscountNumerator: BN;
|
|
748
|
-
refereeDiscountDenominator: BN;
|
|
749
|
-
};
|
|
721
|
+
feeTiers: FeeTier[];
|
|
750
722
|
makerRebateNumerator: BN;
|
|
751
723
|
makerRebateDenominator: BN;
|
|
752
724
|
fillerRewardStructure: OrderFillerRewardStructure;
|
|
753
725
|
flatFillerFee: BN;
|
|
726
|
+
referrerRewardEpochUpperBound: BN;
|
|
727
|
+
};
|
|
728
|
+
|
|
729
|
+
export type FeeTier = {
|
|
730
|
+
feeNumerator: number;
|
|
731
|
+
feeDenominator: number;
|
|
732
|
+
makerRebateNumerator: number;
|
|
733
|
+
makerRebateDenominator: number;
|
|
734
|
+
referrerRewardNumerator: number;
|
|
735
|
+
referrerRewardDenominator: number;
|
|
736
|
+
refereeFeeNumerator: number;
|
|
737
|
+
refereeFeeDenominator: number;
|
|
738
|
+
};
|
|
739
|
+
|
|
740
|
+
export type OrderFillerRewardStructure = {
|
|
741
|
+
rewardNumerator: BN;
|
|
742
|
+
rewardDenominator: BN;
|
|
743
|
+
timeBasedRewardLowerBound: BN;
|
|
754
744
|
};
|
|
755
745
|
|
|
756
746
|
export type OracleGuardRails = {
|
|
@@ -766,12 +756,6 @@ export type OracleGuardRails = {
|
|
|
766
756
|
useForLiquidations: boolean;
|
|
767
757
|
};
|
|
768
758
|
|
|
769
|
-
export type OrderFillerRewardStructure = {
|
|
770
|
-
rewardNumerator: BN;
|
|
771
|
-
rewardDenominator: BN;
|
|
772
|
-
timeBasedRewardLowerBound: BN;
|
|
773
|
-
};
|
|
774
|
-
|
|
775
759
|
export type MarginCategory = 'Initial' | 'Maintenance';
|
|
776
760
|
|
|
777
761
|
export type InsuranceFundStake = {
|
package/src/userName.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
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;
|
|
@@ -0,0 +1,27 @@
|
|
|
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.findComputeUnitConsumption = void 0;
|
|
13
|
+
function findComputeUnitConsumption(programId, connection, txSignature, commitment = 'confirmed') {
|
|
14
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
const tx = yield connection.getTransaction(txSignature, { commitment });
|
|
16
|
+
const computeUnits = [];
|
|
17
|
+
const regex = new RegExp(`Program ${programId.toString()} consumed ([0-9]{0,6}) of ([0-9]{0,7}) compute units`);
|
|
18
|
+
tx.meta.logMessages.forEach((logMessage) => {
|
|
19
|
+
const match = logMessage.match(regex);
|
|
20
|
+
if (match && match[1]) {
|
|
21
|
+
computeUnits.push(match[1]);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
return computeUnits;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
exports.findComputeUnitConsumption = findComputeUnitConsumption;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getTokenAddress = void 0;
|
|
4
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
5
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
+
const getTokenAddress = (mintAddress, userPubKey) => {
|
|
7
|
+
return spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, new web3_js_1.PublicKey(mintAddress), new web3_js_1.PublicKey(userPubKey));
|
|
8
|
+
};
|
|
9
|
+
exports.getTokenAddress = getTokenAddress;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.promiseTimeout = void 0;
|
|
4
|
+
function promiseTimeout(promise, timeoutMs) {
|
|
5
|
+
let timeoutId;
|
|
6
|
+
const timeoutPromise = new Promise((resolve) => {
|
|
7
|
+
timeoutId = setTimeout(() => resolve(null), timeoutMs);
|
|
8
|
+
});
|
|
9
|
+
return Promise.race([promise, timeoutPromise]).then((result) => {
|
|
10
|
+
clearTimeout(timeoutId);
|
|
11
|
+
return result;
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
exports.promiseTimeout = promiseTimeout;
|
package/src/util/tps.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
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.estimateTps = void 0;
|
|
13
|
+
function estimateTps(programId, connection, failed) {
|
|
14
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
let signatures = yield connection.getSignaturesForAddress(programId, undefined, 'finalized');
|
|
16
|
+
if (failed) {
|
|
17
|
+
signatures = signatures.filter((signature) => signature.err);
|
|
18
|
+
}
|
|
19
|
+
const numberOfSignatures = signatures.length;
|
|
20
|
+
if (numberOfSignatures === 0) {
|
|
21
|
+
return 0;
|
|
22
|
+
}
|
|
23
|
+
return (numberOfSignatures /
|
|
24
|
+
(signatures[0].blockTime - signatures[numberOfSignatures - 1].blockTime));
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
exports.estimateTps = estimateTps;
|
package/src/wallet.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
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;
|
package/lib/math/state.d.ts
DELETED
package/lib/math/state.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getExchangeFee = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Get the clearing house percent fee charged on notional of taking trades
|
|
6
|
-
*
|
|
7
|
-
* @param state
|
|
8
|
-
* @returns Precision : basis points (bps)
|
|
9
|
-
*/
|
|
10
|
-
function getExchangeFee(state) {
|
|
11
|
-
const exchangeFee = state.perpFeeStructure.feeNumerator.toNumber() /
|
|
12
|
-
state.perpFeeStructure.feeDenominator.toNumber();
|
|
13
|
-
return exchangeFee;
|
|
14
|
-
}
|
|
15
|
-
exports.getExchangeFee = getExchangeFee;
|
package/src/math/state.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { StateAccount } from '../types';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Get the clearing house percent fee charged on notional of taking trades
|
|
5
|
-
*
|
|
6
|
-
* @param state
|
|
7
|
-
* @returns Precision : basis points (bps)
|
|
8
|
-
*/
|
|
9
|
-
export function getExchangeFee(state: StateAccount): number {
|
|
10
|
-
const exchangeFee =
|
|
11
|
-
state.perpFeeStructure.feeNumerator.toNumber() /
|
|
12
|
-
state.perpFeeStructure.feeDenominator.toNumber();
|
|
13
|
-
return exchangeFee;
|
|
14
|
-
}
|