@drift-labs/sdk 0.2.0-temp.0 → 0.2.0-temp.1
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.d.ts +7 -5
- package/lib/admin.js +34 -11
- package/lib/clearingHouse.d.ts +23 -9
- package/lib/clearingHouse.js +335 -95
- package/lib/clearingHouseUser.d.ts +2 -2
- package/lib/clearingHouseUser.js +8 -17
- package/lib/config.js +1 -1
- package/lib/constants/banks.js +9 -2
- package/lib/constants/numericConstants.d.ts +1 -0
- package/lib/constants/numericConstants.js +2 -1
- package/lib/idl/clearing_house.json +689 -153
- package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/lib/index.d.ts +3 -2
- package/lib/index.js +7 -2
- package/lib/math/amm.js +7 -35
- package/lib/math/auction.js +4 -1
- package/lib/math/orders.d.ts +2 -2
- package/lib/math/orders.js +19 -2
- package/lib/math/position.js +3 -1
- package/lib/math/trade.d.ts +1 -1
- package/lib/math/trade.js +7 -10
- package/lib/orderParams.d.ts +14 -5
- package/lib/orderParams.js +8 -96
- package/lib/orders.js +1 -1
- package/lib/slot/SlotSubscriber.d.ts +7 -0
- package/lib/slot/SlotSubscriber.js +3 -0
- package/lib/{mockUSDCFaucet.d.ts → tokenFaucet.d.ts} +7 -5
- package/lib/{mockUSDCFaucet.js → tokenFaucet.js} +41 -40
- package/lib/tx/utils.js +1 -1
- package/lib/types.d.ts +132 -14
- package/lib/types.js +52 -1
- package/lib/util/computeUnits.js +1 -1
- package/package.json +3 -3
- package/src/accounts/bulkAccountLoader.js +197 -0
- package/src/accounts/bulkUserSubscription.js +33 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +311 -0
- package/src/accounts/pollingOracleSubscriber.js +93 -0
- package/src/accounts/pollingTokenAccountSubscriber.js +90 -0
- package/src/accounts/pollingUserAccountSubscriber.js +132 -0
- package/src/accounts/types.js +10 -0
- package/src/accounts/utils.js +7 -0
- package/src/accounts/webSocketAccountSubscriber.js +93 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +233 -0
- package/src/accounts/webSocketUserAccountSubscriber.js +62 -0
- package/src/addresses/marketAddresses.js +26 -0
- package/src/admin.js +517 -0
- package/src/admin.ts +53 -14
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +510 -131
- package/src/clearingHouseConfig.js +2 -0
- package/src/clearingHouseUser.ts +12 -23
- package/src/clearingHouseUserConfig.js +2 -0
- package/src/config.js +67 -0
- package/src/config.ts +1 -1
- package/src/constants/banks.js +42 -0
- package/src/constants/banks.ts +9 -2
- package/src/constants/markets.js +42 -0
- package/src/constants/numericConstants.js +41 -0
- package/src/constants/numericConstants.ts +1 -0
- package/src/events/eventList.js +77 -0
- package/src/events/eventSubscriber.js +139 -0
- package/src/events/fetchLogs.js +50 -0
- package/src/events/pollingLogProvider.js +64 -0
- package/src/events/sort.js +44 -0
- package/src/events/txEventCache.js +71 -0
- package/src/events/types.js +20 -0
- package/src/events/webSocketLogProvider.js +41 -0
- package/src/examples/makeTradeExample.js +80 -0
- package/src/factory/bigNum.js +390 -0
- package/src/factory/oracleClient.js +20 -0
- package/src/idl/clearing_house.json +689 -153
- package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/src/index.js +69 -0
- package/src/index.ts +3 -2
- package/src/math/amm.js +369 -0
- package/src/math/amm.ts +19 -49
- package/src/math/auction.js +42 -0
- package/src/math/auction.ts +5 -1
- package/src/math/conversion.js +11 -0
- package/src/math/funding.js +248 -0
- package/src/math/oracles.js +26 -0
- package/src/math/orders.ts +17 -3
- package/src/math/position.ts +5 -1
- package/src/math/repeg.js +128 -0
- package/src/math/state.js +15 -0
- package/src/math/trade.js +253 -0
- package/src/math/trade.ts +23 -25
- package/src/math/utils.js +0 -1
- package/src/mockUSDCFaucet.js +280 -0
- package/src/oracles/oracleClientCache.js +19 -0
- package/src/oracles/pythClient.js +46 -0
- package/src/oracles/quoteAssetOracleClient.js +32 -0
- package/src/oracles/switchboardClient.js +69 -0
- package/src/oracles/types.js +2 -0
- package/src/orderParams.js +20 -0
- package/src/orderParams.ts +20 -141
- package/src/orders.ts +2 -1
- package/src/slot/SlotSubscriber.js +39 -0
- package/src/slot/SlotSubscriber.ts +11 -1
- package/src/token/index.js +38 -0
- package/src/tokenFaucet.js +189 -0
- package/src/{mockUSDCFaucet.ts → tokenFaucet.ts} +48 -59
- package/src/tx/types.js +2 -0
- package/src/tx/utils.js +17 -0
- package/src/tx/utils.ts +1 -1
- package/src/types.js +125 -0
- package/src/types.ts +128 -15
- package/src/userName.js +20 -0
- package/src/util/computeUnits.js +21 -11
- package/src/util/computeUnits.ts +1 -1
- package/src/util/getTokenAddress.js +9 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/src/wallet.js +35 -0
- package/src/util/computeUnits.js.map +0 -1
|
@@ -3,7 +3,6 @@ import { AnchorProvider, Idl, Program } from '@project-serum/anchor';
|
|
|
3
3
|
import {
|
|
4
4
|
AccountInfo,
|
|
5
5
|
ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
6
|
-
MintLayout,
|
|
7
6
|
Token,
|
|
8
7
|
TOKEN_PROGRAM_ID,
|
|
9
8
|
} from '@solana/spl-token';
|
|
@@ -11,27 +10,28 @@ import {
|
|
|
11
10
|
ConfirmOptions,
|
|
12
11
|
Connection,
|
|
13
12
|
PublicKey,
|
|
14
|
-
SystemProgram,
|
|
15
13
|
SYSVAR_RENT_PUBKEY,
|
|
16
14
|
Transaction,
|
|
17
15
|
TransactionInstruction,
|
|
18
16
|
TransactionSignature,
|
|
19
17
|
} from '@solana/web3.js';
|
|
20
18
|
import { BN } from '.';
|
|
21
|
-
import
|
|
19
|
+
import tokenFaucet from './idl/token_faucet.json';
|
|
22
20
|
import { IWallet } from './types';
|
|
23
21
|
|
|
24
|
-
export class
|
|
22
|
+
export class TokenFaucet {
|
|
25
23
|
connection: Connection;
|
|
26
24
|
wallet: IWallet;
|
|
27
25
|
public program: Program;
|
|
28
26
|
provider: AnchorProvider;
|
|
27
|
+
mint: PublicKey;
|
|
29
28
|
opts?: ConfirmOptions;
|
|
30
29
|
|
|
31
30
|
public constructor(
|
|
32
31
|
connection: Connection,
|
|
33
32
|
wallet: IWallet,
|
|
34
33
|
programId: PublicKey,
|
|
34
|
+
mint: PublicKey,
|
|
35
35
|
opts?: ConfirmOptions
|
|
36
36
|
) {
|
|
37
37
|
this.connection = connection;
|
|
@@ -39,78 +39,56 @@ export class MockUSDCFaucet {
|
|
|
39
39
|
this.opts = opts || AnchorProvider.defaultOptions();
|
|
40
40
|
const provider = new AnchorProvider(connection, wallet, this.opts);
|
|
41
41
|
this.provider = provider;
|
|
42
|
-
this.program = new Program(
|
|
42
|
+
this.program = new Program(tokenFaucet as Idl, programId, provider);
|
|
43
|
+
this.mint = mint;
|
|
43
44
|
}
|
|
44
45
|
|
|
45
|
-
public async
|
|
46
|
+
public async getFaucetConfigPublicKeyAndNonce(): Promise<
|
|
46
47
|
[PublicKey, number]
|
|
47
48
|
> {
|
|
48
49
|
return anchor.web3.PublicKey.findProgramAddress(
|
|
49
|
-
[
|
|
50
|
+
[
|
|
51
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('faucet_config')),
|
|
52
|
+
this.mint.toBuffer(),
|
|
53
|
+
],
|
|
50
54
|
this.program.programId
|
|
51
55
|
);
|
|
52
56
|
}
|
|
53
57
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
public async getMintAuthority(): Promise<PublicKey> {
|
|
59
|
+
return (
|
|
60
|
+
await anchor.web3.PublicKey.findProgramAddress(
|
|
61
|
+
[
|
|
62
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('mint_authority')),
|
|
63
|
+
this.mint.toBuffer(),
|
|
64
|
+
],
|
|
65
|
+
this.program.programId
|
|
66
|
+
)
|
|
61
67
|
)[0];
|
|
62
|
-
return this.mockUSDCFaucetStatePublicKey;
|
|
63
68
|
}
|
|
64
69
|
|
|
65
|
-
public async
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
);
|
|
69
|
-
if (stateAccountRPCResponse.value !== null) {
|
|
70
|
-
throw new Error('Faucet already initialized');
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const fakeUSDCMint = anchor.web3.Keypair.generate();
|
|
74
|
-
const createUSDCMintAccountIx = SystemProgram.createAccount({
|
|
75
|
-
fromPubkey: this.wallet.publicKey,
|
|
76
|
-
newAccountPubkey: fakeUSDCMint.publicKey,
|
|
77
|
-
lamports: await Token.getMinBalanceRentForExemptMint(this.connection),
|
|
78
|
-
space: MintLayout.span,
|
|
79
|
-
programId: TOKEN_PROGRAM_ID,
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
const [mintAuthority, _mintAuthorityNonce] =
|
|
83
|
-
await PublicKey.findProgramAddress(
|
|
84
|
-
[fakeUSDCMint.publicKey.toBuffer()],
|
|
85
|
-
this.program.programId
|
|
86
|
-
);
|
|
87
|
-
|
|
88
|
-
const initUSDCMintIx = Token.createInitMintInstruction(
|
|
89
|
-
TOKEN_PROGRAM_ID,
|
|
90
|
-
fakeUSDCMint.publicKey,
|
|
91
|
-
6,
|
|
92
|
-
mintAuthority,
|
|
93
|
-
null
|
|
94
|
-
);
|
|
70
|
+
public async getFaucetConfigPublicKey(): Promise<PublicKey> {
|
|
71
|
+
return (await this.getFaucetConfigPublicKeyAndNonce())[0];
|
|
72
|
+
}
|
|
95
73
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
74
|
+
public async initialize(): Promise<TransactionSignature> {
|
|
75
|
+
const [faucetConfigPublicKey] =
|
|
76
|
+
await this.getFaucetConfigPublicKeyAndNonce();
|
|
77
|
+
return await this.program.rpc.initialize({
|
|
99
78
|
accounts: {
|
|
100
|
-
|
|
79
|
+
faucetConfig: faucetConfigPublicKey,
|
|
101
80
|
admin: this.wallet.publicKey,
|
|
102
|
-
mintAccount:
|
|
81
|
+
mintAccount: this.mint,
|
|
103
82
|
rent: SYSVAR_RENT_PUBKEY,
|
|
104
83
|
systemProgram: anchor.web3.SystemProgram.programId,
|
|
84
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
105
85
|
},
|
|
106
|
-
instructions: [createUSDCMintAccountIx, initUSDCMintIx],
|
|
107
|
-
signers: [fakeUSDCMint],
|
|
108
86
|
});
|
|
109
87
|
}
|
|
110
88
|
|
|
111
89
|
public async fetchState(): Promise<any> {
|
|
112
|
-
return await this.program.account.
|
|
113
|
-
await this.
|
|
90
|
+
return await this.program.account.faucetConfig.fetch(
|
|
91
|
+
await this.getFaucetConfigPublicKey()
|
|
114
92
|
);
|
|
115
93
|
}
|
|
116
94
|
|
|
@@ -118,18 +96,29 @@ export class MockUSDCFaucet {
|
|
|
118
96
|
userTokenAccount: PublicKey,
|
|
119
97
|
amount: BN
|
|
120
98
|
): Promise<TransactionSignature> {
|
|
121
|
-
const state: any = await this.fetchState();
|
|
122
99
|
return await this.program.rpc.mintToUser(amount, {
|
|
123
100
|
accounts: {
|
|
124
|
-
|
|
125
|
-
mintAccount:
|
|
101
|
+
faucetConfig: await this.getFaucetConfigPublicKey(),
|
|
102
|
+
mintAccount: this.mint,
|
|
126
103
|
userTokenAccount,
|
|
127
|
-
mintAuthority:
|
|
104
|
+
mintAuthority: await this.getMintAuthority(),
|
|
128
105
|
tokenProgram: TOKEN_PROGRAM_ID,
|
|
129
106
|
},
|
|
130
107
|
});
|
|
131
108
|
}
|
|
132
109
|
|
|
110
|
+
public async transferMintAuthority(): Promise<TransactionSignature> {
|
|
111
|
+
return await this.program.rpc.transferMintAuthority({
|
|
112
|
+
accounts: {
|
|
113
|
+
faucetConfig: await this.getFaucetConfigPublicKey(),
|
|
114
|
+
mintAccount: this.mint,
|
|
115
|
+
mintAuthority: await this.getMintAuthority(),
|
|
116
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
117
|
+
admin: this.wallet.publicKey,
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
133
122
|
public async createAssociatedTokenAccountAndMintTo(
|
|
134
123
|
userPublicKey: PublicKey,
|
|
135
124
|
amount: BN
|
|
@@ -166,7 +155,7 @@ export class MockUSDCFaucet {
|
|
|
166
155
|
|
|
167
156
|
const mintToIx = await this.program.instruction.mintToUser(amount, {
|
|
168
157
|
accounts: {
|
|
169
|
-
|
|
158
|
+
faucetConfig: await this.getFaucetConfigPublicKey(),
|
|
170
159
|
mintAccount: state.mint,
|
|
171
160
|
userTokenAccount: associateTokenPublicKey,
|
|
172
161
|
mintAuthority: state.mintAuthority,
|
package/src/tx/types.js
ADDED
package/src/tx/utils.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.wrapInTx = void 0;
|
|
4
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
5
|
+
const COMPUTE_UNITS_DEFAULT = 200000;
|
|
6
|
+
function wrapInTx(instruction, computeUnits = 600000 // TODO, requires less code change
|
|
7
|
+
) {
|
|
8
|
+
const tx = new web3_js_1.Transaction();
|
|
9
|
+
if (computeUnits != COMPUTE_UNITS_DEFAULT) {
|
|
10
|
+
tx.add(web3_js_1.ComputeBudgetProgram.requestUnits({
|
|
11
|
+
units: computeUnits,
|
|
12
|
+
additionalFee: 0,
|
|
13
|
+
}));
|
|
14
|
+
}
|
|
15
|
+
return tx.add(instruction);
|
|
16
|
+
}
|
|
17
|
+
exports.wrapInTx = wrapInTx;
|
package/src/tx/utils.ts
CHANGED
|
@@ -8,7 +8,7 @@ const COMPUTE_UNITS_DEFAULT = 200_000;
|
|
|
8
8
|
|
|
9
9
|
export function wrapInTx(
|
|
10
10
|
instruction: TransactionInstruction,
|
|
11
|
-
computeUnits =
|
|
11
|
+
computeUnits = 600_000 // TODO, requires less code change
|
|
12
12
|
): Transaction {
|
|
13
13
|
const tx = new Transaction();
|
|
14
14
|
if (computeUnits != COMPUTE_UNITS_DEFAULT) {
|
package/src/types.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DefaultOrderParams = exports.LiquidationType = exports.TradeSide = exports.isOneOfVariant = exports.isVariant = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderDiscountTier = exports.OrderStatus = exports.OrderType = exports.OracleSource = exports.DepositDirection = exports.PositionDirection = exports.BankBalanceType = exports.SwapDirection = void 0;
|
|
4
|
+
const _1 = require(".");
|
|
5
|
+
// # Utility Types / Enums / Constants
|
|
6
|
+
class SwapDirection {
|
|
7
|
+
}
|
|
8
|
+
exports.SwapDirection = SwapDirection;
|
|
9
|
+
SwapDirection.ADD = { add: {} };
|
|
10
|
+
SwapDirection.REMOVE = { remove: {} };
|
|
11
|
+
class BankBalanceType {
|
|
12
|
+
}
|
|
13
|
+
exports.BankBalanceType = BankBalanceType;
|
|
14
|
+
BankBalanceType.DEPOSIT = { deposit: {} };
|
|
15
|
+
BankBalanceType.BORROW = { borrow: {} };
|
|
16
|
+
class PositionDirection {
|
|
17
|
+
}
|
|
18
|
+
exports.PositionDirection = PositionDirection;
|
|
19
|
+
PositionDirection.LONG = { long: {} };
|
|
20
|
+
PositionDirection.SHORT = { short: {} };
|
|
21
|
+
class DepositDirection {
|
|
22
|
+
}
|
|
23
|
+
exports.DepositDirection = DepositDirection;
|
|
24
|
+
DepositDirection.DEPOSIT = { deposit: {} };
|
|
25
|
+
DepositDirection.WITHDRAW = { withdraw: {} };
|
|
26
|
+
class OracleSource {
|
|
27
|
+
}
|
|
28
|
+
exports.OracleSource = OracleSource;
|
|
29
|
+
OracleSource.PYTH = { pyth: {} };
|
|
30
|
+
OracleSource.SWITCHBOARD = { switchboard: {} };
|
|
31
|
+
OracleSource.QUOTE_ASSET = { quoteAsset: {} };
|
|
32
|
+
class OrderType {
|
|
33
|
+
}
|
|
34
|
+
exports.OrderType = OrderType;
|
|
35
|
+
OrderType.LIMIT = { limit: {} };
|
|
36
|
+
OrderType.TRIGGER_MARKET = { triggerMarket: {} };
|
|
37
|
+
OrderType.TRIGGER_LIMIT = { triggerLimit: {} };
|
|
38
|
+
OrderType.MARKET = { market: {} };
|
|
39
|
+
class OrderStatus {
|
|
40
|
+
}
|
|
41
|
+
exports.OrderStatus = OrderStatus;
|
|
42
|
+
OrderStatus.INIT = { init: {} };
|
|
43
|
+
OrderStatus.OPEN = { open: {} };
|
|
44
|
+
class OrderDiscountTier {
|
|
45
|
+
}
|
|
46
|
+
exports.OrderDiscountTier = OrderDiscountTier;
|
|
47
|
+
OrderDiscountTier.NONE = { none: {} };
|
|
48
|
+
OrderDiscountTier.FIRST = { first: {} };
|
|
49
|
+
OrderDiscountTier.SECOND = { second: {} };
|
|
50
|
+
OrderDiscountTier.THIRD = { third: {} };
|
|
51
|
+
OrderDiscountTier.FOURTH = { fourth: {} };
|
|
52
|
+
class OrderAction {
|
|
53
|
+
}
|
|
54
|
+
exports.OrderAction = OrderAction;
|
|
55
|
+
OrderAction.PLACE = { place: {} };
|
|
56
|
+
OrderAction.CANCEL = { cancel: {} };
|
|
57
|
+
OrderAction.EXPIRE = { expire: {} };
|
|
58
|
+
OrderAction.FILL = { fill: {} };
|
|
59
|
+
OrderAction.TRIGGER = { trigger: {} };
|
|
60
|
+
class OrderActionExplanation {
|
|
61
|
+
}
|
|
62
|
+
exports.OrderActionExplanation = OrderActionExplanation;
|
|
63
|
+
OrderActionExplanation.NONE = { none: {} };
|
|
64
|
+
OrderActionExplanation.BREACHED_MARGIN_REQUIREMENT = {
|
|
65
|
+
breachedMarginRequirement: {},
|
|
66
|
+
};
|
|
67
|
+
OrderActionExplanation.ORACLE_PRICE_BREACHED_LIMIT_PRICE = {
|
|
68
|
+
oraclePriceBreachedLimitPrice: {},
|
|
69
|
+
};
|
|
70
|
+
OrderActionExplanation.MARKET_ORDER_FILLED_TO_LIMIT_PRICE = {
|
|
71
|
+
marketOrderFilledToLimitPrice: {},
|
|
72
|
+
};
|
|
73
|
+
class OrderTriggerCondition {
|
|
74
|
+
}
|
|
75
|
+
exports.OrderTriggerCondition = OrderTriggerCondition;
|
|
76
|
+
OrderTriggerCondition.ABOVE = { above: {} };
|
|
77
|
+
OrderTriggerCondition.BELOW = { below: {} };
|
|
78
|
+
function isVariant(object, type) {
|
|
79
|
+
return object.hasOwnProperty(type);
|
|
80
|
+
}
|
|
81
|
+
exports.isVariant = isVariant;
|
|
82
|
+
function isOneOfVariant(object, types) {
|
|
83
|
+
return types.reduce((result, type) => {
|
|
84
|
+
return result || object.hasOwnProperty(type);
|
|
85
|
+
}, false);
|
|
86
|
+
}
|
|
87
|
+
exports.isOneOfVariant = isOneOfVariant;
|
|
88
|
+
var TradeSide;
|
|
89
|
+
(function (TradeSide) {
|
|
90
|
+
TradeSide[TradeSide["None"] = 0] = "None";
|
|
91
|
+
TradeSide[TradeSide["Buy"] = 1] = "Buy";
|
|
92
|
+
TradeSide[TradeSide["Sell"] = 2] = "Sell";
|
|
93
|
+
})(TradeSide = exports.TradeSide || (exports.TradeSide = {}));
|
|
94
|
+
class LiquidationType {
|
|
95
|
+
}
|
|
96
|
+
exports.LiquidationType = LiquidationType;
|
|
97
|
+
LiquidationType.LIQUIDATE_PERP = { liquidatePerp: {} };
|
|
98
|
+
LiquidationType.LIQUIDATE_BORROW = { liquidateBorrow: {} };
|
|
99
|
+
LiquidationType.LIQUIDATE_BORROW_FOR_PERP_PNL = {
|
|
100
|
+
liquidateBorrowForPerpPnl: {},
|
|
101
|
+
};
|
|
102
|
+
LiquidationType.LIQUIDATE_PERP_PNL_FOR_DEPOSIT = {
|
|
103
|
+
liquidatePerpPnlForDeposit: {},
|
|
104
|
+
};
|
|
105
|
+
exports.DefaultOrderParams = {
|
|
106
|
+
orderType: OrderType.MARKET,
|
|
107
|
+
userOrderId: 0,
|
|
108
|
+
direction: PositionDirection.LONG,
|
|
109
|
+
baseAssetAmount: _1.ZERO,
|
|
110
|
+
price: _1.ZERO,
|
|
111
|
+
marketIndex: _1.ZERO,
|
|
112
|
+
reduceOnly: false,
|
|
113
|
+
postOnly: false,
|
|
114
|
+
immediateOrCancel: false,
|
|
115
|
+
triggerPrice: _1.ZERO,
|
|
116
|
+
triggerCondition: OrderTriggerCondition.ABOVE,
|
|
117
|
+
positionLimit: _1.ZERO,
|
|
118
|
+
oraclePriceOffset: _1.ZERO,
|
|
119
|
+
padding0: _1.ZERO,
|
|
120
|
+
padding1: _1.ZERO,
|
|
121
|
+
optionalAccounts: {
|
|
122
|
+
discountToken: false,
|
|
123
|
+
referrer: false,
|
|
124
|
+
},
|
|
125
|
+
};
|
package/src/types.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { PublicKey, Transaction } from '@solana/web3.js';
|
|
2
|
-
import { BN } from '.';
|
|
2
|
+
import { BN, ZERO } from '.';
|
|
3
3
|
|
|
4
4
|
// # Utility Types / Enums / Constants
|
|
5
5
|
export class SwapDirection {
|
|
@@ -17,6 +17,11 @@ export class PositionDirection {
|
|
|
17
17
|
static readonly SHORT = { short: {} };
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
export class DepositDirection {
|
|
21
|
+
static readonly DEPOSIT = { deposit: {} };
|
|
22
|
+
static readonly WITHDRAW = { withdraw: {} };
|
|
23
|
+
}
|
|
24
|
+
|
|
20
25
|
export class OracleSource {
|
|
21
26
|
static readonly PYTH = { pyth: {} };
|
|
22
27
|
static readonly SWITCHBOARD = { switchboard: {} };
|
|
@@ -51,6 +56,19 @@ export class OrderAction {
|
|
|
51
56
|
static readonly TRIGGER = { trigger: {} };
|
|
52
57
|
}
|
|
53
58
|
|
|
59
|
+
export class OrderActionExplanation {
|
|
60
|
+
static readonly NONE = { none: {} };
|
|
61
|
+
static readonly BREACHED_MARGIN_REQUIREMENT = {
|
|
62
|
+
breachedMarginRequirement: {},
|
|
63
|
+
};
|
|
64
|
+
static readonly ORACLE_PRICE_BREACHED_LIMIT_PRICE = {
|
|
65
|
+
oraclePriceBreachedLimitPrice: {},
|
|
66
|
+
};
|
|
67
|
+
static readonly MARKET_ORDER_FILLED_TO_LIMIT_PRICE = {
|
|
68
|
+
marketOrderFilledToLimitPrice: {},
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
54
72
|
export class OrderTriggerCondition {
|
|
55
73
|
static readonly ABOVE = { above: {} };
|
|
56
74
|
static readonly BELOW = { below: {} };
|
|
@@ -92,6 +110,7 @@ export type DepositRecord = {
|
|
|
92
110
|
};
|
|
93
111
|
bankIndex: BN;
|
|
94
112
|
amount: BN;
|
|
113
|
+
oraclePrice: BN;
|
|
95
114
|
from?: PublicKey;
|
|
96
115
|
to?: PublicKey;
|
|
97
116
|
};
|
|
@@ -142,20 +161,67 @@ export type FundingPaymentRecord = {
|
|
|
142
161
|
|
|
143
162
|
export type LiquidationRecord = {
|
|
144
163
|
ts: BN;
|
|
145
|
-
recordId: BN;
|
|
146
|
-
userAuthority: PublicKey;
|
|
147
164
|
user: PublicKey;
|
|
148
|
-
partial: boolean;
|
|
149
|
-
baseAssetValue: BN;
|
|
150
|
-
baseAssetValueClosed: BN;
|
|
151
|
-
liquidationFee: BN;
|
|
152
|
-
feeToLiquidator: BN;
|
|
153
|
-
feeToInsuranceFund: BN;
|
|
154
165
|
liquidator: PublicKey;
|
|
166
|
+
liquidationType: LiquidationType;
|
|
167
|
+
marginRequirement: BN;
|
|
155
168
|
totalCollateral: BN;
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
169
|
+
liquidatePerp: LiquidatePerpRecord;
|
|
170
|
+
liquidateBorrow: LiquidateBorrowRecord;
|
|
171
|
+
liquidateBorrowForPerpPnl: LiquidateBorrowForPerpPnlRecord;
|
|
172
|
+
liquidatePerpPnlForDeposit: LiquidatePerpPnlForDepositRecord;
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
export class LiquidationType {
|
|
176
|
+
static readonly LIQUIDATE_PERP = { liquidatePerp: {} };
|
|
177
|
+
static readonly LIQUIDATE_BORROW = { liquidateBorrow: {} };
|
|
178
|
+
static readonly LIQUIDATE_BORROW_FOR_PERP_PNL = {
|
|
179
|
+
liquidateBorrowForPerpPnl: {},
|
|
180
|
+
};
|
|
181
|
+
static readonly LIQUIDATE_PERP_PNL_FOR_DEPOSIT = {
|
|
182
|
+
liquidatePerpPnlForDeposit: {},
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export type LiquidatePerpRecord = {
|
|
187
|
+
marketIndex: BN;
|
|
188
|
+
orderIds: BN[];
|
|
189
|
+
oraclePrice: BN;
|
|
190
|
+
baseAssetAmount: BN;
|
|
191
|
+
quoteAssetAmount: BN;
|
|
192
|
+
userPnl: BN;
|
|
193
|
+
liquidatorPnl: BN;
|
|
194
|
+
canceledOrdersFee: BN;
|
|
195
|
+
userOrderId: BN;
|
|
196
|
+
liquidatorOrderId: BN;
|
|
197
|
+
fillRecordId: BN;
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
export type LiquidateBorrowRecord = {
|
|
201
|
+
assetBankIndex: BN;
|
|
202
|
+
assetPrice: BN;
|
|
203
|
+
assetTransfer: BN;
|
|
204
|
+
liabilityBankIndex: BN;
|
|
205
|
+
liabilityPrice: BN;
|
|
206
|
+
liabilityTransfer: BN;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
export type LiquidateBorrowForPerpPnlRecord = {
|
|
210
|
+
marketIndex: BN;
|
|
211
|
+
marketOraclePrice: BN;
|
|
212
|
+
pnlTransfer: BN;
|
|
213
|
+
liabilityBankIndex: BN;
|
|
214
|
+
liabilityPrice: BN;
|
|
215
|
+
liabilityTransfer: BN;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
export type LiquidatePerpPnlForDepositRecord = {
|
|
219
|
+
marketIndex: BN;
|
|
220
|
+
marketOraclePrice: BN;
|
|
221
|
+
pnlTransfer: BN;
|
|
222
|
+
assetBankIndex: BN;
|
|
223
|
+
assetPrice: BN;
|
|
224
|
+
assetTransfer: BN;
|
|
159
225
|
};
|
|
160
226
|
|
|
161
227
|
export type OrderRecord = {
|
|
@@ -164,7 +230,10 @@ export type OrderRecord = {
|
|
|
164
230
|
maker: PublicKey;
|
|
165
231
|
takerOrder: Order;
|
|
166
232
|
makerOrder: Order;
|
|
233
|
+
takerUnsettledPnl: BN;
|
|
234
|
+
makerUnsettledPnl: BN;
|
|
167
235
|
action: OrderAction;
|
|
236
|
+
actionExplanation: OrderActionExplanation;
|
|
168
237
|
filler: PublicKey;
|
|
169
238
|
fillRecordId: BN;
|
|
170
239
|
marketIndex: BN;
|
|
@@ -219,9 +288,9 @@ export type MarketAccount = {
|
|
|
219
288
|
openInterest: BN;
|
|
220
289
|
marginRatioInitial: number;
|
|
221
290
|
marginRatioMaintenance: number;
|
|
222
|
-
marginRatioPartial: number;
|
|
223
291
|
nextFillRecordId: BN;
|
|
224
292
|
pnlPool: PoolBalance;
|
|
293
|
+
liquidationFee: BN;
|
|
225
294
|
};
|
|
226
295
|
|
|
227
296
|
export type BankAccount = {
|
|
@@ -245,6 +314,7 @@ export type BankAccount = {
|
|
|
245
314
|
maintenanceAssetWeight: BN;
|
|
246
315
|
initialLiabilityWeight: BN;
|
|
247
316
|
maintenanceLiabilityWeight: BN;
|
|
317
|
+
liquidationFee: BN;
|
|
248
318
|
};
|
|
249
319
|
|
|
250
320
|
export type PoolBalance = {
|
|
@@ -261,6 +331,8 @@ export type AMM = {
|
|
|
261
331
|
lastMarkPriceTwapTs: BN;
|
|
262
332
|
lastOraclePriceTwap: BN;
|
|
263
333
|
lastOraclePriceTwapTs: BN;
|
|
334
|
+
lastOracleMarkSpreadPct: BN;
|
|
335
|
+
lastOracleConfPct: BN;
|
|
264
336
|
oracle: PublicKey;
|
|
265
337
|
oracleSource: OracleSource;
|
|
266
338
|
fundingPeriod: BN;
|
|
@@ -275,6 +347,8 @@ export type AMM = {
|
|
|
275
347
|
totalFee: BN;
|
|
276
348
|
minimumQuoteAssetTradeSize: BN;
|
|
277
349
|
baseAssetAmountStepSize: BN;
|
|
350
|
+
maxBaseAssetAmountRatio: number;
|
|
351
|
+
maxSlippageRatio: number;
|
|
278
352
|
lastOraclePrice: BN;
|
|
279
353
|
baseSpread: number;
|
|
280
354
|
curveUpdateIntensity: number;
|
|
@@ -323,6 +397,7 @@ export type UserAccount = {
|
|
|
323
397
|
};
|
|
324
398
|
positions: UserPosition[];
|
|
325
399
|
orders: Order[];
|
|
400
|
+
beingLiquidated: boolean;
|
|
326
401
|
};
|
|
327
402
|
|
|
328
403
|
export type UserBankBalance = {
|
|
@@ -365,7 +440,6 @@ export type OrderParams = {
|
|
|
365
440
|
orderType: OrderType;
|
|
366
441
|
userOrderId: number;
|
|
367
442
|
direction: PositionDirection;
|
|
368
|
-
quoteAssetAmount: BN;
|
|
369
443
|
baseAssetAmount: BN;
|
|
370
444
|
price: BN;
|
|
371
445
|
marketIndex: BN;
|
|
@@ -384,11 +458,49 @@ export type OrderParams = {
|
|
|
384
458
|
};
|
|
385
459
|
};
|
|
386
460
|
|
|
461
|
+
export type NecessaryOrderParams = {
|
|
462
|
+
orderType: OrderType;
|
|
463
|
+
marketIndex: BN;
|
|
464
|
+
baseAssetAmount: BN;
|
|
465
|
+
direction: PositionDirection;
|
|
466
|
+
};
|
|
467
|
+
|
|
468
|
+
export type OptionalOrderParams = {
|
|
469
|
+
[Property in keyof OrderParams]?: OrderParams[Property];
|
|
470
|
+
} & NecessaryOrderParams;
|
|
471
|
+
|
|
472
|
+
export const DefaultOrderParams = {
|
|
473
|
+
orderType: OrderType.MARKET,
|
|
474
|
+
userOrderId: 0,
|
|
475
|
+
direction: PositionDirection.LONG,
|
|
476
|
+
baseAssetAmount: ZERO,
|
|
477
|
+
price: ZERO,
|
|
478
|
+
marketIndex: ZERO,
|
|
479
|
+
reduceOnly: false,
|
|
480
|
+
postOnly: false,
|
|
481
|
+
immediateOrCancel: false,
|
|
482
|
+
triggerPrice: ZERO,
|
|
483
|
+
triggerCondition: OrderTriggerCondition.ABOVE,
|
|
484
|
+
positionLimit: ZERO,
|
|
485
|
+
oraclePriceOffset: ZERO,
|
|
486
|
+
padding0: ZERO,
|
|
487
|
+
padding1: ZERO,
|
|
488
|
+
optionalAccounts: {
|
|
489
|
+
discountToken: false,
|
|
490
|
+
referrer: false,
|
|
491
|
+
},
|
|
492
|
+
};
|
|
493
|
+
|
|
387
494
|
export type MakerInfo = {
|
|
388
495
|
maker: PublicKey;
|
|
389
496
|
order: Order;
|
|
390
497
|
};
|
|
391
498
|
|
|
499
|
+
export type TakerInfo = {
|
|
500
|
+
taker: PublicKey;
|
|
501
|
+
order: Order;
|
|
502
|
+
};
|
|
503
|
+
|
|
392
504
|
// # Misc Types
|
|
393
505
|
export interface IWallet {
|
|
394
506
|
signTransaction(tx: Transaction): Promise<Transaction>;
|
|
@@ -430,6 +542,7 @@ export type FeeStructure = {
|
|
|
430
542
|
makerRebateNumerator: BN;
|
|
431
543
|
makerRebateDenominator: BN;
|
|
432
544
|
fillerRewardStructure: OrderFillerRewardStructure;
|
|
545
|
+
cancelOrderFee: BN;
|
|
433
546
|
};
|
|
434
547
|
|
|
435
548
|
export type OracleGuardRails = {
|
|
@@ -451,4 +564,4 @@ export type OrderFillerRewardStructure = {
|
|
|
451
564
|
timeBasedRewardLowerBound: BN;
|
|
452
565
|
};
|
|
453
566
|
|
|
454
|
-
export type MarginCategory = 'Initial' | '
|
|
567
|
+
export type MarginCategory = 'Initial' | 'Maintenance';
|
package/src/userName.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.decodeName = exports.encodeName = exports.DEFAULT_USER_NAME = exports.MAX_NAME_LENGTH = void 0;
|
|
4
|
+
exports.MAX_NAME_LENGTH = 32;
|
|
5
|
+
exports.DEFAULT_USER_NAME = 'Main Account';
|
|
6
|
+
function encodeName(name) {
|
|
7
|
+
if (name.length > exports.MAX_NAME_LENGTH) {
|
|
8
|
+
throw Error(`User name (${name}) longer than 32 characters`);
|
|
9
|
+
}
|
|
10
|
+
const buffer = Buffer.alloc(32);
|
|
11
|
+
buffer.fill(name);
|
|
12
|
+
buffer.fill(' ', name.length);
|
|
13
|
+
return Array(...buffer);
|
|
14
|
+
}
|
|
15
|
+
exports.encodeName = encodeName;
|
|
16
|
+
function decodeName(bytes) {
|
|
17
|
+
const buffer = Buffer.from(bytes);
|
|
18
|
+
return buffer.toString('utf8').trim();
|
|
19
|
+
}
|
|
20
|
+
exports.decodeName = decodeName;
|
package/src/util/computeUnits.js
CHANGED
|
@@ -1,17 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.findComputeUnitConsumption = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
function findComputeUnitConsumption(programId, connection, txSignature, commitment = 'confirmed') {
|
|
14
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
const tx = yield connection.getTransaction(txSignature, { commitment });
|
|
16
|
+
const computeUnits = [];
|
|
17
|
+
const regex = new RegExp(`Program ${programId.toString()} consumed ([0-9]{0,6}) of ([0-9]{0,7}) compute units`);
|
|
18
|
+
tx.meta.logMessages.forEach((logMessage) => {
|
|
19
|
+
const match = logMessage.match(regex);
|
|
20
|
+
if (match && match[1]) {
|
|
21
|
+
computeUnits.push(match[1]);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
return computeUnits;
|
|
13
25
|
});
|
|
14
|
-
return computeUnits;
|
|
15
26
|
}
|
|
16
27
|
exports.findComputeUnitConsumption = findComputeUnitConsumption;
|
|
17
|
-
//# sourceMappingURL=computeUnits.js.map
|
package/src/util/computeUnits.ts
CHANGED
|
@@ -9,7 +9,7 @@ export async function findComputeUnitConsumption(
|
|
|
9
9
|
const tx = await connection.getTransaction(txSignature, { commitment });
|
|
10
10
|
const computeUnits = [];
|
|
11
11
|
const regex = new RegExp(
|
|
12
|
-
`Program ${programId.toString()} consumed ([0-9]{0,6}) of
|
|
12
|
+
`Program ${programId.toString()} consumed ([0-9]{0,6}) of ([0-9]{0,7}) compute units`
|
|
13
13
|
);
|
|
14
14
|
tx.meta.logMessages.forEach((logMessage) => {
|
|
15
15
|
const match = logMessage.match(regex);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getTokenAddress = void 0;
|
|
4
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
5
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
+
const getTokenAddress = (mintAddress, userPubKey) => {
|
|
7
|
+
return spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, new web3_js_1.PublicKey(mintAddress), new web3_js_1.PublicKey(userPubKey));
|
|
8
|
+
};
|
|
9
|
+
exports.getTokenAddress = getTokenAddress;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.promiseTimeout = void 0;
|
|
4
|
+
function promiseTimeout(promise, timeoutMs) {
|
|
5
|
+
let timeoutId;
|
|
6
|
+
const timeoutPromise = new Promise((resolve) => {
|
|
7
|
+
timeoutId = setTimeout(() => resolve(null), timeoutMs);
|
|
8
|
+
});
|
|
9
|
+
return Promise.race([promise, timeoutPromise]).then((result) => {
|
|
10
|
+
clearTimeout(timeoutId);
|
|
11
|
+
return result;
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
exports.promiseTimeout = promiseTimeout;
|