@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/src/math/amm.ts CHANGED
@@ -98,6 +98,22 @@ export function calculateAmmReservesAfterSwap(
98
98
  return [newQuoteAssetReserve, newBaseAssetReserve];
99
99
  }
100
100
 
101
+ export function calculateSpread(
102
+ amm: AMM,
103
+ direction: PositionDirection
104
+ ): number {
105
+ let spread;
106
+
107
+ // future logic
108
+ if (isVariant(direction, 'long')) {
109
+ spread = amm.baseSpread;
110
+ } else {
111
+ spread = amm.baseSpread;
112
+ }
113
+
114
+ return spread;
115
+ }
116
+
101
117
  export function calculateSpreadReserves(
102
118
  amm: AMM,
103
119
  direction: PositionDirection
@@ -105,7 +121,9 @@ export function calculateSpreadReserves(
105
121
  baseAssetReserve: BN;
106
122
  quoteAssetReserve: BN;
107
123
  } {
108
- if (amm.baseSpread === 0) {
124
+ const spread = calculateSpread(amm, direction);
125
+
126
+ if (spread === 0) {
109
127
  return {
110
128
  baseAssetReserve: amm.baseAssetReserve,
111
129
  quoteAssetReserve: amm.quoteAssetReserve,
@@ -113,7 +131,7 @@ export function calculateSpreadReserves(
113
131
  }
114
132
 
115
133
  const quoteAsserReserveDelta = amm.quoteAssetReserve.div(
116
- BID_ASK_SPREAD_PRECISION.div(new BN(amm.baseSpread / 4))
134
+ BID_ASK_SPREAD_PRECISION.div(new BN(spread / 4))
117
135
  );
118
136
 
119
137
  let quoteAssetReserve;
@@ -0,0 +1,14 @@
1
+ import { StateAccount } from '../types';
2
+
3
+ /**
4
+ * Get the clearing house percent fee charged on notional of taking trades
5
+ *
6
+ * @param state
7
+ * @returns Precision : basis points (bps)
8
+ */
9
+ export function getExchangeFee(state: StateAccount): number {
10
+ const exchangeFee =
11
+ state.feeStructure.feeNumerator.toNumber() /
12
+ state.feeStructure.feeDenominator.toNumber();
13
+ return exchangeFee;
14
+ }
@@ -1,5 +1,5 @@
1
1
  import * as anchor from '@project-serum/anchor';
2
- import { Idl, Program, Provider } from '@project-serum/anchor';
2
+ import { AnchorProvider, Idl, Program } from '@project-serum/anchor';
3
3
  import {
4
4
  AccountInfo,
5
5
  ASSOCIATED_TOKEN_PROGRAM_ID,
@@ -25,7 +25,7 @@ export class MockUSDCFaucet {
25
25
  connection: Connection;
26
26
  wallet: IWallet;
27
27
  public program: Program;
28
- provider: Provider;
28
+ provider: AnchorProvider;
29
29
  opts?: ConfirmOptions;
30
30
 
31
31
  public constructor(
@@ -36,8 +36,8 @@ export class MockUSDCFaucet {
36
36
  ) {
37
37
  this.connection = connection;
38
38
  this.wallet = wallet;
39
- this.opts = opts || Provider.defaultOptions();
40
- const provider = new Provider(connection, wallet, this.opts);
39
+ this.opts = opts || AnchorProvider.defaultOptions();
40
+ const provider = new AnchorProvider(connection, wallet, this.opts);
41
41
  this.provider = provider;
42
42
  this.program = new Program(mockUSDCFaucetIDL as Idl, programId, provider);
43
43
  }
@@ -140,7 +140,7 @@ export class MockUSDCFaucet {
140
140
  amount
141
141
  );
142
142
  const tx = new Transaction().add(createAssociatedAccountIx).add(mintToTx);
143
- const txSig = await this.program.provider.send(tx, [], this.opts);
143
+ const txSig = await this.program.provider.sendAndConfirm(tx, [], this.opts);
144
144
  return [associatedTokenPublicKey, txSig];
145
145
  }
146
146
 
@@ -4,7 +4,7 @@ import {
4
4
  } from '@switchboard-xyz/switchboard-v2';
5
5
  import { Connection, Keypair, PublicKey } from '@solana/web3.js';
6
6
  import { DriftEnv } from '../config';
7
- import { BN, Provider, Program, Idl } from '@project-serum/anchor';
7
+ import { BN, Program, Idl, AnchorProvider } from '@project-serum/anchor';
8
8
  import { MARK_PRICE_PRECISION, TEN } from '../constants/numericConstants';
9
9
  import { OracleClient, OraclePriceData } from './types';
10
10
  import { Wallet } from '../wallet';
@@ -74,7 +74,7 @@ async function getSwitchboardProgram(
74
74
  const DEFAULT_KEYPAIR = Keypair.fromSeed(new Uint8Array(32).fill(1));
75
75
  const programId = getSwitchboardPid(env);
76
76
  const wallet = new Wallet(DEFAULT_KEYPAIR);
77
- const provider = new Provider(connection, wallet, {});
77
+ const provider = new AnchorProvider(connection, wallet, {});
78
78
 
79
79
  return new Program(switchboardV2Idl as Idl, programId, provider);
80
80
  }
@@ -5,12 +5,12 @@ import {
5
5
  Transaction,
6
6
  TransactionSignature,
7
7
  } from '@solana/web3.js';
8
- import { Provider } from '@project-serum/anchor';
8
+ import { AnchorProvider } from '@project-serum/anchor';
9
9
 
10
10
  export class DefaultTxSender implements TxSender {
11
- provider: Provider;
11
+ provider: AnchorProvider;
12
12
 
13
- public constructor(provider: Provider) {
13
+ public constructor(provider: AnchorProvider) {
14
14
  this.provider = provider;
15
15
  }
16
16
 
@@ -19,6 +19,6 @@ export class DefaultTxSender implements TxSender {
19
19
  additionalSigners?: Array<Signer>,
20
20
  opts?: ConfirmOptions
21
21
  ): Promise<TransactionSignature> {
22
- return this.provider.send(tx, additionalSigners, opts);
22
+ return this.provider.sendAndConfirm(tx, additionalSigners, opts);
23
23
  }
24
24
  }
@@ -10,7 +10,7 @@ import {
10
10
  TransactionSignature,
11
11
  Connection,
12
12
  } from '@solana/web3.js';
13
- import { Provider } from '@project-serum/anchor';
13
+ import { AnchorProvider } from '@project-serum/anchor';
14
14
  import assert from 'assert';
15
15
  import bs58 from 'bs58';
16
16
 
@@ -22,13 +22,13 @@ type ResolveReference = {
22
22
  };
23
23
 
24
24
  export class RetryTxSender implements TxSender {
25
- provider: Provider;
25
+ provider: AnchorProvider;
26
26
  timeout: number;
27
27
  retrySleep: number;
28
28
  additionalConnections: Connection[];
29
29
 
30
30
  public constructor(
31
- provider: Provider,
31
+ provider: AnchorProvider,
32
32
  timeout?: number,
33
33
  retrySleep?: number,
34
34
  additionalConnections = new Array<Connection>()