@drift-labs/sdk 0.1.36-master.4 → 0.1.36-master.7
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/bulkAccountLoader.d.ts +1 -0
- package/lib/accounts/bulkAccountLoader.js +4 -0
- package/lib/addresses.d.ts +1 -0
- package/lib/addresses.js +7 -1
- package/lib/admin.d.ts +5 -0
- package/lib/admin.js +68 -0
- package/lib/clearingHouse.d.ts +11 -3
- package/lib/clearingHouse.js +130 -4
- package/lib/clearingHouseUser.d.ts +5 -1
- package/lib/clearingHouseUser.js +28 -0
- package/lib/idl/clearing_house.json +988 -727
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/math/oracles.d.ts +3 -0
- package/lib/math/oracles.js +26 -0
- package/lib/math/position.d.ts +1 -0
- package/lib/math/position.js +19 -1
- package/lib/math/trade.d.ts +10 -6
- package/lib/math/trade.js +68 -13
- package/lib/oracles/pythClient.js +1 -0
- package/lib/oracles/switchboardClient.js +3 -0
- package/lib/oracles/types.d.ts +1 -0
- package/lib/settlement.d.ts +4 -0
- package/lib/settlement.js +10 -0
- package/lib/types.d.ts +11 -0
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.ts +5 -0
- package/src/addresses.ts +11 -0
- package/src/admin.ts +83 -0
- package/src/clearingHouse.ts +204 -4
- package/src/clearingHouseUser.ts +64 -1
- package/src/idl/clearing_house.json +988 -727
- package/src/index.ts +1 -0
- package/src/math/oracles.ts +36 -0
- package/src/math/position.ts +22 -0
- package/src/math/trade.ts +84 -16
- package/src/oracles/pythClient.ts +1 -0
- package/src/oracles/switchboardClient.ts +5 -0
- package/src/oracles/types.ts +1 -0
- package/src/settlement.ts +9 -0
- package/src/types.ts +12 -0
package/src/clearingHouse.ts
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
Order,
|
|
23
23
|
ExtendedCurveHistoryAccount,
|
|
24
24
|
UserPositionsAccount,
|
|
25
|
+
SettlementStateAccount,
|
|
25
26
|
} from './types';
|
|
26
27
|
import * as anchor from '@project-serum/anchor';
|
|
27
28
|
import clearingHouseIDL from './idl/clearing_house.json';
|
|
@@ -42,6 +43,7 @@ import StrictEventEmitter from 'strict-event-emitter-types';
|
|
|
42
43
|
import {
|
|
43
44
|
getClearingHouseStateAccountPublicKey,
|
|
44
45
|
getOrderStateAccountPublicKey,
|
|
46
|
+
getSettlementStatePublicKey,
|
|
45
47
|
getUserAccountPublicKey,
|
|
46
48
|
getUserAccountPublicKeyAndNonce,
|
|
47
49
|
getUserOrdersAccountPublicKey,
|
|
@@ -56,9 +58,17 @@ import { TxSender } from './tx/types';
|
|
|
56
58
|
import { wrapInTx } from './tx/utils';
|
|
57
59
|
import {
|
|
58
60
|
getClearingHouse,
|
|
61
|
+
getPollingClearingHouseConfig,
|
|
59
62
|
getWebSocketClearingHouseConfig,
|
|
60
63
|
} from './factory/clearingHouse';
|
|
61
64
|
import { ZERO } from './constants/numericConstants';
|
|
65
|
+
import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
66
|
+
import { ClearingHouseUser } from './clearingHouseUser';
|
|
67
|
+
import {
|
|
68
|
+
getClearingHouseUser,
|
|
69
|
+
getPollingClearingHouseUserConfig,
|
|
70
|
+
} from './factory/clearingHouseUser';
|
|
71
|
+
import { bulkPollingUserSubscribe } from './accounts/bulkUserSubscription';
|
|
62
72
|
|
|
63
73
|
/**
|
|
64
74
|
* # ClearingHouse
|
|
@@ -186,6 +196,13 @@ export class ClearingHouse {
|
|
|
186
196
|
return this.accountSubscriber.getStateAccount();
|
|
187
197
|
}
|
|
188
198
|
|
|
199
|
+
public async getSettlementAccount(): Promise<SettlementStateAccount> {
|
|
200
|
+
// @ts-ignore
|
|
201
|
+
return await this.program.account.settlementState.fetch(
|
|
202
|
+
await getSettlementStatePublicKey(this.program.programId)
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
189
206
|
public getMarketsAccount(): MarketsAccount {
|
|
190
207
|
return this.accountSubscriber.getMarketsAccount();
|
|
191
208
|
}
|
|
@@ -917,17 +934,19 @@ export class ClearingHouse {
|
|
|
917
934
|
}
|
|
918
935
|
|
|
919
936
|
public async cancelAllOrders(
|
|
920
|
-
oracles?: PublicKey[]
|
|
937
|
+
oracles?: PublicKey[],
|
|
938
|
+
bestEffort?: boolean
|
|
921
939
|
): Promise<TransactionSignature> {
|
|
922
940
|
return await this.txSender.send(
|
|
923
|
-
wrapInTx(await this.getCancelAllOrdersIx(oracles)),
|
|
941
|
+
wrapInTx(await this.getCancelAllOrdersIx(oracles, bestEffort)),
|
|
924
942
|
[],
|
|
925
943
|
this.opts
|
|
926
944
|
);
|
|
927
945
|
}
|
|
928
946
|
|
|
929
947
|
public async getCancelAllOrdersIx(
|
|
930
|
-
oracles: PublicKey[]
|
|
948
|
+
oracles: PublicKey[],
|
|
949
|
+
bestEffort?: boolean
|
|
931
950
|
): Promise<TransactionInstruction> {
|
|
932
951
|
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
933
952
|
const userAccount = await this.getUserAccount();
|
|
@@ -944,7 +963,7 @@ export class ClearingHouse {
|
|
|
944
963
|
});
|
|
945
964
|
}
|
|
946
965
|
|
|
947
|
-
return await this.program.instruction.cancelAllOrders({
|
|
966
|
+
return await this.program.instruction.cancelAllOrders(bestEffort, {
|
|
948
967
|
accounts: {
|
|
949
968
|
state: await this.getStatePublicKey(),
|
|
950
969
|
user: userAccountPublicKey,
|
|
@@ -961,6 +980,69 @@ export class ClearingHouse {
|
|
|
961
980
|
});
|
|
962
981
|
}
|
|
963
982
|
|
|
983
|
+
public async cancelOrdersByMarketAndSide(
|
|
984
|
+
oracles?: PublicKey[],
|
|
985
|
+
bestEffort?: boolean,
|
|
986
|
+
marketIndexOnly?: BN,
|
|
987
|
+
directionOnly?: PositionDirection
|
|
988
|
+
): Promise<TransactionSignature> {
|
|
989
|
+
return await this.txSender.send(
|
|
990
|
+
wrapInTx(
|
|
991
|
+
await this.getCancelOrdersByMarketAndSideIx(
|
|
992
|
+
oracles,
|
|
993
|
+
bestEffort,
|
|
994
|
+
marketIndexOnly,
|
|
995
|
+
directionOnly
|
|
996
|
+
)
|
|
997
|
+
),
|
|
998
|
+
[],
|
|
999
|
+
this.opts
|
|
1000
|
+
);
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
public async getCancelOrdersByMarketAndSideIx(
|
|
1004
|
+
oracles: PublicKey[],
|
|
1005
|
+
bestEffort?: boolean,
|
|
1006
|
+
marketIndexOnly?: BN,
|
|
1007
|
+
directionOnly?: PositionDirection
|
|
1008
|
+
): Promise<TransactionInstruction> {
|
|
1009
|
+
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
1010
|
+
const userAccount = await this.getUserAccount();
|
|
1011
|
+
|
|
1012
|
+
const state = this.getStateAccount();
|
|
1013
|
+
const orderState = this.getOrderStateAccount();
|
|
1014
|
+
|
|
1015
|
+
const remainingAccounts = [];
|
|
1016
|
+
for (const oracle of oracles) {
|
|
1017
|
+
remainingAccounts.push({
|
|
1018
|
+
pubkey: oracle,
|
|
1019
|
+
isWritable: false,
|
|
1020
|
+
isSigner: false,
|
|
1021
|
+
});
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
return await this.program.instruction.cancelOrdersByMarketAndSide(
|
|
1025
|
+
bestEffort,
|
|
1026
|
+
marketIndexOnly,
|
|
1027
|
+
directionOnly,
|
|
1028
|
+
{
|
|
1029
|
+
accounts: {
|
|
1030
|
+
state: await this.getStatePublicKey(),
|
|
1031
|
+
user: userAccountPublicKey,
|
|
1032
|
+
authority: this.wallet.publicKey,
|
|
1033
|
+
markets: state.markets,
|
|
1034
|
+
userOrders: await this.getUserOrdersAccountPublicKey(),
|
|
1035
|
+
userPositions: userAccount.positions,
|
|
1036
|
+
fundingPaymentHistory: state.fundingPaymentHistory,
|
|
1037
|
+
fundingRateHistory: state.fundingRateHistory,
|
|
1038
|
+
orderState: await this.getOrderStatePublicKey(),
|
|
1039
|
+
orderHistory: orderState.orderHistory,
|
|
1040
|
+
},
|
|
1041
|
+
remainingAccounts,
|
|
1042
|
+
}
|
|
1043
|
+
);
|
|
1044
|
+
}
|
|
1045
|
+
|
|
964
1046
|
public async fillOrder(
|
|
965
1047
|
userAccountPublicKey: PublicKey,
|
|
966
1048
|
userOrdersAccountPublicKey: PublicKey,
|
|
@@ -1340,4 +1422,122 @@ export class ClearingHouse {
|
|
|
1340
1422
|
public triggerEvent(eventName: keyof ClearingHouseAccountEvents, data?: any) {
|
|
1341
1423
|
this.eventEmitter.emit(eventName, data);
|
|
1342
1424
|
}
|
|
1425
|
+
|
|
1426
|
+
public async settlePositionAndClaimCollateral(
|
|
1427
|
+
collateralAccountPublicKey: PublicKey
|
|
1428
|
+
): Promise<TransactionSignature> {
|
|
1429
|
+
const settlePositionIx = await this.getSettlePositionIx();
|
|
1430
|
+
const claimCollateralIx = await this.getClaimCollateralIx(
|
|
1431
|
+
collateralAccountPublicKey
|
|
1432
|
+
);
|
|
1433
|
+
const tx = new Transaction().add(settlePositionIx).add(claimCollateralIx);
|
|
1434
|
+
|
|
1435
|
+
return this.txSender.send(tx, [], this.opts);
|
|
1436
|
+
}
|
|
1437
|
+
|
|
1438
|
+
public async getSettlePositionIx(): Promise<TransactionInstruction> {
|
|
1439
|
+
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
1440
|
+
const user: any = await this.program.account.user.fetch(
|
|
1441
|
+
userAccountPublicKey
|
|
1442
|
+
);
|
|
1443
|
+
const state = this.getStateAccount();
|
|
1444
|
+
|
|
1445
|
+
const settlementState = await getSettlementStatePublicKey(
|
|
1446
|
+
this.program.programId
|
|
1447
|
+
);
|
|
1448
|
+
|
|
1449
|
+
return await this.program.instruction.settlePosition({
|
|
1450
|
+
accounts: {
|
|
1451
|
+
state: await this.getStatePublicKey(),
|
|
1452
|
+
user: userAccountPublicKey,
|
|
1453
|
+
markets: state.markets,
|
|
1454
|
+
authority: this.wallet.publicKey,
|
|
1455
|
+
userPositions: user.positions,
|
|
1456
|
+
settlementState,
|
|
1457
|
+
fundingPaymentHistory: state.fundingPaymentHistory,
|
|
1458
|
+
},
|
|
1459
|
+
});
|
|
1460
|
+
}
|
|
1461
|
+
|
|
1462
|
+
public async getClaimCollateralIx(
|
|
1463
|
+
collateralAccountPublicKey: PublicKey
|
|
1464
|
+
): Promise<TransactionInstruction> {
|
|
1465
|
+
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
1466
|
+
const state = this.getStateAccount();
|
|
1467
|
+
|
|
1468
|
+
const settlementState = await getSettlementStatePublicKey(
|
|
1469
|
+
this.program.programId
|
|
1470
|
+
);
|
|
1471
|
+
|
|
1472
|
+
return await this.program.instruction.claimCollateral({
|
|
1473
|
+
accounts: {
|
|
1474
|
+
state: await this.getStatePublicKey(),
|
|
1475
|
+
user: userAccountPublicKey,
|
|
1476
|
+
collateralVault: state.collateralVault,
|
|
1477
|
+
collateralVaultAuthority: state.collateralVaultAuthority,
|
|
1478
|
+
userCollateralAccount: collateralAccountPublicKey,
|
|
1479
|
+
authority: this.wallet.publicKey,
|
|
1480
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
1481
|
+
settlementState,
|
|
1482
|
+
},
|
|
1483
|
+
});
|
|
1484
|
+
}
|
|
1485
|
+
|
|
1486
|
+
public async getTotalSettlementSize(): Promise<BN> {
|
|
1487
|
+
const accountLoader = new BulkAccountLoader(
|
|
1488
|
+
this.connection,
|
|
1489
|
+
'processed',
|
|
1490
|
+
50000
|
|
1491
|
+
);
|
|
1492
|
+
const clearingHouse = getClearingHouse(
|
|
1493
|
+
getPollingClearingHouseConfig(
|
|
1494
|
+
this.connection,
|
|
1495
|
+
this.wallet,
|
|
1496
|
+
this.program.programId,
|
|
1497
|
+
accountLoader
|
|
1498
|
+
)
|
|
1499
|
+
);
|
|
1500
|
+
|
|
1501
|
+
console.log('loading all users');
|
|
1502
|
+
const programUserAccounts =
|
|
1503
|
+
(await this.program.account.user.all()) as any[];
|
|
1504
|
+
const userArray: ClearingHouseUser[] = [];
|
|
1505
|
+
for (const programUserAccount of programUserAccounts) {
|
|
1506
|
+
const user = getClearingHouseUser(
|
|
1507
|
+
getPollingClearingHouseUserConfig(
|
|
1508
|
+
clearingHouse,
|
|
1509
|
+
programUserAccount.account.authority,
|
|
1510
|
+
accountLoader
|
|
1511
|
+
)
|
|
1512
|
+
);
|
|
1513
|
+
userArray.push(user);
|
|
1514
|
+
}
|
|
1515
|
+
|
|
1516
|
+
console.log('subscribing all users');
|
|
1517
|
+
await bulkPollingUserSubscribe(userArray, accountLoader);
|
|
1518
|
+
|
|
1519
|
+
console.log('calculating settlement size');
|
|
1520
|
+
const settlementSize = userArray.reduce((collateralToBeSettled, user) => {
|
|
1521
|
+
return collateralToBeSettled.add(
|
|
1522
|
+
user.getUserAccount().forgoPositionSettlement === 0
|
|
1523
|
+
? user.getSettledPositionValue()
|
|
1524
|
+
: ZERO
|
|
1525
|
+
);
|
|
1526
|
+
}, ZERO);
|
|
1527
|
+
|
|
1528
|
+
for (const user of userArray) {
|
|
1529
|
+
await user.unsubscribe();
|
|
1530
|
+
}
|
|
1531
|
+
|
|
1532
|
+
return settlementSize;
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1535
|
+
public async updateUserForgoSettlement(): Promise<TransactionSignature> {
|
|
1536
|
+
return await this.program.rpc.updateUserForgoSettlement({
|
|
1537
|
+
accounts: {
|
|
1538
|
+
authority: this.wallet.publicKey,
|
|
1539
|
+
user: await this.getUserAccountPublicKey(),
|
|
1540
|
+
},
|
|
1541
|
+
});
|
|
1542
|
+
}
|
|
1343
1543
|
}
|
package/src/clearingHouseUser.ts
CHANGED
|
@@ -6,12 +6,16 @@ import {
|
|
|
6
6
|
isVariant,
|
|
7
7
|
MarginCategory,
|
|
8
8
|
Order,
|
|
9
|
+
SettlementStateAccount,
|
|
9
10
|
UserAccount,
|
|
10
11
|
UserOrdersAccount,
|
|
11
12
|
UserPosition,
|
|
12
13
|
UserPositionsAccount,
|
|
13
14
|
} from './types';
|
|
14
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
calculateEntryPrice,
|
|
17
|
+
calculateSettledPositionPNL,
|
|
18
|
+
} from './math/position';
|
|
15
19
|
import {
|
|
16
20
|
MARK_PRICE_PRECISION,
|
|
17
21
|
AMM_TO_QUOTE_PRECISION_RATIO,
|
|
@@ -833,4 +837,63 @@ export class ClearingHouseUser {
|
|
|
833
837
|
|
|
834
838
|
return this.getTotalPositionValue().sub(currentMarketPositionValueUSDC);
|
|
835
839
|
}
|
|
840
|
+
|
|
841
|
+
public getClaimableCollateral(settlementState: SettlementStateAccount): BN {
|
|
842
|
+
return this.getSettledPositionValue()
|
|
843
|
+
.mul(
|
|
844
|
+
settlementState.collateralAvailableToClaim.sub(
|
|
845
|
+
this.getUserAccount().lastCollateralAvailableToClaim
|
|
846
|
+
)
|
|
847
|
+
)
|
|
848
|
+
.div(settlementState.totalSettlementValue);
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
public getSettledPositionValue(): BN {
|
|
852
|
+
return BN.max(
|
|
853
|
+
this.getUserAccount()
|
|
854
|
+
.collateral.add(
|
|
855
|
+
this.getUnrealizedFundingPNL().div(PRICE_TO_QUOTE_PRECISION)
|
|
856
|
+
)
|
|
857
|
+
.add(this.getSettledPositionsPNL()),
|
|
858
|
+
ZERO
|
|
859
|
+
);
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
public getSettledPositionsPNL(): BN {
|
|
863
|
+
return this.getUserPositionsAccount().positions.reduce(
|
|
864
|
+
(pnl, marketPosition) => {
|
|
865
|
+
const market = this.clearingHouse.getMarket(marketPosition.marketIndex);
|
|
866
|
+
return pnl.add(calculateSettledPositionPNL(market, marketPosition));
|
|
867
|
+
},
|
|
868
|
+
ZERO
|
|
869
|
+
);
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
public async estimateClaimableCollateral(): Promise<BN> {
|
|
873
|
+
const currentCollateralVaultBalance = new BN(
|
|
874
|
+
(
|
|
875
|
+
await this.clearingHouse.connection.getTokenAccountBalance(
|
|
876
|
+
this.clearingHouse.getStateAccount().collateralVault
|
|
877
|
+
)
|
|
878
|
+
).value.amount
|
|
879
|
+
);
|
|
880
|
+
const currentInsuranceVaultBalance = new BN(
|
|
881
|
+
(
|
|
882
|
+
await this.clearingHouse.connection.getTokenAccountBalance(
|
|
883
|
+
this.clearingHouse.getStateAccount().insuranceVault
|
|
884
|
+
)
|
|
885
|
+
).value.amount
|
|
886
|
+
);
|
|
887
|
+
|
|
888
|
+
const totalClaimableCollateral = currentCollateralVaultBalance.add(
|
|
889
|
+
currentInsuranceVaultBalance
|
|
890
|
+
);
|
|
891
|
+
const totalEstimatedSettlementValue =
|
|
892
|
+
await this.clearingHouse.getTotalSettlementSize();
|
|
893
|
+
const userSettledPositionValue = await this.getSettledPositionValue();
|
|
894
|
+
|
|
895
|
+
return totalClaimableCollateral
|
|
896
|
+
.mul(userSettledPositionValue)
|
|
897
|
+
.div(totalEstimatedSettlementValue);
|
|
898
|
+
}
|
|
836
899
|
}
|