@drift-labs/sdk 0.2.0-master.4 → 0.2.0-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/admin.d.ts +2 -2
- package/lib/admin.js +10 -5
- package/lib/clearingHouse.d.ts +3 -1
- package/lib/clearingHouse.js +77 -30
- package/lib/config.js +1 -1
- package/lib/idl/clearing_house.json +88 -3
- package/lib/math/position.js +3 -1
- package/lib/slot/SlotSubscriber.d.ts +7 -0
- package/lib/slot/SlotSubscriber.js +3 -0
- package/lib/tx/utils.js +1 -1
- package/lib/types.d.ts +13 -0
- package/lib/types.js +6 -1
- package/package.json +1 -1
- package/src/admin.ts +12 -4
- package/src/clearingHouse.ts +105 -32
- package/src/config.ts +1 -1
- package/src/idl/clearing_house.json +88 -3
- package/src/math/position.ts +5 -1
- package/src/slot/SlotSubscriber.ts +11 -1
- package/src/tx/utils.ts +1 -1
- package/src/types.ts +11 -0
package/lib/admin.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { BN } from '@project-serum/anchor';
|
|
|
5
5
|
import { ClearingHouse } from './clearingHouse';
|
|
6
6
|
export declare class Admin extends ClearingHouse {
|
|
7
7
|
initialize(usdcMint: PublicKey, adminControlsPrices: boolean): Promise<[TransactionSignature]>;
|
|
8
|
-
initializeBank(mint: PublicKey, optimalUtilization: BN, optimalRate: BN, maxRate: BN, oracle: PublicKey, oracleSource: OracleSource, initialAssetWeight: BN, maintenanceAssetWeight: BN, initialLiabilityWeight: BN, maintenanceLiabilityWeight: BN): Promise<TransactionSignature>;
|
|
8
|
+
initializeBank(mint: PublicKey, optimalUtilization: BN, optimalRate: BN, maxRate: BN, oracle: PublicKey, oracleSource: OracleSource, initialAssetWeight: BN, maintenanceAssetWeight: BN, initialLiabilityWeight: BN, maintenanceLiabilityWeight: BN, imfFactor?: BN): Promise<TransactionSignature>;
|
|
9
9
|
initializeMarket(priceOracle: PublicKey, baseAssetReserve: BN, quoteAssetReserve: BN, periodicity: BN, pegMultiplier?: BN, oracleSource?: OracleSource, marginRatioInitial?: number, marginRatioPartial?: number, marginRatioMaintenance?: number): Promise<TransactionSignature>;
|
|
10
10
|
moveAmmPrice(baseAssetReserve: BN, quoteAssetReserve: BN, marketIndex: BN): Promise<TransactionSignature>;
|
|
11
11
|
updateK(sqrtK: BN, marketIndex: BN): Promise<TransactionSignature>;
|
|
@@ -14,7 +14,7 @@ export declare class Admin extends ClearingHouse {
|
|
|
14
14
|
updateAmmOracleTwap(marketIndex: BN): Promise<TransactionSignature>;
|
|
15
15
|
resetAmmOracleTwap(marketIndex: BN): Promise<TransactionSignature>;
|
|
16
16
|
withdrawFromInsuranceVault(amount: BN, recipient: PublicKey): Promise<TransactionSignature>;
|
|
17
|
-
|
|
17
|
+
withdrawFromMarketToInsuranceVault(marketIndex: BN, amount: BN, recipient: PublicKey): Promise<TransactionSignature>;
|
|
18
18
|
withdrawFromInsuranceVaultToMarket(marketIndex: BN, amount: BN): Promise<TransactionSignature>;
|
|
19
19
|
updateAdmin(admin: PublicKey): Promise<TransactionSignature>;
|
|
20
20
|
updateCurveUpdateIntensity(marketIndex: BN, curveUpdateIntensity: number): Promise<TransactionSignature>;
|
package/lib/admin.js
CHANGED
|
@@ -58,12 +58,12 @@ class Admin extends clearingHouse_1.ClearingHouse {
|
|
|
58
58
|
const { txSig: initializeTxSig } = await this.txSender.send(initializeTx, [], this.opts);
|
|
59
59
|
return [initializeTxSig];
|
|
60
60
|
}
|
|
61
|
-
async initializeBank(mint, optimalUtilization, optimalRate, maxRate, oracle, oracleSource, initialAssetWeight, maintenanceAssetWeight, initialLiabilityWeight, maintenanceLiabilityWeight) {
|
|
61
|
+
async initializeBank(mint, optimalUtilization, optimalRate, maxRate, oracle, oracleSource, initialAssetWeight, maintenanceAssetWeight, initialLiabilityWeight, maintenanceLiabilityWeight, imfFactor = new anchor_1.BN(0)) {
|
|
62
62
|
const bankIndex = this.getStateAccount().numberOfBanks;
|
|
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 initializeTx = await this.program.transaction.initializeBank(optimalUtilization, optimalRate, maxRate, oracleSource, initialAssetWeight, maintenanceAssetWeight, initialLiabilityWeight, maintenanceLiabilityWeight, {
|
|
66
|
+
const initializeTx = await this.program.transaction.initializeBank(optimalUtilization, optimalRate, maxRate, oracleSource, initialAssetWeight, maintenanceAssetWeight, initialLiabilityWeight, maintenanceLiabilityWeight, imfFactor, {
|
|
67
67
|
accounts: {
|
|
68
68
|
admin: this.wallet.publicKey,
|
|
69
69
|
state: await this.getStatePublicKey(),
|
|
@@ -177,10 +177,12 @@ class Admin extends clearingHouse_1.ClearingHouse {
|
|
|
177
177
|
}
|
|
178
178
|
async withdrawFromInsuranceVault(amount, recipient) {
|
|
179
179
|
const state = await this.getStateAccount();
|
|
180
|
+
const bank = this.getQuoteAssetBankAccount();
|
|
180
181
|
return await this.program.rpc.withdrawFromInsuranceVault(amount, {
|
|
181
182
|
accounts: {
|
|
182
183
|
admin: this.wallet.publicKey,
|
|
183
184
|
state: await this.getStatePublicKey(),
|
|
185
|
+
bank: bank.pubkey,
|
|
184
186
|
insuranceVault: state.insuranceVault,
|
|
185
187
|
insuranceVaultAuthority: state.insuranceVaultAuthority,
|
|
186
188
|
recipient: recipient,
|
|
@@ -188,10 +190,10 @@ class Admin extends clearingHouse_1.ClearingHouse {
|
|
|
188
190
|
},
|
|
189
191
|
});
|
|
190
192
|
}
|
|
191
|
-
async
|
|
193
|
+
async withdrawFromMarketToInsuranceVault(marketIndex, amount, recipient) {
|
|
192
194
|
const marketPublicKey = await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex);
|
|
193
195
|
const bank = this.getQuoteAssetBankAccount();
|
|
194
|
-
return await this.program.rpc.
|
|
196
|
+
return await this.program.rpc.withdrawFromMarketToInsuranceVault(amount, {
|
|
195
197
|
accounts: {
|
|
196
198
|
admin: this.wallet.publicKey,
|
|
197
199
|
state: await this.getStatePublicKey(),
|
|
@@ -206,6 +208,7 @@ class Admin extends clearingHouse_1.ClearingHouse {
|
|
|
206
208
|
}
|
|
207
209
|
async withdrawFromInsuranceVaultToMarket(marketIndex, amount) {
|
|
208
210
|
const state = await this.getStateAccount();
|
|
211
|
+
const bank = this.getQuoteAssetBankAccount();
|
|
209
212
|
return await this.program.rpc.withdrawFromInsuranceVaultToMarket(amount, {
|
|
210
213
|
accounts: {
|
|
211
214
|
admin: this.wallet.publicKey,
|
|
@@ -213,7 +216,9 @@ class Admin extends clearingHouse_1.ClearingHouse {
|
|
|
213
216
|
market: await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex),
|
|
214
217
|
insuranceVault: state.insuranceVault,
|
|
215
218
|
insuranceVaultAuthority: state.insuranceVaultAuthority,
|
|
216
|
-
|
|
219
|
+
bank: bank.pubkey,
|
|
220
|
+
bankVault: bank.vault,
|
|
221
|
+
bankVaultAuthority: bank.vaultAuthority,
|
|
217
222
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
218
223
|
},
|
|
219
224
|
});
|
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, OptionalOrderParams } from './types';
|
|
4
|
+
import { StateAccount, IWallet, PositionDirection, UserAccount, MarketAccount, OrderParams, Order, BankAccount, UserBankBalance, MakerInfo, TakerInfo, OptionalOrderParams } from './types';
|
|
5
5
|
import { Connection, PublicKey, TransactionSignature, ConfirmOptions, TransactionInstruction, AccountMeta } from '@solana/web3.js';
|
|
6
6
|
import { MockUSDCFaucet } from './mockUSDCFaucet';
|
|
7
7
|
import { EventEmitter } from 'events';
|
|
@@ -110,6 +110,8 @@ export declare class ClearingHouse {
|
|
|
110
110
|
getTriggerOrderIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, order: Order): Promise<TransactionInstruction>;
|
|
111
111
|
placeAndTake(orderParams: OptionalOrderParams, makerInfo?: MakerInfo): Promise<TransactionSignature>;
|
|
112
112
|
getPlaceAndTakeIx(orderParams: OptionalOrderParams, makerInfo?: MakerInfo): Promise<TransactionInstruction>;
|
|
113
|
+
placeAndMake(orderParams: OptionalOrderParams, takerInfo: TakerInfo): Promise<TransactionSignature>;
|
|
114
|
+
getPlaceAndMakeIx(orderParams: OptionalOrderParams, takerInfo: TakerInfo): Promise<TransactionInstruction>;
|
|
113
115
|
/**
|
|
114
116
|
* Close an entire position. If you want to reduce a position, use the {@link openPosition} method in the opposite direction of the current position.
|
|
115
117
|
* @param marketIndex
|
package/lib/clearingHouse.js
CHANGED
|
@@ -371,13 +371,19 @@ class ClearingHouse {
|
|
|
371
371
|
});
|
|
372
372
|
}
|
|
373
373
|
else {
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
374
|
+
const bankAccount = this.getBankAccount(bankIndex);
|
|
375
|
+
if (!bankAccount.oracle.equals(web3_js_1.PublicKey.default)) {
|
|
376
|
+
remainingAccounts.push({
|
|
377
|
+
pubkey: bankAccount.oracle,
|
|
377
378
|
isSigner: false,
|
|
378
|
-
isWritable:
|
|
379
|
-
}
|
|
380
|
-
|
|
379
|
+
isWritable: false,
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
remainingAccounts.push({
|
|
383
|
+
pubkey: bankAccount.pubkey,
|
|
384
|
+
isSigner: false,
|
|
385
|
+
isWritable: true,
|
|
386
|
+
});
|
|
381
387
|
}
|
|
382
388
|
const bank = this.getBankAccount(bankIndex);
|
|
383
389
|
return await this.program.instruction.deposit(bankIndex, amount, reduceOnly, {
|
|
@@ -590,44 +596,57 @@ class ClearingHouse {
|
|
|
590
596
|
const fillerPublicKey = await this.getUserAccountPublicKey();
|
|
591
597
|
const marketIndex = order.marketIndex;
|
|
592
598
|
const marketAccount = this.getMarketAccount(marketIndex);
|
|
593
|
-
const
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
599
|
+
const oracleAccountMap = new Map();
|
|
600
|
+
const bankAccountMap = new Map();
|
|
601
|
+
const marketAccountMap = new Map();
|
|
602
|
+
marketAccountMap.set(marketIndex.toNumber(), {
|
|
603
|
+
pubkey: marketAccount.pubkey,
|
|
604
|
+
isWritable: true,
|
|
605
|
+
isSigner: false,
|
|
606
|
+
});
|
|
607
|
+
oracleAccountMap.set(marketAccount.amm.oracle.toString(), {
|
|
608
|
+
pubkey: marketAccount.amm.oracle,
|
|
609
|
+
isWritable: false,
|
|
610
|
+
isSigner: false,
|
|
611
|
+
});
|
|
612
|
+
for (const bankBalance of userAccount.bankBalances) {
|
|
613
|
+
if (!bankBalance.balance.eq(numericConstants_1.ZERO)) {
|
|
614
|
+
const bankAccount = this.getBankAccount(bankBalance.bankIndex);
|
|
615
|
+
bankAccountMap.set(bankBalance.bankIndex.toNumber(), {
|
|
616
|
+
pubkey: bankAccount.pubkey,
|
|
617
|
+
isSigner: false,
|
|
618
|
+
isWritable: true,
|
|
619
|
+
});
|
|
620
|
+
if (!bankAccount.oracle.equals(web3_js_1.PublicKey.default)) {
|
|
621
|
+
oracleAccountMap.set(bankAccount.oracle.toString(), {
|
|
622
|
+
pubkey: bankAccount.oracle,
|
|
623
|
+
isSigner: false,
|
|
624
|
+
isWritable: false,
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
}
|
|
614
629
|
for (const position of userAccount.positions) {
|
|
615
630
|
if (!(0, position_1.positionIsAvailable)(position) &&
|
|
616
631
|
!position.marketIndex.eq(order.marketIndex)) {
|
|
617
632
|
const market = this.getMarketAccount(position.marketIndex);
|
|
618
|
-
|
|
633
|
+
marketAccountMap.set(position.marketIndex.toNumber(), {
|
|
619
634
|
pubkey: market.pubkey,
|
|
620
635
|
isWritable: true,
|
|
621
636
|
isSigner: false,
|
|
622
637
|
});
|
|
623
|
-
|
|
638
|
+
oracleAccountMap.set(market.amm.oracle.toString(), {
|
|
624
639
|
pubkey: market.amm.oracle,
|
|
625
640
|
isWritable: false,
|
|
626
641
|
isSigner: false,
|
|
627
642
|
});
|
|
628
643
|
}
|
|
629
644
|
}
|
|
630
|
-
const remainingAccounts =
|
|
645
|
+
const remainingAccounts = [
|
|
646
|
+
...oracleAccountMap.values(),
|
|
647
|
+
...bankAccountMap.values(),
|
|
648
|
+
...marketAccountMap.values(),
|
|
649
|
+
];
|
|
631
650
|
if (makerInfo) {
|
|
632
651
|
remainingAccounts.push({
|
|
633
652
|
pubkey: makerInfo.maker,
|
|
@@ -734,6 +753,34 @@ class ClearingHouse {
|
|
|
734
753
|
remainingAccounts,
|
|
735
754
|
});
|
|
736
755
|
}
|
|
756
|
+
async placeAndMake(orderParams, takerInfo) {
|
|
757
|
+
const { txSig, slot } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getPlaceAndMakeIx(orderParams, takerInfo)), [], this.opts);
|
|
758
|
+
this.marketLastSlotCache.set(orderParams.marketIndex.toNumber(), slot);
|
|
759
|
+
return txSig;
|
|
760
|
+
}
|
|
761
|
+
async getPlaceAndMakeIx(orderParams, takerInfo) {
|
|
762
|
+
orderParams = this.getOrderParams(orderParams);
|
|
763
|
+
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
764
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
765
|
+
writableMarketIndex: orderParams.marketIndex,
|
|
766
|
+
writableBankIndex: numericConstants_1.QUOTE_ASSET_BANK_INDEX,
|
|
767
|
+
});
|
|
768
|
+
const takerOrderId = takerInfo.order.orderId;
|
|
769
|
+
remainingAccounts.push({
|
|
770
|
+
pubkey: takerInfo.taker,
|
|
771
|
+
isSigner: false,
|
|
772
|
+
isWritable: true,
|
|
773
|
+
});
|
|
774
|
+
return await this.program.instruction.placeAndMake(orderParams, takerOrderId, {
|
|
775
|
+
accounts: {
|
|
776
|
+
state: await this.getStatePublicKey(),
|
|
777
|
+
user: userAccountPublicKey,
|
|
778
|
+
taker: takerInfo.taker,
|
|
779
|
+
authority: this.wallet.publicKey,
|
|
780
|
+
},
|
|
781
|
+
remainingAccounts,
|
|
782
|
+
});
|
|
783
|
+
}
|
|
737
784
|
/**
|
|
738
785
|
* Close an entire position. If you want to reduce a position, use the {@link openPosition} method in the opposite direction of the current position.
|
|
739
786
|
* @param marketIndex
|
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: 'BMow898PH56jD8z4EaqxicoGXkR1HhN17qrER6Uc4AYq',
|
|
11
11
|
USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
|
|
12
12
|
MARKETS: markets_1.DevnetMarkets,
|
|
13
13
|
BANKS: banks_1.DevnetBanks,
|
|
@@ -141,6 +141,10 @@
|
|
|
141
141
|
{
|
|
142
142
|
"name": "maintenanceLiabilityWeight",
|
|
143
143
|
"type": "u128"
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
"name": "imfFactor",
|
|
147
|
+
"type": "u128"
|
|
144
148
|
}
|
|
145
149
|
]
|
|
146
150
|
},
|
|
@@ -534,7 +538,7 @@
|
|
|
534
538
|
},
|
|
535
539
|
{
|
|
536
540
|
"name": "taker",
|
|
537
|
-
"isMut":
|
|
541
|
+
"isMut": true,
|
|
538
542
|
"isSigner": false
|
|
539
543
|
},
|
|
540
544
|
{
|
|
@@ -716,7 +720,7 @@
|
|
|
716
720
|
]
|
|
717
721
|
},
|
|
718
722
|
{
|
|
719
|
-
"name": "
|
|
723
|
+
"name": "withdrawFromMarketToInsuranceVault",
|
|
720
724
|
"accounts": [
|
|
721
725
|
{
|
|
722
726
|
"name": "state",
|
|
@@ -730,7 +734,7 @@
|
|
|
730
734
|
},
|
|
731
735
|
{
|
|
732
736
|
"name": "bank",
|
|
733
|
-
"isMut":
|
|
737
|
+
"isMut": true,
|
|
734
738
|
"isSigner": false
|
|
735
739
|
},
|
|
736
740
|
{
|
|
@@ -835,6 +839,11 @@
|
|
|
835
839
|
"isMut": false,
|
|
836
840
|
"isSigner": false
|
|
837
841
|
},
|
|
842
|
+
{
|
|
843
|
+
"name": "bank",
|
|
844
|
+
"isMut": true,
|
|
845
|
+
"isSigner": false
|
|
846
|
+
},
|
|
838
847
|
{
|
|
839
848
|
"name": "bankVault",
|
|
840
849
|
"isMut": true,
|
|
@@ -1093,6 +1102,62 @@
|
|
|
1093
1102
|
}
|
|
1094
1103
|
]
|
|
1095
1104
|
},
|
|
1105
|
+
{
|
|
1106
|
+
"name": "updateMarketImfFactor",
|
|
1107
|
+
"accounts": [
|
|
1108
|
+
{
|
|
1109
|
+
"name": "admin",
|
|
1110
|
+
"isMut": false,
|
|
1111
|
+
"isSigner": true
|
|
1112
|
+
},
|
|
1113
|
+
{
|
|
1114
|
+
"name": "state",
|
|
1115
|
+
"isMut": false,
|
|
1116
|
+
"isSigner": false
|
|
1117
|
+
},
|
|
1118
|
+
{
|
|
1119
|
+
"name": "market",
|
|
1120
|
+
"isMut": true,
|
|
1121
|
+
"isSigner": false
|
|
1122
|
+
}
|
|
1123
|
+
],
|
|
1124
|
+
"args": [
|
|
1125
|
+
{
|
|
1126
|
+
"name": "imfFactor",
|
|
1127
|
+
"type": "u128"
|
|
1128
|
+
}
|
|
1129
|
+
]
|
|
1130
|
+
},
|
|
1131
|
+
{
|
|
1132
|
+
"name": "updateMarketUnsettledAssetWeight",
|
|
1133
|
+
"accounts": [
|
|
1134
|
+
{
|
|
1135
|
+
"name": "admin",
|
|
1136
|
+
"isMut": false,
|
|
1137
|
+
"isSigner": true
|
|
1138
|
+
},
|
|
1139
|
+
{
|
|
1140
|
+
"name": "state",
|
|
1141
|
+
"isMut": false,
|
|
1142
|
+
"isSigner": false
|
|
1143
|
+
},
|
|
1144
|
+
{
|
|
1145
|
+
"name": "market",
|
|
1146
|
+
"isMut": true,
|
|
1147
|
+
"isSigner": false
|
|
1148
|
+
}
|
|
1149
|
+
],
|
|
1150
|
+
"args": [
|
|
1151
|
+
{
|
|
1152
|
+
"name": "unsettledInitialAssetWeight",
|
|
1153
|
+
"type": "u8"
|
|
1154
|
+
},
|
|
1155
|
+
{
|
|
1156
|
+
"name": "unsettledMaintenanceAssetWeight",
|
|
1157
|
+
"type": "u8"
|
|
1158
|
+
}
|
|
1159
|
+
]
|
|
1160
|
+
},
|
|
1096
1161
|
{
|
|
1097
1162
|
"name": "updateCurveUpdateIntensity",
|
|
1098
1163
|
"accounts": [
|
|
@@ -1731,6 +1796,10 @@
|
|
|
1731
1796
|
{
|
|
1732
1797
|
"name": "maintenanceLiabilityWeight",
|
|
1733
1798
|
"type": "u128"
|
|
1799
|
+
},
|
|
1800
|
+
{
|
|
1801
|
+
"name": "imfFactor",
|
|
1802
|
+
"type": "u128"
|
|
1734
1803
|
}
|
|
1735
1804
|
]
|
|
1736
1805
|
}
|
|
@@ -1808,6 +1877,22 @@
|
|
|
1808
1877
|
"name": "unsettledLoss",
|
|
1809
1878
|
"type": "u128"
|
|
1810
1879
|
},
|
|
1880
|
+
{
|
|
1881
|
+
"name": "imfFactor",
|
|
1882
|
+
"type": "u128"
|
|
1883
|
+
},
|
|
1884
|
+
{
|
|
1885
|
+
"name": "unsettledInitialAssetWeight",
|
|
1886
|
+
"type": "u8"
|
|
1887
|
+
},
|
|
1888
|
+
{
|
|
1889
|
+
"name": "unsettledMaintenanceAssetWeight",
|
|
1890
|
+
"type": "u8"
|
|
1891
|
+
},
|
|
1892
|
+
{
|
|
1893
|
+
"name": "unsettledImfFactor",
|
|
1894
|
+
"type": "u128"
|
|
1895
|
+
},
|
|
1811
1896
|
{
|
|
1812
1897
|
"name": "padding0",
|
|
1813
1898
|
"type": "u32"
|
package/lib/math/position.js
CHANGED
|
@@ -101,7 +101,9 @@ function calculatePositionFundingPNL(market, marketPosition) {
|
|
|
101
101
|
}
|
|
102
102
|
exports.calculatePositionFundingPNL = calculatePositionFundingPNL;
|
|
103
103
|
function positionIsAvailable(position) {
|
|
104
|
-
return position.baseAssetAmount.eq(numericConstants_1.ZERO) &&
|
|
104
|
+
return (position.baseAssetAmount.eq(numericConstants_1.ZERO) &&
|
|
105
|
+
position.openOrders.eq(numericConstants_1.ZERO) &&
|
|
106
|
+
position.unsettledPnl.eq(numericConstants_1.ZERO));
|
|
105
107
|
}
|
|
106
108
|
exports.positionIsAvailable = positionIsAvailable;
|
|
107
109
|
/**
|
|
@@ -1,9 +1,16 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
import { Connection } from '@solana/web3.js';
|
|
3
|
+
import { EventEmitter } from 'events';
|
|
4
|
+
import StrictEventEmitter from 'strict-event-emitter-types/types/src';
|
|
2
5
|
declare type SlotSubscriberConfig = {};
|
|
6
|
+
export interface SlotSubscriberEvents {
|
|
7
|
+
newSlot: (newSlot: number) => void;
|
|
8
|
+
}
|
|
3
9
|
export declare class SlotSubscriber {
|
|
4
10
|
private connection;
|
|
5
11
|
currentSlot: number;
|
|
6
12
|
subscriptionId: number;
|
|
13
|
+
eventEmitter: StrictEventEmitter<EventEmitter, SlotSubscriberEvents>;
|
|
7
14
|
constructor(connection: Connection, _config?: SlotSubscriberConfig);
|
|
8
15
|
subscribe(): Promise<void>;
|
|
9
16
|
getSlot(): number;
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SlotSubscriber = void 0;
|
|
4
|
+
const events_1 = require("events");
|
|
4
5
|
class SlotSubscriber {
|
|
5
6
|
constructor(connection, _config) {
|
|
6
7
|
this.connection = connection;
|
|
8
|
+
this.eventEmitter = new events_1.EventEmitter();
|
|
7
9
|
}
|
|
8
10
|
async subscribe() {
|
|
9
11
|
this.currentSlot = await this.connection.getSlot('confirmed');
|
|
10
12
|
this.subscriptionId = this.connection.onSlotChange((slotInfo) => {
|
|
11
13
|
this.currentSlot = slotInfo.slot;
|
|
14
|
+
this.eventEmitter.emit('newSlot', slotInfo.slot);
|
|
12
15
|
});
|
|
13
16
|
}
|
|
14
17
|
getSlot() {
|
package/lib/tx/utils.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.wrapInTx = void 0;
|
|
4
4
|
const web3_js_1 = require("@solana/web3.js");
|
|
5
5
|
const COMPUTE_UNITS_DEFAULT = 200000;
|
|
6
|
-
function wrapInTx(instruction, computeUnits =
|
|
6
|
+
function wrapInTx(instruction, computeUnits = 600000 // TODO, requires less code change
|
|
7
7
|
) {
|
|
8
8
|
const tx = new web3_js_1.Transaction();
|
|
9
9
|
if (computeUnits != COMPUTE_UNITS_DEFAULT) {
|
package/lib/types.d.ts
CHANGED
|
@@ -25,6 +25,14 @@ export declare class PositionDirection {
|
|
|
25
25
|
short: {};
|
|
26
26
|
};
|
|
27
27
|
}
|
|
28
|
+
export declare class DepositDirection {
|
|
29
|
+
static readonly DEPOSIT: {
|
|
30
|
+
deposit: {};
|
|
31
|
+
};
|
|
32
|
+
static readonly WITHDRAW: {
|
|
33
|
+
withdraw: {};
|
|
34
|
+
};
|
|
35
|
+
}
|
|
28
36
|
export declare class OracleSource {
|
|
29
37
|
static readonly PYTH: {
|
|
30
38
|
pyth: {};
|
|
@@ -132,6 +140,7 @@ export declare type DepositRecord = {
|
|
|
132
140
|
};
|
|
133
141
|
bankIndex: BN;
|
|
134
142
|
amount: BN;
|
|
143
|
+
oraclePrice: BN;
|
|
135
144
|
from?: PublicKey;
|
|
136
145
|
to?: PublicKey;
|
|
137
146
|
};
|
|
@@ -453,6 +462,10 @@ export declare type MakerInfo = {
|
|
|
453
462
|
maker: PublicKey;
|
|
454
463
|
order: Order;
|
|
455
464
|
};
|
|
465
|
+
export declare type TakerInfo = {
|
|
466
|
+
taker: PublicKey;
|
|
467
|
+
order: Order;
|
|
468
|
+
};
|
|
456
469
|
export interface IWallet {
|
|
457
470
|
signTransaction(tx: Transaction): Promise<Transaction>;
|
|
458
471
|
signAllTransactions(txs: Transaction[]): Promise<Transaction[]>;
|
package/lib/types.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DefaultOrderParams = exports.TradeSide = exports.isOneOfVariant = exports.isVariant = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderDiscountTier = exports.OrderStatus = exports.OrderType = exports.OracleSource = exports.PositionDirection = exports.BankBalanceType = exports.SwapDirection = void 0;
|
|
3
|
+
exports.DefaultOrderParams = exports.TradeSide = exports.isOneOfVariant = exports.isVariant = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderDiscountTier = exports.OrderStatus = exports.OrderType = exports.OracleSource = exports.DepositDirection = exports.PositionDirection = exports.BankBalanceType = exports.SwapDirection = void 0;
|
|
4
4
|
const _1 = require(".");
|
|
5
5
|
// # Utility Types / Enums / Constants
|
|
6
6
|
class SwapDirection {
|
|
@@ -18,6 +18,11 @@ class PositionDirection {
|
|
|
18
18
|
exports.PositionDirection = PositionDirection;
|
|
19
19
|
PositionDirection.LONG = { long: {} };
|
|
20
20
|
PositionDirection.SHORT = { short: {} };
|
|
21
|
+
class DepositDirection {
|
|
22
|
+
}
|
|
23
|
+
exports.DepositDirection = DepositDirection;
|
|
24
|
+
DepositDirection.DEPOSIT = { deposit: {} };
|
|
25
|
+
DepositDirection.WITHDRAW = { withdraw: {} };
|
|
21
26
|
class OracleSource {
|
|
22
27
|
}
|
|
23
28
|
exports.OracleSource = OracleSource;
|
package/package.json
CHANGED
package/src/admin.ts
CHANGED
|
@@ -85,7 +85,8 @@ export class Admin extends ClearingHouse {
|
|
|
85
85
|
initialAssetWeight: BN,
|
|
86
86
|
maintenanceAssetWeight: BN,
|
|
87
87
|
initialLiabilityWeight: BN,
|
|
88
|
-
maintenanceLiabilityWeight: BN
|
|
88
|
+
maintenanceLiabilityWeight: BN,
|
|
89
|
+
imfFactor = new BN(0)
|
|
89
90
|
): Promise<TransactionSignature> {
|
|
90
91
|
const bankIndex = this.getStateAccount().numberOfBanks;
|
|
91
92
|
const bank = await getBankPublicKey(this.program.programId, bankIndex);
|
|
@@ -109,6 +110,7 @@ export class Admin extends ClearingHouse {
|
|
|
109
110
|
maintenanceAssetWeight,
|
|
110
111
|
initialLiabilityWeight,
|
|
111
112
|
maintenanceLiabilityWeight,
|
|
113
|
+
imfFactor,
|
|
112
114
|
{
|
|
113
115
|
accounts: {
|
|
114
116
|
admin: this.wallet.publicKey,
|
|
@@ -329,10 +331,12 @@ export class Admin extends ClearingHouse {
|
|
|
329
331
|
recipient: PublicKey
|
|
330
332
|
): Promise<TransactionSignature> {
|
|
331
333
|
const state = await this.getStateAccount();
|
|
334
|
+
const bank = this.getQuoteAssetBankAccount();
|
|
332
335
|
return await this.program.rpc.withdrawFromInsuranceVault(amount, {
|
|
333
336
|
accounts: {
|
|
334
337
|
admin: this.wallet.publicKey,
|
|
335
338
|
state: await this.getStatePublicKey(),
|
|
339
|
+
bank: bank.pubkey,
|
|
336
340
|
insuranceVault: state.insuranceVault,
|
|
337
341
|
insuranceVaultAuthority: state.insuranceVaultAuthority,
|
|
338
342
|
recipient: recipient,
|
|
@@ -341,7 +345,7 @@ export class Admin extends ClearingHouse {
|
|
|
341
345
|
});
|
|
342
346
|
}
|
|
343
347
|
|
|
344
|
-
public async
|
|
348
|
+
public async withdrawFromMarketToInsuranceVault(
|
|
345
349
|
marketIndex: BN,
|
|
346
350
|
amount: BN,
|
|
347
351
|
recipient: PublicKey
|
|
@@ -351,7 +355,7 @@ export class Admin extends ClearingHouse {
|
|
|
351
355
|
marketIndex
|
|
352
356
|
);
|
|
353
357
|
const bank = this.getQuoteAssetBankAccount();
|
|
354
|
-
return await this.program.rpc.
|
|
358
|
+
return await this.program.rpc.withdrawFromMarketToInsuranceVault(amount, {
|
|
355
359
|
accounts: {
|
|
356
360
|
admin: this.wallet.publicKey,
|
|
357
361
|
state: await this.getStatePublicKey(),
|
|
@@ -370,6 +374,8 @@ export class Admin extends ClearingHouse {
|
|
|
370
374
|
amount: BN
|
|
371
375
|
): Promise<TransactionSignature> {
|
|
372
376
|
const state = await this.getStateAccount();
|
|
377
|
+
const bank = this.getQuoteAssetBankAccount();
|
|
378
|
+
|
|
373
379
|
return await this.program.rpc.withdrawFromInsuranceVaultToMarket(amount, {
|
|
374
380
|
accounts: {
|
|
375
381
|
admin: this.wallet.publicKey,
|
|
@@ -377,7 +383,9 @@ export class Admin extends ClearingHouse {
|
|
|
377
383
|
market: await getMarketPublicKey(this.program.programId, marketIndex),
|
|
378
384
|
insuranceVault: state.insuranceVault,
|
|
379
385
|
insuranceVaultAuthority: state.insuranceVaultAuthority,
|
|
380
|
-
|
|
386
|
+
bank: bank.pubkey,
|
|
387
|
+
bankVault: bank.vault,
|
|
388
|
+
bankVaultAuthority: bank.vaultAuthority,
|
|
381
389
|
tokenProgram: TOKEN_PROGRAM_ID,
|
|
382
390
|
},
|
|
383
391
|
});
|
package/src/clearingHouse.ts
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
BankAccount,
|
|
12
12
|
UserBankBalance,
|
|
13
13
|
MakerInfo,
|
|
14
|
+
TakerInfo,
|
|
14
15
|
OptionalOrderParams,
|
|
15
16
|
DefaultOrderParams,
|
|
16
17
|
OrderType,
|
|
@@ -563,13 +564,19 @@ export class ClearingHouse {
|
|
|
563
564
|
writableBankIndex: bankIndex,
|
|
564
565
|
});
|
|
565
566
|
} else {
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
567
|
+
const bankAccount = this.getBankAccount(bankIndex);
|
|
568
|
+
if (!bankAccount.oracle.equals(PublicKey.default)) {
|
|
569
|
+
remainingAccounts.push({
|
|
570
|
+
pubkey: bankAccount.oracle,
|
|
569
571
|
isSigner: false,
|
|
570
|
-
isWritable:
|
|
571
|
-
}
|
|
572
|
-
|
|
572
|
+
isWritable: false,
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
remainingAccounts.push({
|
|
576
|
+
pubkey: bankAccount.pubkey,
|
|
577
|
+
isSigner: false,
|
|
578
|
+
isWritable: true,
|
|
579
|
+
});
|
|
573
580
|
}
|
|
574
581
|
|
|
575
582
|
const bank = this.getBankAccount(bankIndex);
|
|
@@ -971,48 +978,64 @@ export class ClearingHouse {
|
|
|
971
978
|
const marketIndex = order.marketIndex;
|
|
972
979
|
const marketAccount = this.getMarketAccount(marketIndex);
|
|
973
980
|
|
|
974
|
-
const
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
981
|
+
const oracleAccountMap = new Map<string, AccountMeta>();
|
|
982
|
+
const bankAccountMap = new Map<number, AccountMeta>();
|
|
983
|
+
const marketAccountMap = new Map<number, AccountMeta>();
|
|
984
|
+
|
|
985
|
+
marketAccountMap.set(marketIndex.toNumber(), {
|
|
986
|
+
pubkey: marketAccount.pubkey,
|
|
987
|
+
isWritable: true,
|
|
988
|
+
isSigner: false,
|
|
989
|
+
});
|
|
990
|
+
oracleAccountMap.set(marketAccount.amm.oracle.toString(), {
|
|
991
|
+
pubkey: marketAccount.amm.oracle,
|
|
992
|
+
isWritable: false,
|
|
993
|
+
isSigner: false,
|
|
994
|
+
});
|
|
995
|
+
|
|
996
|
+
for (const bankBalance of userAccount.bankBalances) {
|
|
997
|
+
if (!bankBalance.balance.eq(ZERO)) {
|
|
998
|
+
const bankAccount = this.getBankAccount(bankBalance.bankIndex);
|
|
999
|
+
bankAccountMap.set(bankBalance.bankIndex.toNumber(), {
|
|
1000
|
+
pubkey: bankAccount.pubkey,
|
|
1001
|
+
isSigner: false,
|
|
1002
|
+
isWritable: true,
|
|
1003
|
+
});
|
|
1004
|
+
|
|
1005
|
+
if (!bankAccount.oracle.equals(PublicKey.default)) {
|
|
1006
|
+
oracleAccountMap.set(bankAccount.oracle.toString(), {
|
|
1007
|
+
pubkey: bankAccount.oracle,
|
|
1008
|
+
isSigner: false,
|
|
1009
|
+
isWritable: false,
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
|
|
995
1015
|
for (const position of userAccount.positions) {
|
|
996
1016
|
if (
|
|
997
1017
|
!positionIsAvailable(position) &&
|
|
998
1018
|
!position.marketIndex.eq(order.marketIndex)
|
|
999
1019
|
) {
|
|
1000
1020
|
const market = this.getMarketAccount(position.marketIndex);
|
|
1001
|
-
|
|
1021
|
+
marketAccountMap.set(position.marketIndex.toNumber(), {
|
|
1002
1022
|
pubkey: market.pubkey,
|
|
1003
1023
|
isWritable: true,
|
|
1004
1024
|
isSigner: false,
|
|
1005
1025
|
});
|
|
1006
|
-
|
|
1026
|
+
oracleAccountMap.set(market.amm.oracle.toString(), {
|
|
1007
1027
|
pubkey: market.amm.oracle,
|
|
1008
1028
|
isWritable: false,
|
|
1009
1029
|
isSigner: false,
|
|
1010
1030
|
});
|
|
1011
1031
|
}
|
|
1012
1032
|
}
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1033
|
+
|
|
1034
|
+
const remainingAccounts = [
|
|
1035
|
+
...oracleAccountMap.values(),
|
|
1036
|
+
...bankAccountMap.values(),
|
|
1037
|
+
...marketAccountMap.values(),
|
|
1038
|
+
];
|
|
1016
1039
|
|
|
1017
1040
|
if (makerInfo) {
|
|
1018
1041
|
remainingAccounts.push({
|
|
@@ -1024,6 +1047,7 @@ export class ClearingHouse {
|
|
|
1024
1047
|
|
|
1025
1048
|
const orderId = order.orderId;
|
|
1026
1049
|
const makerOrderId = makerInfo ? makerInfo.order.orderId : null;
|
|
1050
|
+
|
|
1027
1051
|
return await this.program.instruction.fillOrder(orderId, makerOrderId, {
|
|
1028
1052
|
accounts: {
|
|
1029
1053
|
state: await this.getStatePublicKey(),
|
|
@@ -1162,6 +1186,55 @@ export class ClearingHouse {
|
|
|
1162
1186
|
);
|
|
1163
1187
|
}
|
|
1164
1188
|
|
|
1189
|
+
public async placeAndMake(
|
|
1190
|
+
orderParams: OptionalOrderParams,
|
|
1191
|
+
takerInfo: TakerInfo
|
|
1192
|
+
): Promise<TransactionSignature> {
|
|
1193
|
+
const { txSig, slot } = await this.txSender.send(
|
|
1194
|
+
wrapInTx(await this.getPlaceAndMakeIx(orderParams, takerInfo)),
|
|
1195
|
+
[],
|
|
1196
|
+
this.opts
|
|
1197
|
+
);
|
|
1198
|
+
|
|
1199
|
+
this.marketLastSlotCache.set(orderParams.marketIndex.toNumber(), slot);
|
|
1200
|
+
|
|
1201
|
+
return txSig;
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
public async getPlaceAndMakeIx(
|
|
1205
|
+
orderParams: OptionalOrderParams,
|
|
1206
|
+
takerInfo: TakerInfo
|
|
1207
|
+
): Promise<TransactionInstruction> {
|
|
1208
|
+
orderParams = this.getOrderParams(orderParams);
|
|
1209
|
+
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
1210
|
+
|
|
1211
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
1212
|
+
writableMarketIndex: orderParams.marketIndex,
|
|
1213
|
+
writableBankIndex: QUOTE_ASSET_BANK_INDEX,
|
|
1214
|
+
});
|
|
1215
|
+
|
|
1216
|
+
const takerOrderId = takerInfo!.order!.orderId;
|
|
1217
|
+
remainingAccounts.push({
|
|
1218
|
+
pubkey: takerInfo.taker,
|
|
1219
|
+
isSigner: false,
|
|
1220
|
+
isWritable: true,
|
|
1221
|
+
});
|
|
1222
|
+
|
|
1223
|
+
return await this.program.instruction.placeAndMake(
|
|
1224
|
+
orderParams,
|
|
1225
|
+
takerOrderId,
|
|
1226
|
+
{
|
|
1227
|
+
accounts: {
|
|
1228
|
+
state: await this.getStatePublicKey(),
|
|
1229
|
+
user: userAccountPublicKey,
|
|
1230
|
+
taker: takerInfo.taker,
|
|
1231
|
+
authority: this.wallet.publicKey,
|
|
1232
|
+
},
|
|
1233
|
+
remainingAccounts,
|
|
1234
|
+
}
|
|
1235
|
+
);
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1165
1238
|
/**
|
|
1166
1239
|
* Close an entire position. If you want to reduce a position, use the {@link openPosition} method in the opposite direction of the current position.
|
|
1167
1240
|
* @param marketIndex
|
package/src/config.ts
CHANGED
|
@@ -28,7 +28,7 @@ export const configs: { [key in DriftEnv]: DriftConfig } = {
|
|
|
28
28
|
devnet: {
|
|
29
29
|
ENV: 'devnet',
|
|
30
30
|
PYTH_ORACLE_MAPPING_ADDRESS: 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2',
|
|
31
|
-
CLEARING_HOUSE_PROGRAM_ID: '
|
|
31
|
+
CLEARING_HOUSE_PROGRAM_ID: 'BMow898PH56jD8z4EaqxicoGXkR1HhN17qrER6Uc4AYq',
|
|
32
32
|
USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
|
|
33
33
|
MARKETS: DevnetMarkets,
|
|
34
34
|
BANKS: DevnetBanks,
|
|
@@ -141,6 +141,10 @@
|
|
|
141
141
|
{
|
|
142
142
|
"name": "maintenanceLiabilityWeight",
|
|
143
143
|
"type": "u128"
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
"name": "imfFactor",
|
|
147
|
+
"type": "u128"
|
|
144
148
|
}
|
|
145
149
|
]
|
|
146
150
|
},
|
|
@@ -534,7 +538,7 @@
|
|
|
534
538
|
},
|
|
535
539
|
{
|
|
536
540
|
"name": "taker",
|
|
537
|
-
"isMut":
|
|
541
|
+
"isMut": true,
|
|
538
542
|
"isSigner": false
|
|
539
543
|
},
|
|
540
544
|
{
|
|
@@ -716,7 +720,7 @@
|
|
|
716
720
|
]
|
|
717
721
|
},
|
|
718
722
|
{
|
|
719
|
-
"name": "
|
|
723
|
+
"name": "withdrawFromMarketToInsuranceVault",
|
|
720
724
|
"accounts": [
|
|
721
725
|
{
|
|
722
726
|
"name": "state",
|
|
@@ -730,7 +734,7 @@
|
|
|
730
734
|
},
|
|
731
735
|
{
|
|
732
736
|
"name": "bank",
|
|
733
|
-
"isMut":
|
|
737
|
+
"isMut": true,
|
|
734
738
|
"isSigner": false
|
|
735
739
|
},
|
|
736
740
|
{
|
|
@@ -835,6 +839,11 @@
|
|
|
835
839
|
"isMut": false,
|
|
836
840
|
"isSigner": false
|
|
837
841
|
},
|
|
842
|
+
{
|
|
843
|
+
"name": "bank",
|
|
844
|
+
"isMut": true,
|
|
845
|
+
"isSigner": false
|
|
846
|
+
},
|
|
838
847
|
{
|
|
839
848
|
"name": "bankVault",
|
|
840
849
|
"isMut": true,
|
|
@@ -1093,6 +1102,62 @@
|
|
|
1093
1102
|
}
|
|
1094
1103
|
]
|
|
1095
1104
|
},
|
|
1105
|
+
{
|
|
1106
|
+
"name": "updateMarketImfFactor",
|
|
1107
|
+
"accounts": [
|
|
1108
|
+
{
|
|
1109
|
+
"name": "admin",
|
|
1110
|
+
"isMut": false,
|
|
1111
|
+
"isSigner": true
|
|
1112
|
+
},
|
|
1113
|
+
{
|
|
1114
|
+
"name": "state",
|
|
1115
|
+
"isMut": false,
|
|
1116
|
+
"isSigner": false
|
|
1117
|
+
},
|
|
1118
|
+
{
|
|
1119
|
+
"name": "market",
|
|
1120
|
+
"isMut": true,
|
|
1121
|
+
"isSigner": false
|
|
1122
|
+
}
|
|
1123
|
+
],
|
|
1124
|
+
"args": [
|
|
1125
|
+
{
|
|
1126
|
+
"name": "imfFactor",
|
|
1127
|
+
"type": "u128"
|
|
1128
|
+
}
|
|
1129
|
+
]
|
|
1130
|
+
},
|
|
1131
|
+
{
|
|
1132
|
+
"name": "updateMarketUnsettledAssetWeight",
|
|
1133
|
+
"accounts": [
|
|
1134
|
+
{
|
|
1135
|
+
"name": "admin",
|
|
1136
|
+
"isMut": false,
|
|
1137
|
+
"isSigner": true
|
|
1138
|
+
},
|
|
1139
|
+
{
|
|
1140
|
+
"name": "state",
|
|
1141
|
+
"isMut": false,
|
|
1142
|
+
"isSigner": false
|
|
1143
|
+
},
|
|
1144
|
+
{
|
|
1145
|
+
"name": "market",
|
|
1146
|
+
"isMut": true,
|
|
1147
|
+
"isSigner": false
|
|
1148
|
+
}
|
|
1149
|
+
],
|
|
1150
|
+
"args": [
|
|
1151
|
+
{
|
|
1152
|
+
"name": "unsettledInitialAssetWeight",
|
|
1153
|
+
"type": "u8"
|
|
1154
|
+
},
|
|
1155
|
+
{
|
|
1156
|
+
"name": "unsettledMaintenanceAssetWeight",
|
|
1157
|
+
"type": "u8"
|
|
1158
|
+
}
|
|
1159
|
+
]
|
|
1160
|
+
},
|
|
1096
1161
|
{
|
|
1097
1162
|
"name": "updateCurveUpdateIntensity",
|
|
1098
1163
|
"accounts": [
|
|
@@ -1731,6 +1796,10 @@
|
|
|
1731
1796
|
{
|
|
1732
1797
|
"name": "maintenanceLiabilityWeight",
|
|
1733
1798
|
"type": "u128"
|
|
1799
|
+
},
|
|
1800
|
+
{
|
|
1801
|
+
"name": "imfFactor",
|
|
1802
|
+
"type": "u128"
|
|
1734
1803
|
}
|
|
1735
1804
|
]
|
|
1736
1805
|
}
|
|
@@ -1808,6 +1877,22 @@
|
|
|
1808
1877
|
"name": "unsettledLoss",
|
|
1809
1878
|
"type": "u128"
|
|
1810
1879
|
},
|
|
1880
|
+
{
|
|
1881
|
+
"name": "imfFactor",
|
|
1882
|
+
"type": "u128"
|
|
1883
|
+
},
|
|
1884
|
+
{
|
|
1885
|
+
"name": "unsettledInitialAssetWeight",
|
|
1886
|
+
"type": "u8"
|
|
1887
|
+
},
|
|
1888
|
+
{
|
|
1889
|
+
"name": "unsettledMaintenanceAssetWeight",
|
|
1890
|
+
"type": "u8"
|
|
1891
|
+
},
|
|
1892
|
+
{
|
|
1893
|
+
"name": "unsettledImfFactor",
|
|
1894
|
+
"type": "u128"
|
|
1895
|
+
},
|
|
1811
1896
|
{
|
|
1812
1897
|
"name": "padding0",
|
|
1813
1898
|
"type": "u32"
|
package/src/math/position.ts
CHANGED
|
@@ -153,7 +153,11 @@ export function calculatePositionFundingPNL(
|
|
|
153
153
|
}
|
|
154
154
|
|
|
155
155
|
export function positionIsAvailable(position: UserPosition): boolean {
|
|
156
|
-
return
|
|
156
|
+
return (
|
|
157
|
+
position.baseAssetAmount.eq(ZERO) &&
|
|
158
|
+
position.openOrders.eq(ZERO) &&
|
|
159
|
+
position.unsettledPnl.eq(ZERO)
|
|
160
|
+
);
|
|
157
161
|
}
|
|
158
162
|
|
|
159
163
|
/**
|
|
@@ -1,22 +1,32 @@
|
|
|
1
1
|
import { Connection } from '@solana/web3.js';
|
|
2
|
+
import { EventEmitter } from 'events';
|
|
3
|
+
import StrictEventEmitter from 'strict-event-emitter-types/types/src';
|
|
2
4
|
|
|
3
5
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
4
6
|
type SlotSubscriberConfig = {}; // for future customization
|
|
5
7
|
|
|
8
|
+
export interface SlotSubscriberEvents {
|
|
9
|
+
newSlot: (newSlot: number) => void;
|
|
10
|
+
}
|
|
11
|
+
|
|
6
12
|
export class SlotSubscriber {
|
|
7
13
|
currentSlot: number;
|
|
8
14
|
subscriptionId: number;
|
|
15
|
+
eventEmitter: StrictEventEmitter<EventEmitter, SlotSubscriberEvents>;
|
|
9
16
|
|
|
10
17
|
public constructor(
|
|
11
18
|
private connection: Connection,
|
|
12
19
|
_config?: SlotSubscriberConfig
|
|
13
|
-
) {
|
|
20
|
+
) {
|
|
21
|
+
this.eventEmitter = new EventEmitter();
|
|
22
|
+
}
|
|
14
23
|
|
|
15
24
|
public async subscribe(): Promise<void> {
|
|
16
25
|
this.currentSlot = await this.connection.getSlot('confirmed');
|
|
17
26
|
|
|
18
27
|
this.subscriptionId = this.connection.onSlotChange((slotInfo) => {
|
|
19
28
|
this.currentSlot = slotInfo.slot;
|
|
29
|
+
this.eventEmitter.emit('newSlot', slotInfo.slot);
|
|
20
30
|
});
|
|
21
31
|
}
|
|
22
32
|
|
package/src/tx/utils.ts
CHANGED
|
@@ -8,7 +8,7 @@ const COMPUTE_UNITS_DEFAULT = 200_000;
|
|
|
8
8
|
|
|
9
9
|
export function wrapInTx(
|
|
10
10
|
instruction: TransactionInstruction,
|
|
11
|
-
computeUnits =
|
|
11
|
+
computeUnits = 600_000 // TODO, requires less code change
|
|
12
12
|
): Transaction {
|
|
13
13
|
const tx = new Transaction();
|
|
14
14
|
if (computeUnits != COMPUTE_UNITS_DEFAULT) {
|
package/src/types.ts
CHANGED
|
@@ -17,6 +17,11 @@ export class PositionDirection {
|
|
|
17
17
|
static readonly SHORT = { short: {} };
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
export class DepositDirection {
|
|
21
|
+
static readonly DEPOSIT = { deposit: {} };
|
|
22
|
+
static readonly WITHDRAW = { withdraw: {} };
|
|
23
|
+
}
|
|
24
|
+
|
|
20
25
|
export class OracleSource {
|
|
21
26
|
static readonly PYTH = { pyth: {} };
|
|
22
27
|
static readonly SWITCHBOARD = { switchboard: {} };
|
|
@@ -105,6 +110,7 @@ export type DepositRecord = {
|
|
|
105
110
|
};
|
|
106
111
|
bankIndex: BN;
|
|
107
112
|
amount: BN;
|
|
113
|
+
oraclePrice: BN;
|
|
108
114
|
from?: PublicKey;
|
|
109
115
|
to?: PublicKey;
|
|
110
116
|
};
|
|
@@ -441,6 +447,11 @@ export type MakerInfo = {
|
|
|
441
447
|
order: Order;
|
|
442
448
|
};
|
|
443
449
|
|
|
450
|
+
export type TakerInfo = {
|
|
451
|
+
taker: PublicKey;
|
|
452
|
+
order: Order;
|
|
453
|
+
};
|
|
454
|
+
|
|
444
455
|
// # Misc Types
|
|
445
456
|
export interface IWallet {
|
|
446
457
|
signTransaction(tx: Transaction): Promise<Transaction>;
|