@drift-labs/sdk 2.23.0 → 2.25.0-beta.0
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/README.md +1 -1
- package/lib/accounts/fetch.d.ts +1 -1
- package/lib/accounts/pollingDriftClientAccountSubscriber.d.ts +1 -1
- package/lib/accounts/pollingOracleAccountSubscriber.d.ts +1 -1
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts +1 -1
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +1 -1
- package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +1 -1
- package/lib/accounts/webSocketAccountSubscriber.d.ts +1 -1
- package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +1 -1
- package/lib/accounts/webSocketUserAccountSubscriber.d.ts +1 -1
- package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +1 -1
- package/lib/addresses/pda.d.ts +1 -2
- package/lib/addresses/pda.js +1 -1
- package/lib/adminClient.d.ts +3 -3
- package/lib/adminClient.js +16 -4
- package/lib/constants/numericConstants.d.ts +59 -61
- package/lib/constants/perpMarkets.js +21 -1
- package/lib/constants/spotMarkets.d.ts +0 -1
- package/lib/dlob/DLOB.d.ts +0 -1
- package/lib/dlob/DLOBNode.d.ts +0 -1
- package/lib/dlob/DLOBOrders.d.ts +1 -1
- package/lib/dlob/DLOBOrders.js +1 -1
- package/lib/dlob/NodeList.d.ts +0 -1
- package/lib/driftClient.d.ts +58 -24
- package/lib/driftClient.js +152 -136
- package/lib/events/eventSubscriber.d.ts +1 -1
- package/lib/events/eventSubscriber.js +3 -2
- package/lib/events/fetchLogs.d.ts +1 -1
- package/lib/events/fetchLogs.js +3 -2
- package/lib/examples/loadDlob.js +1 -1
- package/lib/examples/makeTradeExample.js +1 -1
- package/lib/factory/bigNum.d.ts +8 -9
- package/lib/factory/bigNum.js +1 -1
- package/lib/factory/oracleClient.js +1 -1
- package/lib/idl/drift.json +216 -14
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1 -1
- package/lib/math/amm.d.ts +2 -3
- package/lib/math/amm.js +1 -1
- package/lib/math/auction.d.ts +0 -1
- package/lib/math/conversion.d.ts +1 -2
- package/lib/math/funding.d.ts +1 -2
- package/lib/math/funding.js +1 -1
- package/lib/math/insurance.d.ts +1 -1
- package/lib/math/insurance.js +17 -1
- package/lib/math/margin.d.ts +1 -2
- package/lib/math/margin.js +1 -1
- package/lib/math/market.d.ts +1 -2
- package/lib/math/market.js +1 -1
- package/lib/math/oracles.d.ts +0 -1
- package/lib/math/oracles.js +2 -3
- package/lib/math/orders.d.ts +1 -2
- package/lib/math/orders.js +1 -1
- package/lib/math/position.d.ts +0 -1
- package/lib/math/repeg.d.ts +1 -2
- package/lib/math/repeg.js +1 -1
- package/lib/math/spotBalance.d.ts +1 -2
- package/lib/math/spotBalance.js +1 -1
- package/lib/math/spotMarket.d.ts +1 -2
- package/lib/math/spotMarket.js +1 -1
- package/lib/math/spotPosition.d.ts +1 -2
- package/lib/math/trade.d.ts +1 -2
- package/lib/math/trade.js +1 -1
- package/lib/math/utils.d.ts +0 -1
- package/lib/oracles/pythClient.d.ts +2 -3
- package/lib/oracles/pythClient.js +1 -1
- package/lib/oracles/quoteAssetOracleClient.js +1 -1
- package/lib/oracles/types.d.ts +1 -2
- package/lib/orderParams.d.ts +1 -2
- package/lib/serum/serumSubscriber.d.ts +1 -2
- package/lib/serum/serumSubscriber.js +1 -1
- package/lib/tokenFaucet.d.ts +2 -3
- package/lib/tokenFaucet.js +2 -2
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/types.d.ts +1 -1
- package/lib/types.d.ts +9 -3
- package/lib/types.js +2 -1
- package/lib/user.d.ts +0 -1
- package/package.json +2 -2
- package/src/accounts/fetch.ts +1 -1
- package/src/accounts/pollingDriftClientAccountSubscriber.ts +1 -1
- package/src/accounts/pollingOracleAccountSubscriber.ts +1 -1
- package/src/accounts/pollingTokenAccountSubscriber.ts +1 -1
- package/src/accounts/pollingUserAccountSubscriber.ts +1 -1
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +1 -1
- package/src/accounts/webSocketAccountSubscriber.ts +1 -1
- package/src/accounts/webSocketDriftClientAccountSubscriber.ts +1 -1
- package/src/accounts/webSocketUserAccountSubscriber.ts +1 -1
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +1 -1
- package/src/addresses/pda.ts +2 -2
- package/src/adminClient.ts +30 -2
- package/src/constants/perpMarkets.ts +21 -1
- package/src/dlob/DLOBOrders.ts +2 -2
- package/src/driftClient.ts +266 -163
- package/src/events/eventSubscriber.ts +7 -3
- package/src/events/fetchLogs.ts +7 -3
- package/src/examples/loadDlob.ts +1 -1
- package/src/examples/makeTradeExample.ts +1 -1
- package/src/factory/bigNum.ts +1 -1
- package/src/factory/oracleClient.ts +1 -1
- package/src/idl/drift.json +216 -14
- package/src/index.ts +1 -1
- package/src/math/amm.ts +1 -1
- package/src/math/funding.ts +1 -1
- package/src/math/insurance.ts +29 -0
- package/src/math/margin.ts +1 -1
- package/src/math/market.ts +1 -1
- package/src/math/oracles.ts +2 -3
- package/src/math/orders.ts +1 -1
- package/src/math/repeg.ts +1 -1
- package/src/math/spotBalance.ts +1 -1
- package/src/math/spotMarket.ts +1 -1
- package/src/math/spotPosition.ts +1 -1
- package/src/math/trade.ts +1 -1
- package/src/oracles/pythClient.ts +1 -1
- package/src/oracles/quoteAssetOracleClient.ts +1 -1
- package/src/oracles/types.ts +1 -1
- package/src/orderParams.ts +1 -1
- package/src/serum/serumSubscriber.ts +1 -1
- package/src/tokenFaucet.ts +2 -2
- package/src/tx/retryTxSender.ts +1 -1
- package/src/tx/types.ts +1 -1
- package/src/types.ts +8 -1
- package/src/userMap/userStatsMap.ts +1 -1
package/src/driftClient.ts
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
Idl,
|
|
5
5
|
Program,
|
|
6
6
|
ProgramAccount,
|
|
7
|
-
} from '@
|
|
7
|
+
} from '@coral-xyz/anchor';
|
|
8
8
|
import bs58 from 'bs58';
|
|
9
9
|
import {
|
|
10
10
|
ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
@@ -33,13 +33,12 @@ import {
|
|
|
33
33
|
isVariant,
|
|
34
34
|
ReferrerNameAccount,
|
|
35
35
|
OrderTriggerCondition,
|
|
36
|
-
isOneOfVariant,
|
|
37
|
-
PostOnlyParams,
|
|
38
36
|
SpotBalanceType,
|
|
39
37
|
PerpMarketExtendedInfo,
|
|
40
38
|
UserStatsAccount,
|
|
39
|
+
ModifyOrderParams,
|
|
41
40
|
} from './types';
|
|
42
|
-
import * as anchor from '@
|
|
41
|
+
import * as anchor from '@coral-xyz/anchor';
|
|
43
42
|
import driftIDL from './idl/drift.json';
|
|
44
43
|
|
|
45
44
|
import {
|
|
@@ -766,6 +765,24 @@ export class DriftClient {
|
|
|
766
765
|
);
|
|
767
766
|
}
|
|
768
767
|
|
|
768
|
+
public async getUserAccountsAndAddressesForAuthority(
|
|
769
|
+
authority: PublicKey
|
|
770
|
+
): Promise<ProgramAccount<UserAccount>[]> {
|
|
771
|
+
const programAccounts = await this.program.account.user.all([
|
|
772
|
+
{
|
|
773
|
+
memcmp: {
|
|
774
|
+
offset: 8,
|
|
775
|
+
/** data to match, as base-58 encoded string and limited to less than 129 bytes */
|
|
776
|
+
bytes: bs58.encode(authority.toBuffer()),
|
|
777
|
+
},
|
|
778
|
+
},
|
|
779
|
+
]);
|
|
780
|
+
|
|
781
|
+
return programAccounts.map(
|
|
782
|
+
(programAccount) => programAccount as ProgramAccount<UserAccount>
|
|
783
|
+
);
|
|
784
|
+
}
|
|
785
|
+
|
|
769
786
|
public async getUserAccountsForAuthority(
|
|
770
787
|
authority: PublicKey
|
|
771
788
|
): Promise<UserAccount[]> {
|
|
@@ -3372,221 +3389,205 @@ export class DriftClient {
|
|
|
3372
3389
|
/**
|
|
3373
3390
|
* Modifies an open order (spot or perp) by closing it and replacing it with a new order.
|
|
3374
3391
|
* @param orderId: The open order to modify
|
|
3375
|
-
* @param
|
|
3376
|
-
* @param
|
|
3377
|
-
* @param
|
|
3378
|
-
* @param
|
|
3392
|
+
* @param newDirection: The new direction for the order
|
|
3393
|
+
* @param newBaseAmount: The new base amount for the order
|
|
3394
|
+
* @param newLimitPice: The new limit price for the order
|
|
3395
|
+
* @param newOraclePriceOffset: The new oracle price offset for the order
|
|
3379
3396
|
* @param newTriggerPrice: Optional - Thew new trigger price for the order.
|
|
3380
|
-
* @param
|
|
3381
|
-
* @param
|
|
3382
|
-
* @param
|
|
3383
|
-
* @param
|
|
3397
|
+
* @param auctionDuration:
|
|
3398
|
+
* @param auctionStartPrice:
|
|
3399
|
+
* @param auctionEndPrice:
|
|
3400
|
+
* @param reduceOnly:
|
|
3401
|
+
* @param postOnly:
|
|
3402
|
+
* @param immediateOrCancel:
|
|
3403
|
+
* @param maxTs:
|
|
3384
3404
|
* @returns
|
|
3385
3405
|
*/
|
|
3386
3406
|
public async modifyOrder({
|
|
3387
3407
|
orderId,
|
|
3408
|
+
newDirection,
|
|
3388
3409
|
newBaseAmount,
|
|
3389
3410
|
newLimitPrice,
|
|
3390
3411
|
newOraclePriceOffset,
|
|
3391
|
-
newOrderType,
|
|
3392
3412
|
newTriggerPrice,
|
|
3393
3413
|
newTriggerCondition,
|
|
3394
|
-
isSpot,
|
|
3395
3414
|
auctionDuration,
|
|
3396
3415
|
auctionStartPrice,
|
|
3397
3416
|
auctionEndPrice,
|
|
3417
|
+
reduceOnly,
|
|
3418
|
+
postOnly,
|
|
3419
|
+
immediateOrCancel,
|
|
3420
|
+
maxTs,
|
|
3421
|
+
txParams,
|
|
3398
3422
|
}: {
|
|
3399
3423
|
orderId: number;
|
|
3424
|
+
newDirection?: PositionDirection;
|
|
3400
3425
|
newBaseAmount?: BN;
|
|
3401
3426
|
newLimitPrice?: BN;
|
|
3402
3427
|
newOraclePriceOffset?: number;
|
|
3403
|
-
newOrderType?: OrderType;
|
|
3404
3428
|
newTriggerPrice?: BN;
|
|
3405
3429
|
newTriggerCondition?: OrderTriggerCondition;
|
|
3406
|
-
isSpot?: boolean;
|
|
3407
3430
|
auctionDuration?: number;
|
|
3408
3431
|
auctionStartPrice?: BN;
|
|
3409
3432
|
auctionEndPrice?: BN;
|
|
3433
|
+
reduceOnly?: boolean;
|
|
3434
|
+
postOnly?: boolean;
|
|
3435
|
+
immediateOrCancel?: boolean;
|
|
3436
|
+
maxTs?: BN;
|
|
3437
|
+
txParams?: TxParams;
|
|
3410
3438
|
}): Promise<TransactionSignature> {
|
|
3411
|
-
|
|
3412
|
-
|
|
3413
|
-
|
|
3414
|
-
|
|
3415
|
-
|
|
3416
|
-
|
|
3417
|
-
|
|
3418
|
-
|
|
3419
|
-
|
|
3420
|
-
|
|
3421
|
-
|
|
3422
|
-
|
|
3423
|
-
|
|
3424
|
-
|
|
3425
|
-
: isOneOfVariant(openOrder.orderType, ['triggerLimit', 'triggerMarket']);
|
|
3426
|
-
const orderTypeHasLimitPrice = newOrderType
|
|
3427
|
-
? isOneOfVariant(newOrderType, ['triggerLimit', 'limit'])
|
|
3428
|
-
: isOneOfVariant(openOrder.orderType, ['triggerLimit', 'limit']);
|
|
3429
|
-
|
|
3430
|
-
const newOrderParams: OptionalOrderParams = {
|
|
3431
|
-
orderType: newOrderType || openOrder.orderType,
|
|
3432
|
-
marketType: openOrder.marketType,
|
|
3433
|
-
direction: openOrder.direction,
|
|
3434
|
-
baseAssetAmount: newBaseAmount || openOrder.baseAssetAmount,
|
|
3435
|
-
price: orderTypeHasLimitPrice
|
|
3436
|
-
? newLimitPrice || openOrder.price
|
|
3437
|
-
: undefined,
|
|
3438
|
-
marketIndex: openOrder.marketIndex,
|
|
3439
|
-
reduceOnly: openOrder.reduceOnly,
|
|
3440
|
-
postOnly: openOrder.postOnly
|
|
3441
|
-
? PostOnlyParams.MUST_POST_ONLY
|
|
3442
|
-
: PostOnlyParams.NONE,
|
|
3443
|
-
immediateOrCancel: openOrder.immediateOrCancel,
|
|
3444
|
-
triggerPrice: orderTypeHasTrigger
|
|
3445
|
-
? newTriggerPrice || openOrder.triggerPrice
|
|
3446
|
-
: undefined,
|
|
3447
|
-
triggerCondition: orderTypeHasTrigger
|
|
3448
|
-
? newTriggerCondition || openOrder.triggerCondition
|
|
3449
|
-
: undefined,
|
|
3450
|
-
oraclePriceOffset: newOraclePriceOffset || openOrder.oraclePriceOffset,
|
|
3451
|
-
auctionDuration: auctionDuration ?? openOrder.auctionDuration,
|
|
3452
|
-
maxTs: openOrder.maxTs,
|
|
3453
|
-
auctionStartPrice: auctionStartPrice || openOrder.auctionStartPrice,
|
|
3454
|
-
auctionEndPrice: auctionEndPrice || openOrder.auctionEndPrice,
|
|
3455
|
-
userOrderId: openOrder.userOrderId,
|
|
3439
|
+
const orderParams: ModifyOrderParams = {
|
|
3440
|
+
baseAssetAmount: newBaseAmount || null,
|
|
3441
|
+
direction: newDirection || null,
|
|
3442
|
+
price: newLimitPrice || null,
|
|
3443
|
+
oraclePriceOffset: newOraclePriceOffset || null,
|
|
3444
|
+
triggerPrice: newTriggerPrice || null,
|
|
3445
|
+
triggerCondition: newTriggerCondition || null,
|
|
3446
|
+
auctionDuration: auctionDuration || null,
|
|
3447
|
+
auctionStartPrice: auctionStartPrice || null,
|
|
3448
|
+
auctionEndPrice: auctionEndPrice || null,
|
|
3449
|
+
reduceOnly: reduceOnly || null,
|
|
3450
|
+
postOnly: postOnly || null,
|
|
3451
|
+
immediateOrCancel: immediateOrCancel || null,
|
|
3452
|
+
maxTs: maxTs || null,
|
|
3456
3453
|
};
|
|
3457
|
-
const placeOrderIx = isSpot
|
|
3458
|
-
? await this.getPlaceSpotOrderIx(newOrderParams)
|
|
3459
|
-
: await this.getPlacePerpOrderIx(newOrderParams);
|
|
3460
3454
|
|
|
3461
|
-
const
|
|
3462
|
-
|
|
3463
|
-
|
|
3464
|
-
|
|
3465
|
-
|
|
3455
|
+
const { txSig } = await this.sendTransaction(
|
|
3456
|
+
wrapInTx(
|
|
3457
|
+
await this.getModifyOrderIx(orderId, orderParams),
|
|
3458
|
+
txParams?.computeUnits,
|
|
3459
|
+
txParams?.computeUnitsPrice
|
|
3460
|
+
),
|
|
3461
|
+
[],
|
|
3462
|
+
this.opts
|
|
3466
3463
|
);
|
|
3467
|
-
|
|
3468
|
-
|
|
3469
|
-
const { txSig, slot } = await this.sendTransaction(tx, [], this.opts);
|
|
3464
|
+
return txSig;
|
|
3465
|
+
}
|
|
3470
3466
|
|
|
3471
|
-
|
|
3472
|
-
|
|
3473
|
-
|
|
3474
|
-
|
|
3475
|
-
|
|
3476
|
-
}
|
|
3467
|
+
public async getModifyOrderIx(
|
|
3468
|
+
orderId: number,
|
|
3469
|
+
orderParams: ModifyOrderParams
|
|
3470
|
+
): Promise<TransactionInstruction> {
|
|
3471
|
+
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
3477
3472
|
|
|
3478
|
-
|
|
3473
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
3474
|
+
userAccounts: [this.getUserAccount()],
|
|
3475
|
+
useMarketLastSlotCache: true,
|
|
3476
|
+
});
|
|
3477
|
+
|
|
3478
|
+
return await this.program.instruction.modifyOrder(orderId, orderParams, {
|
|
3479
|
+
accounts: {
|
|
3480
|
+
state: await this.getStatePublicKey(),
|
|
3481
|
+
user: userAccountPublicKey,
|
|
3482
|
+
userStats: this.getUserStatsAccountPublicKey(),
|
|
3483
|
+
authority: this.wallet.publicKey,
|
|
3484
|
+
},
|
|
3485
|
+
remainingAccounts,
|
|
3486
|
+
});
|
|
3479
3487
|
}
|
|
3480
3488
|
|
|
3481
3489
|
/**
|
|
3482
3490
|
* Modifies an open order by closing it and replacing it with a new order.
|
|
3483
3491
|
* @param userOrderId: The open order to modify
|
|
3484
|
-
* @param
|
|
3485
|
-
* @param
|
|
3486
|
-
* @param
|
|
3487
|
-
* @param
|
|
3492
|
+
* @param newDirection: The new direction for the order
|
|
3493
|
+
* @param newBaseAmount: The new base amount for the order
|
|
3494
|
+
* @param newLimitPice: The new limit price for the order
|
|
3495
|
+
* @param newOraclePriceOffset: The new oracle price offset for the order
|
|
3488
3496
|
* @param newTriggerPrice: Optional - Thew new trigger price for the order.
|
|
3489
|
-
* @param isSpot: Set to true if the order is a spot order
|
|
3490
3497
|
* @param auctionDuration: Only required if order type changed to market from something else
|
|
3491
3498
|
* @param auctionStartPrice: Only required if order type changed to market from something else
|
|
3492
3499
|
* @param auctionEndPrice: Only required if order type changed to market from something else
|
|
3500
|
+
* @param reduceOnly:
|
|
3501
|
+
* @param postOnly:
|
|
3502
|
+
* @param immediateOrCancel:
|
|
3503
|
+
* @param maxTs:
|
|
3493
3504
|
* @returns
|
|
3494
3505
|
*/
|
|
3495
3506
|
public async modifyOrderByUserOrderId({
|
|
3496
3507
|
userOrderId,
|
|
3508
|
+
newDirection,
|
|
3497
3509
|
newBaseAmount,
|
|
3498
3510
|
newLimitPrice,
|
|
3499
3511
|
newOraclePriceOffset,
|
|
3500
|
-
newOrderType,
|
|
3501
3512
|
newTriggerPrice,
|
|
3502
3513
|
newTriggerCondition,
|
|
3503
|
-
isSpot,
|
|
3504
3514
|
auctionDuration,
|
|
3505
3515
|
auctionStartPrice,
|
|
3506
3516
|
auctionEndPrice,
|
|
3517
|
+
reduceOnly,
|
|
3518
|
+
postOnly,
|
|
3519
|
+
immediateOrCancel,
|
|
3520
|
+
maxTs,
|
|
3521
|
+
txParams,
|
|
3507
3522
|
}: {
|
|
3508
3523
|
userOrderId: number;
|
|
3524
|
+
newDirection?: PositionDirection;
|
|
3509
3525
|
newBaseAmount?: BN;
|
|
3510
3526
|
newLimitPrice?: BN;
|
|
3511
3527
|
newOraclePriceOffset?: number;
|
|
3512
|
-
newOrderType?: OrderType;
|
|
3513
3528
|
newTriggerPrice?: BN;
|
|
3514
3529
|
newTriggerCondition?: OrderTriggerCondition;
|
|
3515
|
-
isSpot?: boolean;
|
|
3516
3530
|
auctionDuration?: number;
|
|
3517
3531
|
auctionStartPrice?: BN;
|
|
3518
3532
|
auctionEndPrice?: BN;
|
|
3533
|
+
reduceOnly?: boolean;
|
|
3534
|
+
postOnly?: boolean;
|
|
3535
|
+
immediateOrCancel?: boolean;
|
|
3536
|
+
maxTs?: BN;
|
|
3537
|
+
txParams?: TxParams;
|
|
3519
3538
|
}): Promise<TransactionSignature> {
|
|
3520
|
-
|
|
3521
|
-
|
|
3522
|
-
|
|
3523
|
-
|
|
3524
|
-
|
|
3525
|
-
|
|
3526
|
-
|
|
3527
|
-
|
|
3528
|
-
|
|
3529
|
-
|
|
3530
|
-
|
|
3531
|
-
|
|
3532
|
-
|
|
3533
|
-
|
|
3534
|
-
const orderTypeHasTrigger = newOrderType
|
|
3535
|
-
? isOneOfVariant(newOrderType, ['triggerlimit', 'triggerMarket'])
|
|
3536
|
-
: isOneOfVariant(openOrder.orderType, ['triggerLimit', 'triggerMarket']);
|
|
3537
|
-
const orderTypeHasLimitPrice = newOrderType
|
|
3538
|
-
? isOneOfVariant(newOrderType, ['triggerLimit', 'limit'])
|
|
3539
|
-
: isOneOfVariant(openOrder.orderType, ['triggerLimit', 'limit']);
|
|
3540
|
-
|
|
3541
|
-
const newOrderParams: OptionalOrderParams = {
|
|
3542
|
-
orderType: newOrderType || openOrder.orderType,
|
|
3543
|
-
marketType: openOrder.marketType,
|
|
3544
|
-
direction: openOrder.direction,
|
|
3545
|
-
baseAssetAmount: newBaseAmount || openOrder.baseAssetAmount,
|
|
3546
|
-
price: orderTypeHasLimitPrice
|
|
3547
|
-
? newLimitPrice || openOrder.price
|
|
3548
|
-
: undefined,
|
|
3549
|
-
marketIndex: openOrder.marketIndex,
|
|
3550
|
-
reduceOnly: openOrder.reduceOnly,
|
|
3551
|
-
postOnly: openOrder.postOnly
|
|
3552
|
-
? PostOnlyParams.MUST_POST_ONLY
|
|
3553
|
-
: PostOnlyParams.NONE,
|
|
3554
|
-
immediateOrCancel: openOrder.immediateOrCancel,
|
|
3555
|
-
triggerPrice: orderTypeHasTrigger
|
|
3556
|
-
? newTriggerPrice || openOrder.triggerPrice
|
|
3557
|
-
: undefined,
|
|
3558
|
-
triggerCondition: orderTypeHasTrigger
|
|
3559
|
-
? newTriggerCondition || openOrder.triggerCondition
|
|
3560
|
-
: undefined,
|
|
3561
|
-
oraclePriceOffset: newOraclePriceOffset || openOrder.oraclePriceOffset,
|
|
3562
|
-
auctionDuration: auctionDuration || openOrder.auctionDuration,
|
|
3563
|
-
maxTs: openOrder.maxTs,
|
|
3564
|
-
auctionStartPrice: auctionStartPrice || openOrder.auctionStartPrice,
|
|
3565
|
-
auctionEndPrice: auctionEndPrice || openOrder.auctionEndPrice,
|
|
3566
|
-
userOrderId: openOrder.userOrderId,
|
|
3539
|
+
const orderParams: ModifyOrderParams = {
|
|
3540
|
+
baseAssetAmount: newBaseAmount || null,
|
|
3541
|
+
direction: newDirection || null,
|
|
3542
|
+
price: newLimitPrice || null,
|
|
3543
|
+
oraclePriceOffset: newOraclePriceOffset || null,
|
|
3544
|
+
triggerPrice: newTriggerPrice || null,
|
|
3545
|
+
triggerCondition: newTriggerCondition || null,
|
|
3546
|
+
auctionDuration: auctionDuration || null,
|
|
3547
|
+
auctionStartPrice: auctionStartPrice || null,
|
|
3548
|
+
auctionEndPrice: auctionEndPrice || null,
|
|
3549
|
+
reduceOnly: reduceOnly || null,
|
|
3550
|
+
postOnly: postOnly || null,
|
|
3551
|
+
immediateOrCancel: immediateOrCancel || null,
|
|
3552
|
+
maxTs: maxTs || null,
|
|
3567
3553
|
};
|
|
3568
|
-
const placeOrderIx = isSpot
|
|
3569
|
-
? await this.getPlaceSpotOrderIx(newOrderParams)
|
|
3570
|
-
: await this.getPlacePerpOrderIx(newOrderParams);
|
|
3571
3554
|
|
|
3572
|
-
const
|
|
3573
|
-
|
|
3574
|
-
|
|
3575
|
-
|
|
3576
|
-
|
|
3555
|
+
const { txSig } = await this.sendTransaction(
|
|
3556
|
+
wrapInTx(
|
|
3557
|
+
await this.getModifyOrderByUserIdIx(userOrderId, orderParams),
|
|
3558
|
+
txParams?.computeUnits,
|
|
3559
|
+
txParams?.computeUnitsPrice
|
|
3560
|
+
),
|
|
3561
|
+
[],
|
|
3562
|
+
this.opts
|
|
3577
3563
|
);
|
|
3578
|
-
|
|
3579
|
-
|
|
3580
|
-
const { txSig, slot } = await this.sendTransaction(tx, [], this.opts);
|
|
3564
|
+
return txSig;
|
|
3565
|
+
}
|
|
3581
3566
|
|
|
3582
|
-
|
|
3583
|
-
|
|
3584
|
-
|
|
3585
|
-
|
|
3586
|
-
|
|
3587
|
-
}
|
|
3567
|
+
public async getModifyOrderByUserIdIx(
|
|
3568
|
+
userOrderId: number,
|
|
3569
|
+
orderParams: ModifyOrderParams
|
|
3570
|
+
): Promise<TransactionInstruction> {
|
|
3571
|
+
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
3588
3572
|
|
|
3589
|
-
|
|
3573
|
+
const remainingAccounts = this.getRemainingAccounts({
|
|
3574
|
+
userAccounts: [this.getUserAccount()],
|
|
3575
|
+
useMarketLastSlotCache: true,
|
|
3576
|
+
});
|
|
3577
|
+
|
|
3578
|
+
return await this.program.instruction.modifyOrderByUserId(
|
|
3579
|
+
userOrderId,
|
|
3580
|
+
orderParams,
|
|
3581
|
+
{
|
|
3582
|
+
accounts: {
|
|
3583
|
+
state: await this.getStatePublicKey(),
|
|
3584
|
+
user: userAccountPublicKey,
|
|
3585
|
+
userStats: this.getUserStatsAccountPublicKey(),
|
|
3586
|
+
authority: this.wallet.publicKey,
|
|
3587
|
+
},
|
|
3588
|
+
remainingAccounts,
|
|
3589
|
+
}
|
|
3590
|
+
);
|
|
3590
3591
|
}
|
|
3591
3592
|
|
|
3592
3593
|
public async settlePNLs(
|
|
@@ -4205,11 +4206,11 @@ export class DriftClient {
|
|
|
4205
4206
|
);
|
|
4206
4207
|
}
|
|
4207
4208
|
|
|
4208
|
-
public async
|
|
4209
|
+
public async getAddInsuranceFundStakeIx(
|
|
4209
4210
|
marketIndex: number,
|
|
4210
4211
|
amount: BN,
|
|
4211
4212
|
collateralAccountPublicKey: PublicKey
|
|
4212
|
-
): Promise<
|
|
4213
|
+
): Promise<TransactionInstruction> {
|
|
4213
4214
|
const spotMarket = this.getSpotMarketAccount(marketIndex);
|
|
4214
4215
|
const ifStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey(
|
|
4215
4216
|
this.program.programId,
|
|
@@ -4223,7 +4224,7 @@ export class DriftClient {
|
|
|
4223
4224
|
writableSpotMarketIndexes: [marketIndex],
|
|
4224
4225
|
});
|
|
4225
4226
|
|
|
4226
|
-
const
|
|
4227
|
+
const ix = this.program.instruction.addInsuranceFundStake(
|
|
4227
4228
|
marketIndex,
|
|
4228
4229
|
amount,
|
|
4229
4230
|
{
|
|
@@ -4243,7 +4244,88 @@ export class DriftClient {
|
|
|
4243
4244
|
}
|
|
4244
4245
|
);
|
|
4245
4246
|
|
|
4246
|
-
|
|
4247
|
+
return ix;
|
|
4248
|
+
}
|
|
4249
|
+
|
|
4250
|
+
/**
|
|
4251
|
+
* Add to an insurance fund stake and optionally initialize the account
|
|
4252
|
+
*/
|
|
4253
|
+
public async addInsuranceFundStake({
|
|
4254
|
+
marketIndex,
|
|
4255
|
+
amount,
|
|
4256
|
+
collateralAccountPublicKey,
|
|
4257
|
+
initializeStakeAccount,
|
|
4258
|
+
}: {
|
|
4259
|
+
/**
|
|
4260
|
+
* Spot market index
|
|
4261
|
+
*/
|
|
4262
|
+
marketIndex: number;
|
|
4263
|
+
amount: BN;
|
|
4264
|
+
/**
|
|
4265
|
+
* The account where the funds to stake come from. Usually an associated token account
|
|
4266
|
+
*/
|
|
4267
|
+
collateralAccountPublicKey: PublicKey;
|
|
4268
|
+
/**
|
|
4269
|
+
* Add instructions to initialize the staking account -- required if its the first time the currrent authority has staked in this market
|
|
4270
|
+
*/
|
|
4271
|
+
initializeStakeAccount?: boolean;
|
|
4272
|
+
}): Promise<TransactionSignature> {
|
|
4273
|
+
const tx = new Transaction();
|
|
4274
|
+
|
|
4275
|
+
const additionalSigners: Array<Signer> = [];
|
|
4276
|
+
const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
|
|
4277
|
+
const isSolMarket = spotMarketAccount.mint.equals(WRAPPED_SOL_MINT);
|
|
4278
|
+
const createWSOLTokenAccount =
|
|
4279
|
+
isSolMarket && collateralAccountPublicKey.equals(this.wallet.publicKey);
|
|
4280
|
+
|
|
4281
|
+
let tokenAccount;
|
|
4282
|
+
|
|
4283
|
+
if (createWSOLTokenAccount) {
|
|
4284
|
+
const { ixs, signers, pubkey } =
|
|
4285
|
+
await this.getWrappedSolAccountCreationIxs(amount, true);
|
|
4286
|
+
tokenAccount = pubkey;
|
|
4287
|
+
ixs.forEach((ix) => {
|
|
4288
|
+
tx.add(ix);
|
|
4289
|
+
});
|
|
4290
|
+
|
|
4291
|
+
signers.forEach((signer) => additionalSigners.push(signer));
|
|
4292
|
+
} else {
|
|
4293
|
+
tokenAccount = collateralAccountPublicKey;
|
|
4294
|
+
}
|
|
4295
|
+
|
|
4296
|
+
if (initializeStakeAccount) {
|
|
4297
|
+
const initializeIx = await this.getInitializeInsuranceFundStakeIx(
|
|
4298
|
+
marketIndex
|
|
4299
|
+
);
|
|
4300
|
+
tx.add(initializeIx);
|
|
4301
|
+
}
|
|
4302
|
+
|
|
4303
|
+
const addFundsIx = await this.getAddInsuranceFundStakeIx(
|
|
4304
|
+
marketIndex,
|
|
4305
|
+
amount,
|
|
4306
|
+
tokenAccount
|
|
4307
|
+
);
|
|
4308
|
+
|
|
4309
|
+
tx.add(addFundsIx);
|
|
4310
|
+
|
|
4311
|
+
if (createWSOLTokenAccount) {
|
|
4312
|
+
tx.add(
|
|
4313
|
+
Token.createCloseAccountInstruction(
|
|
4314
|
+
TOKEN_PROGRAM_ID,
|
|
4315
|
+
tokenAccount,
|
|
4316
|
+
this.wallet.publicKey,
|
|
4317
|
+
this.wallet.publicKey,
|
|
4318
|
+
[]
|
|
4319
|
+
)
|
|
4320
|
+
);
|
|
4321
|
+
}
|
|
4322
|
+
|
|
4323
|
+
const { txSig } = await this.sendTransaction(
|
|
4324
|
+
tx,
|
|
4325
|
+
additionalSigners,
|
|
4326
|
+
this.opts
|
|
4327
|
+
);
|
|
4328
|
+
|
|
4247
4329
|
return txSig;
|
|
4248
4330
|
}
|
|
4249
4331
|
|
|
@@ -4324,12 +4406,33 @@ export class DriftClient {
|
|
|
4324
4406
|
marketIndex: number,
|
|
4325
4407
|
collateralAccountPublicKey: PublicKey
|
|
4326
4408
|
): Promise<TransactionSignature> {
|
|
4409
|
+
const tx = new Transaction();
|
|
4327
4410
|
const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
|
|
4328
4411
|
const ifStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey(
|
|
4329
4412
|
this.program.programId,
|
|
4330
4413
|
this.wallet.publicKey,
|
|
4331
4414
|
marketIndex
|
|
4332
4415
|
);
|
|
4416
|
+
const tokenAccount = collateralAccountPublicKey;
|
|
4417
|
+
|
|
4418
|
+
// Todo wsol remove iF stake... how do we determine the amount?
|
|
4419
|
+
// const amount = // get balance here...?
|
|
4420
|
+
|
|
4421
|
+
// const additionalSigners: Array<Signer> = [];
|
|
4422
|
+
// const isSolMarket = spotMarketAccount.mint.equals(WRAPPED_SOL_MINT);
|
|
4423
|
+
// const createWSOLTokenAccount =
|
|
4424
|
+
// isSolMarket && collateralAccountPublicKey.equals(this.wallet.publicKey);
|
|
4425
|
+
|
|
4426
|
+
// if (createWSOLTokenAccount) {
|
|
4427
|
+
// const { ixs, signers, pubkey } =
|
|
4428
|
+
// await this.getWrappedSolAccountCreationIxs(amount, true);
|
|
4429
|
+
// tokenAccount = pubkey;
|
|
4430
|
+
// ixs.forEach((ix) => {
|
|
4431
|
+
// tx.add(ix);
|
|
4432
|
+
// });
|
|
4433
|
+
|
|
4434
|
+
// signers.forEach((signer) => additionalSigners.push(signer));
|
|
4435
|
+
// }
|
|
4333
4436
|
|
|
4334
4437
|
const remainingAccounts = this.getRemainingAccounts({
|
|
4335
4438
|
userAccounts: [this.getUserAccount()],
|
|
@@ -4337,9 +4440,8 @@ export class DriftClient {
|
|
|
4337
4440
|
writableSpotMarketIndexes: [marketIndex],
|
|
4338
4441
|
});
|
|
4339
4442
|
|
|
4340
|
-
const
|
|
4341
|
-
marketIndex,
|
|
4342
|
-
{
|
|
4443
|
+
const removeStakeIx =
|
|
4444
|
+
await this.program.instruction.removeInsuranceFundStake(marketIndex, {
|
|
4343
4445
|
accounts: {
|
|
4344
4446
|
state: await this.getStatePublicKey(),
|
|
4345
4447
|
spotMarket: spotMarketAccount.pubkey,
|
|
@@ -4348,12 +4450,13 @@ export class DriftClient {
|
|
|
4348
4450
|
authority: this.wallet.publicKey,
|
|
4349
4451
|
insuranceFundVault: spotMarketAccount.insuranceFund.vault,
|
|
4350
4452
|
driftSigner: this.getSignerPublicKey(),
|
|
4351
|
-
userTokenAccount:
|
|
4453
|
+
userTokenAccount: tokenAccount,
|
|
4352
4454
|
tokenProgram: TOKEN_PROGRAM_ID,
|
|
4353
4455
|
},
|
|
4354
4456
|
remainingAccounts,
|
|
4355
|
-
}
|
|
4356
|
-
|
|
4457
|
+
});
|
|
4458
|
+
|
|
4459
|
+
tx.add(removeStakeIx);
|
|
4357
4460
|
|
|
4358
4461
|
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
4359
4462
|
return txSig;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Connection, TransactionSignature } from '@solana/web3.js';
|
|
2
|
-
import { Program } from '@
|
|
2
|
+
import { Program } from '@coral-xyz/anchor';
|
|
3
3
|
import {
|
|
4
4
|
DefaultEventSubscriptionOptions,
|
|
5
5
|
EventSubscriptionOptions,
|
|
@@ -164,7 +164,11 @@ export class EventSubscriber {
|
|
|
164
164
|
): WrappedEvents {
|
|
165
165
|
const records = [];
|
|
166
166
|
// @ts-ignore
|
|
167
|
-
this.program._events._eventParser.parseLogs(
|
|
167
|
+
const eventGenerator = this.program._events._eventParser.parseLogs(
|
|
168
|
+
logs,
|
|
169
|
+
false
|
|
170
|
+
);
|
|
171
|
+
for (const event of eventGenerator) {
|
|
168
172
|
const expectRecordType = this.eventListMap.has(event.name);
|
|
169
173
|
if (expectRecordType) {
|
|
170
174
|
event.data.txSig = txSig;
|
|
@@ -172,7 +176,7 @@ export class EventSubscriber {
|
|
|
172
176
|
event.data.eventType = event.name;
|
|
173
177
|
records.push(event.data);
|
|
174
178
|
}
|
|
175
|
-
}
|
|
179
|
+
}
|
|
176
180
|
return records;
|
|
177
181
|
}
|
|
178
182
|
|
package/src/events/fetchLogs.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Program } from '@
|
|
1
|
+
import { Program } from '@coral-xyz/anchor';
|
|
2
2
|
import {
|
|
3
3
|
Connection,
|
|
4
4
|
Finality,
|
|
@@ -120,12 +120,16 @@ export class LogParser {
|
|
|
120
120
|
public parseEventsFromLogs(event: Log): WrappedEvents {
|
|
121
121
|
const records: WrappedEvents = [];
|
|
122
122
|
// @ts-ignore
|
|
123
|
-
this.program._events._eventParser.parseLogs(
|
|
123
|
+
const eventGenerator = this.program._events._eventParser.parseLogs(
|
|
124
|
+
event.logs,
|
|
125
|
+
false
|
|
126
|
+
);
|
|
127
|
+
for (const eventLog of eventGenerator) {
|
|
124
128
|
eventLog.data.txSig = event.txSig;
|
|
125
129
|
eventLog.data.slot = event.slot;
|
|
126
130
|
eventLog.data.eventType = eventLog.name;
|
|
127
131
|
records.push(eventLog.data);
|
|
128
|
-
}
|
|
132
|
+
}
|
|
129
133
|
return records;
|
|
130
134
|
}
|
|
131
135
|
}
|
package/src/examples/loadDlob.ts
CHANGED
package/src/factory/bigNum.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { OracleClient } from '../oracles/types';
|
|
|
4
4
|
import { PythClient } from '../oracles/pythClient';
|
|
5
5
|
// import { SwitchboardClient } from '../oracles/switchboardClient';
|
|
6
6
|
import { QuoteAssetOracleClient } from '../oracles/quoteAssetOracleClient';
|
|
7
|
-
import { BN } from '@
|
|
7
|
+
import { BN } from '@coral-xyz/anchor';
|
|
8
8
|
|
|
9
9
|
export function getOracleClient(
|
|
10
10
|
oracleSource: OracleSource,
|