@drift-labs/sdk 0.2.0-master.20 → 0.2.0-master.23
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/addresses/pda.d.ts +3 -0
- package/lib/addresses/pda.js +23 -1
- package/lib/admin.d.ts +3 -0
- package/lib/admin.js +31 -0
- package/lib/clearingHouse.d.ts +11 -4
- package/lib/clearingHouse.js +157 -23
- package/lib/clearingHouseUser.d.ts +9 -1
- package/lib/clearingHouseUser.js +62 -25
- package/lib/config.js +1 -1
- package/lib/constants/banks.d.ts +2 -0
- package/lib/constants/banks.js +9 -0
- package/lib/constants/numericConstants.d.ts +2 -0
- package/lib/constants/numericConstants.js +4 -1
- package/lib/events/sort.js +7 -10
- package/lib/events/types.d.ts +2 -1
- package/lib/events/types.js +1 -0
- package/lib/idl/clearing_house.json +1008 -147
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/math/amm.js +2 -2
- package/lib/math/insurance.d.ts +4 -0
- package/lib/math/insurance.js +27 -0
- package/lib/math/margin.d.ts +1 -1
- package/lib/math/margin.js +5 -7
- package/lib/math/orders.d.ts +2 -1
- package/lib/math/orders.js +14 -5
- package/lib/math/position.js +1 -1
- package/lib/types.d.ts +59 -20
- package/lib/types.js +0 -3
- package/package.json +1 -1
- package/src/addresses/pda.ts +47 -0
- package/src/admin.ts +65 -0
- package/src/clearingHouse.ts +244 -32
- package/src/clearingHouseUser.ts +77 -48
- package/src/config.ts +1 -1
- package/src/constants/banks.ts +16 -0
- package/src/constants/numericConstants.ts +4 -0
- package/src/events/sort.ts +10 -14
- package/src/events/types.ts +3 -0
- package/src/idl/clearing_house.json +1008 -147
- package/src/index.ts +1 -0
- package/src/math/amm.ts +2 -2
- package/src/math/insurance.ts +35 -0
- package/src/math/margin.ts +3 -10
- package/src/math/orders.ts +26 -9
- package/src/math/position.ts +2 -2
- package/src/types.ts +67 -20
- package/src/events/eventSubscriber.js +0 -139
- package/src/events/fetchLogs.js +0 -50
- package/src/events/pollingLogProvider.js +0 -64
- package/src/events/sort.js +0 -44
- package/src/events/webSocketLogProvider.js +0 -41
package/lib/addresses/pda.d.ts
CHANGED
|
@@ -11,3 +11,6 @@ export declare function getMarketPublicKey(programId: PublicKey, marketIndex: BN
|
|
|
11
11
|
export declare function getBankPublicKey(programId: PublicKey, bankIndex: BN): Promise<PublicKey>;
|
|
12
12
|
export declare function getBankVaultPublicKey(programId: PublicKey, bankIndex: BN): Promise<PublicKey>;
|
|
13
13
|
export declare function getBankVaultAuthorityPublicKey(programId: PublicKey, bankIndex: BN): Promise<PublicKey>;
|
|
14
|
+
export declare function getInsuranceFundVaultPublicKey(programId: PublicKey, bankIndex: BN): Promise<PublicKey>;
|
|
15
|
+
export declare function getInsuranceFundVaultAuthorityPublicKey(programId: PublicKey, bankIndex: BN): Promise<PublicKey>;
|
|
16
|
+
export declare function getInsuranceFundStakeAccountPublicKey(programId: PublicKey, authority: PublicKey, bankIndex: BN): PublicKey;
|
package/lib/addresses/pda.js
CHANGED
|
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.getBankVaultAuthorityPublicKey = exports.getBankVaultPublicKey = exports.getBankPublicKey = exports.getMarketPublicKey = exports.getUserStatsAccountPublicKey = exports.getUserAccountPublicKeySync = exports.getUserAccountPublicKey = exports.getUserAccountPublicKeyAndNonce = exports.getClearingHouseStateAccountPublicKey = exports.getClearingHouseStateAccountPublicKeyAndNonce = void 0;
|
|
26
|
+
exports.getInsuranceFundStakeAccountPublicKey = exports.getInsuranceFundVaultAuthorityPublicKey = exports.getInsuranceFundVaultPublicKey = exports.getBankVaultAuthorityPublicKey = exports.getBankVaultPublicKey = exports.getBankPublicKey = exports.getMarketPublicKey = exports.getUserStatsAccountPublicKey = exports.getUserAccountPublicKeySync = exports.getUserAccountPublicKey = exports.getUserAccountPublicKeyAndNonce = exports.getClearingHouseStateAccountPublicKey = exports.getClearingHouseStateAccountPublicKeyAndNonce = void 0;
|
|
27
27
|
const anchor = __importStar(require("@project-serum/anchor"));
|
|
28
28
|
async function getClearingHouseStateAccountPublicKeyAndNonce(programId) {
|
|
29
29
|
return anchor.web3.PublicKey.findProgramAddress([Buffer.from(anchor.utils.bytes.utf8.encode('clearing_house'))], programId);
|
|
@@ -88,3 +88,25 @@ async function getBankVaultAuthorityPublicKey(programId, bankIndex) {
|
|
|
88
88
|
], programId))[0];
|
|
89
89
|
}
|
|
90
90
|
exports.getBankVaultAuthorityPublicKey = getBankVaultAuthorityPublicKey;
|
|
91
|
+
async function getInsuranceFundVaultPublicKey(programId, bankIndex) {
|
|
92
|
+
return (await anchor.web3.PublicKey.findProgramAddress([
|
|
93
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('insurance_fund_vault')),
|
|
94
|
+
bankIndex.toArrayLike(Buffer, 'le', 8),
|
|
95
|
+
], programId))[0];
|
|
96
|
+
}
|
|
97
|
+
exports.getInsuranceFundVaultPublicKey = getInsuranceFundVaultPublicKey;
|
|
98
|
+
async function getInsuranceFundVaultAuthorityPublicKey(programId, bankIndex) {
|
|
99
|
+
return (await anchor.web3.PublicKey.findProgramAddress([
|
|
100
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('insurance_fund_vault_authority')),
|
|
101
|
+
bankIndex.toArrayLike(Buffer, 'le', 8),
|
|
102
|
+
], programId))[0];
|
|
103
|
+
}
|
|
104
|
+
exports.getInsuranceFundVaultAuthorityPublicKey = getInsuranceFundVaultAuthorityPublicKey;
|
|
105
|
+
function getInsuranceFundStakeAccountPublicKey(programId, authority, bankIndex) {
|
|
106
|
+
return anchor.web3.PublicKey.findProgramAddressSync([
|
|
107
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('insurance_fund_stake')),
|
|
108
|
+
authority.toBuffer(),
|
|
109
|
+
bankIndex.toArrayLike(Buffer, 'le', 8),
|
|
110
|
+
], programId)[0];
|
|
111
|
+
}
|
|
112
|
+
exports.getInsuranceFundStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey;
|
package/lib/admin.d.ts
CHANGED
|
@@ -20,6 +20,7 @@ export declare class Admin extends ClearingHouse {
|
|
|
20
20
|
updateCurveUpdateIntensity(marketIndex: BN, curveUpdateIntensity: number): Promise<TransactionSignature>;
|
|
21
21
|
updateMarginRatio(marketIndex: BN, marginRatioInitial: number, marginRatioMaintenance: number): Promise<TransactionSignature>;
|
|
22
22
|
updateMarketBaseSpread(marketIndex: BN, baseSpread: number): Promise<TransactionSignature>;
|
|
23
|
+
updateAmmJitIntensity(marketIndex: BN, ammJitIntensity: number): Promise<TransactionSignature>;
|
|
23
24
|
updateMarketMaxSpread(marketIndex: BN, maxSpread: number): Promise<TransactionSignature>;
|
|
24
25
|
updatePartialLiquidationClosePercentage(numerator: BN, denominator: BN): Promise<TransactionSignature>;
|
|
25
26
|
updatePartialLiquidationPenaltyPercentage(numerator: BN, denominator: BN): Promise<TransactionSignature>;
|
|
@@ -30,6 +31,8 @@ export declare class Admin extends ClearingHouse {
|
|
|
30
31
|
updateFee(fees: FeeStructure): Promise<TransactionSignature>;
|
|
31
32
|
updateOracleGuardRails(oracleGuardRails: OracleGuardRails): Promise<TransactionSignature>;
|
|
32
33
|
updateBankWithdrawGuardThreshold(bankIndex: BN, withdrawGuardThreshold: BN): Promise<TransactionSignature>;
|
|
34
|
+
updateBankIfFactor(bankIndex: BN, userIfFactor: BN, totalIfFactor: BN, liquidationIfFactor: BN): Promise<TransactionSignature>;
|
|
35
|
+
updateBankInsuranceWithdrawEscrowPeriod(bankIndex: BN, insuranceWithdrawEscrowPeriod: BN): Promise<TransactionSignature>;
|
|
33
36
|
updateLpCooldownTime(marketIndex: BN, cooldownTime: BN): Promise<TransactionSignature>;
|
|
34
37
|
updateMarketOracle(marketIndex: BN, oracle: PublicKey, oracleSource: OracleSource): Promise<TransactionSignature>;
|
|
35
38
|
updateMarketMinimumQuoteAssetTradeSize(marketIndex: BN, minimumTradeSize: BN): Promise<TransactionSignature>;
|
package/lib/admin.js
CHANGED
|
@@ -63,6 +63,8 @@ class Admin extends clearingHouse_1.ClearingHouse {
|
|
|
63
63
|
const bank = await (0, pda_1.getBankPublicKey)(this.program.programId, bankIndex);
|
|
64
64
|
const bankVault = await (0, pda_1.getBankVaultPublicKey)(this.program.programId, bankIndex);
|
|
65
65
|
const bankVaultAuthority = await (0, pda_1.getBankVaultAuthorityPublicKey)(this.program.programId, bankIndex);
|
|
66
|
+
const insuranceFundVault = await (0, pda_1.getInsuranceFundVaultPublicKey)(this.program.programId, bankIndex);
|
|
67
|
+
const insuranceFundVaultAuthority = await (0, pda_1.getInsuranceFundVaultAuthorityPublicKey)(this.program.programId, bankIndex);
|
|
66
68
|
const initializeTx = await this.program.transaction.initializeBank(optimalUtilization, optimalRate, maxRate, oracleSource, initialAssetWeight, maintenanceAssetWeight, initialLiabilityWeight, maintenanceLiabilityWeight, imfFactor, liquidationFee, {
|
|
67
69
|
accounts: {
|
|
68
70
|
admin: this.wallet.publicKey,
|
|
@@ -70,6 +72,8 @@ class Admin extends clearingHouse_1.ClearingHouse {
|
|
|
70
72
|
bank,
|
|
71
73
|
bankVault,
|
|
72
74
|
bankVaultAuthority,
|
|
75
|
+
insuranceFundVault,
|
|
76
|
+
insuranceFundVaultAuthority,
|
|
73
77
|
bankMint: mint,
|
|
74
78
|
oracle,
|
|
75
79
|
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
|
@@ -260,6 +264,15 @@ class Admin extends clearingHouse_1.ClearingHouse {
|
|
|
260
264
|
},
|
|
261
265
|
});
|
|
262
266
|
}
|
|
267
|
+
async updateAmmJitIntensity(marketIndex, ammJitIntensity) {
|
|
268
|
+
return await this.program.rpc.updateAmmJitIntensity(ammJitIntensity, {
|
|
269
|
+
accounts: {
|
|
270
|
+
admin: this.wallet.publicKey,
|
|
271
|
+
state: await this.getStatePublicKey(),
|
|
272
|
+
market: await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex),
|
|
273
|
+
},
|
|
274
|
+
});
|
|
275
|
+
}
|
|
263
276
|
async updateMarketMaxSpread(marketIndex, maxSpread) {
|
|
264
277
|
return await this.program.rpc.updateMarketMaxSpread(maxSpread, {
|
|
265
278
|
accounts: {
|
|
@@ -342,6 +355,24 @@ class Admin extends clearingHouse_1.ClearingHouse {
|
|
|
342
355
|
},
|
|
343
356
|
});
|
|
344
357
|
}
|
|
358
|
+
async updateBankIfFactor(bankIndex, userIfFactor, totalIfFactor, liquidationIfFactor) {
|
|
359
|
+
return await this.program.rpc.updateBankIfFactor(bankIndex, userIfFactor, totalIfFactor, liquidationIfFactor, {
|
|
360
|
+
accounts: {
|
|
361
|
+
admin: this.wallet.publicKey,
|
|
362
|
+
state: await this.getStatePublicKey(),
|
|
363
|
+
bank: await (0, pda_1.getBankPublicKey)(this.program.programId, bankIndex),
|
|
364
|
+
},
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
async updateBankInsuranceWithdrawEscrowPeriod(bankIndex, insuranceWithdrawEscrowPeriod) {
|
|
368
|
+
return await this.program.rpc.updateBankInsuranceWithdrawEscrowPeriod(insuranceWithdrawEscrowPeriod, {
|
|
369
|
+
accounts: {
|
|
370
|
+
admin: this.wallet.publicKey,
|
|
371
|
+
state: await this.getStatePublicKey(),
|
|
372
|
+
bank: await (0, pda_1.getBankPublicKey)(this.program.programId, bankIndex),
|
|
373
|
+
},
|
|
374
|
+
});
|
|
375
|
+
}
|
|
345
376
|
async updateLpCooldownTime(marketIndex, cooldownTime) {
|
|
346
377
|
return await this.program.rpc.updateLpCooldownTime(cooldownTime, {
|
|
347
378
|
accounts: {
|
package/lib/clearingHouse.d.ts
CHANGED
|
@@ -149,12 +149,12 @@ export declare class ClearingHouse {
|
|
|
149
149
|
getLiquidateBorrowForPerpPnlIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, perpMarketIndex: BN, liabilityBankIndex: BN, maxLiabilityTransfer: BN): Promise<TransactionInstruction>;
|
|
150
150
|
liquidatePerpPnlForDeposit(userAccountPublicKey: PublicKey, userAccount: UserAccount, perpMarketIndex: BN, assetBankIndex: BN, maxPnlTransfer: BN): Promise<TransactionSignature>;
|
|
151
151
|
getLiquidatePerpPnlForDepositIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, perpMarketIndex: BN, assetBankIndex: BN, maxPnlTransfer: BN): Promise<TransactionInstruction>;
|
|
152
|
-
resolvePerpBankruptcy(userAccountPublicKey: PublicKey, userAccount: UserAccount, marketIndex: BN): Promise<TransactionSignature>;
|
|
153
|
-
getResolvePerpBankruptcyIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, marketIndex: BN): Promise<TransactionInstruction>;
|
|
152
|
+
resolvePerpBankruptcy(userAccountPublicKey: PublicKey, userAccount: UserAccount, bankIndex: BN, marketIndex: BN): Promise<TransactionSignature>;
|
|
153
|
+
getResolvePerpBankruptcyIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, bankIndex: BN, marketIndex: BN): Promise<TransactionInstruction>;
|
|
154
154
|
resolveBorrowBankruptcy(userAccountPublicKey: PublicKey, userAccount: UserAccount, bankIndex: BN): Promise<TransactionSignature>;
|
|
155
155
|
getResolveBorrowBankruptcyIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, bankIndex: BN): Promise<TransactionInstruction>;
|
|
156
|
-
|
|
157
|
-
|
|
156
|
+
getRemainingAccountsWithCounterparty(params: {
|
|
157
|
+
counterPartyUserAccount: UserAccount;
|
|
158
158
|
writableMarketIndex?: BN;
|
|
159
159
|
writableBankIndexes?: BN[];
|
|
160
160
|
}): AccountMeta[];
|
|
@@ -164,4 +164,11 @@ export declare class ClearingHouse {
|
|
|
164
164
|
getSettleFundingPaymentIx(userAccount: PublicKey): Promise<TransactionInstruction>;
|
|
165
165
|
triggerEvent(eventName: keyof ClearingHouseAccountEvents, data?: any): void;
|
|
166
166
|
getOracleDataForMarket(marketIndex: BN): OraclePriceData;
|
|
167
|
+
initializeInsuranceFundStake(bankIndex: BN): Promise<TransactionSignature>;
|
|
168
|
+
getInitializeInsuranceFundStakeIx(bankIndex: BN): Promise<TransactionInstruction>;
|
|
169
|
+
addInsuranceFundStake(bankIndex: BN, amount: BN, collateralAccountPublicKey: PublicKey): Promise<TransactionSignature>;
|
|
170
|
+
requestRemoveInsuranceFundStake(bankIndex: BN, amount: BN): Promise<TransactionSignature>;
|
|
171
|
+
cancelRequestRemoveInsuranceFundStake(bankIndex: BN): Promise<TransactionSignature>;
|
|
172
|
+
removeInsuranceFundStake(bankIndex: BN, collateralAccountPublicKey: PublicKey): Promise<TransactionSignature>;
|
|
173
|
+
settleRevenueToInsuranceFund(bankIndex: BN): Promise<TransactionSignature>;
|
|
167
174
|
}
|
package/lib/clearingHouse.js
CHANGED
|
@@ -133,9 +133,13 @@ class ClearingHouse {
|
|
|
133
133
|
* Forces the accountSubscriber to fetch account updates from rpc
|
|
134
134
|
*/
|
|
135
135
|
async fetchAccounts() {
|
|
136
|
-
|
|
136
|
+
const promises = [...this.users.values()]
|
|
137
137
|
.map((user) => user.fetchAccounts())
|
|
138
|
-
.concat(this.accountSubscriber.fetch())
|
|
138
|
+
.concat(this.accountSubscriber.fetch());
|
|
139
|
+
if (this.userStats) {
|
|
140
|
+
promises.concat(this.userStats.fetchAccounts());
|
|
141
|
+
}
|
|
142
|
+
await Promise.all(promises);
|
|
139
143
|
}
|
|
140
144
|
async unsubscribe() {
|
|
141
145
|
const unsubscribePromises = this.unsubscribeUsers().concat(this.accountSubscriber.unsubscribe());
|
|
@@ -1077,7 +1081,9 @@ class ClearingHouse {
|
|
|
1077
1081
|
orderParams = this.getOrderParams(orderParams);
|
|
1078
1082
|
const userStatsPublicKey = this.getUserStatsAccountPublicKey();
|
|
1079
1083
|
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
1080
|
-
|
|
1084
|
+
// todo merge this with getRemainingAccounts
|
|
1085
|
+
const remainingAccounts = this.getRemainingAccountsWithCounterparty({
|
|
1086
|
+
counterPartyUserAccount: takerInfo.takerUserAccount,
|
|
1081
1087
|
writableMarketIndex: orderParams.marketIndex,
|
|
1082
1088
|
});
|
|
1083
1089
|
if (referrerInfo) {
|
|
@@ -1216,9 +1222,9 @@ class ClearingHouse {
|
|
|
1216
1222
|
const userStatsPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, userAccount.authority);
|
|
1217
1223
|
const liquidatorPublicKey = await this.getUserAccountPublicKey();
|
|
1218
1224
|
const liquidatorStatsPublicKey = this.getUserStatsAccountPublicKey();
|
|
1219
|
-
const remainingAccounts = this.
|
|
1225
|
+
const remainingAccounts = this.getRemainingAccountsWithCounterparty({
|
|
1220
1226
|
writableMarketIndex: marketIndex,
|
|
1221
|
-
userAccount,
|
|
1227
|
+
counterPartyUserAccount: userAccount,
|
|
1222
1228
|
});
|
|
1223
1229
|
return await this.program.instruction.liquidatePerp(marketIndex, maxBaseAssetAmount, {
|
|
1224
1230
|
accounts: {
|
|
@@ -1238,8 +1244,8 @@ class ClearingHouse {
|
|
|
1238
1244
|
}
|
|
1239
1245
|
async getLiquidateBorrowIx(userAccountPublicKey, userAccount, assetBankIndex, liabilityBankIndex, maxLiabilityTransfer) {
|
|
1240
1246
|
const liquidatorPublicKey = await this.getUserAccountPublicKey();
|
|
1241
|
-
const remainingAccounts = this.
|
|
1242
|
-
userAccount,
|
|
1247
|
+
const remainingAccounts = this.getRemainingAccountsWithCounterparty({
|
|
1248
|
+
counterPartyUserAccount: userAccount,
|
|
1243
1249
|
writableBankIndexes: [liabilityBankIndex, assetBankIndex],
|
|
1244
1250
|
});
|
|
1245
1251
|
return await this.program.instruction.liquidateBorrow(assetBankIndex, liabilityBankIndex, maxLiabilityTransfer, {
|
|
@@ -1258,8 +1264,8 @@ class ClearingHouse {
|
|
|
1258
1264
|
}
|
|
1259
1265
|
async getLiquidateBorrowForPerpPnlIx(userAccountPublicKey, userAccount, perpMarketIndex, liabilityBankIndex, maxLiabilityTransfer) {
|
|
1260
1266
|
const liquidatorPublicKey = await this.getUserAccountPublicKey();
|
|
1261
|
-
const remainingAccounts = this.
|
|
1262
|
-
userAccount,
|
|
1267
|
+
const remainingAccounts = this.getRemainingAccountsWithCounterparty({
|
|
1268
|
+
counterPartyUserAccount: userAccount,
|
|
1263
1269
|
writableMarketIndex: perpMarketIndex,
|
|
1264
1270
|
writableBankIndexes: [liabilityBankIndex],
|
|
1265
1271
|
});
|
|
@@ -1279,8 +1285,8 @@ class ClearingHouse {
|
|
|
1279
1285
|
}
|
|
1280
1286
|
async getLiquidatePerpPnlForDepositIx(userAccountPublicKey, userAccount, perpMarketIndex, assetBankIndex, maxPnlTransfer) {
|
|
1281
1287
|
const liquidatorPublicKey = await this.getUserAccountPublicKey();
|
|
1282
|
-
const remainingAccounts = this.
|
|
1283
|
-
userAccount,
|
|
1288
|
+
const remainingAccounts = this.getRemainingAccountsWithCounterparty({
|
|
1289
|
+
counterPartyUserAccount: userAccount,
|
|
1284
1290
|
writableMarketIndex: perpMarketIndex,
|
|
1285
1291
|
writableBankIndexes: [assetBankIndex],
|
|
1286
1292
|
});
|
|
@@ -1294,22 +1300,28 @@ class ClearingHouse {
|
|
|
1294
1300
|
remainingAccounts: remainingAccounts,
|
|
1295
1301
|
});
|
|
1296
1302
|
}
|
|
1297
|
-
async resolvePerpBankruptcy(userAccountPublicKey, userAccount, marketIndex) {
|
|
1298
|
-
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getResolvePerpBankruptcyIx(userAccountPublicKey, userAccount, marketIndex)), [], this.opts);
|
|
1303
|
+
async resolvePerpBankruptcy(userAccountPublicKey, userAccount, bankIndex, marketIndex) {
|
|
1304
|
+
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getResolvePerpBankruptcyIx(userAccountPublicKey, userAccount, bankIndex, marketIndex)), [], this.opts);
|
|
1299
1305
|
return txSig;
|
|
1300
1306
|
}
|
|
1301
|
-
async getResolvePerpBankruptcyIx(userAccountPublicKey, userAccount, marketIndex) {
|
|
1307
|
+
async getResolvePerpBankruptcyIx(userAccountPublicKey, userAccount, bankIndex, marketIndex) {
|
|
1302
1308
|
const liquidatorPublicKey = await this.getUserAccountPublicKey();
|
|
1303
|
-
const remainingAccounts = this.
|
|
1309
|
+
const remainingAccounts = this.getRemainingAccountsWithCounterparty({
|
|
1304
1310
|
writableMarketIndex: marketIndex,
|
|
1305
|
-
|
|
1311
|
+
writableBankIndexes: [bankIndex],
|
|
1312
|
+
counterPartyUserAccount: userAccount,
|
|
1306
1313
|
});
|
|
1307
|
-
|
|
1314
|
+
const bank = this.getBankAccount(bankIndex);
|
|
1315
|
+
return await this.program.instruction.resolvePerpBankruptcy(bankIndex, marketIndex, {
|
|
1308
1316
|
accounts: {
|
|
1309
1317
|
state: await this.getStatePublicKey(),
|
|
1310
1318
|
authority: this.wallet.publicKey,
|
|
1311
1319
|
user: userAccountPublicKey,
|
|
1312
1320
|
liquidator: liquidatorPublicKey,
|
|
1321
|
+
bankVault: bank.vault,
|
|
1322
|
+
insuranceFundVault: bank.insuranceFundVault,
|
|
1323
|
+
insuranceFundVaultAuthority: bank.insuranceFundVaultAuthority,
|
|
1324
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1313
1325
|
},
|
|
1314
1326
|
remainingAccounts: remainingAccounts,
|
|
1315
1327
|
});
|
|
@@ -1320,26 +1332,31 @@ class ClearingHouse {
|
|
|
1320
1332
|
}
|
|
1321
1333
|
async getResolveBorrowBankruptcyIx(userAccountPublicKey, userAccount, bankIndex) {
|
|
1322
1334
|
const liquidatorPublicKey = await this.getUserAccountPublicKey();
|
|
1323
|
-
const remainingAccounts = this.
|
|
1335
|
+
const remainingAccounts = this.getRemainingAccountsWithCounterparty({
|
|
1324
1336
|
writableBankIndexes: [bankIndex],
|
|
1325
|
-
userAccount,
|
|
1337
|
+
counterPartyUserAccount: userAccount,
|
|
1326
1338
|
});
|
|
1339
|
+
const bank = this.getBankAccount(bankIndex);
|
|
1327
1340
|
return await this.program.instruction.resolveBorrowBankruptcy(bankIndex, {
|
|
1328
1341
|
accounts: {
|
|
1329
1342
|
state: await this.getStatePublicKey(),
|
|
1330
1343
|
authority: this.wallet.publicKey,
|
|
1331
1344
|
user: userAccountPublicKey,
|
|
1332
1345
|
liquidator: liquidatorPublicKey,
|
|
1346
|
+
bankVault: bank.vault,
|
|
1347
|
+
insuranceFundVault: bank.insuranceFundVault,
|
|
1348
|
+
insuranceFundVaultAuthority: bank.insuranceFundVaultAuthority,
|
|
1349
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1333
1350
|
},
|
|
1334
1351
|
remainingAccounts: remainingAccounts,
|
|
1335
1352
|
});
|
|
1336
1353
|
}
|
|
1337
|
-
|
|
1338
|
-
const
|
|
1354
|
+
getRemainingAccountsWithCounterparty(params) {
|
|
1355
|
+
const counterPartyUserAccount = params.counterPartyUserAccount;
|
|
1339
1356
|
const oracleAccountMap = new Map();
|
|
1340
1357
|
const bankAccountMap = new Map();
|
|
1341
1358
|
const marketAccountMap = new Map();
|
|
1342
|
-
for (const bankBalance of
|
|
1359
|
+
for (const bankBalance of counterPartyUserAccount.bankBalances) {
|
|
1343
1360
|
if (!bankBalance.balance.eq(numericConstants_1.ZERO)) {
|
|
1344
1361
|
const bankAccount = this.getBankAccount(bankBalance.bankIndex);
|
|
1345
1362
|
bankAccountMap.set(bankBalance.bankIndex.toNumber(), {
|
|
@@ -1356,7 +1373,7 @@ class ClearingHouse {
|
|
|
1356
1373
|
}
|
|
1357
1374
|
}
|
|
1358
1375
|
}
|
|
1359
|
-
for (const position of
|
|
1376
|
+
for (const position of counterPartyUserAccount.positions) {
|
|
1360
1377
|
if (!(0, position_1.positionIsAvailable)(position)) {
|
|
1361
1378
|
const market = this.getMarketAccount(position.marketIndex);
|
|
1362
1379
|
marketAccountMap.set(position.marketIndex.toNumber(), {
|
|
@@ -1511,5 +1528,122 @@ class ClearingHouse {
|
|
|
1511
1528
|
const oracleData = this.getOraclePriceDataAndSlot(oracleKey).data;
|
|
1512
1529
|
return oracleData;
|
|
1513
1530
|
}
|
|
1531
|
+
async initializeInsuranceFundStake(bankIndex) {
|
|
1532
|
+
const { txSig } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getInitializeInsuranceFundStakeIx(bankIndex)), [], this.opts);
|
|
1533
|
+
return txSig;
|
|
1534
|
+
}
|
|
1535
|
+
async getInitializeInsuranceFundStakeIx(bankIndex) {
|
|
1536
|
+
const ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, bankIndex);
|
|
1537
|
+
return await this.program.instruction.initializeInsuranceFundStake(bankIndex, {
|
|
1538
|
+
accounts: {
|
|
1539
|
+
insuranceFundStake: ifStakeAccountPublicKey,
|
|
1540
|
+
bank: this.getBankAccount(bankIndex).pubkey,
|
|
1541
|
+
userStats: this.getUserStatsAccountPublicKey(),
|
|
1542
|
+
authority: this.wallet.publicKey,
|
|
1543
|
+
payer: this.wallet.publicKey,
|
|
1544
|
+
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
|
|
1545
|
+
systemProgram: anchor.web3.SystemProgram.programId,
|
|
1546
|
+
state: await this.getStatePublicKey(),
|
|
1547
|
+
},
|
|
1548
|
+
});
|
|
1549
|
+
}
|
|
1550
|
+
async addInsuranceFundStake(bankIndex, amount, collateralAccountPublicKey) {
|
|
1551
|
+
const bank = this.getBankAccount(bankIndex);
|
|
1552
|
+
const ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, bankIndex);
|
|
1553
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
1554
|
+
writableBankIndex: bankIndex,
|
|
1555
|
+
});
|
|
1556
|
+
return await this.program.rpc.addInsuranceFundStake(bankIndex, amount, {
|
|
1557
|
+
accounts: {
|
|
1558
|
+
state: await this.getStatePublicKey(),
|
|
1559
|
+
bank: bank.pubkey,
|
|
1560
|
+
insuranceFundStake: ifStakeAccountPublicKey,
|
|
1561
|
+
userStats: this.getUserStatsAccountPublicKey(),
|
|
1562
|
+
authority: this.wallet.publicKey,
|
|
1563
|
+
insuranceFundVault: bank.insuranceFundVault,
|
|
1564
|
+
userTokenAccount: collateralAccountPublicKey,
|
|
1565
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1566
|
+
},
|
|
1567
|
+
remainingAccounts,
|
|
1568
|
+
});
|
|
1569
|
+
}
|
|
1570
|
+
async requestRemoveInsuranceFundStake(bankIndex, amount) {
|
|
1571
|
+
const bank = this.getBankAccount(bankIndex);
|
|
1572
|
+
const ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, bankIndex);
|
|
1573
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
1574
|
+
writableBankIndex: bankIndex,
|
|
1575
|
+
});
|
|
1576
|
+
return await this.program.rpc.requestRemoveInsuranceFundStake(bankIndex, amount, {
|
|
1577
|
+
accounts: {
|
|
1578
|
+
state: await this.getStatePublicKey(),
|
|
1579
|
+
bank: bank.pubkey,
|
|
1580
|
+
insuranceFundStake: ifStakeAccountPublicKey,
|
|
1581
|
+
userStats: this.getUserStatsAccountPublicKey(),
|
|
1582
|
+
authority: this.wallet.publicKey,
|
|
1583
|
+
insuranceFundVault: bank.insuranceFundVault,
|
|
1584
|
+
// userTokenAccount: collateralAccountPublicKey,
|
|
1585
|
+
// tokenProgram: TOKEN_PROGRAM_ID,
|
|
1586
|
+
},
|
|
1587
|
+
remainingAccounts,
|
|
1588
|
+
});
|
|
1589
|
+
}
|
|
1590
|
+
async cancelRequestRemoveInsuranceFundStake(bankIndex) {
|
|
1591
|
+
const bank = this.getBankAccount(bankIndex);
|
|
1592
|
+
const ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, bankIndex);
|
|
1593
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
1594
|
+
writableBankIndex: bankIndex,
|
|
1595
|
+
});
|
|
1596
|
+
return await this.program.rpc.cancelRequestRemoveInsuranceFundStake(bankIndex, {
|
|
1597
|
+
accounts: {
|
|
1598
|
+
state: await this.getStatePublicKey(),
|
|
1599
|
+
bank: bank.pubkey,
|
|
1600
|
+
insuranceFundStake: ifStakeAccountPublicKey,
|
|
1601
|
+
userStats: this.getUserStatsAccountPublicKey(),
|
|
1602
|
+
authority: this.wallet.publicKey,
|
|
1603
|
+
insuranceFundVault: bank.insuranceFundVault,
|
|
1604
|
+
// userTokenAccount: collateralAccountPublicKey,
|
|
1605
|
+
// tokenProgram: TOKEN_PROGRAM_ID,
|
|
1606
|
+
},
|
|
1607
|
+
remainingAccounts,
|
|
1608
|
+
});
|
|
1609
|
+
}
|
|
1610
|
+
async removeInsuranceFundStake(bankIndex, collateralAccountPublicKey) {
|
|
1611
|
+
const bank = this.getBankAccount(bankIndex);
|
|
1612
|
+
const ifStakeAccountPublicKey = (0, pda_1.getInsuranceFundStakeAccountPublicKey)(this.program.programId, this.wallet.publicKey, bankIndex);
|
|
1613
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
1614
|
+
writableBankIndex: bankIndex,
|
|
1615
|
+
});
|
|
1616
|
+
return await this.program.rpc.removeInsuranceFundStake(bankIndex, {
|
|
1617
|
+
accounts: {
|
|
1618
|
+
state: await this.getStatePublicKey(),
|
|
1619
|
+
bank: bank.pubkey,
|
|
1620
|
+
insuranceFundStake: ifStakeAccountPublicKey,
|
|
1621
|
+
userStats: this.getUserStatsAccountPublicKey(),
|
|
1622
|
+
authority: this.wallet.publicKey,
|
|
1623
|
+
insuranceFundVault: bank.insuranceFundVault,
|
|
1624
|
+
insuranceFundVaultAuthority: bank.insuranceFundVaultAuthority,
|
|
1625
|
+
userTokenAccount: collateralAccountPublicKey,
|
|
1626
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1627
|
+
},
|
|
1628
|
+
remainingAccounts,
|
|
1629
|
+
});
|
|
1630
|
+
}
|
|
1631
|
+
async settleRevenueToInsuranceFund(bankIndex) {
|
|
1632
|
+
const bank = this.getBankAccount(bankIndex);
|
|
1633
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
1634
|
+
writableBankIndex: bankIndex,
|
|
1635
|
+
});
|
|
1636
|
+
return await this.program.rpc.settleRevenueToInsuranceFund(bankIndex, {
|
|
1637
|
+
accounts: {
|
|
1638
|
+
state: await this.getStatePublicKey(),
|
|
1639
|
+
bank: bank.pubkey,
|
|
1640
|
+
bankVault: bank.vault,
|
|
1641
|
+
bankVaultAuthority: bank.vaultAuthority,
|
|
1642
|
+
insuranceFundVault: bank.insuranceFundVault,
|
|
1643
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1644
|
+
},
|
|
1645
|
+
remainingAccounts,
|
|
1646
|
+
});
|
|
1647
|
+
}
|
|
1514
1648
|
}
|
|
1515
1649
|
exports.ClearingHouse = ClearingHouse;
|
|
@@ -37,6 +37,7 @@ export declare class ClearingHouseUser {
|
|
|
37
37
|
*/
|
|
38
38
|
getUserPosition(marketIndex: BN): UserPosition | undefined;
|
|
39
39
|
getEmptyPosition(marketIndex: BN): UserPosition;
|
|
40
|
+
getClonedPosition(position: UserPosition): UserPosition;
|
|
40
41
|
/**
|
|
41
42
|
* @param orderId
|
|
42
43
|
* @returns Order
|
|
@@ -53,7 +54,7 @@ export declare class ClearingHouseUser {
|
|
|
53
54
|
* calculates the market position if the lp position was settled
|
|
54
55
|
* @returns : userPosition
|
|
55
56
|
*/
|
|
56
|
-
getSettledLPPosition(marketIndex: BN): [UserPosition, BN];
|
|
57
|
+
getSettledLPPosition(marketIndex: BN): [UserPosition, BN, BN];
|
|
57
58
|
/**
|
|
58
59
|
* calculates Buying Power = FC * MAX_LEVERAGE
|
|
59
60
|
* @returns : Precision QUOTE_PRECISION
|
|
@@ -64,6 +65,13 @@ export declare class ClearingHouseUser {
|
|
|
64
65
|
* @returns : Precision QUOTE_PRECISION
|
|
65
66
|
*/
|
|
66
67
|
getFreeCollateral(): BN;
|
|
68
|
+
/**
|
|
69
|
+
* @returns The margin requirement of a certain type (Initial or Maintenance) in USDC. : QUOTE_PRECISION
|
|
70
|
+
*/
|
|
71
|
+
getMarginRequirement(type: MarginCategory): BN;
|
|
72
|
+
/**
|
|
73
|
+
* @returns The initial margin requirement in USDC. : QUOTE_PRECISION
|
|
74
|
+
*/
|
|
67
75
|
getInitialMarginRequirement(): BN;
|
|
68
76
|
/**
|
|
69
77
|
* @returns The maintenance margin requirement in USDC. : QUOTE_PRECISION
|
package/lib/clearingHouseUser.js
CHANGED
|
@@ -78,6 +78,10 @@ class ClearingHouseUser {
|
|
|
78
78
|
lastNetQuoteAssetAmountPerLp: numericConstants_1.ZERO,
|
|
79
79
|
};
|
|
80
80
|
}
|
|
81
|
+
getClonedPosition(position) {
|
|
82
|
+
const clonedPosition = Object.assign({}, position);
|
|
83
|
+
return clonedPosition;
|
|
84
|
+
}
|
|
81
85
|
/**
|
|
82
86
|
* @param orderId
|
|
83
87
|
* @returns Order
|
|
@@ -104,7 +108,8 @@ class ClearingHouseUser {
|
|
|
104
108
|
* @returns : userPosition
|
|
105
109
|
*/
|
|
106
110
|
getSettledLPPosition(marketIndex) {
|
|
107
|
-
const
|
|
111
|
+
const _position = this.getUserPosition(marketIndex);
|
|
112
|
+
const position = this.getClonedPosition(_position);
|
|
108
113
|
const market = this.clearingHouse.getMarketAccount(position.marketIndex);
|
|
109
114
|
const nShares = position.lpShares;
|
|
110
115
|
const deltaBaa = market.amm.marketPositionPerLp.baseAssetAmount
|
|
@@ -180,7 +185,7 @@ class ClearingHouseUser {
|
|
|
180
185
|
else {
|
|
181
186
|
position.lastCumulativeFundingRate = numericConstants_1.ZERO;
|
|
182
187
|
}
|
|
183
|
-
return [position, pnl];
|
|
188
|
+
return [position, remainderBaa, pnl];
|
|
184
189
|
}
|
|
185
190
|
/**
|
|
186
191
|
* calculates Buying Power = FC * MAX_LEVERAGE
|
|
@@ -201,37 +206,69 @@ class ClearingHouseUser {
|
|
|
201
206
|
const freeCollateral = totalCollateral.sub(initialMarginRequirement);
|
|
202
207
|
return freeCollateral.gte(numericConstants_1.ZERO) ? freeCollateral : numericConstants_1.ZERO;
|
|
203
208
|
}
|
|
204
|
-
getInitialMarginRequirement() {
|
|
205
|
-
const postionMarginRequirement = this.getUserAccount().positions.reduce((marginRequirement, marketPosition) => {
|
|
206
|
-
const market = this.clearingHouse.getMarketAccount(marketPosition.marketIndex);
|
|
207
|
-
const worstCaseBaseAssetAmount = (0, margin_1.calculateWorstCaseBaseAssetAmount)(marketPosition);
|
|
208
|
-
const worstCaseAssetValue = worstCaseBaseAssetAmount
|
|
209
|
-
.abs()
|
|
210
|
-
.mul(this.getOracleDataForMarket(market.marketIndex).price)
|
|
211
|
-
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.MARK_PRICE_PRECISION));
|
|
212
|
-
const marketMarginRatio = new _1.BN((0, _1.calculateMarketMarginRatio)(market, worstCaseBaseAssetAmount.abs(), 'Initial'));
|
|
213
|
-
return marginRequirement.add(worstCaseAssetValue.mul(marketMarginRatio).div(numericConstants_1.MARGIN_PRECISION));
|
|
214
|
-
}, numericConstants_1.ZERO);
|
|
215
|
-
const bankMarginRequirement = this.getBankLiabilityValue(undefined, 'Initial');
|
|
216
|
-
return bankMarginRequirement.add(postionMarginRequirement);
|
|
217
|
-
}
|
|
218
209
|
/**
|
|
219
|
-
* @returns The
|
|
210
|
+
* @returns The margin requirement of a certain type (Initial or Maintenance) in USDC. : QUOTE_PRECISION
|
|
220
211
|
*/
|
|
221
|
-
|
|
212
|
+
getMarginRequirement(type) {
|
|
222
213
|
return this.getUserAccount()
|
|
223
214
|
.positions.reduce((marginRequirement, marketPosition) => {
|
|
224
215
|
const market = this.clearingHouse.getMarketAccount(marketPosition.marketIndex);
|
|
216
|
+
if (marketPosition.lpShares.gt(numericConstants_1.ZERO)) {
|
|
217
|
+
// is an lp
|
|
218
|
+
// clone so we dont mutate the position
|
|
219
|
+
marketPosition = this.getClonedPosition(marketPosition);
|
|
220
|
+
// settle position
|
|
221
|
+
const [settledPosition, dustBaa, _] = this.getSettledLPPosition(market.marketIndex);
|
|
222
|
+
marketPosition.baseAssetAmount =
|
|
223
|
+
settledPosition.baseAssetAmount.add(dustBaa);
|
|
224
|
+
marketPosition.quoteAssetAmount = settledPosition.quoteAssetAmount;
|
|
225
|
+
// open orders
|
|
226
|
+
let openAsks;
|
|
227
|
+
if (market.amm.maxBaseAssetReserve > market.amm.baseAssetReserve) {
|
|
228
|
+
openAsks = market.amm.maxBaseAssetReserve
|
|
229
|
+
.sub(market.amm.baseAssetReserve)
|
|
230
|
+
.mul(marketPosition.lpShares)
|
|
231
|
+
.div(market.amm.sqrtK)
|
|
232
|
+
.mul(new _1.BN(-1));
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
openAsks = numericConstants_1.ZERO;
|
|
236
|
+
}
|
|
237
|
+
let openBids;
|
|
238
|
+
if (market.amm.minBaseAssetReserve < market.amm.baseAssetReserve) {
|
|
239
|
+
openBids = market.amm.baseAssetReserve
|
|
240
|
+
.sub(market.amm.minBaseAssetReserve)
|
|
241
|
+
.mul(marketPosition.lpShares)
|
|
242
|
+
.div(market.amm.sqrtK);
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
openBids = numericConstants_1.ZERO;
|
|
246
|
+
}
|
|
247
|
+
marketPosition.openAsks = marketPosition.openAsks.add(openAsks);
|
|
248
|
+
marketPosition.openBids = marketPosition.openBids.add(openBids);
|
|
249
|
+
}
|
|
225
250
|
const worstCaseBaseAssetAmount = (0, margin_1.calculateWorstCaseBaseAssetAmount)(marketPosition);
|
|
226
251
|
const worstCaseAssetValue = worstCaseBaseAssetAmount
|
|
227
252
|
.abs()
|
|
228
253
|
.mul(this.getOracleDataForMarket(market.marketIndex).price)
|
|
229
254
|
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.MARK_PRICE_PRECISION));
|
|
230
255
|
return marginRequirement.add(worstCaseAssetValue
|
|
231
|
-
.mul(new _1.BN((0, _1.calculateMarketMarginRatio)(market, worstCaseBaseAssetAmount.abs(),
|
|
256
|
+
.mul(new _1.BN((0, _1.calculateMarketMarginRatio)(market, worstCaseBaseAssetAmount.abs(), type)))
|
|
232
257
|
.div(numericConstants_1.MARGIN_PRECISION));
|
|
233
258
|
}, numericConstants_1.ZERO)
|
|
234
|
-
.add(this.getBankLiabilityValue(undefined,
|
|
259
|
+
.add(this.getBankLiabilityValue(undefined, type));
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* @returns The initial margin requirement in USDC. : QUOTE_PRECISION
|
|
263
|
+
*/
|
|
264
|
+
getInitialMarginRequirement() {
|
|
265
|
+
return this.getMarginRequirement('Initial');
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* @returns The maintenance margin requirement in USDC. : QUOTE_PRECISION
|
|
269
|
+
*/
|
|
270
|
+
getMaintenanceMarginRequirement() {
|
|
271
|
+
return this.getMarginRequirement('Maintenance');
|
|
235
272
|
}
|
|
236
273
|
/**
|
|
237
274
|
* calculates unrealized position price pnl
|
|
@@ -326,7 +363,7 @@ class ClearingHouseUser {
|
|
|
326
363
|
getTotalPositionValue() {
|
|
327
364
|
return this.getUserAccount().positions.reduce((positionValue, marketPosition) => {
|
|
328
365
|
const market = this.clearingHouse.getMarketAccount(marketPosition.marketIndex);
|
|
329
|
-
const posVal = (0, margin_1.
|
|
366
|
+
const posVal = (0, margin_1.calculateBaseAssetValueWithOracle)(market, marketPosition, this.getOracleDataForMarket(market.marketIndex));
|
|
330
367
|
return positionValue.add(posVal);
|
|
331
368
|
}, numericConstants_1.ZERO);
|
|
332
369
|
}
|
|
@@ -337,7 +374,7 @@ class ClearingHouseUser {
|
|
|
337
374
|
getPositionValue(marketIndex, oraclePriceData) {
|
|
338
375
|
const userPosition = this.getUserPosition(marketIndex) || this.getEmptyPosition(marketIndex);
|
|
339
376
|
const market = this.clearingHouse.getMarketAccount(userPosition.marketIndex);
|
|
340
|
-
return (0, margin_1.
|
|
377
|
+
return (0, margin_1.calculateBaseAssetValueWithOracle)(market, userPosition, oraclePriceData);
|
|
341
378
|
}
|
|
342
379
|
getPositionSide(currentPosition) {
|
|
343
380
|
if (currentPosition.baseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
@@ -374,7 +411,7 @@ class ClearingHouseUser {
|
|
|
374
411
|
baseAssetValue = (0, _1.calculateBaseAssetValue)(market, position, oraclePriceData);
|
|
375
412
|
}
|
|
376
413
|
else {
|
|
377
|
-
baseAssetValue = (0, margin_1.
|
|
414
|
+
baseAssetValue = (0, margin_1.calculateBaseAssetValueWithOracle)(market, position, oraclePriceData);
|
|
378
415
|
}
|
|
379
416
|
if (position.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
380
417
|
return [numericConstants_1.ZERO, numericConstants_1.ZERO];
|
|
@@ -496,13 +533,13 @@ class ClearingHouseUser {
|
|
|
496
533
|
if (proposedBaseAssetAmount.eq(numericConstants_1.ZERO))
|
|
497
534
|
return new _1.BN(-1);
|
|
498
535
|
const market = this.clearingHouse.getMarketAccount(proposedMarketPosition.marketIndex);
|
|
499
|
-
const proposedMarketPositionValue = (0, margin_1.
|
|
536
|
+
const proposedMarketPositionValue = (0, margin_1.calculateBaseAssetValueWithOracle)(market, proposedMarketPosition, this.getOracleDataForMarket(market.marketIndex));
|
|
500
537
|
// total position value after trade
|
|
501
538
|
const totalPositionValueAfterTrade = totalPositionValueExcludingTargetMarket.add(proposedMarketPositionValue);
|
|
502
539
|
const marginRequirementExcludingTargetMarket = this.getUserAccount().positions.reduce((totalMarginRequirement, position) => {
|
|
503
540
|
if (!position.marketIndex.eq(marketPosition.marketIndex)) {
|
|
504
541
|
const market = this.clearingHouse.getMarketAccount(position.marketIndex);
|
|
505
|
-
const positionValue = (0, margin_1.
|
|
542
|
+
const positionValue = (0, margin_1.calculateBaseAssetValueWithOracle)(market, position, this.getOracleDataForMarket(market.marketIndex));
|
|
506
543
|
const marketMarginRequirement = positionValue
|
|
507
544
|
.mul(new _1.BN((0, _1.calculateMarketMarginRatio)(market, position.baseAssetAmount.abs(), 'Maintenance')))
|
|
508
545
|
.div(numericConstants_1.MARGIN_PRECISION);
|
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: 'AXmmKr4MHipFp4SDr6CTUBzJtD1rZPHTzPDVPJ6L6r64',
|
|
11
11
|
USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
|
|
12
12
|
MARKETS: markets_1.DevnetMarkets,
|
|
13
13
|
BANKS: banks_1.DevnetBanks,
|
package/lib/constants/banks.d.ts
CHANGED