@drift-labs/sdk 0.1.19-master.0 → 0.1.21-master.0
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/accounts/bulkAccountLoader.d.ts +32 -0
- package/lib/accounts/bulkAccountLoader.d.ts.map +1 -0
- package/lib/accounts/bulkAccountLoader.js +175 -0
- package/lib/accounts/bulkUserSubscription.d.ts +8 -0
- package/lib/accounts/bulkUserSubscription.d.ts.map +1 -0
- package/lib/accounts/bulkUserSubscription.js +28 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +46 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +206 -0
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts +26 -0
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/pollingTokenAccountSubscriber.js +79 -0
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +31 -0
- package/lib/accounts/pollingUserAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/pollingUserAccountSubscriber.js +123 -0
- package/lib/accounts/types.d.ts +30 -0
- package/lib/accounts/types.d.ts.map +1 -1
- package/lib/accounts/utils.d.ts +2 -0
- package/lib/accounts/utils.d.ts.map +1 -0
- package/lib/accounts/utils.js +7 -0
- package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -2
- package/lib/accounts/webSocketAccountSubscriber.d.ts.map +1 -1
- package/lib/accounts/webSocketAccountSubscriber.js +43 -12
- package/lib/accounts/{defaultClearingHouseAccountSubscriber.d.ts → webSocketClearingHouseAccountSubscriber.d.ts} +4 -2
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/{defaultClearingHouseAccountSubscriber.js → webSocketClearingHouseAccountSubscriber.js} +5 -4
- package/lib/accounts/{defaultUserAccountSubscriber.d.ts → webSocketUserAccountSubscriber.d.ts} +4 -2
- package/lib/accounts/webSocketUserAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/{defaultUserAccountSubscriber.js → webSocketUserAccountSubscriber.js} +5 -4
- package/lib/admin.d.ts +1 -0
- package/lib/admin.d.ts.map +1 -1
- package/lib/admin.js +23 -14
- package/lib/clearingHouse.d.ts +12 -2
- package/lib/clearingHouse.d.ts.map +1 -1
- package/lib/clearingHouse.js +28 -17
- package/lib/clearingHouseUser.d.ts +11 -3
- package/lib/clearingHouseUser.d.ts.map +1 -1
- package/lib/clearingHouseUser.js +46 -36
- package/lib/constants/markets.d.ts +2 -1
- package/lib/constants/markets.d.ts.map +1 -1
- package/lib/constants/markets.js +20 -15
- package/lib/constants/numericConstants.d.ts +3 -1
- package/lib/constants/numericConstants.d.ts.map +1 -1
- package/lib/constants/numericConstants.js +15 -17
- package/lib/examples/makeTradeExample.js +6 -6
- package/lib/factory/clearingHouse.d.ts +26 -0
- package/lib/factory/clearingHouse.d.ts.map +1 -0
- package/lib/factory/clearingHouse.js +64 -0
- package/lib/factory/clearingHouseUser.d.ts +20 -0
- package/lib/factory/clearingHouseUser.d.ts.map +1 -0
- package/lib/factory/clearingHouseUser.js +34 -0
- package/lib/index.d.ts +8 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +9 -2
- package/lib/math/amm.js +7 -7
- package/lib/math/conversion.d.ts +1 -1
- package/lib/math/conversion.d.ts.map +1 -1
- package/lib/math/conversion.js +1 -1
- package/lib/math/funding.js +1 -1
- package/lib/math/insuranceFund.d.ts +2 -1
- package/lib/math/insuranceFund.d.ts.map +1 -1
- package/lib/math/insuranceFund.js +3 -6
- package/lib/math/market.js +1 -1
- package/lib/math/position.d.ts +2 -1
- package/lib/math/position.d.ts.map +1 -1
- package/lib/math/position.js +3 -6
- package/lib/math/trade.js +16 -16
- package/lib/math/utils.d.ts +2 -1
- package/lib/math/utils.d.ts.map +1 -1
- package/lib/math/utils.js +3 -3
- package/lib/mockUSDCFaucet.d.ts +2 -1
- package/lib/mockUSDCFaucet.d.ts.map +1 -1
- package/lib/pythClient.js +1 -1
- package/lib/token/index.d.ts +4 -0
- package/lib/token/index.d.ts.map +1 -0
- package/lib/token/index.js +38 -0
- package/lib/types.d.ts +2 -1
- package/lib/types.d.ts.map +1 -1
- package/package.json +11 -3
- package/src/accounts/bulkAccountLoader.ts +215 -0
- package/src/accounts/bulkUserSubscription.ts +28 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +293 -0
- package/src/accounts/pollingTokenAccountSubscriber.ts +99 -0
- package/src/accounts/pollingUserAccountSubscriber.ts +166 -0
- package/src/accounts/types.ts +38 -0
- package/src/accounts/utils.ts +3 -0
- package/src/accounts/webSocketAccountSubscriber.ts +67 -17
- package/src/accounts/{defaultClearingHouseAccountSubscriber.ts → webSocketClearingHouseAccountSubscriber.ts} +4 -1
- package/src/accounts/{defaultUserAccountSubscriber.ts → webSocketUserAccountSubscriber.ts} +4 -1
- package/src/admin.ts +27 -18
- package/src/clearingHouse.ts +25 -17
- package/src/clearingHouseUser.ts +26 -6
- package/src/constants/markets.ts +9 -1
- package/src/constants/numericConstants.ts +2 -1
- package/src/examples/makeTradeExample.ts +4 -1
- package/src/factory/clearingHouse.ts +125 -0
- package/src/factory/clearingHouseUser.ts +73 -0
- package/src/index.ts +8 -2
- package/src/math/conversion.ts +1 -1
- package/src/math/insuranceFund.ts +1 -1
- package/src/math/position.ts +1 -1
- package/src/math/utils.ts +1 -1
- package/src/mockUSDCFaucet.ts +1 -1
- package/src/token/index.ts +37 -0
- package/src/types.ts +1 -1
- package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts.map +0 -1
- package/lib/accounts/defaultUserAccountSubscriber.d.ts.map +0 -1
|
@@ -21,7 +21,7 @@ const getTokenAddress = (mintAddress, userPubKey) => {
|
|
|
21
21
|
exports.getTokenAddress = getTokenAddress;
|
|
22
22
|
const main = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
23
23
|
// Initialize Drift SDK
|
|
24
|
-
const sdkConfig =
|
|
24
|
+
const sdkConfig = __2.initialize({ env: 'devnet' });
|
|
25
25
|
// Set up the Wallet and Provider
|
|
26
26
|
const privateKey = process.env.BOT_PRIVATE_KEY; // stored as an array string
|
|
27
27
|
const keypair = web3_js_1.Keypair.fromSecretKey(Uint8Array.from(JSON.parse(privateKey)));
|
|
@@ -35,7 +35,7 @@ const main = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
35
35
|
const lamportsBalance = yield connection.getBalance(wallet.publicKey);
|
|
36
36
|
console.log('SOL balance:', lamportsBalance / Math.pow(10, 9));
|
|
37
37
|
// Misc. other things to set up
|
|
38
|
-
const usdcTokenAddress = yield
|
|
38
|
+
const usdcTokenAddress = yield exports.getTokenAddress(sdkConfig.USDC_MINT_ADDRESS, wallet.publicKey.toString());
|
|
39
39
|
// Set up the Drift Clearing House
|
|
40
40
|
const clearingHousePublicKey = new web3_js_1.PublicKey(sdkConfig.CLEARING_HOUSE_PROGRAM_ID);
|
|
41
41
|
const clearingHouse = __2.ClearingHouse.from(connection, provider.wallet, clearingHousePublicKey);
|
|
@@ -47,18 +47,18 @@ const main = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
47
47
|
if (!userAccountExists) {
|
|
48
48
|
//// Create a Clearing House account by Depositing some USDC ($10,000 in this case)
|
|
49
49
|
const depositAmount = new anchor_1.BN(10000).mul(__2.QUOTE_PRECISION);
|
|
50
|
-
yield clearingHouse.initializeUserAccountAndDepositCollateral(depositAmount, yield
|
|
50
|
+
yield clearingHouse.initializeUserAccountAndDepositCollateral(depositAmount, yield exports.getTokenAddress(usdcTokenAddress.toString(), wallet.publicKey.toString()));
|
|
51
51
|
}
|
|
52
52
|
yield user.subscribe();
|
|
53
53
|
// Get current price
|
|
54
54
|
const solMarketInfo = __2.Markets.find((market) => market.baseAssetSymbol === 'SOL');
|
|
55
|
-
const currentMarketPrice =
|
|
56
|
-
const formattedPrice =
|
|
55
|
+
const currentMarketPrice = __2.calculateMarkPrice(clearingHouse.getMarket(solMarketInfo.marketIndex));
|
|
56
|
+
const formattedPrice = __2.convertToNumber(currentMarketPrice, __2.MARK_PRICE_PRECISION);
|
|
57
57
|
console.log(`Current Market Price is $${formattedPrice}`);
|
|
58
58
|
// Estimate the slippage for a $5000 LONG trade
|
|
59
59
|
const solMarketAccount = clearingHouse.getMarket(solMarketInfo.marketIndex);
|
|
60
60
|
const longAmount = new anchor_1.BN(5000).mul(__2.QUOTE_PRECISION);
|
|
61
|
-
const slippage =
|
|
61
|
+
const slippage = __2.convertToNumber(__2.calculateTradeSlippage(__2.PositionDirection.LONG, longAmount, solMarketAccount)[0], __2.MARK_PRICE_PRECISION);
|
|
62
62
|
console.log(`Slippage for a $5000 LONG on the SOL market would be $${slippage}`);
|
|
63
63
|
// Make a $5000 LONG trade
|
|
64
64
|
yield clearingHouse.openPosition(__2.PositionDirection.LONG, longAmount, solMarketInfo.marketIndex);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ConfirmOptions, Connection, PublicKey } from '@solana/web3.js';
|
|
2
|
+
import { IWallet } from '../types';
|
|
3
|
+
import { BulkAccountLoader } from '../accounts/bulkAccountLoader';
|
|
4
|
+
import { TxSender } from '../tx/types';
|
|
5
|
+
import { ClearingHouse } from '../clearingHouse';
|
|
6
|
+
import { Admin } from '../admin';
|
|
7
|
+
export declare type ClearingHouseConfigType = 'websocket' | 'polling' | 'custom';
|
|
8
|
+
declare type BaseClearingHouseConfig = {
|
|
9
|
+
type: ClearingHouseConfigType;
|
|
10
|
+
connection: Connection;
|
|
11
|
+
wallet: IWallet;
|
|
12
|
+
programID: PublicKey;
|
|
13
|
+
opts?: ConfirmOptions;
|
|
14
|
+
txSender?: TxSender;
|
|
15
|
+
};
|
|
16
|
+
declare type WebSocketClearingHouseConfiguration = BaseClearingHouseConfig;
|
|
17
|
+
declare type PollingClearingHouseConfiguration = BaseClearingHouseConfig & {
|
|
18
|
+
accountLoader: BulkAccountLoader;
|
|
19
|
+
};
|
|
20
|
+
declare type ClearingHouseConfig = PollingClearingHouseConfiguration | WebSocketClearingHouseConfiguration;
|
|
21
|
+
export declare function getWebSocketClearingHouseConfig(connection: Connection, wallet: IWallet, programID: PublicKey, opts?: ConfirmOptions, txSender?: TxSender): WebSocketClearingHouseConfiguration;
|
|
22
|
+
export declare function getPollingClearingHouseConfig(connection: Connection, wallet: IWallet, programID: PublicKey, accountLoader: BulkAccountLoader, opts?: ConfirmOptions, txSender?: TxSender): PollingClearingHouseConfiguration;
|
|
23
|
+
export declare function getClearingHouse(config: ClearingHouseConfig): ClearingHouse;
|
|
24
|
+
export declare function getAdmin(config: ClearingHouseConfig): Admin;
|
|
25
|
+
export {};
|
|
26
|
+
//# sourceMappingURL=clearingHouse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clearingHouse.d.ts","sourceRoot":"","sources":["../../src/factory/clearingHouse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACxE,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AACnC,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAClE,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAMjD,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAEjC,oBAAY,uBAAuB,GAAG,WAAW,GAAG,SAAS,GAAG,QAAQ,CAAC;AAEzE,aAAK,uBAAuB,GAAG;IAC9B,IAAI,EAAE,uBAAuB,CAAC;IAC9B,UAAU,EAAE,UAAU,CAAC;IACvB,MAAM,EAAE,OAAO,CAAC;IAChB,SAAS,EAAE,SAAS,CAAC;IACrB,IAAI,CAAC,EAAE,cAAc,CAAC;IACtB,QAAQ,CAAC,EAAE,QAAQ,CAAC;CACpB,CAAC;AAEF,aAAK,mCAAmC,GAAG,uBAAuB,CAAC;AAEnE,aAAK,iCAAiC,GAAG,uBAAuB,GAAG;IAClE,aAAa,EAAE,iBAAiB,CAAC;CACjC,CAAC;AAEF,aAAK,mBAAmB,GACrB,iCAAiC,GACjC,mCAAmC,CAAC;AAEvC,wBAAgB,+BAA+B,CAC9C,UAAU,EAAE,UAAU,EACtB,MAAM,EAAE,OAAO,EACf,SAAS,EAAE,SAAS,EACpB,IAAI,GAAE,cAA0C,EAChD,QAAQ,CAAC,EAAE,QAAQ,GACjB,mCAAmC,CASrC;AAED,wBAAgB,6BAA6B,CAC5C,UAAU,EAAE,UAAU,EACtB,MAAM,EAAE,OAAO,EACf,SAAS,EAAE,SAAS,EACpB,aAAa,EAAE,iBAAiB,EAChC,IAAI,GAAE,cAA0C,EAChD,QAAQ,CAAC,EAAE,QAAQ,GACjB,iCAAiC,CAUnC;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,mBAAmB,GAAG,aAAa,CA0B3E;AAED,wBAAgB,QAAQ,CAAC,MAAM,EAAE,mBAAmB,GAAG,KAAK,CA0B3D"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getAdmin = exports.getClearingHouse = exports.getPollingClearingHouseConfig = exports.getWebSocketClearingHouseConfig = void 0;
|
|
7
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
8
|
+
const clearingHouse_1 = require("../clearingHouse");
|
|
9
|
+
const clearing_house_json_1 = __importDefault(require("../idl/clearing_house.json"));
|
|
10
|
+
const webSocketClearingHouseAccountSubscriber_1 = require("../accounts/webSocketClearingHouseAccountSubscriber");
|
|
11
|
+
const defaultTxSender_1 = require("../tx/defaultTxSender");
|
|
12
|
+
const pollingClearingHouseAccountSubscriber_1 = require("../accounts/pollingClearingHouseAccountSubscriber");
|
|
13
|
+
const admin_1 = require("../admin");
|
|
14
|
+
function getWebSocketClearingHouseConfig(connection, wallet, programID, opts = anchor_1.Provider.defaultOptions(), txSender) {
|
|
15
|
+
return {
|
|
16
|
+
type: 'websocket',
|
|
17
|
+
connection,
|
|
18
|
+
wallet,
|
|
19
|
+
programID,
|
|
20
|
+
opts,
|
|
21
|
+
txSender,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
exports.getWebSocketClearingHouseConfig = getWebSocketClearingHouseConfig;
|
|
25
|
+
function getPollingClearingHouseConfig(connection, wallet, programID, accountLoader, opts = anchor_1.Provider.defaultOptions(), txSender) {
|
|
26
|
+
return {
|
|
27
|
+
type: 'polling',
|
|
28
|
+
connection,
|
|
29
|
+
wallet,
|
|
30
|
+
programID,
|
|
31
|
+
accountLoader,
|
|
32
|
+
opts,
|
|
33
|
+
txSender,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
exports.getPollingClearingHouseConfig = getPollingClearingHouseConfig;
|
|
37
|
+
function getClearingHouse(config) {
|
|
38
|
+
const provider = new anchor_1.Provider(config.connection, config.wallet, config.opts);
|
|
39
|
+
const program = new anchor_1.Program(clearing_house_json_1.default, config.programID, provider);
|
|
40
|
+
let accountSubscriber;
|
|
41
|
+
if (config.type === 'websocket') {
|
|
42
|
+
accountSubscriber = new webSocketClearingHouseAccountSubscriber_1.WebSocketClearingHouseAccountSubscriber(program);
|
|
43
|
+
}
|
|
44
|
+
else if (config.type === 'polling') {
|
|
45
|
+
accountSubscriber = new pollingClearingHouseAccountSubscriber_1.PollingClearingHouseAccountSubscriber(program, config.accountLoader);
|
|
46
|
+
}
|
|
47
|
+
const txSender = config.txSender || new defaultTxSender_1.DefaultTxSender(provider);
|
|
48
|
+
return new clearingHouse_1.ClearingHouse(config.connection, config.wallet, program, accountSubscriber, txSender, config.opts);
|
|
49
|
+
}
|
|
50
|
+
exports.getClearingHouse = getClearingHouse;
|
|
51
|
+
function getAdmin(config) {
|
|
52
|
+
const provider = new anchor_1.Provider(config.connection, config.wallet, config.opts);
|
|
53
|
+
const program = new anchor_1.Program(clearing_house_json_1.default, config.programID, provider);
|
|
54
|
+
let accountSubscriber;
|
|
55
|
+
if (config.type === 'websocket') {
|
|
56
|
+
accountSubscriber = new webSocketClearingHouseAccountSubscriber_1.WebSocketClearingHouseAccountSubscriber(program);
|
|
57
|
+
}
|
|
58
|
+
else if (config.type === 'polling') {
|
|
59
|
+
accountSubscriber = new pollingClearingHouseAccountSubscriber_1.PollingClearingHouseAccountSubscriber(program, config.accountLoader);
|
|
60
|
+
}
|
|
61
|
+
const txSender = config.txSender || new defaultTxSender_1.DefaultTxSender(provider);
|
|
62
|
+
return new admin_1.Admin(config.connection, config.wallet, program, accountSubscriber, txSender, config.opts);
|
|
63
|
+
}
|
|
64
|
+
exports.getAdmin = getAdmin;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { PublicKey } from '@solana/web3.js';
|
|
2
|
+
import { ClearingHouse } from '../clearingHouse';
|
|
3
|
+
import { BulkAccountLoader } from '../accounts/bulkAccountLoader';
|
|
4
|
+
import { ClearingHouseUser } from '../clearingHouseUser';
|
|
5
|
+
export declare type ClearingHouseUserConfigType = 'websocket' | 'polling' | 'custom';
|
|
6
|
+
declare type BaseClearingHouseUserConfig = {
|
|
7
|
+
type: ClearingHouseUserConfigType;
|
|
8
|
+
clearingHouse: ClearingHouse;
|
|
9
|
+
authority: PublicKey;
|
|
10
|
+
};
|
|
11
|
+
declare type WebSocketClearingHouseUserConfig = BaseClearingHouseUserConfig;
|
|
12
|
+
declare type PollingClearingHouseUserConfig = BaseClearingHouseUserConfig & {
|
|
13
|
+
accountLoader: BulkAccountLoader;
|
|
14
|
+
};
|
|
15
|
+
declare type ClearingHouseUserConfig = PollingClearingHouseUserConfig | WebSocketClearingHouseUserConfig;
|
|
16
|
+
export declare function getWebSocketClearingHouseUserConfig(clearingHouse: ClearingHouse, authority: PublicKey): WebSocketClearingHouseUserConfig;
|
|
17
|
+
export declare function getPollingClearingHouseUserConfig(clearingHouse: ClearingHouse, authority: PublicKey, accountLoader: BulkAccountLoader): PollingClearingHouseUserConfig;
|
|
18
|
+
export declare function getClearingHouseUser(config: ClearingHouseUserConfig): ClearingHouseUser;
|
|
19
|
+
export {};
|
|
20
|
+
//# sourceMappingURL=clearingHouseUser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clearingHouseUser.d.ts","sourceRoot":"","sources":["../../src/factory/clearingHouseUser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAClE,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAKzD,oBAAY,2BAA2B,GAAG,WAAW,GAAG,SAAS,GAAG,QAAQ,CAAC;AAE7E,aAAK,2BAA2B,GAAG;IAClC,IAAI,EAAE,2BAA2B,CAAC;IAClC,aAAa,EAAE,aAAa,CAAC;IAC7B,SAAS,EAAE,SAAS,CAAC;CACrB,CAAC;AAEF,aAAK,gCAAgC,GAAG,2BAA2B,CAAC;AAEpE,aAAK,8BAA8B,GAAG,2BAA2B,GAAG;IACnE,aAAa,EAAE,iBAAiB,CAAC;CACjC,CAAC;AAEF,aAAK,uBAAuB,GACzB,8BAA8B,GAC9B,gCAAgC,CAAC;AAEpC,wBAAgB,mCAAmC,CAClD,aAAa,EAAE,aAAa,EAC5B,SAAS,EAAE,SAAS,GAClB,gCAAgC,CAMlC;AAED,wBAAgB,iCAAiC,CAChD,aAAa,EAAE,aAAa,EAC5B,SAAS,EAAE,SAAS,EACpB,aAAa,EAAE,iBAAiB,GAC9B,8BAA8B,CAOhC;AAED,wBAAgB,oBAAoB,CACnC,MAAM,EAAE,uBAAuB,GAC7B,iBAAiB,CAoBnB"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getClearingHouseUser = exports.getPollingClearingHouseUserConfig = exports.getWebSocketClearingHouseUserConfig = void 0;
|
|
4
|
+
const clearingHouseUser_1 = require("../clearingHouseUser");
|
|
5
|
+
const webSocketUserAccountSubscriber_1 = require("../accounts/webSocketUserAccountSubscriber");
|
|
6
|
+
const pollingUserAccountSubscriber_1 = require("../accounts/pollingUserAccountSubscriber");
|
|
7
|
+
function getWebSocketClearingHouseUserConfig(clearingHouse, authority) {
|
|
8
|
+
return {
|
|
9
|
+
type: 'websocket',
|
|
10
|
+
clearingHouse,
|
|
11
|
+
authority,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
exports.getWebSocketClearingHouseUserConfig = getWebSocketClearingHouseUserConfig;
|
|
15
|
+
function getPollingClearingHouseUserConfig(clearingHouse, authority, accountLoader) {
|
|
16
|
+
return {
|
|
17
|
+
type: 'polling',
|
|
18
|
+
clearingHouse,
|
|
19
|
+
authority,
|
|
20
|
+
accountLoader,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
exports.getPollingClearingHouseUserConfig = getPollingClearingHouseUserConfig;
|
|
24
|
+
function getClearingHouseUser(config) {
|
|
25
|
+
let accountSubscriber;
|
|
26
|
+
if (config.type === 'websocket') {
|
|
27
|
+
accountSubscriber = new webSocketUserAccountSubscriber_1.WebSocketUserAccountSubscriber(config.clearingHouse.program, config.authority);
|
|
28
|
+
}
|
|
29
|
+
else if (config.type === 'polling') {
|
|
30
|
+
accountSubscriber = new pollingUserAccountSubscriber_1.PollingUserAccountSubscriber(config.clearingHouse.program, config.authority, config.accountLoader);
|
|
31
|
+
}
|
|
32
|
+
return new clearingHouseUser_1.ClearingHouseUser(config.clearingHouse, config.authority, accountSubscriber);
|
|
33
|
+
}
|
|
34
|
+
exports.getClearingHouseUser = getClearingHouseUser;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
import { BN } from '@project-serum/anchor';
|
|
2
|
+
import { PublicKey } from '@solana/web3.js';
|
|
2
3
|
export * from './mockUSDCFaucet';
|
|
3
4
|
export * from './pythClient';
|
|
4
5
|
export * from './types';
|
|
5
6
|
export * from './constants/markets';
|
|
6
|
-
export * from './accounts/
|
|
7
|
+
export * from './accounts/webSocketClearingHouseAccountSubscriber';
|
|
8
|
+
export * from './accounts/bulkAccountLoader';
|
|
9
|
+
export * from './accounts/pollingClearingHouseAccountSubscriber';
|
|
10
|
+
export * from './accounts/pollingTokenAccountSubscriber';
|
|
7
11
|
export * from './accounts/types';
|
|
8
12
|
export * from './addresses';
|
|
9
13
|
export * from './admin';
|
|
10
14
|
export * from './clearingHouseUser';
|
|
11
15
|
export * from './clearingHouse';
|
|
16
|
+
export * from './factory/clearingHouse';
|
|
17
|
+
export * from './factory/clearingHouseUser';
|
|
12
18
|
export * from './math/conversion';
|
|
13
19
|
export * from './math/funding';
|
|
14
20
|
export * from './math/insuranceFund';
|
|
@@ -23,5 +29,5 @@ export * from './config';
|
|
|
23
29
|
export * from './constants/numericConstants';
|
|
24
30
|
export * from './util/computeUnits';
|
|
25
31
|
export * from './util/tps';
|
|
26
|
-
export { BN };
|
|
32
|
+
export { BN, PublicKey };
|
|
27
33
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,uBAAuB,CAAC;AAC3C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,SAAS,CAAC;AACxB,cAAc,qBAAqB,CAAC;AACpC,cAAc,oDAAoD,CAAC;AACnE,cAAc,8BAA8B,CAAC;AAC7C,cAAc,kDAAkD,CAAC;AACjE,cAAc,0CAA0C,CAAC;AACzD,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,qBAAqB,CAAC;AACpC,cAAc,iBAAiB,CAAC;AAChC,cAAc,yBAAyB,CAAC;AACxC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,eAAe,CAAC;AAC9B,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,8BAA8B,CAAC;AAC7C,cAAc,qBAAqB,CAAC;AACpC,cAAc,YAAY,CAAC;AAE3B,OAAO,EAAE,EAAE,EAAE,SAAS,EAAE,CAAC"}
|
package/lib/index.js
CHANGED
|
@@ -10,19 +10,26 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
10
10
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
exports.BN = void 0;
|
|
13
|
+
exports.PublicKey = exports.BN = void 0;
|
|
14
14
|
const anchor_1 = require("@project-serum/anchor");
|
|
15
15
|
Object.defineProperty(exports, "BN", { enumerable: true, get: function () { return anchor_1.BN; } });
|
|
16
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
17
|
+
Object.defineProperty(exports, "PublicKey", { enumerable: true, get: function () { return web3_js_1.PublicKey; } });
|
|
16
18
|
__exportStar(require("./mockUSDCFaucet"), exports);
|
|
17
19
|
__exportStar(require("./pythClient"), exports);
|
|
18
20
|
__exportStar(require("./types"), exports);
|
|
19
21
|
__exportStar(require("./constants/markets"), exports);
|
|
20
|
-
__exportStar(require("./accounts/
|
|
22
|
+
__exportStar(require("./accounts/webSocketClearingHouseAccountSubscriber"), exports);
|
|
23
|
+
__exportStar(require("./accounts/bulkAccountLoader"), exports);
|
|
24
|
+
__exportStar(require("./accounts/pollingClearingHouseAccountSubscriber"), exports);
|
|
25
|
+
__exportStar(require("./accounts/pollingTokenAccountSubscriber"), exports);
|
|
21
26
|
__exportStar(require("./accounts/types"), exports);
|
|
22
27
|
__exportStar(require("./addresses"), exports);
|
|
23
28
|
__exportStar(require("./admin"), exports);
|
|
24
29
|
__exportStar(require("./clearingHouseUser"), exports);
|
|
25
30
|
__exportStar(require("./clearingHouse"), exports);
|
|
31
|
+
__exportStar(require("./factory/clearingHouse"), exports);
|
|
32
|
+
__exportStar(require("./factory/clearingHouseUser"), exports);
|
|
26
33
|
__exportStar(require("./math/conversion"), exports);
|
|
27
34
|
__exportStar(require("./math/funding"), exports);
|
|
28
35
|
__exportStar(require("./math/insuranceFund"), exports);
|
package/lib/math/amm.js
CHANGED
|
@@ -36,7 +36,7 @@ exports.calculatePrice = calculatePrice;
|
|
|
36
36
|
* @returns quoteAssetReserve and baseAssetReserve after swap. : Precision AMM_RESERVE_PRECISION
|
|
37
37
|
*/
|
|
38
38
|
function calculateAmmReservesAfterSwap(amm, inputAssetType, swapAmount, swapDirection) {
|
|
39
|
-
|
|
39
|
+
assert_1.assert(swapAmount.gte(numericConstants_1.ZERO), 'swapAmount must be greater than 0');
|
|
40
40
|
let newQuoteAssetReserve;
|
|
41
41
|
let newBaseAssetReserve;
|
|
42
42
|
if (inputAssetType === 'quote') {
|
|
@@ -105,7 +105,7 @@ function calculateAdjustKCost(market, marketIndex, numerator, denomenator) {
|
|
|
105
105
|
marketIndex: new anchor_1.BN(marketIndex),
|
|
106
106
|
quoteAssetAmount: new anchor_1.BN(0),
|
|
107
107
|
};
|
|
108
|
-
const currentValue =
|
|
108
|
+
const currentValue = position_1.calculateBaseAssetValue(market, netUserPosition);
|
|
109
109
|
const marketNewK = Object.assign({}, market);
|
|
110
110
|
marketNewK.amm = Object.assign({}, market.amm);
|
|
111
111
|
marketNewK.amm.baseAssetReserve = market.amm.baseAssetReserve
|
|
@@ -116,7 +116,7 @@ function calculateAdjustKCost(market, marketIndex, numerator, denomenator) {
|
|
|
116
116
|
.div(denomenator);
|
|
117
117
|
marketNewK.amm.sqrtK = market.amm.sqrtK.mul(numerator).div(denomenator);
|
|
118
118
|
netUserPosition.quoteAssetAmount = currentValue;
|
|
119
|
-
const cost =
|
|
119
|
+
const cost = __1.calculatePositionPNL(marketNewK, netUserPosition);
|
|
120
120
|
return cost;
|
|
121
121
|
}
|
|
122
122
|
exports.calculateAdjustKCost = calculateAdjustKCost;
|
|
@@ -135,15 +135,15 @@ function calculateRepegCost(market, marketIndex, newPeg) {
|
|
|
135
135
|
marketIndex: new anchor_1.BN(marketIndex),
|
|
136
136
|
quoteAssetAmount: new anchor_1.BN(0),
|
|
137
137
|
};
|
|
138
|
-
const currentValue =
|
|
138
|
+
const currentValue = position_1.calculateBaseAssetValue(market, netUserPosition);
|
|
139
139
|
netUserPosition.quoteAssetAmount = currentValue;
|
|
140
|
-
const prevMarketPrice =
|
|
140
|
+
const prevMarketPrice = __1.calculateMarkPrice(market);
|
|
141
141
|
const marketNewPeg = Object.assign({}, market);
|
|
142
142
|
marketNewPeg.amm = Object.assign({}, market.amm);
|
|
143
143
|
// const marketNewPeg = JSON.parse(JSON.stringify(market));
|
|
144
144
|
marketNewPeg.amm.pegMultiplier = newPeg;
|
|
145
|
-
console.log('Price moves from',
|
|
146
|
-
const cost =
|
|
145
|
+
console.log('Price moves from', __1.convertToNumber(prevMarketPrice), 'to', __1.convertToNumber(__1.calculateMarkPrice(marketNewPeg)));
|
|
146
|
+
const cost = __1.calculatePositionPNL(marketNewPeg, netUserPosition);
|
|
147
147
|
return cost;
|
|
148
148
|
}
|
|
149
149
|
exports.calculateRepegCost = calculateRepegCost;
|
package/lib/math/conversion.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conversion.d.ts","sourceRoot":"","sources":["../../src/math/conversion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"conversion.d.ts","sourceRoot":"","sources":["../../src/math/conversion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,KAAK,CAAC;AAMzB,eAAO,MAAM,eAAe,cAChB,EAAE,cACF,EAAE,WAOb,CAAC;AAEF,eAAO,MAAM,8BAA8B,oBAAqB,EAAE,WAKjE,CAAC"}
|
package/lib/math/conversion.js
CHANGED
|
@@ -10,6 +10,6 @@ const convertToNumber = (bigNumber, precision = numericConstants_1.MARK_PRICE_PR
|
|
|
10
10
|
};
|
|
11
11
|
exports.convertToNumber = convertToNumber;
|
|
12
12
|
const convertBaseAssetAmountToNumber = (baseAssetAmount) => {
|
|
13
|
-
return
|
|
13
|
+
return exports.convertToNumber(baseAssetAmount, numericConstants_1.MARK_PRICE_PRECISION.mul(numericConstants_1.PEG_PRECISION));
|
|
14
14
|
};
|
|
15
15
|
exports.convertBaseAssetAmountToNumber = convertBaseAssetAmountToNumber;
|
package/lib/math/funding.js
CHANGED
|
@@ -40,7 +40,7 @@ function calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustm
|
|
|
40
40
|
const lastMarkPriceTwapTs = market.amm.lastMarkPriceTwapTs;
|
|
41
41
|
const timeSinceLastMarkChange = now.sub(lastMarkPriceTwapTs);
|
|
42
42
|
const markTwapTimeSinceLastUpdate = anchor_1.BN.max(secondsInHour, secondsInHour.sub(timeSinceLastMarkChange));
|
|
43
|
-
const baseAssetPriceWithMantissa =
|
|
43
|
+
const baseAssetPriceWithMantissa = market_1.calculateMarkPrice(market);
|
|
44
44
|
const markTwapWithMantissa = markTwapTimeSinceLastUpdate
|
|
45
45
|
.mul(lastMarkTwapWithMantissa)
|
|
46
46
|
.add(timeSinceLastMarkChange.mul(baseAssetPriceWithMantissa))
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
1
2
|
import { MarketsAccount, StateAccount } from '../types';
|
|
2
|
-
import BN from '
|
|
3
|
+
import { BN } from '../';
|
|
3
4
|
import { Connection } from '@solana/web3.js';
|
|
4
5
|
/**
|
|
5
6
|
* In the case of a levered loss, the exchange first pays out undistributed fees and then the insurance fund.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"insuranceFund.d.ts","sourceRoot":"","sources":["../../src/math/insuranceFund.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACxD,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"insuranceFund.d.ts","sourceRoot":"","sources":["../../src/math/insuranceFund.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACxD,OAAO,EAAE,EAAE,EAAE,MAAM,KAAK,CAAC;AACzB,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C;;;;;;;;;GASG;AACH,wBAAsB,0BAA0B,CAC/C,UAAU,EAAE,UAAU,EACtB,KAAK,EAAE,YAAY,EACnB,cAAc,EAAE,cAAc,GAC5B,OAAO,CAAC,EAAE,CAAC,CAUb"}
|
|
@@ -8,12 +8,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
12
|
exports.calculateInsuranceFundSize = void 0;
|
|
16
|
-
const
|
|
13
|
+
const __1 = require("../");
|
|
17
14
|
/**
|
|
18
15
|
* In the case of a levered loss, the exchange first pays out undistributed fees and then the insurance fund.
|
|
19
16
|
* Thus the de facto size of the insurance fund is the amount in the insurance vault plus the sum of each markets
|
|
@@ -27,9 +24,9 @@ const bn_js_1 = __importDefault(require("bn.js"));
|
|
|
27
24
|
function calculateInsuranceFundSize(connection, state, marketsAccount) {
|
|
28
25
|
return __awaiter(this, void 0, void 0, function* () {
|
|
29
26
|
const insuranceVaultPublicKey = state.insuranceVault;
|
|
30
|
-
const insuranceVaultAmount = new
|
|
27
|
+
const insuranceVaultAmount = new __1.BN((yield connection.getTokenAccountBalance(insuranceVaultPublicKey)).value.amount);
|
|
31
28
|
return marketsAccount.markets.reduce((insuranceVaultAmount, market) => {
|
|
32
|
-
return insuranceVaultAmount.add(market.amm.totalFee.div(new
|
|
29
|
+
return insuranceVaultAmount.add(market.amm.totalFee.div(new __1.BN(2)));
|
|
33
30
|
}, insuranceVaultAmount);
|
|
34
31
|
});
|
|
35
32
|
}
|
package/lib/math/market.js
CHANGED
|
@@ -9,6 +9,6 @@ const amm_1 = require("./amm");
|
|
|
9
9
|
* @return markPrice : Precision MARK_PRICE_PRECISION
|
|
10
10
|
*/
|
|
11
11
|
function calculateMarkPrice(market) {
|
|
12
|
-
return
|
|
12
|
+
return amm_1.calculatePrice(market.amm.baseAssetReserve, market.amm.quoteAssetReserve, market.amm.pegMultiplier);
|
|
13
13
|
}
|
|
14
14
|
exports.calculateMarkPrice = calculateMarkPrice;
|
package/lib/math/position.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"position.d.ts","sourceRoot":"","sources":["../../src/math/position.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"position.d.ts","sourceRoot":"","sources":["../../src/math/position.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,KAAK,CAAC;AAWzB,OAAO,EAAE,MAAM,EAAqB,YAAY,EAAE,MAAM,UAAU,CAAC;AAGnE;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CACtC,MAAM,EAAE,MAAM,EACd,YAAY,EAAE,YAAY,GACxB,EAAE,CA6BJ;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CACnC,MAAM,EAAE,MAAM,EACd,cAAc,EAAE,YAAY,EAC5B,WAAW,UAAQ,GACjB,EAAE,CAwBJ;AAED;;;;;GAKG;AACH,wBAAgB,2BAA2B,CAC1C,MAAM,EAAE,MAAM,EACd,cAAc,EAAE,YAAY,GAC1B,EAAE,CAoBJ;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,YAAY,EAAE,YAAY,GAAG,EAAE,CAUlE"}
|
package/lib/math/position.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.calculateEntryPrice = exports.calculatePositionFundingPNL = exports.calculatePositionPNL = exports.calculateBaseAssetValue = void 0;
|
|
7
|
-
const
|
|
4
|
+
const __1 = require("../");
|
|
8
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
9
6
|
const types_1 = require("../types");
|
|
10
7
|
const amm_1 = require("./amm");
|
|
@@ -22,7 +19,7 @@ function calculateBaseAssetValue(market, userPosition) {
|
|
|
22
19
|
const directionToClose = userPosition.baseAssetAmount.gt(numericConstants_1.ZERO)
|
|
23
20
|
? types_1.PositionDirection.SHORT
|
|
24
21
|
: types_1.PositionDirection.LONG;
|
|
25
|
-
const [newQuoteAssetReserve, _] =
|
|
22
|
+
const [newQuoteAssetReserve, _] = amm_1.calculateAmmReservesAfterSwap(market.amm, 'base', userPosition.baseAssetAmount.abs(), amm_1.getSwapDirection('base', directionToClose));
|
|
26
23
|
switch (directionToClose) {
|
|
27
24
|
case types_1.PositionDirection.SHORT:
|
|
28
25
|
return market.amm.quoteAssetReserve
|
|
@@ -86,7 +83,7 @@ function calculatePositionFundingPNL(market, marketPosition) {
|
|
|
86
83
|
.mul(marketPosition.baseAssetAmount)
|
|
87
84
|
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
88
85
|
.div(numericConstants_1.FUNDING_PAYMENT_PRECISION)
|
|
89
|
-
.mul(new
|
|
86
|
+
.mul(new __1.BN(-1));
|
|
90
87
|
return perPositionFundingRate;
|
|
91
88
|
}
|
|
92
89
|
exports.calculatePositionFundingPNL = calculatePositionFundingPNL;
|
package/lib/math/trade.js
CHANGED
|
@@ -25,18 +25,18 @@ const MAXPCT = new anchor_1.BN(1000); //percentage units are [0,1000] => [0,1]
|
|
|
25
25
|
* 'newPrice' => the price of the asset after the trade : Precision MARK_PRICE_PRECISION
|
|
26
26
|
*/
|
|
27
27
|
function calculateTradeSlippage(direction, amount, market, inputAssetType = 'quote') {
|
|
28
|
-
const oldPrice =
|
|
28
|
+
const oldPrice = market_1.calculateMarkPrice(market);
|
|
29
29
|
if (amount.eq(numericConstants_1.ZERO)) {
|
|
30
30
|
return [numericConstants_1.ZERO, numericConstants_1.ZERO, oldPrice, oldPrice];
|
|
31
31
|
}
|
|
32
32
|
const [acquiredBase, acquiredQuote] = calculateTradeAcquiredAmounts(direction, amount, market, inputAssetType);
|
|
33
|
-
const entryPrice =
|
|
34
|
-
const newPrice =
|
|
33
|
+
const entryPrice = amm_1.calculatePrice(acquiredBase, acquiredQuote, market.amm.pegMultiplier).mul(new anchor_1.BN(-1));
|
|
34
|
+
const newPrice = amm_1.calculatePrice(market.amm.baseAssetReserve.sub(acquiredBase), market.amm.quoteAssetReserve.sub(acquiredQuote), market.amm.pegMultiplier);
|
|
35
35
|
if (direction == types_1.PositionDirection.SHORT) {
|
|
36
|
-
|
|
36
|
+
assert_1.assert(newPrice.lt(oldPrice));
|
|
37
37
|
}
|
|
38
38
|
else {
|
|
39
|
-
|
|
39
|
+
assert_1.assert(oldPrice.lt(newPrice));
|
|
40
40
|
}
|
|
41
41
|
const pctMaxSlippage = newPrice
|
|
42
42
|
.sub(oldPrice)
|
|
@@ -64,7 +64,7 @@ function calculateTradeAcquiredAmounts(direction, amount, market, inputAssetType
|
|
|
64
64
|
if (amount.eq(numericConstants_1.ZERO)) {
|
|
65
65
|
return [numericConstants_1.ZERO, numericConstants_1.ZERO];
|
|
66
66
|
}
|
|
67
|
-
const [newQuoteAssetReserve, newBaseAssetReserve] =
|
|
67
|
+
const [newQuoteAssetReserve, newBaseAssetReserve] = amm_1.calculateAmmReservesAfterSwap(market.amm, inputAssetType, amount, amm_1.getSwapDirection(inputAssetType, direction));
|
|
68
68
|
const acquiredBase = market.amm.baseAssetReserve.sub(newBaseAssetReserve);
|
|
69
69
|
const acquiredQuote = market.amm.quoteAssetReserve.sub(newQuoteAssetReserve);
|
|
70
70
|
return [acquiredBase, acquiredQuote];
|
|
@@ -86,10 +86,10 @@ exports.calculateTradeAcquiredAmounts = calculateTradeAcquiredAmounts;
|
|
|
86
86
|
* ]
|
|
87
87
|
*/
|
|
88
88
|
function calculateTargetPriceTrade(market, targetPrice, pct = MAXPCT, outputAssetType = 'quote') {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const markPriceBefore =
|
|
89
|
+
assert_1.assert(market.amm.baseAssetReserve.gt(numericConstants_1.ZERO));
|
|
90
|
+
assert_1.assert(targetPrice.gt(numericConstants_1.ZERO));
|
|
91
|
+
assert_1.assert(pct.lte(MAXPCT) && pct.gt(numericConstants_1.ZERO));
|
|
92
|
+
const markPriceBefore = market_1.calculateMarkPrice(market);
|
|
93
93
|
if (targetPrice.gt(markPriceBefore)) {
|
|
94
94
|
const priceGap = targetPrice.sub(markPriceBefore);
|
|
95
95
|
const priceGapScaled = priceGap.mul(pct).div(MAXPCT);
|
|
@@ -114,11 +114,11 @@ function calculateTargetPriceTrade(market, targetPrice, pct = MAXPCT, outputAsse
|
|
|
114
114
|
let markPriceAfter;
|
|
115
115
|
if (markPriceBefore.gt(targetPrice)) {
|
|
116
116
|
// overestimate y2
|
|
117
|
-
baseAssetReserveAfter =
|
|
117
|
+
baseAssetReserveAfter = utils_1.squareRootBN(k.div(targetPrice).mul(peg).div(numericConstants_1.PEG_PRECISION).sub(biasModifier)).sub(new anchor_1.BN(1));
|
|
118
118
|
quoteAssetReserveAfter = k
|
|
119
119
|
.div(numericConstants_1.MARK_PRICE_PRECISION)
|
|
120
120
|
.div(baseAssetReserveAfter);
|
|
121
|
-
markPriceAfter =
|
|
121
|
+
markPriceAfter = amm_1.calculatePrice(baseAssetReserveAfter, quoteAssetReserveAfter, peg);
|
|
122
122
|
direction = types_1.PositionDirection.SHORT;
|
|
123
123
|
tradeSize = quoteAssetReserveBefore
|
|
124
124
|
.sub(quoteAssetReserveAfter)
|
|
@@ -129,11 +129,11 @@ function calculateTargetPriceTrade(market, targetPrice, pct = MAXPCT, outputAsse
|
|
|
129
129
|
}
|
|
130
130
|
else if (markPriceBefore.lt(targetPrice)) {
|
|
131
131
|
// underestimate y2
|
|
132
|
-
baseAssetReserveAfter =
|
|
132
|
+
baseAssetReserveAfter = utils_1.squareRootBN(k.div(targetPrice).mul(peg).div(numericConstants_1.PEG_PRECISION).add(biasModifier)).add(new anchor_1.BN(1));
|
|
133
133
|
quoteAssetReserveAfter = k
|
|
134
134
|
.div(numericConstants_1.MARK_PRICE_PRECISION)
|
|
135
135
|
.div(baseAssetReserveAfter);
|
|
136
|
-
markPriceAfter =
|
|
136
|
+
markPriceAfter = amm_1.calculatePrice(baseAssetReserveAfter, quoteAssetReserveAfter, peg);
|
|
137
137
|
direction = types_1.PositionDirection.LONG;
|
|
138
138
|
tradeSize = quoteAssetReserveAfter
|
|
139
139
|
.sub(quoteAssetReserveBefore)
|
|
@@ -160,8 +160,8 @@ function calculateTargetPriceTrade(market, targetPrice, pct = MAXPCT, outputAsse
|
|
|
160
160
|
.mul(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
|
|
161
161
|
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
162
162
|
.div(baseSize.abs());
|
|
163
|
-
|
|
164
|
-
|
|
163
|
+
assert_1.assert(tp1.sub(tp2).lte(originalDiff), 'Target Price Calculation incorrect');
|
|
164
|
+
assert_1.assert(tp2.lte(tp1) || tp2.sub(tp1).abs() < 100000, 'Target Price Calculation incorrect' +
|
|
165
165
|
tp2.toString() +
|
|
166
166
|
'>=' +
|
|
167
167
|
tp1.toString() +
|
package/lib/math/utils.d.ts
CHANGED
package/lib/math/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/math/utils.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/math/utils.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,KAAK,CAAC;AAEzB,eAAO,MAAM,YAAY,iCA0BxB,CAAC"}
|
package/lib/math/utils.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.squareRootBN = void 0;
|
|
4
|
-
const
|
|
5
|
-
const squareRootBN = (n, closeness = new
|
|
4
|
+
const __1 = require("../");
|
|
5
|
+
const squareRootBN = (n, closeness = new __1.BN(1)) => {
|
|
6
6
|
// Assuming the sqrt of n as n only
|
|
7
7
|
let x = n;
|
|
8
8
|
// The closed guess will be stored in the root
|
|
9
9
|
let root;
|
|
10
10
|
// To count the number of iterations
|
|
11
11
|
let count = 0;
|
|
12
|
-
const TWO = new
|
|
12
|
+
const TWO = new __1.BN(2);
|
|
13
13
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
14
14
|
while (count < Number.MAX_SAFE_INTEGER) {
|
|
15
15
|
count++;
|
package/lib/mockUSDCFaucet.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
1
2
|
import * as anchor from '@project-serum/anchor';
|
|
2
3
|
import { Program, Provider } from '@project-serum/anchor';
|
|
3
4
|
import { AccountInfo } from '@solana/spl-token';
|
|
4
5
|
import { ConfirmOptions, Connection, PublicKey, TransactionInstruction, TransactionSignature } from '@solana/web3.js';
|
|
5
|
-
import BN from '
|
|
6
|
+
import { BN } from '.';
|
|
6
7
|
import { IWallet } from './types';
|
|
7
8
|
export declare class MockUSDCFaucet {
|
|
8
9
|
connection: Connection;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mockUSDCFaucet.d.ts","sourceRoot":"","sources":["../src/mockUSDCFaucet.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,uBAAuB,CAAC;AAChD,OAAO,EAAO,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAC/D,OAAO,EACN,WAAW,EAKX,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACN,cAAc,EACd,UAAU,EACV,SAAS,EAIT,sBAAsB,EACtB,oBAAoB,EACpB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"mockUSDCFaucet.d.ts","sourceRoot":"","sources":["../src/mockUSDCFaucet.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,MAAM,MAAM,uBAAuB,CAAC;AAChD,OAAO,EAAO,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAC/D,OAAO,EACN,WAAW,EAKX,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACN,cAAc,EACd,UAAU,EACV,SAAS,EAIT,sBAAsB,EACtB,oBAAoB,EACpB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,EAAE,EAAE,MAAM,GAAG,CAAC;AAEvB,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC,qBAAa,cAAc;IAC1B,UAAU,EAAE,UAAU,CAAC;IACvB,MAAM,EAAE,OAAO,CAAC;IACT,OAAO,EAAE,OAAO,CAAC;IACxB,QAAQ,EAAE,QAAQ,CAAC;IACnB,IAAI,CAAC,EAAE,cAAc,CAAC;gBAGrB,UAAU,EAAE,UAAU,EACtB,MAAM,EAAE,OAAO,EACf,SAAS,EAAE,SAAS,EACpB,IAAI,CAAC,EAAE,cAAc;IAUT,uCAAuC,IAAI,OAAO,CAC9D;QAAC,SAAS;QAAE,MAAM;KAAC,CACnB;IAOD,4BAA4B,CAAC,EAAE,SAAS,CAAC;IAC5B,+BAA+B,IAAI,OAAO,CAAC,SAAS,CAAC;IAUrD,UAAU,IAAI,OAAO,CAAC,oBAAoB,CAAC;IA8C3C,UAAU,IAAI,OAAO,CAAC,GAAG,CAAC;IAM1B,UAAU,CACtB,gBAAgB,EAAE,SAAS,EAC3B,MAAM,EAAE,EAAE,GACR,OAAO,CAAC,oBAAoB,CAAC;IAanB,qCAAqC,CACjD,aAAa,EAAE,SAAS,EACxB,MAAM,EAAE,EAAE,GACR,OAAO,CAAC,CAAC,SAAS,EAAE,oBAAoB,CAAC,CAAC;IAWhC,iDAAiD,CAC7D,aAAa,EAAE,SAAS,EACxB,MAAM,EAAE,EAAE,GACR,OAAO,CAAC,CAAC,SAAS,EAAE,sBAAsB,EAAE,sBAAsB,CAAC,CAAC;IA8B1D,gCAAgC,CAAC,KAAK,EAAE;QACpD,UAAU,EAAE,SAAS,CAAC;KACtB,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC;IAWrB,mBAAmB,CAAC,KAAK,EAAE;QACvC,UAAU,EAAE,SAAS,CAAC;KACtB,GAAG,OAAO,CAAC,WAAW,CAAC;IAiBX,uBAAuB,CAAC,KAAK,EAAE;QAC3C,UAAU,EAAE,SAAS,CAAC;QACtB,QAAQ,EAAE,CAAC,WAAW,EAAE,WAAW,KAAK,IAAI,CAAC;KAC7C,GAAG,OAAO,CAAC,OAAO,CAAC;CAuBpB"}
|
package/lib/pythClient.js
CHANGED
|
@@ -18,7 +18,7 @@ class PythClient {
|
|
|
18
18
|
getPriceData(pricePublicKey) {
|
|
19
19
|
return __awaiter(this, void 0, void 0, function* () {
|
|
20
20
|
const account = yield this.connection.getAccountInfo(pricePublicKey);
|
|
21
|
-
return
|
|
21
|
+
return client_1.parsePriceData(account.data);
|
|
22
22
|
});
|
|
23
23
|
}
|
|
24
24
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/token/index.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,WAAW,EAAsB,MAAM,mBAAmB,CAAC;AAGpE,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,CAiC3D"}
|