@drift-labs/sdk 0.1.36-master.1 → 0.1.36-master.4

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/math/amm.d.ts CHANGED
@@ -21,6 +21,7 @@ export declare type AssetType = 'quote' | 'base';
21
21
  * @returns quoteAssetReserve and baseAssetReserve after swap. : Precision AMM_RESERVE_PRECISION
22
22
  */
23
23
  export declare function calculateAmmReservesAfterSwap(amm: Pick<AMM, 'pegMultiplier' | 'quoteAssetReserve' | 'sqrtK' | 'baseAssetReserve'>, inputAssetType: AssetType, swapAmount: BN, swapDirection: SwapDirection): [BN, BN];
24
+ export declare function calculateSpread(amm: AMM, direction: PositionDirection): number;
24
25
  export declare function calculateSpreadReserves(amm: AMM, direction: PositionDirection): {
25
26
  baseAssetReserve: BN;
26
27
  quoteAssetReserve: BN;
package/lib/math/amm.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.calculateQuoteAssetAmountSwapped = exports.calculateBudgetedPeg = exports.calculateBudgetedK = exports.calculateMaxBaseAssetAmountToTrade = exports.calculateTerminalPrice = exports.calculateRepegCost = exports.calculateAdjustKCost = exports.getSwapDirection = exports.calculateSwapOutput = exports.calculateSpreadReserves = exports.calculateAmmReservesAfterSwap = exports.calculatePrice = void 0;
3
+ exports.calculateQuoteAssetAmountSwapped = exports.calculateBudgetedPeg = exports.calculateBudgetedK = exports.calculateMaxBaseAssetAmountToTrade = exports.calculateTerminalPrice = exports.calculateRepegCost = exports.calculateAdjustKCost = exports.getSwapDirection = exports.calculateSwapOutput = exports.calculateSpreadReserves = exports.calculateSpread = exports.calculateAmmReservesAfterSwap = exports.calculatePrice = void 0;
4
4
  const anchor_1 = require("@project-serum/anchor");
5
5
  const numericConstants_1 = require("../constants/numericConstants");
6
6
  const position_1 = require("./position");
@@ -51,14 +51,27 @@ function calculateAmmReservesAfterSwap(amm, inputAssetType, swapAmount, swapDire
51
51
  return [newQuoteAssetReserve, newBaseAssetReserve];
52
52
  }
53
53
  exports.calculateAmmReservesAfterSwap = calculateAmmReservesAfterSwap;
54
+ function calculateSpread(amm, direction) {
55
+ let spread;
56
+ // future logic
57
+ if ((0, types_1.isVariant)(direction, 'long')) {
58
+ spread = amm.baseSpread;
59
+ }
60
+ else {
61
+ spread = amm.baseSpread;
62
+ }
63
+ return spread;
64
+ }
65
+ exports.calculateSpread = calculateSpread;
54
66
  function calculateSpreadReserves(amm, direction) {
55
- if (amm.baseSpread === 0) {
67
+ const spread = calculateSpread(amm, direction);
68
+ if (spread === 0) {
56
69
  return {
57
70
  baseAssetReserve: amm.baseAssetReserve,
58
71
  quoteAssetReserve: amm.quoteAssetReserve,
59
72
  };
60
73
  }
61
- const quoteAsserReserveDelta = amm.quoteAssetReserve.div(numericConstants_1.BID_ASK_SPREAD_PRECISION.div(new anchor_1.BN(amm.baseSpread / 4)));
74
+ const quoteAsserReserveDelta = amm.quoteAssetReserve.div(numericConstants_1.BID_ASK_SPREAD_PRECISION.div(new anchor_1.BN(spread / 4)));
62
75
  let quoteAssetReserve;
63
76
  if ((0, types_1.isVariant)(direction, 'long')) {
64
77
  quoteAssetReserve = amm.quoteAssetReserve.add(quoteAsserReserveDelta);
@@ -0,0 +1,8 @@
1
+ import { StateAccount } from '../types';
2
+ /**
3
+ * Get the clearing house percent fee charged on notional of taking trades
4
+ *
5
+ * @param state
6
+ * @returns Precision : basis points (bps)
7
+ */
8
+ export declare function getExchangeFee(state: StateAccount): number;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getExchangeFee = void 0;
4
+ /**
5
+ * Get the clearing house percent fee charged on notional of taking trades
6
+ *
7
+ * @param state
8
+ * @returns Precision : basis points (bps)
9
+ */
10
+ function getExchangeFee(state) {
11
+ const exchangeFee = state.feeStructure.feeNumerator.toNumber() /
12
+ state.feeStructure.feeDenominator.toNumber();
13
+ return exchangeFee;
14
+ }
15
+ exports.getExchangeFee = getExchangeFee;
@@ -1,6 +1,6 @@
1
1
  /// <reference types="bn.js" />
2
2
  import * as anchor from '@project-serum/anchor';
3
- import { Program, Provider } from '@project-serum/anchor';
3
+ import { AnchorProvider, Program } from '@project-serum/anchor';
4
4
  import { AccountInfo } from '@solana/spl-token';
5
5
  import { ConfirmOptions, Connection, PublicKey, TransactionInstruction, TransactionSignature } from '@solana/web3.js';
6
6
  import { BN } from '.';
@@ -9,7 +9,7 @@ export declare class MockUSDCFaucet {
9
9
  connection: Connection;
10
10
  wallet: IWallet;
11
11
  program: Program;
12
- provider: Provider;
12
+ provider: AnchorProvider;
13
13
  opts?: ConfirmOptions;
14
14
  constructor(connection: Connection, wallet: IWallet, programId: PublicKey, opts?: ConfirmOptions);
15
15
  getMockUSDCFaucetStatePublicKeyAndNonce(): Promise<[
@@ -45,8 +45,8 @@ class MockUSDCFaucet {
45
45
  constructor(connection, wallet, programId, opts) {
46
46
  this.connection = connection;
47
47
  this.wallet = wallet;
48
- this.opts = opts || anchor_1.Provider.defaultOptions();
49
- const provider = new anchor_1.Provider(connection, wallet, this.opts);
48
+ this.opts = opts || anchor_1.AnchorProvider.defaultOptions();
49
+ const provider = new anchor_1.AnchorProvider(connection, wallet, this.opts);
50
50
  this.provider = provider;
51
51
  this.program = new anchor_1.Program(mock_usdc_faucet_json_1.default, programId, provider);
52
52
  }
@@ -117,7 +117,7 @@ class MockUSDCFaucet {
117
117
  return __awaiter(this, void 0, void 0, function* () {
118
118
  const [associatedTokenPublicKey, createAssociatedAccountIx, mintToTx] = yield this.createAssociatedTokenAccountAndMintToInstructions(userPublicKey, amount);
119
119
  const tx = new web3_js_1.Transaction().add(createAssociatedAccountIx).add(mintToTx);
120
- const txSig = yield this.program.provider.send(tx, [], this.opts);
120
+ const txSig = yield this.program.provider.sendAndConfirm(tx, [], this.opts);
121
121
  return [associatedTokenPublicKey, txSig];
122
122
  });
123
123
  }
@@ -64,7 +64,7 @@ function getSwitchboardProgram(env, connection) {
64
64
  const DEFAULT_KEYPAIR = web3_js_1.Keypair.fromSeed(new Uint8Array(32).fill(1));
65
65
  const programId = (0, switchboard_v2_1.getSwitchboardPid)(env);
66
66
  const wallet = new wallet_1.Wallet(DEFAULT_KEYPAIR);
67
- const provider = new anchor_1.Provider(connection, wallet, {});
67
+ const provider = new anchor_1.AnchorProvider(connection, wallet, {});
68
68
  return new anchor_1.Program(switchboard_v2_json_1.default, programId, provider);
69
69
  });
70
70
  }
@@ -1,8 +1,8 @@
1
1
  import { TxSender } from './types';
2
2
  import { ConfirmOptions, Signer, Transaction, TransactionSignature } from '@solana/web3.js';
3
- import { Provider } from '@project-serum/anchor';
3
+ import { AnchorProvider } from '@project-serum/anchor';
4
4
  export declare class DefaultTxSender implements TxSender {
5
- provider: Provider;
6
- constructor(provider: Provider);
5
+ provider: AnchorProvider;
6
+ constructor(provider: AnchorProvider);
7
7
  send(tx: Transaction, additionalSigners?: Array<Signer>, opts?: ConfirmOptions): Promise<TransactionSignature>;
8
8
  }
@@ -6,7 +6,7 @@ class DefaultTxSender {
6
6
  this.provider = provider;
7
7
  }
8
8
  send(tx, additionalSigners, opts) {
9
- return this.provider.send(tx, additionalSigners, opts);
9
+ return this.provider.sendAndConfirm(tx, additionalSigners, opts);
10
10
  }
11
11
  }
12
12
  exports.DefaultTxSender = DefaultTxSender;
@@ -1,16 +1,16 @@
1
1
  /// <reference types="node" />
2
2
  import { TxSender } from './types';
3
3
  import { Commitment, ConfirmOptions, RpcResponseAndContext, Signer, SignatureResult, Transaction, TransactionSignature, Connection } from '@solana/web3.js';
4
- import { Provider } from '@project-serum/anchor';
4
+ import { AnchorProvider } from '@project-serum/anchor';
5
5
  declare type ResolveReference = {
6
6
  resolve?: () => void;
7
7
  };
8
8
  export declare class RetryTxSender implements TxSender {
9
- provider: Provider;
9
+ provider: AnchorProvider;
10
10
  timeout: number;
11
11
  retrySleep: number;
12
12
  additionalConnections: Connection[];
13
- constructor(provider: Provider, timeout?: number, retrySleep?: number, additionalConnections?: Connection[]);
13
+ constructor(provider: AnchorProvider, timeout?: number, retrySleep?: number, additionalConnections?: Connection[]);
14
14
  send(tx: Transaction, additionalSigners?: Array<Signer>, opts?: ConfirmOptions): Promise<TransactionSignature>;
15
15
  prepareTx(tx: Transaction, additionalSigners: Array<Signer>, opts: ConfirmOptions): Promise<Transaction>;
16
16
  confirmTransaction(signature: TransactionSignature, commitment?: Commitment): Promise<RpcResponseAndContext<SignatureResult>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "0.1.36-master.1",
3
+ "version": "0.1.36-master.4",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -28,7 +28,7 @@
28
28
  "access": "public"
29
29
  },
30
30
  "dependencies": {
31
- "@project-serum/anchor": "0.19.1-beta.1",
31
+ "@project-serum/anchor": "0.24.2",
32
32
  "@pythnetwork/client": "^2.5.1",
33
33
  "@solana/spl-token": "^0.1.6",
34
34
  "@solana/web3.js": "^1.22.0",
@@ -1,5 +1,5 @@
1
1
  import { AccountData, AccountSubscriber } from './types';
2
- import { Program } from '@project-serum/anchor';
2
+ import { AnchorProvider, Program } from '@project-serum/anchor';
3
3
  import { AccountInfo, Context, PublicKey } from '@solana/web3.js';
4
4
  import { capitalize } from './utils';
5
5
  import * as Buffer from 'buffer';
@@ -36,7 +36,7 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
36
36
  (accountInfo, context) => {
37
37
  this.handleRpcResponse(context, accountInfo);
38
38
  },
39
- this.program.provider.opts.commitment
39
+ (this.program.provider as AnchorProvider).opts.commitment
40
40
  );
41
41
  }
42
42
 
@@ -44,7 +44,7 @@ export class WebSocketAccountSubscriber<T> implements AccountSubscriber<T> {
44
44
  const rpcResponse =
45
45
  await this.program.provider.connection.getAccountInfoAndContext(
46
46
  this.accountPublicKey,
47
- this.program.provider.opts.commitment
47
+ (this.program.provider as AnchorProvider).opts.commitment
48
48
  );
49
49
  this.handleRpcResponse(rpcResponse.context, rpcResponse?.value);
50
50
  }
package/src/admin.ts CHANGED
@@ -12,7 +12,7 @@ import {
12
12
  OracleSource,
13
13
  OrderFillerRewardStructure,
14
14
  } from './types';
15
- import { BN, Provider } from '@project-serum/anchor';
15
+ import { BN, AnchorProvider } from '@project-serum/anchor';
16
16
  import * as anchor from '@project-serum/anchor';
17
17
  import {
18
18
  getClearingHouseStateAccountPublicKey,
@@ -34,7 +34,7 @@ export class Admin extends ClearingHouse {
34
34
  connection: Connection,
35
35
  wallet: IWallet,
36
36
  clearingHouseProgramId: PublicKey,
37
- opts: ConfirmOptions = Provider.defaultOptions()
37
+ opts: ConfirmOptions = AnchorProvider.defaultOptions()
38
38
  ): Admin {
39
39
  const config = getWebSocketClearingHouseConfig(
40
40
  connection,
@@ -1,4 +1,4 @@
1
- import { BN, Idl, Program, Provider } from '@project-serum/anchor';
1
+ import { AnchorProvider, BN, Idl, Program } from '@project-serum/anchor';
2
2
  import {
3
3
  ASSOCIATED_TOKEN_PROGRAM_ID,
4
4
  Token,
@@ -71,7 +71,7 @@ export class ClearingHouse {
71
71
  connection: Connection;
72
72
  wallet: IWallet;
73
73
  public program: Program;
74
- provider: Provider;
74
+ provider: AnchorProvider;
75
75
  opts?: ConfirmOptions;
76
76
  accountSubscriber: ClearingHouseAccountSubscriber;
77
77
  eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
@@ -98,7 +98,7 @@ export class ClearingHouse {
98
98
  connection: Connection,
99
99
  wallet: IWallet,
100
100
  clearingHouseProgramId: PublicKey,
101
- opts: ConfirmOptions = Provider.defaultOptions()
101
+ opts: ConfirmOptions = AnchorProvider.defaultOptions()
102
102
  ): ClearingHouse {
103
103
  const config = getWebSocketClearingHouseConfig(
104
104
  connection,
@@ -245,7 +245,11 @@ export class ClearingHouse {
245
245
  * @param newWallet
246
246
  */
247
247
  public updateWallet(newWallet: IWallet): void {
248
- const newProvider = new Provider(this.connection, newWallet, this.opts);
248
+ const newProvider = new AnchorProvider(
249
+ this.connection,
250
+ newWallet,
251
+ this.opts
252
+ );
249
253
  const newProgram = new Program(
250
254
  clearingHouseIDL as Idl,
251
255
  this.program.programId,
@@ -503,7 +507,7 @@ export class ClearingHouse {
503
507
  .add(initializeUserOrdersAccountIx)
504
508
  .add(depositCollateralIx);
505
509
 
506
- const txSig = await this.program.provider.send(tx, [userPositionsAccount]);
510
+ const txSig = await this.txSender.send(tx, [userPositionsAccount]);
507
511
 
508
512
  return [txSig, userAccountPublicKey];
509
513
  }
@@ -538,7 +542,9 @@ export class ClearingHouse {
538
542
  .add(initializeUserOrdersAccountIx)
539
543
  .add(depositCollateralIx);
540
544
 
541
- const txSig = await this.program.provider.send(tx, [userPositionsAccount]);
545
+ const txSig = await this.program.provider.sendAndConfirm(tx, [
546
+ userPositionsAccount,
547
+ ]);
542
548
 
543
549
  return [txSig, userAccountPublicKey];
544
550
  }
@@ -1,4 +1,4 @@
1
- import { BN, Provider } from '@project-serum/anchor';
1
+ import { AnchorProvider, BN } from '@project-serum/anchor';
2
2
  import { Wallet } from '..';
3
3
  import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token';
4
4
  import { Connection, Keypair, PublicKey } from '@solana/web3.js';
@@ -43,7 +43,11 @@ const main = async () => {
43
43
  const connection = new Connection(rpcAddress);
44
44
 
45
45
  // Set up the Provider
46
- const provider = new Provider(connection, wallet, Provider.defaultOptions());
46
+ const provider = new AnchorProvider(
47
+ connection,
48
+ wallet,
49
+ AnchorProvider.defaultOptions()
50
+ );
47
51
 
48
52
  // Check SOL Balance
49
53
  const lamportsBalance = await connection.getBalance(wallet.publicKey);
@@ -2,7 +2,7 @@ import { ConfirmOptions, Connection, PublicKey } from '@solana/web3.js';
2
2
  import { IWallet } from '../types';
3
3
  import { BulkAccountLoader } from '../accounts/bulkAccountLoader';
4
4
  import { TxSender } from '../tx/types';
5
- import { Idl, Program, Provider } from '@project-serum/anchor';
5
+ import { AnchorProvider, Idl, Program } from '@project-serum/anchor';
6
6
  import { ClearingHouse } from '../clearingHouse';
7
7
  import clearingHouseIDL from '../idl/clearing_house.json';
8
8
  import { WebSocketClearingHouseAccountSubscriber } from '../accounts/webSocketClearingHouseAccountSubscriber';
@@ -53,7 +53,7 @@ export function getWebSocketClearingHouseConfig(
53
53
  connection: Connection,
54
54
  wallet: IWallet,
55
55
  programID: PublicKey,
56
- opts: ConfirmOptions = Provider.defaultOptions(),
56
+ opts: ConfirmOptions = AnchorProvider.defaultOptions(),
57
57
  txSenderConfig?: TxSenderConfig
58
58
  ): WebSocketClearingHouseConfiguration {
59
59
  return {
@@ -71,7 +71,7 @@ export function getPollingClearingHouseConfig(
71
71
  wallet: IWallet,
72
72
  programID: PublicKey,
73
73
  accountLoader: BulkAccountLoader,
74
- opts: ConfirmOptions = Provider.defaultOptions(),
74
+ opts: ConfirmOptions = AnchorProvider.defaultOptions(),
75
75
  txSenderConfig?: TxSenderConfig
76
76
  ): PollingClearingHouseConfiguration {
77
77
  return {
@@ -86,7 +86,11 @@ export function getPollingClearingHouseConfig(
86
86
  }
87
87
 
88
88
  export function getClearingHouse(config: ClearingHouseConfig): ClearingHouse {
89
- const provider = new Provider(config.connection, config.wallet, config.opts);
89
+ const provider = new AnchorProvider(
90
+ config.connection,
91
+ config.wallet,
92
+ config.opts
93
+ );
90
94
  const program = new Program(
91
95
  clearingHouseIDL as Idl,
92
96
  config.programID,
@@ -126,7 +130,11 @@ export function getClearingHouse(config: ClearingHouseConfig): ClearingHouse {
126
130
  }
127
131
 
128
132
  export function getAdmin(config: ClearingHouseConfig): Admin {
129
- const provider = new Provider(config.connection, config.wallet, config.opts);
133
+ const provider = new AnchorProvider(
134
+ config.connection,
135
+ config.wallet,
136
+ config.opts
137
+ );
130
138
  const program = new Program(
131
139
  clearingHouseIDL as Idl,
132
140
  config.programID,