@drift-labs/sdk 2.96.0-beta.15 → 2.96.0-beta.16
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/VERSION +1 -1
- package/lib/config.d.ts +3 -1
- package/lib/config.js +5 -1
- package/lib/driftClient.d.ts +15 -19
- package/lib/driftClient.js +58 -36
- package/lib/idl/drift.json +79 -15
- package/lib/types.d.ts +11 -4
- package/package.json +2 -2
- package/src/config.ts +9 -1
- package/src/driftClient.ts +118 -72
- package/src/idl/drift.json +79 -15
- package/src/tx/txHandler.ts +0 -1
- package/src/types.ts +13 -4
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.96.0-beta.
|
|
1
|
+
2.96.0-beta.16
|
package/lib/config.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ConfirmOptions } from '@solana/web3.js';
|
|
2
|
-
import { PerpMarketAccount, SpotMarketAccount } from '.';
|
|
2
|
+
import { PerpMarketAccount, PublicKey, SpotMarketAccount } from '.';
|
|
3
3
|
import { PerpMarketConfig } from './constants/perpMarkets';
|
|
4
4
|
import { SpotMarketConfig } from './constants/spotMarkets';
|
|
5
5
|
import { OracleInfo } from './oracles/types';
|
|
@@ -20,10 +20,12 @@ type DriftConfig = {
|
|
|
20
20
|
MARKET_LOOKUP_TABLE: string;
|
|
21
21
|
SERUM_LOOKUP_TABLE?: string;
|
|
22
22
|
PYTH_PULL_ORACLE_LOOKUP_TABLE?: string;
|
|
23
|
+
SB_ON_DEMAND_PID: PublicKey;
|
|
23
24
|
};
|
|
24
25
|
export type DriftEnv = 'devnet' | 'mainnet-beta';
|
|
25
26
|
export declare const DRIFT_PROGRAM_ID = "dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH";
|
|
26
27
|
export declare const DRIFT_ORACLE_RECEIVER_ID = "G6EoTTTgpkNBtVXo96EQp2m6uwwVh2Kt6YidjkmQqoha";
|
|
28
|
+
export declare const SWIFT_ID = "SW1fThqrxLzVprnCMpiybiqYQfoNCdduC5uWsSUKChS";
|
|
27
29
|
export declare const DEFAULT_CONFIRMATION_OPTS: ConfirmOptions;
|
|
28
30
|
export declare const configs: {
|
|
29
31
|
[key in DriftEnv]: DriftConfig;
|
package/lib/config.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.findAllMarketAndOracles = exports.getMarketsAndOraclesForSubscription = exports.initialize = exports.getConfig = exports.configs = exports.DEFAULT_CONFIRMATION_OPTS = exports.DRIFT_ORACLE_RECEIVER_ID = exports.DRIFT_PROGRAM_ID = void 0;
|
|
3
|
+
exports.findAllMarketAndOracles = exports.getMarketsAndOraclesForSubscription = exports.initialize = exports.getConfig = exports.configs = exports.DEFAULT_CONFIRMATION_OPTS = exports.SWIFT_ID = exports.DRIFT_ORACLE_RECEIVER_ID = exports.DRIFT_PROGRAM_ID = void 0;
|
|
4
4
|
const perpMarkets_1 = require("./constants/perpMarkets");
|
|
5
5
|
const spotMarkets_1 = require("./constants/spotMarkets");
|
|
6
|
+
const on_demand_1 = require("@switchboard-xyz/on-demand");
|
|
6
7
|
exports.DRIFT_PROGRAM_ID = 'dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH';
|
|
7
8
|
exports.DRIFT_ORACLE_RECEIVER_ID = 'G6EoTTTgpkNBtVXo96EQp2m6uwwVh2Kt6YidjkmQqoha';
|
|
9
|
+
exports.SWIFT_ID = 'SW1fThqrxLzVprnCMpiybiqYQfoNCdduC5uWsSUKChS';
|
|
8
10
|
exports.DEFAULT_CONFIRMATION_OPTS = {
|
|
9
11
|
preflightCommitment: 'confirmed',
|
|
10
12
|
commitment: 'confirmed',
|
|
@@ -24,6 +26,7 @@ exports.configs = {
|
|
|
24
26
|
SPOT_MARKETS: spotMarkets_1.DevnetSpotMarkets,
|
|
25
27
|
MARKET_LOOKUP_TABLE: 'FaMS3U4uBojvGn5FSDEPimddcXsCfwkKsFgMVVnDdxGb',
|
|
26
28
|
DRIFT_ORACLE_RECEIVER_ID: exports.DRIFT_ORACLE_RECEIVER_ID,
|
|
29
|
+
SB_ON_DEMAND_PID: on_demand_1.ON_DEMAND_DEVNET_PID,
|
|
27
30
|
},
|
|
28
31
|
'mainnet-beta': {
|
|
29
32
|
ENV: 'mainnet-beta',
|
|
@@ -40,6 +43,7 @@ exports.configs = {
|
|
|
40
43
|
MARKET_LOOKUP_TABLE: 'D9cnvzswDikQDf53k4HpQ3KJ9y1Fv3HGGDFYMXnK5T6c',
|
|
41
44
|
SERUM_LOOKUP_TABLE: 'GPZkp76cJtNL2mphCvT6FXkJCVPpouidnacckR6rzKDN',
|
|
42
45
|
DRIFT_ORACLE_RECEIVER_ID: exports.DRIFT_ORACLE_RECEIVER_ID,
|
|
46
|
+
SB_ON_DEMAND_PID: on_demand_1.ON_DEMAND_MAINNET_PID,
|
|
43
47
|
},
|
|
44
48
|
};
|
|
45
49
|
let currentConfig = exports.configs.devnet;
|
package/lib/driftClient.d.ts
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
import * as anchor from '@coral-xyz/anchor';
|
|
5
5
|
import { AnchorProvider, BN, Program, ProgramAccount } from '@coral-xyz/anchor';
|
|
6
6
|
import { Idl as Idl30, Program as Program30 } from '@coral-xyz/anchor-30';
|
|
7
|
-
import { DriftClientMetricsEvents, IWallet, MakerInfo, MappedRecord, MarketType, ModifyOrderPolicy, OpenbookV2FulfillmentConfigAccount, OptionalOrderParams, Order, OrderParams, OrderTriggerCondition, PerpMarketAccount, PerpMarketExtendedInfo, PhoenixV1FulfillmentConfigAccount, PlaceAndTakeOrderSuccessCondition, PositionDirection, ReferrerInfo, ReferrerNameAccount, SerumV3FulfillmentConfigAccount, SettlePnlMode, SignedTxData, SpotMarketAccount, SpotPosition, StateAccount, SwapReduceOnly, SwiftOrderParamsMessage, TakerInfo, TxParams, UserAccount, UserStatsAccount } from './types';
|
|
8
|
-
import { AccountMeta, AddressLookupTableAccount, BlockhashWithExpiryBlockHeight, ConfirmOptions, Connection, PublicKey, Signer, Transaction, TransactionInstruction, TransactionSignature, TransactionVersion, VersionedTransaction } from '@solana/web3.js';
|
|
7
|
+
import { DriftClientMetricsEvents, IWallet, MakerInfo, MappedRecord, MarketType, ModifyOrderPolicy, OpenbookV2FulfillmentConfigAccount, OptionalOrderParams, Order, OrderParams, OrderTriggerCondition, PerpMarketAccount, PerpMarketExtendedInfo, PhoenixV1FulfillmentConfigAccount, PlaceAndTakeOrderSuccessCondition, PositionDirection, ReferrerInfo, ReferrerNameAccount, SerumV3FulfillmentConfigAccount, SettlePnlMode, SignedTxData, SpotMarketAccount, SpotPosition, StateAccount, SwapReduceOnly, SwiftOrderParamsMessage, SwiftServerMessage, TakerInfo, TxParams, UserAccount, UserStatsAccount } from './types';
|
|
8
|
+
import { AccountMeta, AddressLookupTableAccount, BlockhashWithExpiryBlockHeight, ConfirmOptions, Connection, Keypair, PublicKey, Signer, Transaction, TransactionInstruction, TransactionSignature, TransactionVersion, VersionedTransaction } from '@solana/web3.js';
|
|
9
9
|
import { TokenFaucet } from './tokenFaucet';
|
|
10
10
|
import { EventEmitter } from 'events';
|
|
11
11
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
@@ -65,6 +65,7 @@ export declare class DriftClient {
|
|
|
65
65
|
txHandler: TxHandler;
|
|
66
66
|
receiverProgram?: Program<PythSolanaReceiver>;
|
|
67
67
|
wormholeProgram?: Program<WormholeCoreBridgeSolana>;
|
|
68
|
+
sbOnDemandProgramdId: PublicKey;
|
|
68
69
|
sbOnDemandProgram?: Program30<Idl30>;
|
|
69
70
|
sbProgramFeedConfigs?: Map<string, any>;
|
|
70
71
|
get isSubscribed(): boolean;
|
|
@@ -504,34 +505,29 @@ export declare class DriftClient {
|
|
|
504
505
|
getPlaceAndTakePerpOrderIx(orderParams: OptionalOrderParams, makerInfo?: MakerInfo | MakerInfo[], referrerInfo?: ReferrerInfo, successCondition?: PlaceAndTakeOrderSuccessCondition, subAccountId?: number): Promise<TransactionInstruction>;
|
|
505
506
|
placeAndMakePerpOrder(orderParams: OptionalOrderParams, takerInfo: TakerInfo, referrerInfo?: ReferrerInfo, txParams?: TxParams, subAccountId?: number): Promise<TransactionSignature>;
|
|
506
507
|
getPlaceAndMakePerpOrderIx(orderParams: OptionalOrderParams, takerInfo: TakerInfo, referrerInfo?: ReferrerInfo, subAccountId?: number): Promise<TransactionInstruction>;
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
market_index: number;
|
|
516
|
-
market_type: 'perp' | 'spot';
|
|
517
|
-
slot: BN;
|
|
518
|
-
};
|
|
519
|
-
placeSwiftTakerOrder(takerOrderParamsMessage: Buffer, takerSignature: Buffer, marketIndex: number, takerInfo: {
|
|
508
|
+
encodeSwiftServerMessage(message: SwiftServerMessage): Buffer;
|
|
509
|
+
decodeSwiftServerMessage(encodedMessage: Buffer): SwiftServerMessage;
|
|
510
|
+
signSwiftServerMessage(message: SwiftServerMessage): Promise<Buffer>;
|
|
511
|
+
signSwiftOrderParamsMessage(orderParamsMessage: SwiftOrderParamsMessage): Promise<Buffer>;
|
|
512
|
+
encodeSwiftOrderParamsMessage(orderParamsMessage: SwiftOrderParamsMessage): Buffer;
|
|
513
|
+
decodeSwiftOrderParamsMessage(encodedMessage: Buffer): SwiftOrderParamsMessage;
|
|
514
|
+
signMessage(message: Uint8Array, keypair?: Keypair): Promise<Buffer>;
|
|
515
|
+
placeSwiftTakerOrder(swiftServerMessage: Buffer, swiftSignature: Buffer, swiftOrderParamsMessage: Buffer, swiftOrderParamsSignature: Buffer, marketIndex: number, takerInfo: {
|
|
520
516
|
taker: PublicKey;
|
|
521
517
|
takerStats: PublicKey;
|
|
522
518
|
takerUserAccount: UserAccount;
|
|
523
519
|
}, txParams?: TxParams): Promise<TransactionSignature>;
|
|
524
|
-
|
|
520
|
+
getPlaceSwiftTakerPerpOrderIxs(encodedSwiftServerMessage: Buffer, swiftSignature: Buffer, encodedSwiftOrderParamsMessage: Buffer, swiftOrderParamsSignature: Buffer, marketIndex: number, takerInfo: {
|
|
525
521
|
taker: PublicKey;
|
|
526
522
|
takerStats: PublicKey;
|
|
527
523
|
takerUserAccount: UserAccount;
|
|
528
524
|
}): Promise<TransactionInstruction[]>;
|
|
529
|
-
placeAndMakeSwiftPerpOrder(
|
|
525
|
+
placeAndMakeSwiftPerpOrder(encodedSwiftMessage: Buffer, swiftSignature: Buffer, encodedSwiftOrderParamsMessage: Buffer, swiftOrderParamsSignature: Buffer, takerExpectedOrderId: number, takerInfo: {
|
|
530
526
|
taker: PublicKey;
|
|
531
527
|
takerStats: PublicKey;
|
|
532
528
|
takerUserAccount: UserAccount;
|
|
533
529
|
}, orderParams: OptionalOrderParams, referrerInfo?: ReferrerInfo, txParams?: TxParams, subAccountId?: number): Promise<TransactionSignature>;
|
|
534
|
-
getPlaceAndMakeSwiftPerpOrderIxs(
|
|
530
|
+
getPlaceAndMakeSwiftPerpOrderIxs(encodedSwiftMessage: Buffer, swiftSignature: Buffer, encodedSwiftOrderParamsMessage: Buffer, swiftOrderParamsSignature: Buffer, takerExpectedOrderId: number, takerInfo: {
|
|
535
531
|
taker: PublicKey;
|
|
536
532
|
takerStats: PublicKey;
|
|
537
533
|
takerUserAccount: UserAccount;
|
|
@@ -774,7 +770,7 @@ export declare class DriftClient {
|
|
|
774
770
|
marketType: MarketType;
|
|
775
771
|
} | undefined;
|
|
776
772
|
getReceiverProgram(): Program<PythSolanaReceiver>;
|
|
777
|
-
getSwitchboardOnDemandProgram(): Program30<Idl30
|
|
773
|
+
getSwitchboardOnDemandProgram(): Promise<Program30<Idl30>>;
|
|
778
774
|
postPythPullOracleUpdateAtomic(vaaString: string, feedId: string): Promise<TransactionSignature>;
|
|
779
775
|
postMultiPythPullOracleUpdatesAtomic(vaaString: string, feedIds: string[]): Promise<TransactionSignature>;
|
|
780
776
|
getPostPythPullOracleUpdateAtomicIxs(vaaString: string, feedIds: string | string[], numSignatures?: number): Promise<TransactionInstruction[]>;
|
package/lib/driftClient.js
CHANGED
|
@@ -67,7 +67,6 @@ const pythPullOracleUtils_1 = require("./util/pythPullOracleUtils");
|
|
|
67
67
|
const utils_2 = require("./tx/utils");
|
|
68
68
|
const pyth_solana_receiver_json_1 = __importDefault(require("./idl/pyth_solana_receiver.json"));
|
|
69
69
|
const on_demand_1 = require("@switchboard-xyz/on-demand");
|
|
70
|
-
const switchboard_on_demand_30_json_1 = __importDefault(require("./idl/switchboard_on_demand_30.json"));
|
|
71
70
|
const ed = __importStar(require("@noble/ed25519"));
|
|
72
71
|
/**
|
|
73
72
|
* # DriftClient
|
|
@@ -81,7 +80,7 @@ class DriftClient {
|
|
|
81
80
|
this._isSubscribed = val;
|
|
82
81
|
}
|
|
83
82
|
constructor(config) {
|
|
84
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6;
|
|
83
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7;
|
|
85
84
|
this.users = new Map();
|
|
86
85
|
this._isSubscribed = false;
|
|
87
86
|
this.perpMarketLastSlotCache = new Map();
|
|
@@ -195,6 +194,8 @@ class DriftClient {
|
|
|
195
194
|
opts: this.opts,
|
|
196
195
|
txHandler: this.txHandler,
|
|
197
196
|
});
|
|
197
|
+
this.sbOnDemandProgramdId =
|
|
198
|
+
config_1.configs[(_7 = config.env) !== null && _7 !== void 0 ? _7 : 'mainnet-beta'].SB_ON_DEMAND_PID;
|
|
198
199
|
}
|
|
199
200
|
getUserMapKey(subAccountId, authority) {
|
|
200
201
|
return `${subAccountId}_${authority.toString()}`;
|
|
@@ -2912,46 +2913,59 @@ class DriftClient {
|
|
|
2912
2913
|
remainingAccounts,
|
|
2913
2914
|
});
|
|
2914
2915
|
}
|
|
2915
|
-
|
|
2916
|
-
const
|
|
2916
|
+
encodeSwiftServerMessage(message) {
|
|
2917
|
+
const messageWithBuffer = {
|
|
2918
|
+
slot: message.slot,
|
|
2919
|
+
swiftOrderSignature: message.swiftOrderSignature,
|
|
2920
|
+
};
|
|
2921
|
+
return this.program.coder.types.encode('SwiftServerMessage', messageWithBuffer);
|
|
2922
|
+
}
|
|
2923
|
+
decodeSwiftServerMessage(encodedMessage) {
|
|
2924
|
+
const decodedSwiftMessage = this.program.coder.types.decode('SwiftServerMessage', encodedMessage);
|
|
2925
|
+
return {
|
|
2926
|
+
slot: decodedSwiftMessage.slot,
|
|
2927
|
+
swiftOrderSignature: decodedSwiftMessage.swiftSignature,
|
|
2928
|
+
};
|
|
2929
|
+
}
|
|
2930
|
+
async signSwiftServerMessage(message) {
|
|
2931
|
+
const swiftServerMessage = Uint8Array.from(this.encodeSwiftServerMessage(message));
|
|
2932
|
+
return await this.signMessage(swiftServerMessage);
|
|
2933
|
+
}
|
|
2934
|
+
async signSwiftOrderParamsMessage(orderParamsMessage) {
|
|
2935
|
+
const takerOrderParamsMessage = Uint8Array.from(this.encodeSwiftOrderParamsMessage(orderParamsMessage));
|
|
2917
2936
|
return await this.signMessage(takerOrderParamsMessage);
|
|
2918
2937
|
}
|
|
2919
|
-
|
|
2938
|
+
encodeSwiftOrderParamsMessage(orderParamsMessage) {
|
|
2920
2939
|
return this.program.coder.types.encode('SwiftOrderParamsMessage', orderParamsMessage);
|
|
2921
2940
|
}
|
|
2922
|
-
|
|
2941
|
+
decodeSwiftOrderParamsMessage(encodedMessage) {
|
|
2923
2942
|
return this.program.coder.types.decode('SwiftOrderParamsMessage', encodedMessage);
|
|
2924
2943
|
}
|
|
2925
|
-
async signMessage(message) {
|
|
2926
|
-
return Buffer.from(await ed.sign(message,
|
|
2927
|
-
}
|
|
2928
|
-
assembleSwiftServerMessage(message, signature, takerPubkey, marketIndex, marketType, slot) {
|
|
2929
|
-
return {
|
|
2930
|
-
message: message.toString('base64'),
|
|
2931
|
-
signature: signature.toString('base64'),
|
|
2932
|
-
taker_pubkey: takerPubkey.toBase58(),
|
|
2933
|
-
market_index: marketIndex,
|
|
2934
|
-
market_type: (0, types_1.isVariant)(marketType, 'perp') ? 'perp' : 'spot',
|
|
2935
|
-
slot: typeof slot === 'number' ? new anchor_1.BN(slot) : slot,
|
|
2936
|
-
};
|
|
2944
|
+
async signMessage(message, keypair = this.wallet.payer) {
|
|
2945
|
+
return Buffer.from(await ed.sign(message, keypair.secretKey.slice(0, 32)));
|
|
2937
2946
|
}
|
|
2938
|
-
async placeSwiftTakerOrder(
|
|
2939
|
-
const ixs = await this.
|
|
2947
|
+
async placeSwiftTakerOrder(swiftServerMessage, swiftSignature, swiftOrderParamsMessage, swiftOrderParamsSignature, marketIndex, takerInfo, txParams) {
|
|
2948
|
+
const ixs = await this.getPlaceSwiftTakerPerpOrderIxs(swiftServerMessage, swiftSignature, swiftOrderParamsMessage, swiftOrderParamsSignature, marketIndex, takerInfo);
|
|
2940
2949
|
const { txSig } = await this.sendTransaction(await this.buildTransaction(ixs, txParams), [], this.opts);
|
|
2941
2950
|
return txSig;
|
|
2942
2951
|
}
|
|
2943
|
-
async
|
|
2952
|
+
async getPlaceSwiftTakerPerpOrderIxs(encodedSwiftServerMessage, swiftSignature, encodedSwiftOrderParamsMessage, swiftOrderParamsSignature, marketIndex, takerInfo) {
|
|
2944
2953
|
const remainingAccounts = this.getRemainingAccounts({
|
|
2945
2954
|
userAccounts: [takerInfo.takerUserAccount],
|
|
2946
2955
|
useMarketLastSlotCache: true,
|
|
2947
2956
|
readablePerpMarketIndex: marketIndex,
|
|
2948
2957
|
});
|
|
2949
|
-
const
|
|
2958
|
+
const swiftServerSignatureIx = web3_js_1.Ed25519Program.createInstructionWithPublicKey({
|
|
2959
|
+
publicKey: new web3_js_1.PublicKey(config_1.SWIFT_ID).toBytes(),
|
|
2960
|
+
signature: Uint8Array.from(swiftSignature),
|
|
2961
|
+
message: Uint8Array.from(encodedSwiftServerMessage),
|
|
2962
|
+
});
|
|
2963
|
+
const swiftOrderParamsSignatureIx = web3_js_1.Ed25519Program.createInstructionWithPublicKey({
|
|
2950
2964
|
publicKey: takerInfo.takerUserAccount.authority.toBytes(),
|
|
2951
|
-
signature: Uint8Array.from(
|
|
2952
|
-
message: Uint8Array.from(
|
|
2965
|
+
signature: Uint8Array.from(swiftOrderParamsSignature),
|
|
2966
|
+
message: Uint8Array.from(encodedSwiftOrderParamsMessage),
|
|
2953
2967
|
});
|
|
2954
|
-
const placeTakerSwiftPerpOrderIx = await this.program.instruction.placeSwiftTakerOrder(
|
|
2968
|
+
const placeTakerSwiftPerpOrderIx = await this.program.instruction.placeSwiftTakerOrder(encodedSwiftServerMessage, encodedSwiftOrderParamsMessage, swiftSignature, {
|
|
2955
2969
|
accounts: {
|
|
2956
2970
|
state: await this.getStatePublicKey(),
|
|
2957
2971
|
user: takerInfo.taker,
|
|
@@ -2961,16 +2975,20 @@ class DriftClient {
|
|
|
2961
2975
|
},
|
|
2962
2976
|
remainingAccounts,
|
|
2963
2977
|
});
|
|
2964
|
-
return [
|
|
2978
|
+
return [
|
|
2979
|
+
swiftServerSignatureIx,
|
|
2980
|
+
swiftOrderParamsSignatureIx,
|
|
2981
|
+
placeTakerSwiftPerpOrderIx,
|
|
2982
|
+
];
|
|
2965
2983
|
}
|
|
2966
|
-
async placeAndMakeSwiftPerpOrder(
|
|
2967
|
-
const ixs = await this.getPlaceAndMakeSwiftPerpOrderIxs(
|
|
2984
|
+
async placeAndMakeSwiftPerpOrder(encodedSwiftMessage, swiftSignature, encodedSwiftOrderParamsMessage, swiftOrderParamsSignature, takerExpectedOrderId, takerInfo, orderParams, referrerInfo, txParams, subAccountId) {
|
|
2985
|
+
const ixs = await this.getPlaceAndMakeSwiftPerpOrderIxs(encodedSwiftMessage, swiftSignature, encodedSwiftOrderParamsMessage, swiftOrderParamsSignature, takerExpectedOrderId, takerInfo, orderParams, referrerInfo, subAccountId);
|
|
2968
2986
|
const { txSig, slot } = await this.sendTransaction(await this.buildTransaction(ixs, txParams), [], this.opts);
|
|
2969
2987
|
this.perpMarketLastSlotCache.set(orderParams.marketIndex, slot);
|
|
2970
2988
|
return txSig;
|
|
2971
2989
|
}
|
|
2972
|
-
async getPlaceAndMakeSwiftPerpOrderIxs(
|
|
2973
|
-
const [
|
|
2990
|
+
async getPlaceAndMakeSwiftPerpOrderIxs(encodedSwiftMessage, swiftSignature, encodedSwiftOrderParamsMessage, swiftOrderParamsSignature, takerExpectedOrderId, takerInfo, orderParams, referrerInfo, subAccountId) {
|
|
2991
|
+
const [swiftServerSignatureIx, swiftOrderSignatureIx, placeTakerSwiftPerpOrderIx,] = await this.getPlaceSwiftTakerPerpOrderIxs(encodedSwiftMessage, swiftSignature, encodedSwiftOrderParamsMessage, swiftOrderParamsSignature, orderParams.marketIndex, takerInfo);
|
|
2974
2992
|
orderParams = (0, orderParams_1.getOrderParams)(orderParams, { marketType: types_1.MarketType.PERP });
|
|
2975
2993
|
const userStatsPublicKey = this.getUserStatsAccountPublicKey();
|
|
2976
2994
|
const user = await this.getUserAccountPublicKey(subAccountId);
|
|
@@ -3005,7 +3023,12 @@ class DriftClient {
|
|
|
3005
3023
|
},
|
|
3006
3024
|
remainingAccounts,
|
|
3007
3025
|
});
|
|
3008
|
-
return [
|
|
3026
|
+
return [
|
|
3027
|
+
swiftServerSignatureIx,
|
|
3028
|
+
swiftOrderSignatureIx,
|
|
3029
|
+
placeTakerSwiftPerpOrderIx,
|
|
3030
|
+
placeAndMakeIx,
|
|
3031
|
+
];
|
|
3009
3032
|
}
|
|
3010
3033
|
async preparePlaceAndTakeSpotOrder(orderParams, fulfillmentConfig, makerInfo, referrerInfo, txParams, subAccountId) {
|
|
3011
3034
|
const tx = await this.buildTransaction(await this.getPlaceAndTakeSpotOrderIx(orderParams, fulfillmentConfig, makerInfo, referrerInfo, subAccountId), txParams);
|
|
@@ -4028,9 +4051,10 @@ class DriftClient {
|
|
|
4028
4051
|
}
|
|
4029
4052
|
return this.receiverProgram;
|
|
4030
4053
|
}
|
|
4031
|
-
getSwitchboardOnDemandProgram() {
|
|
4054
|
+
async getSwitchboardOnDemandProgram() {
|
|
4055
|
+
const idl = (await anchor_30_1.Program.fetchIdl(this.sbOnDemandProgramdId, this.provider));
|
|
4032
4056
|
if (this.sbOnDemandProgram === undefined) {
|
|
4033
|
-
this.sbOnDemandProgram = new anchor_30_1.Program(
|
|
4057
|
+
this.sbOnDemandProgram = new anchor_30_1.Program(idl, this.provider);
|
|
4034
4058
|
}
|
|
4035
4059
|
return this.sbOnDemandProgram;
|
|
4036
4060
|
}
|
|
@@ -4129,7 +4153,7 @@ class DriftClient {
|
|
|
4129
4153
|
});
|
|
4130
4154
|
}
|
|
4131
4155
|
async getPostSwitchboardOnDemandUpdateAtomicIx(feed, numSignatures = 3) {
|
|
4132
|
-
const program = this.getSwitchboardOnDemandProgram();
|
|
4156
|
+
const program = await this.getSwitchboardOnDemandProgram();
|
|
4133
4157
|
const feedAccount = new on_demand_1.PullFeed(program, feed);
|
|
4134
4158
|
if (!this.sbProgramFeedConfigs) {
|
|
4135
4159
|
this.sbProgramFeedConfigs = new Map();
|
|
@@ -4140,8 +4164,6 @@ class DriftClient {
|
|
|
4140
4164
|
}
|
|
4141
4165
|
const [pullIx, _responses, success] = await feedAccount.fetchUpdateIx({
|
|
4142
4166
|
numSignatures,
|
|
4143
|
-
// @ts-ignore :: TODO someone needs to look at this
|
|
4144
|
-
feedConfigs: this.sbProgramFeedConfigs.get(feed.toString()),
|
|
4145
4167
|
});
|
|
4146
4168
|
if (!success) {
|
|
4147
4169
|
return undefined;
|
package/lib/idl/drift.json
CHANGED
|
@@ -554,7 +554,7 @@
|
|
|
554
554
|
}
|
|
555
555
|
},
|
|
556
556
|
{
|
|
557
|
-
"name": "
|
|
557
|
+
"name": "successCondition",
|
|
558
558
|
"type": {
|
|
559
559
|
"option": "u32"
|
|
560
560
|
}
|
|
@@ -644,11 +644,15 @@
|
|
|
644
644
|
],
|
|
645
645
|
"args": [
|
|
646
646
|
{
|
|
647
|
-
"name": "
|
|
647
|
+
"name": "swiftMessageBytes",
|
|
648
648
|
"type": "bytes"
|
|
649
649
|
},
|
|
650
650
|
{
|
|
651
|
-
"name": "
|
|
651
|
+
"name": "swiftOrderParamsMessageBytes",
|
|
652
|
+
"type": "bytes"
|
|
653
|
+
},
|
|
654
|
+
{
|
|
655
|
+
"name": "swiftMessageSignature",
|
|
652
656
|
"type": {
|
|
653
657
|
"array": [
|
|
654
658
|
"u8",
|
|
@@ -8100,6 +8104,27 @@
|
|
|
8100
8104
|
]
|
|
8101
8105
|
}
|
|
8102
8106
|
},
|
|
8107
|
+
{
|
|
8108
|
+
"name": "SwiftServerMessage",
|
|
8109
|
+
"type": {
|
|
8110
|
+
"kind": "struct",
|
|
8111
|
+
"fields": [
|
|
8112
|
+
{
|
|
8113
|
+
"name": "swiftOrderSignature",
|
|
8114
|
+
"type": {
|
|
8115
|
+
"array": [
|
|
8116
|
+
"u8",
|
|
8117
|
+
64
|
|
8118
|
+
]
|
|
8119
|
+
}
|
|
8120
|
+
},
|
|
8121
|
+
{
|
|
8122
|
+
"name": "slot",
|
|
8123
|
+
"type": "u64"
|
|
8124
|
+
}
|
|
8125
|
+
]
|
|
8126
|
+
}
|
|
8127
|
+
},
|
|
8103
8128
|
{
|
|
8104
8129
|
"name": "SwiftOrderParamsMessage",
|
|
8105
8130
|
"type": {
|
|
@@ -8108,27 +8133,47 @@
|
|
|
8108
8133
|
{
|
|
8109
8134
|
"name": "swiftOrderParams",
|
|
8110
8135
|
"type": {
|
|
8111
|
-
"
|
|
8112
|
-
"defined": "OrderParams"
|
|
8113
|
-
}
|
|
8136
|
+
"defined": "OrderParams"
|
|
8114
8137
|
}
|
|
8115
8138
|
},
|
|
8116
8139
|
{
|
|
8117
|
-
"name": "
|
|
8140
|
+
"name": "expectedOrderId",
|
|
8141
|
+
"type": "i32"
|
|
8142
|
+
},
|
|
8143
|
+
{
|
|
8144
|
+
"name": "subAccountId",
|
|
8118
8145
|
"type": "u16"
|
|
8119
8146
|
},
|
|
8120
8147
|
{
|
|
8121
|
-
"name": "
|
|
8148
|
+
"name": "takeProfitOrderParams",
|
|
8122
8149
|
"type": {
|
|
8123
|
-
"
|
|
8150
|
+
"option": {
|
|
8151
|
+
"defined": "SwiftTriggerOrderParams"
|
|
8152
|
+
}
|
|
8124
8153
|
}
|
|
8125
8154
|
},
|
|
8126
8155
|
{
|
|
8127
|
-
"name": "
|
|
8128
|
-
"type":
|
|
8156
|
+
"name": "stopLossOrderParams",
|
|
8157
|
+
"type": {
|
|
8158
|
+
"option": {
|
|
8159
|
+
"defined": "SwiftTriggerOrderParams"
|
|
8160
|
+
}
|
|
8161
|
+
}
|
|
8162
|
+
}
|
|
8163
|
+
]
|
|
8164
|
+
}
|
|
8165
|
+
},
|
|
8166
|
+
{
|
|
8167
|
+
"name": "SwiftTriggerOrderParams",
|
|
8168
|
+
"type": {
|
|
8169
|
+
"kind": "struct",
|
|
8170
|
+
"fields": [
|
|
8171
|
+
{
|
|
8172
|
+
"name": "triggerPrice",
|
|
8173
|
+
"type": "u64"
|
|
8129
8174
|
},
|
|
8130
8175
|
{
|
|
8131
|
-
"name": "
|
|
8176
|
+
"name": "baseAssetAmount",
|
|
8132
8177
|
"type": "u64"
|
|
8133
8178
|
}
|
|
8134
8179
|
]
|
|
@@ -10206,6 +10251,20 @@
|
|
|
10206
10251
|
]
|
|
10207
10252
|
}
|
|
10208
10253
|
},
|
|
10254
|
+
{
|
|
10255
|
+
"name": "PlaceAndTakeOrderSuccessCondition",
|
|
10256
|
+
"type": {
|
|
10257
|
+
"kind": "enum",
|
|
10258
|
+
"variants": [
|
|
10259
|
+
{
|
|
10260
|
+
"name": "PartialFill"
|
|
10261
|
+
},
|
|
10262
|
+
{
|
|
10263
|
+
"name": "FullFill"
|
|
10264
|
+
}
|
|
10265
|
+
]
|
|
10266
|
+
}
|
|
10267
|
+
},
|
|
10209
10268
|
{
|
|
10210
10269
|
"name": "PerpOperation",
|
|
10211
10270
|
"type": {
|
|
@@ -13051,7 +13110,7 @@
|
|
|
13051
13110
|
{
|
|
13052
13111
|
"code": 6286,
|
|
13053
13112
|
"name": "SigVerificationFailed",
|
|
13054
|
-
"msg": "Swift
|
|
13113
|
+
"msg": "Swift message verificaiton failed"
|
|
13055
13114
|
},
|
|
13056
13115
|
{
|
|
13057
13116
|
"code": 6287,
|
|
@@ -13060,8 +13119,13 @@
|
|
|
13060
13119
|
},
|
|
13061
13120
|
{
|
|
13062
13121
|
"code": 6288,
|
|
13063
|
-
"name": "
|
|
13064
|
-
"msg": "Swift
|
|
13122
|
+
"name": "InvalidSwiftOrderParam",
|
|
13123
|
+
"msg": "Swift only available for market/oracle perp orders"
|
|
13124
|
+
},
|
|
13125
|
+
{
|
|
13126
|
+
"code": 6289,
|
|
13127
|
+
"name": "PlaceAndTakeOrderSuccessConditionFailed",
|
|
13128
|
+
"msg": "Place and take order success condition failed"
|
|
13065
13129
|
}
|
|
13066
13130
|
],
|
|
13067
13131
|
"metadata": {
|
package/lib/types.d.ts
CHANGED
|
@@ -1096,12 +1096,19 @@ export declare class ModifyOrderPolicy {
|
|
|
1096
1096
|
};
|
|
1097
1097
|
}
|
|
1098
1098
|
export declare const DefaultOrderParams: OrderParams;
|
|
1099
|
+
export type SwiftServerMessage = {
|
|
1100
|
+
slot: BN;
|
|
1101
|
+
swiftOrderSignature: Uint8Array;
|
|
1102
|
+
};
|
|
1099
1103
|
export type SwiftOrderParamsMessage = {
|
|
1100
|
-
|
|
1101
|
-
swiftOrderParams: OptionalOrderParams[];
|
|
1104
|
+
swiftOrderParams: OptionalOrderParams;
|
|
1102
1105
|
expectedOrderId: number;
|
|
1103
|
-
|
|
1104
|
-
|
|
1106
|
+
takeProfitOrderParams: SwiftTriggerOrderParams | null;
|
|
1107
|
+
stopLossOrderParams: SwiftTriggerOrderParams | null;
|
|
1108
|
+
};
|
|
1109
|
+
export type SwiftTriggerOrderParams = {
|
|
1110
|
+
triggerPrice: BN;
|
|
1111
|
+
baseAssetAmount: BN;
|
|
1105
1112
|
};
|
|
1106
1113
|
export type MakerInfo = {
|
|
1107
1114
|
maker: PublicKey;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drift-labs/sdk",
|
|
3
|
-
"version": "2.96.0-beta.
|
|
3
|
+
"version": "2.96.0-beta.16",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "lib/index.d.ts",
|
|
6
6
|
"author": "crispheaney",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"@pythnetwork/pyth-solana-receiver": "^0.7.0",
|
|
45
45
|
"@solana/spl-token": "0.3.7",
|
|
46
46
|
"@solana/web3.js": "1.92.3",
|
|
47
|
-
"@switchboard-xyz/on-demand": "1.2.
|
|
47
|
+
"@switchboard-xyz/on-demand": "1.2.42",
|
|
48
48
|
"anchor-bankrun": "^0.3.0",
|
|
49
49
|
"node-cache": "^5.1.2",
|
|
50
50
|
"rpc-websockets": "7.5.1",
|
package/src/config.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ConfirmOptions } from '@solana/web3.js';
|
|
2
|
-
import { PerpMarketAccount, SpotMarketAccount } from '.';
|
|
2
|
+
import { PerpMarketAccount, PublicKey, SpotMarketAccount } from '.';
|
|
3
3
|
import {
|
|
4
4
|
DevnetPerpMarkets,
|
|
5
5
|
MainnetPerpMarkets,
|
|
@@ -14,6 +14,10 @@ import {
|
|
|
14
14
|
} from './constants/spotMarkets';
|
|
15
15
|
import { OracleInfo } from './oracles/types';
|
|
16
16
|
import { Program, ProgramAccount } from '@coral-xyz/anchor';
|
|
17
|
+
import {
|
|
18
|
+
ON_DEMAND_DEVNET_PID,
|
|
19
|
+
ON_DEMAND_MAINNET_PID,
|
|
20
|
+
} from '@switchboard-xyz/on-demand';
|
|
17
21
|
|
|
18
22
|
type DriftConfig = {
|
|
19
23
|
ENV: DriftEnv;
|
|
@@ -31,6 +35,7 @@ type DriftConfig = {
|
|
|
31
35
|
MARKET_LOOKUP_TABLE: string;
|
|
32
36
|
SERUM_LOOKUP_TABLE?: string;
|
|
33
37
|
PYTH_PULL_ORACLE_LOOKUP_TABLE?: string;
|
|
38
|
+
SB_ON_DEMAND_PID: PublicKey;
|
|
34
39
|
};
|
|
35
40
|
|
|
36
41
|
export type DriftEnv = 'devnet' | 'mainnet-beta';
|
|
@@ -38,6 +43,7 @@ export type DriftEnv = 'devnet' | 'mainnet-beta';
|
|
|
38
43
|
export const DRIFT_PROGRAM_ID = 'dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH';
|
|
39
44
|
export const DRIFT_ORACLE_RECEIVER_ID =
|
|
40
45
|
'G6EoTTTgpkNBtVXo96EQp2m6uwwVh2Kt6YidjkmQqoha';
|
|
46
|
+
export const SWIFT_ID = 'SW1fThqrxLzVprnCMpiybiqYQfoNCdduC5uWsSUKChS';
|
|
41
47
|
|
|
42
48
|
export const DEFAULT_CONFIRMATION_OPTS: ConfirmOptions = {
|
|
43
49
|
preflightCommitment: 'confirmed',
|
|
@@ -60,6 +66,7 @@ export const configs: { [key in DriftEnv]: DriftConfig } = {
|
|
|
60
66
|
SPOT_MARKETS: DevnetSpotMarkets,
|
|
61
67
|
MARKET_LOOKUP_TABLE: 'FaMS3U4uBojvGn5FSDEPimddcXsCfwkKsFgMVVnDdxGb',
|
|
62
68
|
DRIFT_ORACLE_RECEIVER_ID,
|
|
69
|
+
SB_ON_DEMAND_PID: ON_DEMAND_DEVNET_PID,
|
|
63
70
|
},
|
|
64
71
|
'mainnet-beta': {
|
|
65
72
|
ENV: 'mainnet-beta',
|
|
@@ -77,6 +84,7 @@ export const configs: { [key in DriftEnv]: DriftConfig } = {
|
|
|
77
84
|
MARKET_LOOKUP_TABLE: 'D9cnvzswDikQDf53k4HpQ3KJ9y1Fv3HGGDFYMXnK5T6c',
|
|
78
85
|
SERUM_LOOKUP_TABLE: 'GPZkp76cJtNL2mphCvT6FXkJCVPpouidnacckR6rzKDN',
|
|
79
86
|
DRIFT_ORACLE_RECEIVER_ID,
|
|
87
|
+
SB_ON_DEMAND_PID: ON_DEMAND_MAINNET_PID,
|
|
80
88
|
},
|
|
81
89
|
};
|
|
82
90
|
|
package/src/driftClient.ts
CHANGED
|
@@ -48,6 +48,7 @@ import {
|
|
|
48
48
|
StateAccount,
|
|
49
49
|
SwapReduceOnly,
|
|
50
50
|
SwiftOrderParamsMessage,
|
|
51
|
+
SwiftServerMessage,
|
|
51
52
|
TakerInfo,
|
|
52
53
|
TxParams,
|
|
53
54
|
UserAccount,
|
|
@@ -124,6 +125,7 @@ import {
|
|
|
124
125
|
DRIFT_ORACLE_RECEIVER_ID,
|
|
125
126
|
DEFAULT_CONFIRMATION_OPTS,
|
|
126
127
|
DRIFT_PROGRAM_ID,
|
|
128
|
+
SWIFT_ID,
|
|
127
129
|
} from './config';
|
|
128
130
|
import { WRAPPED_SOL_MINT } from './constants/spotMarkets';
|
|
129
131
|
import { UserStats } from './userStats';
|
|
@@ -160,7 +162,6 @@ import { getFeedIdUint8Array, trimFeedId } from './util/pythPullOracleUtils';
|
|
|
160
162
|
import { isVersionedTransaction } from './tx/utils';
|
|
161
163
|
import pythSolanaReceiverIdl from './idl/pyth_solana_receiver.json';
|
|
162
164
|
import { asV0Tx, PullFeed } from '@switchboard-xyz/on-demand';
|
|
163
|
-
import switchboardOnDemandIdl from './idl/switchboard_on_demand_30.json';
|
|
164
165
|
import * as ed from '@noble/ed25519';
|
|
165
166
|
|
|
166
167
|
type RemainingAccountParams = {
|
|
@@ -213,6 +214,7 @@ export class DriftClient {
|
|
|
213
214
|
|
|
214
215
|
receiverProgram?: Program<PythSolanaReceiver>;
|
|
215
216
|
wormholeProgram?: Program<WormholeCoreBridgeSolana>;
|
|
217
|
+
sbOnDemandProgramdId: PublicKey;
|
|
216
218
|
sbOnDemandProgram?: Program30<Idl30>;
|
|
217
219
|
sbProgramFeedConfigs?: Map<string, any>;
|
|
218
220
|
|
|
@@ -383,6 +385,9 @@ export class DriftClient {
|
|
|
383
385
|
opts: this.opts,
|
|
384
386
|
txHandler: this.txHandler,
|
|
385
387
|
});
|
|
388
|
+
|
|
389
|
+
this.sbOnDemandProgramdId =
|
|
390
|
+
configs[config.env ?? 'mainnet-beta'].SB_ON_DEMAND_PID;
|
|
386
391
|
}
|
|
387
392
|
|
|
388
393
|
public getUserMapKey(subAccountId: number, authority: PublicKey): string {
|
|
@@ -5364,16 +5369,47 @@ export class DriftClient {
|
|
|
5364
5369
|
);
|
|
5365
5370
|
}
|
|
5366
5371
|
|
|
5367
|
-
public
|
|
5372
|
+
public encodeSwiftServerMessage(message: SwiftServerMessage): Buffer {
|
|
5373
|
+
const messageWithBuffer = {
|
|
5374
|
+
slot: message.slot,
|
|
5375
|
+
swiftOrderSignature: message.swiftOrderSignature,
|
|
5376
|
+
};
|
|
5377
|
+
return this.program.coder.types.encode(
|
|
5378
|
+
'SwiftServerMessage',
|
|
5379
|
+
messageWithBuffer
|
|
5380
|
+
);
|
|
5381
|
+
}
|
|
5382
|
+
|
|
5383
|
+
public decodeSwiftServerMessage(encodedMessage: Buffer): SwiftServerMessage {
|
|
5384
|
+
const decodedSwiftMessage = this.program.coder.types.decode(
|
|
5385
|
+
'SwiftServerMessage',
|
|
5386
|
+
encodedMessage
|
|
5387
|
+
);
|
|
5388
|
+
return {
|
|
5389
|
+
slot: decodedSwiftMessage.slot,
|
|
5390
|
+
swiftOrderSignature: decodedSwiftMessage.swiftSignature,
|
|
5391
|
+
};
|
|
5392
|
+
}
|
|
5393
|
+
|
|
5394
|
+
public async signSwiftServerMessage(
|
|
5395
|
+
message: SwiftServerMessage
|
|
5396
|
+
): Promise<Buffer> {
|
|
5397
|
+
const swiftServerMessage = Uint8Array.from(
|
|
5398
|
+
this.encodeSwiftServerMessage(message)
|
|
5399
|
+
);
|
|
5400
|
+
return await this.signMessage(swiftServerMessage);
|
|
5401
|
+
}
|
|
5402
|
+
|
|
5403
|
+
public async signSwiftOrderParamsMessage(
|
|
5368
5404
|
orderParamsMessage: SwiftOrderParamsMessage
|
|
5369
5405
|
): Promise<Buffer> {
|
|
5370
5406
|
const takerOrderParamsMessage = Uint8Array.from(
|
|
5371
|
-
this.
|
|
5407
|
+
this.encodeSwiftOrderParamsMessage(orderParamsMessage)
|
|
5372
5408
|
);
|
|
5373
5409
|
return await this.signMessage(takerOrderParamsMessage);
|
|
5374
5410
|
}
|
|
5375
5411
|
|
|
5376
|
-
public
|
|
5412
|
+
public encodeSwiftOrderParamsMessage(
|
|
5377
5413
|
orderParamsMessage: SwiftOrderParamsMessage
|
|
5378
5414
|
): Buffer {
|
|
5379
5415
|
return this.program.coder.types.encode(
|
|
@@ -5382,7 +5418,7 @@ export class DriftClient {
|
|
|
5382
5418
|
);
|
|
5383
5419
|
}
|
|
5384
5420
|
|
|
5385
|
-
public
|
|
5421
|
+
public decodeSwiftOrderParamsMessage(
|
|
5386
5422
|
encodedMessage: Buffer
|
|
5387
5423
|
): SwiftOrderParamsMessage {
|
|
5388
5424
|
return this.program.coder.types.decode(
|
|
@@ -5391,40 +5427,18 @@ export class DriftClient {
|
|
|
5391
5427
|
);
|
|
5392
5428
|
}
|
|
5393
5429
|
|
|
5394
|
-
public async signMessage(
|
|
5395
|
-
|
|
5396
|
-
|
|
5397
|
-
|
|
5398
|
-
|
|
5399
|
-
|
|
5400
|
-
public assembleSwiftServerMessage(
|
|
5401
|
-
message: Buffer,
|
|
5402
|
-
signature: Buffer,
|
|
5403
|
-
takerPubkey: PublicKey,
|
|
5404
|
-
marketIndex: number,
|
|
5405
|
-
marketType: MarketType,
|
|
5406
|
-
slot: number | BN
|
|
5407
|
-
): {
|
|
5408
|
-
message: string;
|
|
5409
|
-
signature: string;
|
|
5410
|
-
taker_pubkey: string;
|
|
5411
|
-
market_index: number;
|
|
5412
|
-
market_type: 'perp' | 'spot';
|
|
5413
|
-
slot: BN;
|
|
5414
|
-
} {
|
|
5415
|
-
return {
|
|
5416
|
-
message: message.toString('base64'),
|
|
5417
|
-
signature: signature.toString('base64'),
|
|
5418
|
-
taker_pubkey: takerPubkey.toBase58(),
|
|
5419
|
-
market_index: marketIndex,
|
|
5420
|
-
market_type: isVariant(marketType, 'perp') ? 'perp' : 'spot',
|
|
5421
|
-
slot: typeof slot === 'number' ? new BN(slot) : slot,
|
|
5422
|
-
};
|
|
5430
|
+
public async signMessage(
|
|
5431
|
+
message: Uint8Array,
|
|
5432
|
+
keypair: Keypair = this.wallet.payer
|
|
5433
|
+
): Promise<Buffer> {
|
|
5434
|
+
return Buffer.from(await ed.sign(message, keypair.secretKey.slice(0, 32)));
|
|
5423
5435
|
}
|
|
5424
5436
|
|
|
5425
5437
|
public async placeSwiftTakerOrder(
|
|
5426
|
-
|
|
5427
|
-
|
|
5438
|
+
swiftServerMessage: Buffer,
|
|
5439
|
+
swiftSignature: Buffer,
|
|
5440
|
+
swiftOrderParamsMessage: Buffer,
|
|
5441
|
+
swiftOrderParamsSignature: Buffer,
|
|
5428
5442
|
marketIndex: number,
|
|
5429
5443
|
takerInfo: {
|
|
5430
5444
|
taker: PublicKey;
|
|
@@ -5433,9 +5447,11 @@ export class DriftClient {
|
|
|
5433
5447
|
},
|
|
5434
5448
|
txParams?: TxParams
|
|
5435
5449
|
): Promise<TransactionSignature> {
|
|
5436
|
-
const ixs = await this.
|
|
5437
|
-
|
|
5438
|
-
|
|
5450
|
+
const ixs = await this.getPlaceSwiftTakerPerpOrderIxs(
|
|
5451
|
+
swiftServerMessage,
|
|
5452
|
+
swiftSignature,
|
|
5453
|
+
swiftOrderParamsMessage,
|
|
5454
|
+
swiftOrderParamsSignature,
|
|
5439
5455
|
marketIndex,
|
|
5440
5456
|
takerInfo
|
|
5441
5457
|
);
|
|
@@ -5447,9 +5463,11 @@ export class DriftClient {
|
|
|
5447
5463
|
return txSig;
|
|
5448
5464
|
}
|
|
5449
5465
|
|
|
5450
|
-
public async
|
|
5451
|
-
|
|
5452
|
-
|
|
5466
|
+
public async getPlaceSwiftTakerPerpOrderIxs(
|
|
5467
|
+
encodedSwiftServerMessage: Buffer,
|
|
5468
|
+
swiftSignature: Buffer,
|
|
5469
|
+
encodedSwiftOrderParamsMessage: Buffer,
|
|
5470
|
+
swiftOrderParamsSignature: Buffer,
|
|
5453
5471
|
marketIndex: number,
|
|
5454
5472
|
takerInfo: {
|
|
5455
5473
|
taker: PublicKey;
|
|
@@ -5463,16 +5481,25 @@ export class DriftClient {
|
|
|
5463
5481
|
readablePerpMarketIndex: marketIndex,
|
|
5464
5482
|
});
|
|
5465
5483
|
|
|
5466
|
-
const
|
|
5467
|
-
|
|
5468
|
-
|
|
5469
|
-
|
|
5470
|
-
|
|
5484
|
+
const swiftServerSignatureIx =
|
|
5485
|
+
Ed25519Program.createInstructionWithPublicKey({
|
|
5486
|
+
publicKey: new PublicKey(SWIFT_ID).toBytes(),
|
|
5487
|
+
signature: Uint8Array.from(swiftSignature),
|
|
5488
|
+
message: Uint8Array.from(encodedSwiftServerMessage),
|
|
5489
|
+
});
|
|
5490
|
+
|
|
5491
|
+
const swiftOrderParamsSignatureIx =
|
|
5492
|
+
Ed25519Program.createInstructionWithPublicKey({
|
|
5493
|
+
publicKey: takerInfo.takerUserAccount.authority.toBytes(),
|
|
5494
|
+
signature: Uint8Array.from(swiftOrderParamsSignature),
|
|
5495
|
+
message: Uint8Array.from(encodedSwiftOrderParamsMessage),
|
|
5496
|
+
});
|
|
5471
5497
|
|
|
5472
5498
|
const placeTakerSwiftPerpOrderIx =
|
|
5473
5499
|
await this.program.instruction.placeSwiftTakerOrder(
|
|
5474
|
-
|
|
5475
|
-
|
|
5500
|
+
encodedSwiftServerMessage,
|
|
5501
|
+
encodedSwiftOrderParamsMessage,
|
|
5502
|
+
swiftSignature,
|
|
5476
5503
|
{
|
|
5477
5504
|
accounts: {
|
|
5478
5505
|
state: await this.getStatePublicKey(),
|
|
@@ -5485,12 +5512,18 @@ export class DriftClient {
|
|
|
5485
5512
|
}
|
|
5486
5513
|
);
|
|
5487
5514
|
|
|
5488
|
-
return [
|
|
5515
|
+
return [
|
|
5516
|
+
swiftServerSignatureIx,
|
|
5517
|
+
swiftOrderParamsSignatureIx,
|
|
5518
|
+
placeTakerSwiftPerpOrderIx,
|
|
5519
|
+
];
|
|
5489
5520
|
}
|
|
5490
5521
|
|
|
5491
5522
|
public async placeAndMakeSwiftPerpOrder(
|
|
5492
|
-
|
|
5493
|
-
|
|
5523
|
+
encodedSwiftMessage: Buffer,
|
|
5524
|
+
swiftSignature: Buffer,
|
|
5525
|
+
encodedSwiftOrderParamsMessage: Buffer,
|
|
5526
|
+
swiftOrderParamsSignature: Buffer,
|
|
5494
5527
|
takerExpectedOrderId: number,
|
|
5495
5528
|
takerInfo: {
|
|
5496
5529
|
taker: PublicKey;
|
|
@@ -5503,8 +5536,10 @@ export class DriftClient {
|
|
|
5503
5536
|
subAccountId?: number
|
|
5504
5537
|
): Promise<TransactionSignature> {
|
|
5505
5538
|
const ixs = await this.getPlaceAndMakeSwiftPerpOrderIxs(
|
|
5506
|
-
|
|
5507
|
-
|
|
5539
|
+
encodedSwiftMessage,
|
|
5540
|
+
swiftSignature,
|
|
5541
|
+
encodedSwiftOrderParamsMessage,
|
|
5542
|
+
swiftOrderParamsSignature,
|
|
5508
5543
|
takerExpectedOrderId,
|
|
5509
5544
|
takerInfo,
|
|
5510
5545
|
orderParams,
|
|
@@ -5522,8 +5557,10 @@ export class DriftClient {
|
|
|
5522
5557
|
}
|
|
5523
5558
|
|
|
5524
5559
|
public async getPlaceAndMakeSwiftPerpOrderIxs(
|
|
5525
|
-
|
|
5526
|
-
|
|
5560
|
+
encodedSwiftMessage: Buffer,
|
|
5561
|
+
swiftSignature: Buffer,
|
|
5562
|
+
encodedSwiftOrderParamsMessage: Buffer,
|
|
5563
|
+
swiftOrderParamsSignature: Buffer,
|
|
5527
5564
|
takerExpectedOrderId: number,
|
|
5528
5565
|
takerInfo: {
|
|
5529
5566
|
taker: PublicKey;
|
|
@@ -5534,13 +5571,18 @@ export class DriftClient {
|
|
|
5534
5571
|
referrerInfo?: ReferrerInfo,
|
|
5535
5572
|
subAccountId?: number
|
|
5536
5573
|
): Promise<TransactionInstruction[]> {
|
|
5537
|
-
const [
|
|
5538
|
-
|
|
5539
|
-
|
|
5540
|
-
|
|
5541
|
-
|
|
5542
|
-
|
|
5543
|
-
|
|
5574
|
+
const [
|
|
5575
|
+
swiftServerSignatureIx,
|
|
5576
|
+
swiftOrderSignatureIx,
|
|
5577
|
+
placeTakerSwiftPerpOrderIx,
|
|
5578
|
+
] = await this.getPlaceSwiftTakerPerpOrderIxs(
|
|
5579
|
+
encodedSwiftMessage,
|
|
5580
|
+
swiftSignature,
|
|
5581
|
+
encodedSwiftOrderParamsMessage,
|
|
5582
|
+
swiftOrderParamsSignature,
|
|
5583
|
+
orderParams.marketIndex,
|
|
5584
|
+
takerInfo
|
|
5585
|
+
);
|
|
5544
5586
|
|
|
5545
5587
|
orderParams = getOrderParams(orderParams, { marketType: MarketType.PERP });
|
|
5546
5588
|
const userStatsPublicKey = this.getUserStatsAccountPublicKey();
|
|
@@ -5584,7 +5626,12 @@ export class DriftClient {
|
|
|
5584
5626
|
}
|
|
5585
5627
|
);
|
|
5586
5628
|
|
|
5587
|
-
return [
|
|
5629
|
+
return [
|
|
5630
|
+
swiftServerSignatureIx,
|
|
5631
|
+
swiftOrderSignatureIx,
|
|
5632
|
+
placeTakerSwiftPerpOrderIx,
|
|
5633
|
+
placeAndMakeIx,
|
|
5634
|
+
];
|
|
5588
5635
|
}
|
|
5589
5636
|
|
|
5590
5637
|
public async preparePlaceAndTakeSpotOrder(
|
|
@@ -7656,12 +7703,13 @@ export class DriftClient {
|
|
|
7656
7703
|
return this.receiverProgram;
|
|
7657
7704
|
}
|
|
7658
7705
|
|
|
7659
|
-
public getSwitchboardOnDemandProgram(): Program30<Idl30
|
|
7706
|
+
public async getSwitchboardOnDemandProgram(): Promise<Program30<Idl30>> {
|
|
7707
|
+
const idl = (await Program30.fetchIdl(
|
|
7708
|
+
this.sbOnDemandProgramdId,
|
|
7709
|
+
this.provider
|
|
7710
|
+
))!;
|
|
7660
7711
|
if (this.sbOnDemandProgram === undefined) {
|
|
7661
|
-
this.sbOnDemandProgram = new Program30(
|
|
7662
|
-
switchboardOnDemandIdl as Idl30,
|
|
7663
|
-
this.provider
|
|
7664
|
-
);
|
|
7712
|
+
this.sbOnDemandProgram = new Program30(idl, this.provider);
|
|
7665
7713
|
}
|
|
7666
7714
|
return this.sbOnDemandProgram;
|
|
7667
7715
|
}
|
|
@@ -7883,7 +7931,7 @@ export class DriftClient {
|
|
|
7883
7931
|
feed: PublicKey,
|
|
7884
7932
|
numSignatures = 3
|
|
7885
7933
|
): Promise<TransactionInstruction | undefined> {
|
|
7886
|
-
const program = this.getSwitchboardOnDemandProgram();
|
|
7934
|
+
const program = await this.getSwitchboardOnDemandProgram();
|
|
7887
7935
|
const feedAccount = new PullFeed(program, feed);
|
|
7888
7936
|
if (!this.sbProgramFeedConfigs) {
|
|
7889
7937
|
this.sbProgramFeedConfigs = new Map();
|
|
@@ -7895,8 +7943,6 @@ export class DriftClient {
|
|
|
7895
7943
|
|
|
7896
7944
|
const [pullIx, _responses, success] = await feedAccount.fetchUpdateIx({
|
|
7897
7945
|
numSignatures,
|
|
7898
|
-
// @ts-ignore :: TODO someone needs to look at this
|
|
7899
|
-
feedConfigs: this.sbProgramFeedConfigs.get(feed.toString()),
|
|
7900
7946
|
});
|
|
7901
7947
|
if (!success) {
|
|
7902
7948
|
return undefined;
|
package/src/idl/drift.json
CHANGED
|
@@ -554,7 +554,7 @@
|
|
|
554
554
|
}
|
|
555
555
|
},
|
|
556
556
|
{
|
|
557
|
-
"name": "
|
|
557
|
+
"name": "successCondition",
|
|
558
558
|
"type": {
|
|
559
559
|
"option": "u32"
|
|
560
560
|
}
|
|
@@ -644,11 +644,15 @@
|
|
|
644
644
|
],
|
|
645
645
|
"args": [
|
|
646
646
|
{
|
|
647
|
-
"name": "
|
|
647
|
+
"name": "swiftMessageBytes",
|
|
648
648
|
"type": "bytes"
|
|
649
649
|
},
|
|
650
650
|
{
|
|
651
|
-
"name": "
|
|
651
|
+
"name": "swiftOrderParamsMessageBytes",
|
|
652
|
+
"type": "bytes"
|
|
653
|
+
},
|
|
654
|
+
{
|
|
655
|
+
"name": "swiftMessageSignature",
|
|
652
656
|
"type": {
|
|
653
657
|
"array": [
|
|
654
658
|
"u8",
|
|
@@ -8100,6 +8104,27 @@
|
|
|
8100
8104
|
]
|
|
8101
8105
|
}
|
|
8102
8106
|
},
|
|
8107
|
+
{
|
|
8108
|
+
"name": "SwiftServerMessage",
|
|
8109
|
+
"type": {
|
|
8110
|
+
"kind": "struct",
|
|
8111
|
+
"fields": [
|
|
8112
|
+
{
|
|
8113
|
+
"name": "swiftOrderSignature",
|
|
8114
|
+
"type": {
|
|
8115
|
+
"array": [
|
|
8116
|
+
"u8",
|
|
8117
|
+
64
|
|
8118
|
+
]
|
|
8119
|
+
}
|
|
8120
|
+
},
|
|
8121
|
+
{
|
|
8122
|
+
"name": "slot",
|
|
8123
|
+
"type": "u64"
|
|
8124
|
+
}
|
|
8125
|
+
]
|
|
8126
|
+
}
|
|
8127
|
+
},
|
|
8103
8128
|
{
|
|
8104
8129
|
"name": "SwiftOrderParamsMessage",
|
|
8105
8130
|
"type": {
|
|
@@ -8108,27 +8133,47 @@
|
|
|
8108
8133
|
{
|
|
8109
8134
|
"name": "swiftOrderParams",
|
|
8110
8135
|
"type": {
|
|
8111
|
-
"
|
|
8112
|
-
"defined": "OrderParams"
|
|
8113
|
-
}
|
|
8136
|
+
"defined": "OrderParams"
|
|
8114
8137
|
}
|
|
8115
8138
|
},
|
|
8116
8139
|
{
|
|
8117
|
-
"name": "
|
|
8140
|
+
"name": "expectedOrderId",
|
|
8141
|
+
"type": "i32"
|
|
8142
|
+
},
|
|
8143
|
+
{
|
|
8144
|
+
"name": "subAccountId",
|
|
8118
8145
|
"type": "u16"
|
|
8119
8146
|
},
|
|
8120
8147
|
{
|
|
8121
|
-
"name": "
|
|
8148
|
+
"name": "takeProfitOrderParams",
|
|
8122
8149
|
"type": {
|
|
8123
|
-
"
|
|
8150
|
+
"option": {
|
|
8151
|
+
"defined": "SwiftTriggerOrderParams"
|
|
8152
|
+
}
|
|
8124
8153
|
}
|
|
8125
8154
|
},
|
|
8126
8155
|
{
|
|
8127
|
-
"name": "
|
|
8128
|
-
"type":
|
|
8156
|
+
"name": "stopLossOrderParams",
|
|
8157
|
+
"type": {
|
|
8158
|
+
"option": {
|
|
8159
|
+
"defined": "SwiftTriggerOrderParams"
|
|
8160
|
+
}
|
|
8161
|
+
}
|
|
8162
|
+
}
|
|
8163
|
+
]
|
|
8164
|
+
}
|
|
8165
|
+
},
|
|
8166
|
+
{
|
|
8167
|
+
"name": "SwiftTriggerOrderParams",
|
|
8168
|
+
"type": {
|
|
8169
|
+
"kind": "struct",
|
|
8170
|
+
"fields": [
|
|
8171
|
+
{
|
|
8172
|
+
"name": "triggerPrice",
|
|
8173
|
+
"type": "u64"
|
|
8129
8174
|
},
|
|
8130
8175
|
{
|
|
8131
|
-
"name": "
|
|
8176
|
+
"name": "baseAssetAmount",
|
|
8132
8177
|
"type": "u64"
|
|
8133
8178
|
}
|
|
8134
8179
|
]
|
|
@@ -10206,6 +10251,20 @@
|
|
|
10206
10251
|
]
|
|
10207
10252
|
}
|
|
10208
10253
|
},
|
|
10254
|
+
{
|
|
10255
|
+
"name": "PlaceAndTakeOrderSuccessCondition",
|
|
10256
|
+
"type": {
|
|
10257
|
+
"kind": "enum",
|
|
10258
|
+
"variants": [
|
|
10259
|
+
{
|
|
10260
|
+
"name": "PartialFill"
|
|
10261
|
+
},
|
|
10262
|
+
{
|
|
10263
|
+
"name": "FullFill"
|
|
10264
|
+
}
|
|
10265
|
+
]
|
|
10266
|
+
}
|
|
10267
|
+
},
|
|
10209
10268
|
{
|
|
10210
10269
|
"name": "PerpOperation",
|
|
10211
10270
|
"type": {
|
|
@@ -13051,7 +13110,7 @@
|
|
|
13051
13110
|
{
|
|
13052
13111
|
"code": 6286,
|
|
13053
13112
|
"name": "SigVerificationFailed",
|
|
13054
|
-
"msg": "Swift
|
|
13113
|
+
"msg": "Swift message verificaiton failed"
|
|
13055
13114
|
},
|
|
13056
13115
|
{
|
|
13057
13116
|
"code": 6287,
|
|
@@ -13060,8 +13119,13 @@
|
|
|
13060
13119
|
},
|
|
13061
13120
|
{
|
|
13062
13121
|
"code": 6288,
|
|
13063
|
-
"name": "
|
|
13064
|
-
"msg": "Swift
|
|
13122
|
+
"name": "InvalidSwiftOrderParam",
|
|
13123
|
+
"msg": "Swift only available for market/oracle perp orders"
|
|
13124
|
+
},
|
|
13125
|
+
{
|
|
13126
|
+
"code": 6289,
|
|
13127
|
+
"name": "PlaceAndTakeOrderSuccessConditionFailed",
|
|
13128
|
+
"msg": "Place and take order success condition failed"
|
|
13065
13129
|
}
|
|
13066
13130
|
],
|
|
13067
13131
|
"metadata": {
|
package/src/tx/txHandler.ts
CHANGED
package/src/types.ts
CHANGED
|
@@ -1056,12 +1056,21 @@ export const DefaultOrderParams: OrderParams = {
|
|
|
1056
1056
|
auctionEndPrice: null,
|
|
1057
1057
|
};
|
|
1058
1058
|
|
|
1059
|
+
export type SwiftServerMessage = {
|
|
1060
|
+
slot: BN;
|
|
1061
|
+
swiftOrderSignature: Uint8Array;
|
|
1062
|
+
};
|
|
1063
|
+
|
|
1059
1064
|
export type SwiftOrderParamsMessage = {
|
|
1060
|
-
|
|
1061
|
-
swiftOrderParams: OptionalOrderParams[];
|
|
1065
|
+
swiftOrderParams: OptionalOrderParams;
|
|
1062
1066
|
expectedOrderId: number;
|
|
1063
|
-
|
|
1064
|
-
|
|
1067
|
+
takeProfitOrderParams: SwiftTriggerOrderParams | null;
|
|
1068
|
+
stopLossOrderParams: SwiftTriggerOrderParams | null;
|
|
1069
|
+
};
|
|
1070
|
+
|
|
1071
|
+
export type SwiftTriggerOrderParams = {
|
|
1072
|
+
triggerPrice: BN;
|
|
1073
|
+
baseAssetAmount: BN;
|
|
1065
1074
|
};
|
|
1066
1075
|
|
|
1067
1076
|
export type MakerInfo = {
|