@drift-labs/sdk 0.2.0-master.18 → 0.2.0-master.20
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/clearingHouse.d.ts +11 -11
- package/lib/clearingHouse.js +78 -18
- package/lib/clearingHouseUser.js +7 -25
- package/lib/clearingHouseUserStats.d.ts +2 -1
- package/lib/clearingHouseUserStats.js +13 -0
- package/lib/config.js +1 -1
- package/lib/events/types.d.ts +2 -1
- package/lib/events/types.js +1 -0
- package/lib/idl/clearing_house.json +200 -129
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -1
- package/lib/math/amm.d.ts +1 -0
- package/lib/math/amm.js +13 -1
- package/lib/math/market.d.ts +1 -1
- package/lib/math/market.js +5 -5
- package/lib/math/orders.d.ts +3 -0
- package/lib/math/orders.js +35 -1
- package/lib/types.d.ts +25 -7
- package/package.json +1 -1
- package/src/clearingHouse.ts +113 -19
- package/src/clearingHouseUser.ts +11 -49
- package/src/clearingHouseUserStats.ts +23 -1
- package/src/config.ts +1 -1
- package/src/events/types.ts +3 -0
- package/src/idl/clearing_house.json +200 -129
- package/src/index.ts +0 -1
- package/src/math/amm.ts +27 -1
- package/src/math/market.ts +6 -6
- package/src/math/orders.ts +96 -1
- package/src/types.ts +27 -7
- package/lib/orders.d.ts +0 -7
- package/lib/orders.js +0 -59
- package/src/events/types.js +0 -20
- package/src/math/amm.js +0 -369
- package/src/orders.ts +0 -91
- package/src/types.js +0 -125
package/lib/clearingHouse.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="bn.js" />
|
|
3
3
|
import { AnchorProvider, BN, Program } from '@project-serum/anchor';
|
|
4
|
-
import { StateAccount, IWallet, PositionDirection, UserAccount, MarketAccount, OrderParams, Order, BankAccount, UserBankBalance, MakerInfo, TakerInfo, OptionalOrderParams } from './types';
|
|
4
|
+
import { StateAccount, IWallet, PositionDirection, UserAccount, MarketAccount, OrderParams, Order, BankAccount, UserBankBalance, MakerInfo, TakerInfo, OptionalOrderParams, ReferrerInfo } from './types';
|
|
5
5
|
import { Connection, PublicKey, TransactionSignature, ConfirmOptions, TransactionInstruction, AccountMeta } from '@solana/web3.js';
|
|
6
6
|
import { TokenFaucet } from './tokenFaucet';
|
|
7
7
|
import { EventEmitter } from 'events';
|
|
@@ -62,8 +62,8 @@ export declare class ClearingHouse {
|
|
|
62
62
|
updateWallet(newWallet: IWallet, userIds?: number[], activeUserId?: number): Promise<void>;
|
|
63
63
|
switchActiveUser(userId: number): Promise<void>;
|
|
64
64
|
addUser(userId: number): Promise<void>;
|
|
65
|
-
initializeUserAccount(userId?: number, name?: string): Promise<[TransactionSignature, PublicKey]>;
|
|
66
|
-
getInitializeUserInstructions(userId?: number, name?: string): Promise<[PublicKey, TransactionInstruction]>;
|
|
65
|
+
initializeUserAccount(userId?: number, name?: string, referrerInfo?: ReferrerInfo): Promise<[TransactionSignature, PublicKey]>;
|
|
66
|
+
getInitializeUserInstructions(userId?: number, name?: string, referrerInfo?: ReferrerInfo): Promise<[PublicKey, TransactionInstruction]>;
|
|
67
67
|
getInitializeUserStatsIx(): Promise<TransactionInstruction>;
|
|
68
68
|
getUser(userId?: number): ClearingHouseUser;
|
|
69
69
|
getUsers(): ClearingHouseUser[];
|
|
@@ -97,8 +97,8 @@ export declare class ClearingHouse {
|
|
|
97
97
|
* @param fromUserId
|
|
98
98
|
* @returns
|
|
99
99
|
*/
|
|
100
|
-
initializeUserAccountAndDepositCollateral(amount: BN, userTokenAccount: PublicKey, bankIndex?: BN, userId?: number, name?: string, fromUserId?: number): Promise<[TransactionSignature, PublicKey]>;
|
|
101
|
-
initializeUserAccountForDevnet(userId: number, name: string, bankIndex: BN, tokenFaucet: TokenFaucet, amount: BN): Promise<[TransactionSignature, PublicKey]>;
|
|
100
|
+
initializeUserAccountAndDepositCollateral(amount: BN, userTokenAccount: PublicKey, bankIndex?: BN, userId?: number, name?: string, fromUserId?: number, referrerInfo?: ReferrerInfo): Promise<[TransactionSignature, PublicKey]>;
|
|
101
|
+
initializeUserAccountForDevnet(userId: number, name: string, bankIndex: BN, tokenFaucet: TokenFaucet, amount: BN, referrerInfo?: ReferrerInfo): Promise<[TransactionSignature, PublicKey]>;
|
|
102
102
|
withdraw(amount: BN, bankIndex: BN, userTokenAccount: PublicKey, reduceOnly?: boolean): Promise<TransactionSignature>;
|
|
103
103
|
getWithdrawIx(amount: BN, bankIndex: BN, userTokenAccount: PublicKey, reduceOnly?: boolean): Promise<TransactionInstruction>;
|
|
104
104
|
transferDeposit(amount: BN, bankIndex: BN, fromUserId: number, toUserId: number): Promise<TransactionSignature>;
|
|
@@ -121,14 +121,14 @@ export declare class ClearingHouse {
|
|
|
121
121
|
getCancelOrderIx(orderId?: BN): Promise<TransactionInstruction>;
|
|
122
122
|
cancelOrderByUserId(userOrderId: number): Promise<TransactionSignature>;
|
|
123
123
|
getCancelOrderByUserIdIx(userOrderId: number): Promise<TransactionInstruction>;
|
|
124
|
-
fillOrder(userAccountPublicKey: PublicKey, user: UserAccount, order?: Order, makerInfo?: MakerInfo): Promise<TransactionSignature>;
|
|
125
|
-
getFillOrderIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, order: Order, makerInfo?: MakerInfo): Promise<TransactionInstruction>;
|
|
124
|
+
fillOrder(userAccountPublicKey: PublicKey, user: UserAccount, order?: Order, makerInfo?: MakerInfo, referrerInfo?: ReferrerInfo): Promise<TransactionSignature>;
|
|
125
|
+
getFillOrderIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, order: Order, makerInfo?: MakerInfo, referrerInfo?: ReferrerInfo): Promise<TransactionInstruction>;
|
|
126
126
|
triggerOrder(userAccountPublicKey: PublicKey, user: UserAccount, order: Order): Promise<TransactionSignature>;
|
|
127
127
|
getTriggerOrderIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, order: Order): Promise<TransactionInstruction>;
|
|
128
|
-
placeAndTake(orderParams: OptionalOrderParams, makerInfo?: MakerInfo): Promise<TransactionSignature>;
|
|
129
|
-
getPlaceAndTakeIx(orderParams: OptionalOrderParams, makerInfo?: MakerInfo): Promise<TransactionInstruction>;
|
|
130
|
-
placeAndMake(orderParams: OptionalOrderParams, takerInfo: TakerInfo): Promise<TransactionSignature>;
|
|
131
|
-
getPlaceAndMakeIx(orderParams: OptionalOrderParams, takerInfo: TakerInfo): Promise<TransactionInstruction>;
|
|
128
|
+
placeAndTake(orderParams: OptionalOrderParams, makerInfo?: MakerInfo, referrerInfo?: ReferrerInfo): Promise<TransactionSignature>;
|
|
129
|
+
getPlaceAndTakeIx(orderParams: OptionalOrderParams, makerInfo?: MakerInfo, referrerInfo?: ReferrerInfo): Promise<TransactionInstruction>;
|
|
130
|
+
placeAndMake(orderParams: OptionalOrderParams, takerInfo: TakerInfo, referrerInfo?: ReferrerInfo): Promise<TransactionSignature>;
|
|
131
|
+
getPlaceAndMakeIx(orderParams: OptionalOrderParams, takerInfo: TakerInfo, referrerInfo?: ReferrerInfo): Promise<TransactionInstruction>;
|
|
132
132
|
/**
|
|
133
133
|
* Close an entire position. If you want to reduce a position, use the {@link openPosition} method in the opposite direction of the current position.
|
|
134
134
|
* @param marketIndex
|
package/lib/clearingHouse.js
CHANGED
|
@@ -215,8 +215,8 @@ class ClearingHouse {
|
|
|
215
215
|
await user.subscribe();
|
|
216
216
|
this.users.set(userId, user);
|
|
217
217
|
}
|
|
218
|
-
async initializeUserAccount(userId = 0, name = userName_1.DEFAULT_USER_NAME) {
|
|
219
|
-
const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(userId, name);
|
|
218
|
+
async initializeUserAccount(userId = 0, name = userName_1.DEFAULT_USER_NAME, referrerInfo) {
|
|
219
|
+
const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(userId, name, referrerInfo);
|
|
220
220
|
const tx = new web3_js_1.Transaction();
|
|
221
221
|
if (userId === 0) {
|
|
222
222
|
// not the safest assumption, can explicitly check if user stats account exists if it causes problems
|
|
@@ -226,8 +226,21 @@ class ClearingHouse {
|
|
|
226
226
|
const { txSig } = await this.txSender.send(tx, [], this.opts);
|
|
227
227
|
return [txSig, userAccountPublicKey];
|
|
228
228
|
}
|
|
229
|
-
async getInitializeUserInstructions(userId = 0, name = userName_1.DEFAULT_USER_NAME) {
|
|
229
|
+
async getInitializeUserInstructions(userId = 0, name = userName_1.DEFAULT_USER_NAME, referrerInfo) {
|
|
230
230
|
const userAccountPublicKey = await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.wallet.publicKey, userId);
|
|
231
|
+
const remainingAccounts = new Array();
|
|
232
|
+
if (referrerInfo !== undefined) {
|
|
233
|
+
remainingAccounts.push({
|
|
234
|
+
pubkey: referrerInfo.referrer,
|
|
235
|
+
isWritable: true,
|
|
236
|
+
isSigner: false,
|
|
237
|
+
});
|
|
238
|
+
remainingAccounts.push({
|
|
239
|
+
pubkey: referrerInfo.referrerStats,
|
|
240
|
+
isWritable: true,
|
|
241
|
+
isSigner: false,
|
|
242
|
+
});
|
|
243
|
+
}
|
|
231
244
|
const nameBuffer = (0, userName_1.encodeName)(name);
|
|
232
245
|
const initializeUserAccountIx = await this.program.instruction.initializeUser(userId, nameBuffer, {
|
|
233
246
|
accounts: {
|
|
@@ -239,6 +252,7 @@ class ClearingHouse {
|
|
|
239
252
|
systemProgram: anchor.web3.SystemProgram.programId,
|
|
240
253
|
state: await this.getStatePublicKey(),
|
|
241
254
|
},
|
|
255
|
+
remainingAccounts,
|
|
242
256
|
});
|
|
243
257
|
return [userAccountPublicKey, initializeUserAccountIx];
|
|
244
258
|
}
|
|
@@ -466,6 +480,7 @@ class ClearingHouse {
|
|
|
466
480
|
bank: bank.pubkey,
|
|
467
481
|
bankVault: bank.vault,
|
|
468
482
|
user: userAccountPublicKey,
|
|
483
|
+
userStats: this.getUserStatsAccountPublicKey(),
|
|
469
484
|
userTokenAccount: userTokenAccount,
|
|
470
485
|
authority: this.wallet.publicKey,
|
|
471
486
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
@@ -535,8 +550,8 @@ class ClearingHouse {
|
|
|
535
550
|
* @param fromUserId
|
|
536
551
|
* @returns
|
|
537
552
|
*/
|
|
538
|
-
async initializeUserAccountAndDepositCollateral(amount, userTokenAccount, bankIndex = new anchor_1.BN(0), userId = 0, name = userName_1.DEFAULT_USER_NAME, fromUserId) {
|
|
539
|
-
const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(userId, name);
|
|
553
|
+
async initializeUserAccountAndDepositCollateral(amount, userTokenAccount, bankIndex = new anchor_1.BN(0), userId = 0, name = userName_1.DEFAULT_USER_NAME, fromUserId, referrerInfo) {
|
|
554
|
+
const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(userId, name, referrerInfo);
|
|
540
555
|
const additionalSigners = [];
|
|
541
556
|
const bank = this.getBankAccount(bankIndex);
|
|
542
557
|
const isSolBank = bank.mint.equals(banks_1.WRAPPED_SOL_MINT);
|
|
@@ -565,9 +580,9 @@ class ClearingHouse {
|
|
|
565
580
|
const { txSig } = await this.txSender.send(tx, additionalSigners, this.opts);
|
|
566
581
|
return [txSig, userAccountPublicKey];
|
|
567
582
|
}
|
|
568
|
-
async initializeUserAccountForDevnet(userId = 0, name = userName_1.DEFAULT_USER_NAME, bankIndex, tokenFaucet, amount) {
|
|
583
|
+
async initializeUserAccountForDevnet(userId = 0, name = userName_1.DEFAULT_USER_NAME, bankIndex, tokenFaucet, amount, referrerInfo) {
|
|
569
584
|
const [associateTokenPublicKey, createAssociatedAccountIx, mintToIx] = await tokenFaucet.createAssociatedTokenAccountAndMintToInstructions(this.wallet.publicKey, amount);
|
|
570
|
-
const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(userId, name);
|
|
585
|
+
const [userAccountPublicKey, initializeUserAccountIx] = await this.getInitializeUserInstructions(userId, name, referrerInfo);
|
|
571
586
|
const depositCollateralIx = await this.getDepositInstruction(amount, bankIndex, associateTokenPublicKey, userId, false, false);
|
|
572
587
|
const tx = new web3_js_1.Transaction().add(createAssociatedAccountIx).add(mintToIx);
|
|
573
588
|
if (userId === 0) {
|
|
@@ -614,6 +629,7 @@ class ClearingHouse {
|
|
|
614
629
|
bankVault: bank.vault,
|
|
615
630
|
bankVaultAuthority: bank.vaultAuthority,
|
|
616
631
|
user: userAccountPublicKey,
|
|
632
|
+
userStats: this.getUserStatsAccountPublicKey(),
|
|
617
633
|
userTokenAccount: userTokenAccount,
|
|
618
634
|
authority: this.wallet.publicKey,
|
|
619
635
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
@@ -636,6 +652,7 @@ class ClearingHouse {
|
|
|
636
652
|
authority: this.wallet.publicKey,
|
|
637
653
|
fromUser,
|
|
638
654
|
toUser,
|
|
655
|
+
userStats: this.getUserStatsAccountPublicKey(),
|
|
639
656
|
state: await this.getStatePublicKey(),
|
|
640
657
|
},
|
|
641
658
|
remainingAccounts,
|
|
@@ -831,15 +848,17 @@ class ClearingHouse {
|
|
|
831
848
|
remainingAccounts,
|
|
832
849
|
});
|
|
833
850
|
}
|
|
834
|
-
async fillOrder(userAccountPublicKey, user, order, makerInfo) {
|
|
835
|
-
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getFillOrderIx(userAccountPublicKey, user, order, makerInfo)), [], this.opts);
|
|
851
|
+
async fillOrder(userAccountPublicKey, user, order, makerInfo, referrerInfo) {
|
|
852
|
+
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getFillOrderIx(userAccountPublicKey, user, order, makerInfo, referrerInfo)), [], this.opts);
|
|
836
853
|
return txSig;
|
|
837
854
|
}
|
|
838
|
-
async getFillOrderIx(userAccountPublicKey, userAccount, order, makerInfo) {
|
|
855
|
+
async getFillOrderIx(userAccountPublicKey, userAccount, order, makerInfo, referrerInfo) {
|
|
839
856
|
const userStatsPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, userAccount.authority);
|
|
840
857
|
const fillerPublicKey = await this.getUserAccountPublicKey();
|
|
841
858
|
const fillerStatsPublicKey = this.getUserStatsAccountPublicKey();
|
|
842
|
-
const marketIndex = order
|
|
859
|
+
const marketIndex = order
|
|
860
|
+
? order.marketIndex
|
|
861
|
+
: userAccount.orders.find((order) => order.orderId.eq(userAccount.nextOrderId.sub(numericConstants_1.ONE))).marketIndex;
|
|
843
862
|
const marketAccount = this.getMarketAccount(marketIndex);
|
|
844
863
|
const oracleAccountMap = new Map();
|
|
845
864
|
const bankAccountMap = new Map();
|
|
@@ -904,6 +923,18 @@ class ClearingHouse {
|
|
|
904
923
|
isSigner: false,
|
|
905
924
|
});
|
|
906
925
|
}
|
|
926
|
+
if (referrerInfo) {
|
|
927
|
+
remainingAccounts.push({
|
|
928
|
+
pubkey: referrerInfo.referrer,
|
|
929
|
+
isWritable: true,
|
|
930
|
+
isSigner: false,
|
|
931
|
+
});
|
|
932
|
+
remainingAccounts.push({
|
|
933
|
+
pubkey: referrerInfo.referrerStats,
|
|
934
|
+
isWritable: true,
|
|
935
|
+
isSigner: false,
|
|
936
|
+
});
|
|
937
|
+
}
|
|
907
938
|
const orderId = order.orderId;
|
|
908
939
|
const makerOrderId = makerInfo ? makerInfo.order.orderId : null;
|
|
909
940
|
return await this.program.instruction.fillOrder(orderId, makerOrderId, {
|
|
@@ -988,12 +1019,12 @@ class ClearingHouse {
|
|
|
988
1019
|
remainingAccounts,
|
|
989
1020
|
});
|
|
990
1021
|
}
|
|
991
|
-
async placeAndTake(orderParams, makerInfo) {
|
|
992
|
-
const { txSig, slot } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getPlaceAndTakeIx(orderParams, makerInfo)), [], this.opts);
|
|
1022
|
+
async placeAndTake(orderParams, makerInfo, referrerInfo) {
|
|
1023
|
+
const { txSig, slot } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getPlaceAndTakeIx(orderParams, makerInfo, referrerInfo)), [], this.opts);
|
|
993
1024
|
this.marketLastSlotCache.set(orderParams.marketIndex.toNumber(), slot);
|
|
994
1025
|
return txSig;
|
|
995
1026
|
}
|
|
996
|
-
async getPlaceAndTakeIx(orderParams, makerInfo) {
|
|
1027
|
+
async getPlaceAndTakeIx(orderParams, makerInfo, referrerInfo) {
|
|
997
1028
|
orderParams = this.getOrderParams(orderParams);
|
|
998
1029
|
const userStatsPublicKey = await this.getUserStatsAccountPublicKey();
|
|
999
1030
|
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
@@ -1015,6 +1046,18 @@ class ClearingHouse {
|
|
|
1015
1046
|
isWritable: true,
|
|
1016
1047
|
});
|
|
1017
1048
|
}
|
|
1049
|
+
if (referrerInfo) {
|
|
1050
|
+
remainingAccounts.push({
|
|
1051
|
+
pubkey: referrerInfo.referrer,
|
|
1052
|
+
isWritable: true,
|
|
1053
|
+
isSigner: false,
|
|
1054
|
+
});
|
|
1055
|
+
remainingAccounts.push({
|
|
1056
|
+
pubkey: referrerInfo.referrerStats,
|
|
1057
|
+
isWritable: true,
|
|
1058
|
+
isSigner: false,
|
|
1059
|
+
});
|
|
1060
|
+
}
|
|
1018
1061
|
return await this.program.instruction.placeAndTake(orderParams, makerOrderId, {
|
|
1019
1062
|
accounts: {
|
|
1020
1063
|
state: await this.getStatePublicKey(),
|
|
@@ -1025,18 +1068,30 @@ class ClearingHouse {
|
|
|
1025
1068
|
remainingAccounts,
|
|
1026
1069
|
});
|
|
1027
1070
|
}
|
|
1028
|
-
async placeAndMake(orderParams, takerInfo) {
|
|
1029
|
-
const { txSig, slot } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getPlaceAndMakeIx(orderParams, takerInfo)), [], this.opts);
|
|
1071
|
+
async placeAndMake(orderParams, takerInfo, referrerInfo) {
|
|
1072
|
+
const { txSig, slot } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getPlaceAndMakeIx(orderParams, takerInfo, referrerInfo)), [], this.opts);
|
|
1030
1073
|
this.marketLastSlotCache.set(orderParams.marketIndex.toNumber(), slot);
|
|
1031
1074
|
return txSig;
|
|
1032
1075
|
}
|
|
1033
|
-
async getPlaceAndMakeIx(orderParams, takerInfo) {
|
|
1076
|
+
async getPlaceAndMakeIx(orderParams, takerInfo, referrerInfo) {
|
|
1034
1077
|
orderParams = this.getOrderParams(orderParams);
|
|
1035
1078
|
const userStatsPublicKey = this.getUserStatsAccountPublicKey();
|
|
1036
1079
|
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
1037
1080
|
const remainingAccounts = this.getRemainingAccounts({
|
|
1038
1081
|
writableMarketIndex: orderParams.marketIndex,
|
|
1039
1082
|
});
|
|
1083
|
+
if (referrerInfo) {
|
|
1084
|
+
remainingAccounts.push({
|
|
1085
|
+
pubkey: referrerInfo.referrer,
|
|
1086
|
+
isWritable: true,
|
|
1087
|
+
isSigner: false,
|
|
1088
|
+
});
|
|
1089
|
+
remainingAccounts.push({
|
|
1090
|
+
pubkey: referrerInfo.referrerStats,
|
|
1091
|
+
isWritable: true,
|
|
1092
|
+
isSigner: false,
|
|
1093
|
+
});
|
|
1094
|
+
}
|
|
1040
1095
|
const takerOrderId = takerInfo.order.orderId;
|
|
1041
1096
|
return await this.program.instruction.placeAndMake(orderParams, takerOrderId, {
|
|
1042
1097
|
accounts: {
|
|
@@ -1074,7 +1129,12 @@ class ClearingHouse {
|
|
|
1074
1129
|
for (const { settleeUserAccountPublicKey, settleeUserAccount } of users) {
|
|
1075
1130
|
ixs.push(await this.settlePNLIx(settleeUserAccountPublicKey, settleeUserAccount, marketIndex));
|
|
1076
1131
|
}
|
|
1077
|
-
const tx = new web3_js_1.Transaction()
|
|
1132
|
+
const tx = new web3_js_1.Transaction()
|
|
1133
|
+
.add(web3_js_1.ComputeBudgetProgram.requestUnits({
|
|
1134
|
+
units: 1000000,
|
|
1135
|
+
additionalFee: 0,
|
|
1136
|
+
}))
|
|
1137
|
+
.add(...ixs);
|
|
1078
1138
|
const { txSig } = await this.txSender.send(tx, [], this.opts);
|
|
1079
1139
|
return txSig;
|
|
1080
1140
|
}
|
package/lib/clearingHouseUser.js
CHANGED
|
@@ -240,17 +240,17 @@ class ClearingHouseUser {
|
|
|
240
240
|
getUnrealizedPNL(withFunding, marketIndex, withWeightMarginCategory) {
|
|
241
241
|
return this.getUserAccount()
|
|
242
242
|
.positions.filter((pos) => marketIndex ? pos.marketIndex === marketIndex : true)
|
|
243
|
-
.reduce((
|
|
243
|
+
.reduce((unrealizedPnl, marketPosition) => {
|
|
244
244
|
const market = this.clearingHouse.getMarketAccount(marketPosition.marketIndex);
|
|
245
|
-
let
|
|
245
|
+
let positionUnrealizedPnl = (0, _1.calculatePositionPNL)(market, marketPosition, withFunding, this.getOracleDataForMarket(market.marketIndex));
|
|
246
246
|
if (withWeightMarginCategory !== undefined) {
|
|
247
|
-
if (
|
|
248
|
-
|
|
249
|
-
.mul((0, _1.
|
|
247
|
+
if (positionUnrealizedPnl.gt(numericConstants_1.ZERO)) {
|
|
248
|
+
positionUnrealizedPnl = positionUnrealizedPnl
|
|
249
|
+
.mul((0, _1.calculateUnrealizedAssetWeight)(market, positionUnrealizedPnl, withWeightMarginCategory))
|
|
250
250
|
.div(new _1.BN(numericConstants_1.BANK_WEIGHT_PRECISION));
|
|
251
251
|
}
|
|
252
252
|
}
|
|
253
|
-
return
|
|
253
|
+
return unrealizedPnl.add(positionUnrealizedPnl);
|
|
254
254
|
}, numericConstants_1.ZERO);
|
|
255
255
|
}
|
|
256
256
|
/**
|
|
@@ -317,25 +317,7 @@ class ClearingHouseUser {
|
|
|
317
317
|
* @returns : Precision QUOTE_PRECISION
|
|
318
318
|
*/
|
|
319
319
|
getTotalCollateral(marginCategory = 'Initial') {
|
|
320
|
-
return this.
|
|
321
|
-
.bankBalances.reduce((totalAssetValue, bankBalance) => {
|
|
322
|
-
if (bankBalance.balance.eq(numericConstants_1.ZERO) ||
|
|
323
|
-
(0, types_1.isVariant)(bankBalance.balanceType, 'borrow')) {
|
|
324
|
-
return totalAssetValue;
|
|
325
|
-
}
|
|
326
|
-
// Todo this needs to account for whether it's based on initial or maintenance requirements
|
|
327
|
-
const bankAccount = this.clearingHouse.getBankAccount(bankBalance.bankIndex);
|
|
328
|
-
const tokenAmount = (0, bankBalance_1.getTokenAmount)(bankBalance.balance, bankAccount, bankBalance.balanceType);
|
|
329
|
-
const weight = (0, bankBalance_1.calculateAssetWeight)(tokenAmount, bankAccount, marginCategory);
|
|
330
|
-
return totalAssetValue.add(tokenAmount
|
|
331
|
-
.mul(this.getOracleDataForBank(bankAccount.bankIndex).price)
|
|
332
|
-
.mul(weight)
|
|
333
|
-
.div(numericConstants_1.BANK_WEIGHT_PRECISION)
|
|
334
|
-
.div(numericConstants_1.MARK_PRICE_PRECISION)
|
|
335
|
-
// Adjust for decimals of bank account
|
|
336
|
-
.div(new _1.BN(10).pow(new _1.BN(bankAccount.decimals).sub(numericConstants_1.BANK_BALANCE_PRECISION_EXP))));
|
|
337
|
-
}, numericConstants_1.ZERO)
|
|
338
|
-
.add(this.getUnrealizedPNL(true, undefined, marginCategory));
|
|
320
|
+
return this.getBankAssetValue(undefined, marginCategory).add(this.getUnrealizedPNL(true, undefined, marginCategory));
|
|
339
321
|
}
|
|
340
322
|
/**
|
|
341
323
|
* calculates sum of position value across all positions in margin system
|
|
@@ -2,7 +2,7 @@ import { ClearingHouse } from './clearingHouse';
|
|
|
2
2
|
import { PublicKey } from '@solana/web3.js';
|
|
3
3
|
import { DataAndSlot, UserStatsAccountSubscriber } from './accounts/types';
|
|
4
4
|
import { ClearingHouseUserStatsConfig } from './clearingHouseUserStatsConfig';
|
|
5
|
-
import { UserStatsAccount } from './types';
|
|
5
|
+
import { ReferrerInfo, UserStatsAccount } from './types';
|
|
6
6
|
export declare class ClearingHouseUserStats {
|
|
7
7
|
clearingHouse: ClearingHouse;
|
|
8
8
|
userStatsAccountPublicKey: PublicKey;
|
|
@@ -14,4 +14,5 @@ export declare class ClearingHouseUserStats {
|
|
|
14
14
|
unsubscribe(): Promise<void>;
|
|
15
15
|
getAccountAndSlot(): DataAndSlot<UserStatsAccount>;
|
|
16
16
|
getAccount(): UserStatsAccount;
|
|
17
|
+
getReferrerInfo(): ReferrerInfo | undefined;
|
|
17
18
|
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ClearingHouseUserStats = void 0;
|
|
4
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
4
5
|
const pollingUserStatsAccountSubscriber_1 = require("./accounts/pollingUserStatsAccountSubscriber");
|
|
5
6
|
const webSocketUserStatsAccountSubsriber_1 = require("./accounts/webSocketUserStatsAccountSubsriber");
|
|
7
|
+
const pda_1 = require("./addresses/pda");
|
|
6
8
|
class ClearingHouseUserStats {
|
|
7
9
|
constructor(config) {
|
|
8
10
|
var _a;
|
|
@@ -32,5 +34,16 @@ class ClearingHouseUserStats {
|
|
|
32
34
|
getAccount() {
|
|
33
35
|
return this.accountSubscriber.getUserStatsAccountAndSlot().data;
|
|
34
36
|
}
|
|
37
|
+
getReferrerInfo() {
|
|
38
|
+
if (this.getAccount().referrer.equals(web3_js_1.PublicKey.default)) {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
return {
|
|
43
|
+
referrer: (0, pda_1.getUserAccountPublicKeySync)(this.clearingHouse.program.programId, this.getAccount().referrer, 0),
|
|
44
|
+
referrerStats: (0, pda_1.getUserStatsAccountPublicKey)(this.clearingHouse.program.programId, this.getAccount().referrer),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
35
48
|
}
|
|
36
49
|
exports.ClearingHouseUserStats = ClearingHouseUserStats;
|
package/lib/config.js
CHANGED
|
@@ -7,7 +7,7 @@ exports.configs = {
|
|
|
7
7
|
devnet: {
|
|
8
8
|
ENV: 'devnet',
|
|
9
9
|
PYTH_ORACLE_MAPPING_ADDRESS: 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2',
|
|
10
|
-
CLEARING_HOUSE_PROGRAM_ID: '
|
|
10
|
+
CLEARING_HOUSE_PROGRAM_ID: '65sz7dRiWDRPZjiRxcTxPM7AE6VK4Nag9HEK6oBJXhJn',
|
|
11
11
|
USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
|
|
12
12
|
MARKETS: markets_1.DevnetMarkets,
|
|
13
13
|
BANKS: banks_1.DevnetBanks,
|
package/lib/events/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Commitment, TransactionSignature } from '@solana/web3.js';
|
|
2
|
-
import { DepositRecord, FundingPaymentRecord, FundingRateRecord, LiquidationRecord, OrderRecord, SettlePnlRecord } from '../index';
|
|
2
|
+
import { DepositRecord, FundingPaymentRecord, FundingRateRecord, LiquidationRecord, NewUserRecord, OrderRecord, SettlePnlRecord } from '../index';
|
|
3
3
|
export declare type EventSubscriptionOptions = {
|
|
4
4
|
eventTypes?: EventType[];
|
|
5
5
|
maxEventsPerType?: number;
|
|
@@ -28,6 +28,7 @@ export declare type EventMap = {
|
|
|
28
28
|
FundingRateRecord: Event<FundingRateRecord>;
|
|
29
29
|
OrderRecord: Event<OrderRecord>;
|
|
30
30
|
SettlePnlRecord: Event<SettlePnlRecord>;
|
|
31
|
+
NewUserRecord: Event<NewUserRecord>;
|
|
31
32
|
};
|
|
32
33
|
export declare type EventType = keyof EventMap;
|
|
33
34
|
export interface EventSubscriberEvents {
|