@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/admin.js +1 -1
- package/lib/clearingHouse.d.ts +2 -2
- package/lib/clearingHouse.js +6 -4
- package/lib/examples/makeTradeExample.js +1 -1
- package/lib/factory/clearingHouse.js +4 -4
- package/lib/idl/clearing_house.json +286 -286
- package/lib/math/amm.d.ts +1 -0
- package/lib/math/amm.js +16 -3
- package/lib/math/state.d.ts +8 -0
- package/lib/math/state.js +15 -0
- package/lib/mockUSDCFaucet.d.ts +2 -2
- package/lib/mockUSDCFaucet.js +3 -3
- package/lib/oracles/switchboardClient.js +1 -1
- package/lib/tx/defaultTxSender.d.ts +3 -3
- package/lib/tx/defaultTxSender.js +1 -1
- package/lib/tx/retryTxSender.d.ts +3 -3
- package/package.json +2 -2
- package/src/accounts/webSocketAccountSubscriber.ts +3 -3
- package/src/admin.ts +2 -2
- package/src/clearingHouse.ts +12 -6
- package/src/examples/makeTradeExample.ts +6 -2
- package/src/factory/clearingHouse.ts +13 -5
- package/src/idl/clearing_house.json +286 -286
- package/src/math/amm.ts +20 -2
- package/src/math/state.ts +14 -0
- package/src/mockUSDCFaucet.ts +5 -5
- package/src/oracles/switchboardClient.ts +2 -2
- package/src/tx/defaultTxSender.ts +4 -4
- package/src/tx/retryTxSender.ts +3 -3
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
|
-
|
|
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(
|
|
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,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;
|
package/lib/mockUSDCFaucet.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import * as anchor from '@project-serum/anchor';
|
|
3
|
-
import {
|
|
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:
|
|
12
|
+
provider: AnchorProvider;
|
|
13
13
|
opts?: ConfirmOptions;
|
|
14
14
|
constructor(connection: Connection, wallet: IWallet, programId: PublicKey, opts?: ConfirmOptions);
|
|
15
15
|
getMockUSDCFaucetStatePublicKeyAndNonce(): Promise<[
|
package/lib/mockUSDCFaucet.js
CHANGED
|
@@ -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.
|
|
49
|
-
const provider = new anchor_1.
|
|
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.
|
|
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.
|
|
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 {
|
|
3
|
+
import { AnchorProvider } from '@project-serum/anchor';
|
|
4
4
|
export declare class DefaultTxSender implements TxSender {
|
|
5
|
-
provider:
|
|
6
|
-
constructor(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.
|
|
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 {
|
|
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:
|
|
9
|
+
provider: AnchorProvider;
|
|
10
10
|
timeout: number;
|
|
11
11
|
retrySleep: number;
|
|
12
12
|
additionalConnections: Connection[];
|
|
13
|
-
constructor(provider:
|
|
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.
|
|
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.
|
|
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,
|
|
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 =
|
|
37
|
+
opts: ConfirmOptions = AnchorProvider.defaultOptions()
|
|
38
38
|
): Admin {
|
|
39
39
|
const config = getWebSocketClearingHouseConfig(
|
|
40
40
|
connection,
|
package/src/clearingHouse.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BN, Idl, Program
|
|
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:
|
|
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 =
|
|
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
|
|
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.
|
|
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.
|
|
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 {
|
|
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
|
|
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
|
|
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 =
|
|
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 =
|
|
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
|
|
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
|
|
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,
|