@drift-labs/sdk 0.2.0-master.3 → 0.2.0-master.30
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/README.md +13 -13
- package/lib/accounts/bulkUserStatsSubscription.d.ts +7 -0
- package/lib/accounts/bulkUserStatsSubscription.js +21 -0
- package/lib/accounts/bulkUserSubscription.js +0 -1
- package/lib/accounts/fetch.d.ts +2 -1
- package/lib/accounts/fetch.js +9 -1
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +16 -16
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +31 -28
- package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
- package/lib/accounts/types.d.ts +22 -11
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +17 -17
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +37 -35
- package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
- package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
- package/lib/addresses/marketAddresses.d.ts +1 -3
- package/lib/addresses/pda.d.ts +10 -4
- package/lib/addresses/pda.js +51 -14
- package/lib/admin.d.ts +34 -22
- package/lib/admin.js +182 -73
- package/lib/clearingHouse.d.ts +120 -42
- package/lib/clearingHouse.js +1505 -254
- package/lib/clearingHouseConfig.d.ts +4 -4
- package/lib/clearingHouseUser.d.ts +50 -38
- package/lib/clearingHouseUser.js +410 -190
- package/lib/clearingHouseUserStats.d.ts +18 -0
- package/lib/clearingHouseUserStats.js +49 -0
- package/lib/clearingHouseUserStatsConfig.d.ts +14 -0
- package/lib/clearingHouseUserStatsConfig.js +2 -0
- package/lib/config.d.ts +7 -9
- package/lib/config.js +21 -21
- package/lib/constants/numericConstants.d.ts +18 -12
- package/lib/constants/numericConstants.js +28 -21
- package/lib/constants/perpMarkets.d.ts +18 -0
- package/lib/constants/{markets.js → perpMarkets.js} +7 -7
- package/lib/constants/spotMarkets.d.ts +19 -0
- package/lib/constants/spotMarkets.js +53 -0
- package/lib/dlob/DLOB.d.ts +73 -0
- package/lib/dlob/DLOB.js +553 -0
- package/lib/dlob/DLOBNode.d.ts +52 -0
- package/lib/dlob/DLOBNode.js +82 -0
- package/lib/dlob/NodeList.d.ts +26 -0
- package/lib/dlob/NodeList.js +138 -0
- package/lib/events/eventList.js +3 -0
- package/lib/events/eventSubscriber.d.ts +4 -2
- package/lib/events/eventSubscriber.js +16 -9
- package/lib/events/fetchLogs.d.ts +10 -1
- package/lib/events/fetchLogs.js +27 -7
- package/lib/events/pollingLogProvider.d.ts +2 -1
- package/lib/events/pollingLogProvider.js +6 -2
- package/lib/events/sort.js +8 -11
- package/lib/events/types.d.ts +7 -2
- package/lib/events/types.js +5 -0
- package/lib/examples/makeTradeExample.js +20 -8
- package/lib/factory/bigNum.d.ts +1 -0
- package/lib/factory/bigNum.js +34 -10
- package/lib/idl/clearing_house.json +4313 -1433
- package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/lib/index.d.ts +21 -6
- package/lib/index.js +25 -6
- package/lib/math/amm.d.ts +8 -5
- package/lib/math/amm.js +68 -46
- package/lib/math/auction.js +4 -1
- package/lib/math/conversion.js +1 -1
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +11 -10
- package/lib/math/insurance.d.ts +4 -0
- package/lib/math/insurance.js +27 -0
- package/lib/math/margin.d.ts +11 -0
- package/lib/math/margin.js +77 -0
- package/lib/math/market.d.ts +14 -9
- package/lib/math/market.js +69 -7
- package/lib/math/oracles.d.ts +4 -0
- package/lib/math/oracles.js +36 -8
- package/lib/math/orders.d.ts +6 -2
- package/lib/math/orders.js +78 -3
- package/lib/math/position.d.ts +21 -13
- package/lib/math/position.js +76 -36
- package/lib/math/repeg.js +14 -5
- package/lib/math/spotBalance.d.ts +22 -0
- package/lib/math/spotBalance.js +193 -0
- package/lib/math/spotMarket.d.ts +4 -0
- package/lib/math/spotMarket.js +8 -0
- package/lib/math/spotPosition.d.ts +6 -0
- package/lib/math/spotPosition.js +23 -0
- package/lib/math/trade.d.ts +10 -10
- package/lib/math/trade.js +22 -29
- package/lib/oracles/pythClient.js +1 -1
- package/lib/oracles/quoteAssetOracleClient.js +1 -1
- package/lib/oracles/switchboardClient.js +1 -1
- package/lib/orderParams.d.ts +14 -5
- package/lib/orderParams.js +12 -92
- package/lib/serum/serumSubscriber.d.ts +23 -0
- package/lib/serum/serumSubscriber.js +41 -0
- package/lib/serum/types.d.ts +11 -0
- package/lib/serum/types.js +2 -0
- package/lib/slot/SlotSubscriber.d.ts +7 -0
- package/lib/slot/SlotSubscriber.js +3 -0
- package/lib/{mockUSDCFaucet.d.ts → tokenFaucet.d.ts} +8 -5
- package/lib/{mockUSDCFaucet.js → tokenFaucet.js} +63 -51
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/retryTxSender.js +13 -4
- package/lib/tx/types.d.ts +1 -1
- package/lib/tx/utils.js +1 -1
- package/lib/types.d.ts +474 -123
- package/lib/types.js +99 -5
- package/lib/userMap/userMap.d.ts +25 -0
- package/lib/userMap/userMap.js +73 -0
- package/lib/userMap/userStatsMap.d.ts +19 -0
- package/lib/userMap/userStatsMap.js +68 -0
- package/lib/util/computeUnits.js +1 -1
- package/lib/util/getTokenAddress.d.ts +2 -0
- package/lib/util/getTokenAddress.js +9 -0
- package/package.json +9 -5
- package/src/accounts/bulkUserStatsSubscription.ts +33 -0
- package/src/accounts/bulkUserSubscription.ts +0 -1
- package/src/accounts/fetch.ts +27 -2
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +46 -42
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
- package/src/accounts/types.ts +31 -11
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +64 -59
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
- package/src/addresses/marketAddresses.ts +1 -2
- package/src/addresses/pda.ts +88 -14
- package/src/admin.ts +333 -128
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +2464 -458
- package/src/clearingHouseConfig.ts +4 -3
- package/src/clearingHouseUser.ts +747 -291
- package/src/clearingHouseUserStats.ts +75 -0
- package/src/clearingHouseUserStatsConfig.ts +18 -0
- package/src/config.ts +30 -31
- package/src/constants/numericConstants.ts +41 -25
- package/src/constants/{markets.ts → perpMarkets.ts} +10 -10
- package/src/constants/spotMarkets.ts +72 -0
- package/src/dlob/DLOB.ts +868 -0
- package/src/dlob/DLOBNode.ts +162 -0
- package/src/dlob/NodeList.ts +185 -0
- package/src/events/eventList.js +77 -0
- package/src/events/eventList.ts +3 -0
- package/src/events/eventSubscriber.ts +20 -12
- package/src/events/fetchLogs.ts +35 -8
- package/src/events/pollingLogProvider.ts +10 -2
- package/src/events/sort.ts +11 -15
- package/src/events/types.ts +16 -1
- package/src/examples/makeTradeExample.js +157 -0
- package/src/examples/makeTradeExample.ts +32 -14
- package/src/factory/bigNum.ts +42 -13
- package/src/idl/clearing_house.json +4313 -1433
- package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/src/index.ts +21 -6
- package/src/math/amm.ts +136 -66
- package/src/math/auction.ts +5 -1
- package/src/math/conversion.ts +2 -2
- package/src/math/funding.ts +20 -18
- package/src/math/insurance.ts +35 -0
- package/src/math/margin.ts +127 -0
- package/src/math/market.ts +138 -12
- package/src/math/oracles.ts +63 -9
- package/src/math/orders.ts +138 -4
- package/src/math/position.ts +119 -58
- package/src/math/repeg.ts +16 -6
- package/src/math/spotBalance.ts +316 -0
- package/src/math/spotMarket.ts +9 -0
- package/src/math/spotPosition.ts +47 -0
- package/src/math/trade.ts +43 -49
- package/src/oracles/pythClient.ts +2 -2
- package/src/oracles/quoteAssetOracleClient.ts +2 -2
- package/src/oracles/switchboardClient.ts +2 -2
- package/src/orderParams.ts +24 -137
- package/src/serum/serumSubscriber.ts +80 -0
- package/src/serum/types.ts +13 -0
- package/src/slot/SlotSubscriber.ts +11 -1
- package/src/token/index.js +38 -0
- package/src/{mockUSDCFaucet.ts → tokenFaucet.ts} +82 -70
- package/src/tx/retryTxSender.ts +16 -5
- package/src/tx/types.js +2 -0
- package/src/tx/types.ts +2 -1
- package/src/tx/utils.js +17 -0
- package/src/tx/utils.ts +1 -1
- package/src/types.ts +477 -125
- package/src/userMap/userMap.ts +100 -0
- package/src/userMap/userStatsMap.ts +110 -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/getTokenAddress.ts +18 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/tests/bn/test.ts +12 -3
- package/tests/dlob/helpers.ts +374 -0
- package/tests/dlob/test.ts +2865 -0
- package/lib/constants/banks.d.ts +0 -16
- package/lib/constants/banks.js +0 -34
- package/lib/constants/markets.d.ts +0 -19
- package/lib/math/bankBalance.d.ts +0 -9
- package/lib/math/bankBalance.js +0 -75
- package/lib/math/state.d.ts +0 -8
- package/lib/math/state.js +0 -15
- package/lib/orders.d.ts +0 -8
- package/lib/orders.js +0 -134
- package/src/constants/banks.ts +0 -43
- package/src/math/bankBalance.ts +0 -112
- package/src/math/state.ts +0 -14
- package/src/math/utils.js +0 -27
- package/src/math/utils.js.map +0 -1
- package/src/orders.ts +0 -244
- package/src/util/computeUnits.js.map +0 -1
package/lib/constants/banks.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/// <reference types="bn.js" />
|
|
2
|
-
import { BN, OracleSource } from '../';
|
|
3
|
-
import { DriftEnv } from '../';
|
|
4
|
-
import { PublicKey } from '@solana/web3.js';
|
|
5
|
-
export declare type BankConfig = {
|
|
6
|
-
symbol: string;
|
|
7
|
-
bankIndex: BN;
|
|
8
|
-
oracle: PublicKey;
|
|
9
|
-
mint: PublicKey;
|
|
10
|
-
oracleSource: OracleSource;
|
|
11
|
-
};
|
|
12
|
-
export declare const DevnetBanks: BankConfig[];
|
|
13
|
-
export declare const MainnetBanks: BankConfig[];
|
|
14
|
-
export declare const Banks: {
|
|
15
|
-
[key in DriftEnv]: BankConfig[];
|
|
16
|
-
};
|
package/lib/constants/banks.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Banks = exports.MainnetBanks = exports.DevnetBanks = void 0;
|
|
4
|
-
const __1 = require("../");
|
|
5
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
-
exports.DevnetBanks = [
|
|
7
|
-
{
|
|
8
|
-
symbol: 'USDC',
|
|
9
|
-
bankIndex: new __1.BN(0),
|
|
10
|
-
oracle: web3_js_1.PublicKey.default,
|
|
11
|
-
oracleSource: __1.OracleSource.QUOTE_ASSET,
|
|
12
|
-
mint: new web3_js_1.PublicKey('8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2'),
|
|
13
|
-
},
|
|
14
|
-
{
|
|
15
|
-
symbol: 'BTC',
|
|
16
|
-
bankIndex: new __1.BN(1),
|
|
17
|
-
oracle: new web3_js_1.PublicKey('HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J'),
|
|
18
|
-
oracleSource: __1.OracleSource.PYTH,
|
|
19
|
-
mint: new web3_js_1.PublicKey('Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr'),
|
|
20
|
-
},
|
|
21
|
-
];
|
|
22
|
-
exports.MainnetBanks = [
|
|
23
|
-
{
|
|
24
|
-
symbol: 'USDC',
|
|
25
|
-
bankIndex: new __1.BN(0),
|
|
26
|
-
oracle: web3_js_1.PublicKey.default,
|
|
27
|
-
oracleSource: __1.OracleSource.QUOTE_ASSET,
|
|
28
|
-
mint: new web3_js_1.PublicKey('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'),
|
|
29
|
-
},
|
|
30
|
-
];
|
|
31
|
-
exports.Banks = {
|
|
32
|
-
devnet: exports.DevnetBanks,
|
|
33
|
-
'mainnet-beta': exports.MainnetBanks,
|
|
34
|
-
};
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/// <reference types="bn.js" />
|
|
2
|
-
import { BN, OracleSource } from '../';
|
|
3
|
-
import { DriftEnv } from '../';
|
|
4
|
-
import { PublicKey } from '@solana/web3.js';
|
|
5
|
-
export declare type MarketConfig = {
|
|
6
|
-
fullName?: string;
|
|
7
|
-
category?: string[];
|
|
8
|
-
symbol: string;
|
|
9
|
-
baseAssetSymbol: string;
|
|
10
|
-
marketIndex: BN;
|
|
11
|
-
launchTs: number;
|
|
12
|
-
oracle: PublicKey;
|
|
13
|
-
oracleSource: OracleSource;
|
|
14
|
-
};
|
|
15
|
-
export declare const DevnetMarkets: MarketConfig[];
|
|
16
|
-
export declare const MainnetMarkets: MarketConfig[];
|
|
17
|
-
export declare const Markets: {
|
|
18
|
-
[key in DriftEnv]: MarketConfig[];
|
|
19
|
-
};
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/// <reference types="bn.js" />
|
|
2
|
-
import { BankAccount, BankBalanceType } from '../types';
|
|
3
|
-
import { BN } from '@project-serum/anchor';
|
|
4
|
-
export declare function getBalance(tokenAmount: BN, bank: BankAccount, balanceType: BankBalanceType): BN;
|
|
5
|
-
export declare function getTokenAmount(balanceAmount: BN, bank: BankAccount, balanceType: BankBalanceType): BN;
|
|
6
|
-
export declare function calculateInterestAccumulated(bank: BankAccount, now: BN): {
|
|
7
|
-
borrowInterest: BN;
|
|
8
|
-
depositInterest: BN;
|
|
9
|
-
};
|
package/lib/math/bankBalance.js
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateInterestAccumulated = exports.getTokenAmount = exports.getBalance = void 0;
|
|
4
|
-
const types_1 = require("../types");
|
|
5
|
-
const anchor_1 = require("@project-serum/anchor");
|
|
6
|
-
const numericConstants_1 = require("../constants/numericConstants");
|
|
7
|
-
function getBalance(tokenAmount, bank, balanceType) {
|
|
8
|
-
const precisionIncrease = numericConstants_1.TEN.pow(new anchor_1.BN(16 - bank.decimals));
|
|
9
|
-
const cumulativeInterest = (0, types_1.isVariant)(balanceType, 'deposit')
|
|
10
|
-
? bank.cumulativeDepositInterest
|
|
11
|
-
: bank.cumulativeBorrowInterest;
|
|
12
|
-
let balance = tokenAmount.mul(precisionIncrease).div(cumulativeInterest);
|
|
13
|
-
if (!balance.eq(numericConstants_1.ZERO) && (0, types_1.isVariant)(balanceType, 'borrow')) {
|
|
14
|
-
balance = balance.add(numericConstants_1.ONE);
|
|
15
|
-
}
|
|
16
|
-
return balance;
|
|
17
|
-
}
|
|
18
|
-
exports.getBalance = getBalance;
|
|
19
|
-
function getTokenAmount(balanceAmount, bank, balanceType) {
|
|
20
|
-
const precisionDecrease = numericConstants_1.TEN.pow(new anchor_1.BN(16 - bank.decimals));
|
|
21
|
-
const cumulativeInterest = (0, types_1.isVariant)(balanceType, 'deposit')
|
|
22
|
-
? bank.cumulativeDepositInterest
|
|
23
|
-
: bank.cumulativeBorrowInterest;
|
|
24
|
-
return balanceAmount.mul(cumulativeInterest).div(precisionDecrease);
|
|
25
|
-
}
|
|
26
|
-
exports.getTokenAmount = getTokenAmount;
|
|
27
|
-
function calculateInterestAccumulated(bank, now) {
|
|
28
|
-
const token_deposit_amount = getTokenAmount(bank.depositBalance, bank, types_1.BankBalanceType.DEPOSIT);
|
|
29
|
-
const token_borrow_amount = getTokenAmount(bank.borrowBalance, bank, types_1.BankBalanceType.BORROW);
|
|
30
|
-
let utilization;
|
|
31
|
-
if (token_borrow_amount.eq(numericConstants_1.ZERO) && token_deposit_amount.eq(numericConstants_1.ZERO)) {
|
|
32
|
-
utilization = numericConstants_1.ZERO;
|
|
33
|
-
}
|
|
34
|
-
else if (token_deposit_amount.eq(numericConstants_1.ZERO)) {
|
|
35
|
-
utilization = numericConstants_1.BANK_UTILIZATION_PRECISION;
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
utilization = token_borrow_amount
|
|
39
|
-
.mul(numericConstants_1.BANK_UTILIZATION_PRECISION)
|
|
40
|
-
.div(token_deposit_amount);
|
|
41
|
-
}
|
|
42
|
-
let interest_rate;
|
|
43
|
-
if (utilization.gt(bank.optimalUtilization)) {
|
|
44
|
-
const surplusUtilization = utilization.sub(bank.optimalUtilization);
|
|
45
|
-
const borrowRateSlope = bank.maxBorrowRate
|
|
46
|
-
.sub(bank.optimalBorrowRate)
|
|
47
|
-
.mul(numericConstants_1.BANK_UTILIZATION_PRECISION)
|
|
48
|
-
.div(numericConstants_1.BANK_UTILIZATION_PRECISION.sub(bank.optimalUtilization));
|
|
49
|
-
interest_rate = bank.optimalBorrowRate.add(surplusUtilization.mul(borrowRateSlope).div(numericConstants_1.BANK_UTILIZATION_PRECISION));
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
const borrowRateSlope = bank.optimalBorrowRate
|
|
53
|
-
.mul(numericConstants_1.BANK_UTILIZATION_PRECISION)
|
|
54
|
-
.div(numericConstants_1.BANK_UTILIZATION_PRECISION.sub(bank.optimalUtilization));
|
|
55
|
-
interest_rate = utilization
|
|
56
|
-
.mul(borrowRateSlope)
|
|
57
|
-
.div(numericConstants_1.BANK_UTILIZATION_PRECISION);
|
|
58
|
-
}
|
|
59
|
-
const timeSinceLastUpdate = now.sub(bank.lastUpdated);
|
|
60
|
-
const modifiedBorrowRate = interest_rate.mul(timeSinceLastUpdate);
|
|
61
|
-
const modifiedDepositRate = modifiedBorrowRate
|
|
62
|
-
.mul(utilization)
|
|
63
|
-
.div(numericConstants_1.BANK_UTILIZATION_PRECISION);
|
|
64
|
-
const borrowInterest = bank.cumulativeBorrowInterest
|
|
65
|
-
.mul(modifiedBorrowRate)
|
|
66
|
-
.div(numericConstants_1.ONE_YEAR)
|
|
67
|
-
.div(numericConstants_1.BANK_INTEREST_PRECISION)
|
|
68
|
-
.add(numericConstants_1.ONE);
|
|
69
|
-
const depositInterest = bank.cumulativeDepositInterest
|
|
70
|
-
.mul(modifiedDepositRate)
|
|
71
|
-
.div(numericConstants_1.ONE_YEAR)
|
|
72
|
-
.div(numericConstants_1.BANK_INTEREST_PRECISION);
|
|
73
|
-
return { borrowInterest, depositInterest };
|
|
74
|
-
}
|
|
75
|
-
exports.calculateInterestAccumulated = calculateInterestAccumulated;
|
package/lib/math/state.d.ts
DELETED
package/lib/math/state.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
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/orders.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/// <reference types="bn.js" />
|
|
2
|
-
import { MarketAccount, Order, UserAccount, UserPosition } from './types';
|
|
3
|
-
import { BN } from '.';
|
|
4
|
-
import { OraclePriceData } from '.';
|
|
5
|
-
export declare function calculateNewStateAfterOrder(userAccount: UserAccount, userPosition: UserPosition, market: MarketAccount, order: Order): [UserAccount, UserPosition, MarketAccount] | null;
|
|
6
|
-
export declare function calculateBaseAssetAmountMarketCanExecute(market: MarketAccount, order: Order, oraclePriceData?: OraclePriceData): BN;
|
|
7
|
-
export declare function calculateAmountToTradeForLimit(market: MarketAccount, order: Order, oraclePriceData?: OraclePriceData): BN;
|
|
8
|
-
export declare function calculateAmountToTradeForTriggerLimit(market: MarketAccount, order: Order): BN;
|
package/lib/orders.js
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateAmountToTradeForTriggerLimit = exports.calculateAmountToTradeForLimit = exports.calculateBaseAssetAmountMarketCanExecute = exports.calculateNewStateAfterOrder = void 0;
|
|
4
|
-
const types_1 = require("./types");
|
|
5
|
-
const _1 = require(".");
|
|
6
|
-
const market_1 = require("./math/market");
|
|
7
|
-
const numericConstants_1 = require("./constants/numericConstants");
|
|
8
|
-
const amm_1 = require("./math/amm");
|
|
9
|
-
const position_1 = require("./math/position");
|
|
10
|
-
function calculateNewStateAfterOrder(userAccount, userPosition, market, order) {
|
|
11
|
-
if ((0, types_1.isVariant)(order.status, 'init')) {
|
|
12
|
-
return null;
|
|
13
|
-
}
|
|
14
|
-
const baseAssetAmountToTrade = calculateBaseAssetAmountMarketCanExecute(market, order);
|
|
15
|
-
if (baseAssetAmountToTrade.lt(market.amm.baseAssetAmountStepSize)) {
|
|
16
|
-
return null;
|
|
17
|
-
}
|
|
18
|
-
const userAccountAfter = Object.assign({}, userAccount);
|
|
19
|
-
const userPositionAfter = Object.assign({}, userPosition);
|
|
20
|
-
const currentPositionDirection = (0, position_1.positionCurrentDirection)(userPosition);
|
|
21
|
-
const increasePosition = userPosition.baseAssetAmount.eq(numericConstants_1.ZERO) ||
|
|
22
|
-
isSameDirection(order.direction, currentPositionDirection);
|
|
23
|
-
if (increasePosition) {
|
|
24
|
-
const marketAfter = (0, market_1.calculateNewMarketAfterTrade)(baseAssetAmountToTrade, order.direction, market);
|
|
25
|
-
const { quoteAssetAmountSwapped, baseAssetAmountSwapped } = calculateAmountSwapped(market, marketAfter);
|
|
26
|
-
userPositionAfter.baseAssetAmount = userPositionAfter.baseAssetAmount.add(baseAssetAmountSwapped);
|
|
27
|
-
userPositionAfter.quoteAssetAmount = userPositionAfter.quoteAssetAmount.add(quoteAssetAmountSwapped);
|
|
28
|
-
return [userAccountAfter, userPositionAfter, marketAfter];
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
const reversePosition = baseAssetAmountToTrade.gt(userPosition.baseAssetAmount.abs());
|
|
32
|
-
if (reversePosition) {
|
|
33
|
-
const intermediateMarket = (0, market_1.calculateNewMarketAfterTrade)(userPosition.baseAssetAmount, (0, position_1.findDirectionToClose)(userPosition), market);
|
|
34
|
-
const { quoteAssetAmountSwapped: baseAssetValue } = calculateAmountSwapped(market, intermediateMarket);
|
|
35
|
-
let pnl;
|
|
36
|
-
if ((0, types_1.isVariant)(currentPositionDirection, 'long')) {
|
|
37
|
-
pnl = baseAssetValue.sub(userPosition.quoteAssetAmount);
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
pnl = userPosition.quoteAssetAmount.sub(baseAssetValue);
|
|
41
|
-
}
|
|
42
|
-
userAccountAfter.collateral = userAccountAfter.collateral.add(pnl);
|
|
43
|
-
const baseAssetAmountLeft = baseAssetAmountToTrade.sub(userPosition.baseAssetAmount.abs());
|
|
44
|
-
const marketAfter = (0, market_1.calculateNewMarketAfterTrade)(baseAssetAmountLeft, order.direction, intermediateMarket);
|
|
45
|
-
const { quoteAssetAmountSwapped, baseAssetAmountSwapped } = calculateAmountSwapped(intermediateMarket, marketAfter);
|
|
46
|
-
userPositionAfter.quoteAssetAmount = quoteAssetAmountSwapped;
|
|
47
|
-
userPositionAfter.baseAssetAmount = baseAssetAmountSwapped;
|
|
48
|
-
return [userAccountAfter, userPositionAfter, marketAfter];
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
const marketAfter = (0, market_1.calculateNewMarketAfterTrade)(baseAssetAmountToTrade, order.direction, market);
|
|
52
|
-
const { quoteAssetAmountSwapped: baseAssetValue, baseAssetAmountSwapped, } = calculateAmountSwapped(market, marketAfter);
|
|
53
|
-
const costBasisRealized = userPosition.quoteAssetAmount
|
|
54
|
-
.mul(baseAssetAmountSwapped.abs())
|
|
55
|
-
.div(userPosition.baseAssetAmount.abs());
|
|
56
|
-
let pnl;
|
|
57
|
-
if ((0, types_1.isVariant)(currentPositionDirection, 'long')) {
|
|
58
|
-
pnl = baseAssetValue.sub(costBasisRealized);
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
pnl = costBasisRealized.sub(baseAssetValue);
|
|
62
|
-
}
|
|
63
|
-
userAccountAfter.collateral = userAccountAfter.collateral.add(pnl);
|
|
64
|
-
userPositionAfter.baseAssetAmount = userPositionAfter.baseAssetAmount.add(baseAssetAmountSwapped);
|
|
65
|
-
userPositionAfter.quoteAssetAmount =
|
|
66
|
-
userPositionAfter.quoteAssetAmount.sub(costBasisRealized);
|
|
67
|
-
return [userAccountAfter, userPositionAfter, marketAfter];
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
exports.calculateNewStateAfterOrder = calculateNewStateAfterOrder;
|
|
72
|
-
function calculateAmountSwapped(marketBefore, marketAfter) {
|
|
73
|
-
return {
|
|
74
|
-
quoteAssetAmountSwapped: marketBefore.amm.quoteAssetReserve
|
|
75
|
-
.sub(marketAfter.amm.quoteAssetReserve)
|
|
76
|
-
.abs()
|
|
77
|
-
.mul(marketBefore.amm.pegMultiplier)
|
|
78
|
-
.div(numericConstants_1.PEG_PRECISION)
|
|
79
|
-
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO),
|
|
80
|
-
baseAssetAmountSwapped: marketBefore.amm.baseAssetReserve.sub(marketAfter.amm.baseAssetReserve),
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
function calculateBaseAssetAmountMarketCanExecute(market, order, oraclePriceData) {
|
|
84
|
-
if ((0, types_1.isVariant)(order.orderType, 'limit')) {
|
|
85
|
-
return calculateAmountToTradeForLimit(market, order, oraclePriceData);
|
|
86
|
-
}
|
|
87
|
-
else if ((0, types_1.isVariant)(order.orderType, 'triggerLimit')) {
|
|
88
|
-
return calculateAmountToTradeForTriggerLimit(market, order);
|
|
89
|
-
}
|
|
90
|
-
else if ((0, types_1.isVariant)(order.orderType, 'market')) {
|
|
91
|
-
return numericConstants_1.ZERO;
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
return calculateAmountToTradeForTriggerMarket(market, order);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
exports.calculateBaseAssetAmountMarketCanExecute = calculateBaseAssetAmountMarketCanExecute;
|
|
98
|
-
function calculateAmountToTradeForLimit(market, order, oraclePriceData) {
|
|
99
|
-
let limitPrice = order.price;
|
|
100
|
-
if (!order.oraclePriceOffset.eq(numericConstants_1.ZERO)) {
|
|
101
|
-
if (!oraclePriceData) {
|
|
102
|
-
throw Error('Cant calculate limit price for oracle offset oracle without OraclePriceData');
|
|
103
|
-
}
|
|
104
|
-
limitPrice = oraclePriceData.price.add(order.oraclePriceOffset);
|
|
105
|
-
}
|
|
106
|
-
const [maxAmountToTrade, direction] = (0, amm_1.calculateMaxBaseAssetAmountToTrade)(market.amm, limitPrice, order.direction);
|
|
107
|
-
const baseAssetAmount = (0, _1.standardizeBaseAssetAmount)(maxAmountToTrade, market.amm.baseAssetAmountStepSize);
|
|
108
|
-
// Check that directions are the same
|
|
109
|
-
const sameDirection = isSameDirection(direction, order.direction);
|
|
110
|
-
if (!sameDirection) {
|
|
111
|
-
return numericConstants_1.ZERO;
|
|
112
|
-
}
|
|
113
|
-
return baseAssetAmount.gt(order.baseAssetAmount)
|
|
114
|
-
? order.baseAssetAmount
|
|
115
|
-
: baseAssetAmount;
|
|
116
|
-
}
|
|
117
|
-
exports.calculateAmountToTradeForLimit = calculateAmountToTradeForLimit;
|
|
118
|
-
function calculateAmountToTradeForTriggerLimit(market, order) {
|
|
119
|
-
if (!order.triggered) {
|
|
120
|
-
return numericConstants_1.ZERO;
|
|
121
|
-
}
|
|
122
|
-
return calculateAmountToTradeForLimit(market, order);
|
|
123
|
-
}
|
|
124
|
-
exports.calculateAmountToTradeForTriggerLimit = calculateAmountToTradeForTriggerLimit;
|
|
125
|
-
function isSameDirection(firstDirection, secondDirection) {
|
|
126
|
-
return (((0, types_1.isVariant)(firstDirection, 'long') && (0, types_1.isVariant)(secondDirection, 'long')) ||
|
|
127
|
-
((0, types_1.isVariant)(firstDirection, 'short') && (0, types_1.isVariant)(secondDirection, 'short')));
|
|
128
|
-
}
|
|
129
|
-
function calculateAmountToTradeForTriggerMarket(market, order) {
|
|
130
|
-
if (!order.triggered) {
|
|
131
|
-
return numericConstants_1.ZERO;
|
|
132
|
-
}
|
|
133
|
-
return order.baseAssetAmount;
|
|
134
|
-
}
|
package/src/constants/banks.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { BN, OracleSource } from '../';
|
|
2
|
-
import { DriftEnv } from '../';
|
|
3
|
-
import { PublicKey } from '@solana/web3.js';
|
|
4
|
-
|
|
5
|
-
export type BankConfig = {
|
|
6
|
-
symbol: string;
|
|
7
|
-
bankIndex: BN;
|
|
8
|
-
oracle: PublicKey;
|
|
9
|
-
mint: PublicKey;
|
|
10
|
-
oracleSource: OracleSource;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export const DevnetBanks: BankConfig[] = [
|
|
14
|
-
{
|
|
15
|
-
symbol: 'USDC',
|
|
16
|
-
bankIndex: new BN(0),
|
|
17
|
-
oracle: PublicKey.default,
|
|
18
|
-
oracleSource: OracleSource.QUOTE_ASSET,
|
|
19
|
-
mint: new PublicKey('8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2'),
|
|
20
|
-
},
|
|
21
|
-
{
|
|
22
|
-
symbol: 'BTC',
|
|
23
|
-
bankIndex: new BN(1),
|
|
24
|
-
oracle: new PublicKey('HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J'),
|
|
25
|
-
oracleSource: OracleSource.PYTH,
|
|
26
|
-
mint: new PublicKey('Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr'),
|
|
27
|
-
},
|
|
28
|
-
];
|
|
29
|
-
|
|
30
|
-
export const MainnetBanks: BankConfig[] = [
|
|
31
|
-
{
|
|
32
|
-
symbol: 'USDC',
|
|
33
|
-
bankIndex: new BN(0),
|
|
34
|
-
oracle: PublicKey.default,
|
|
35
|
-
oracleSource: OracleSource.QUOTE_ASSET,
|
|
36
|
-
mint: new PublicKey('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'),
|
|
37
|
-
},
|
|
38
|
-
];
|
|
39
|
-
|
|
40
|
-
export const Banks: { [key in DriftEnv]: BankConfig[] } = {
|
|
41
|
-
devnet: DevnetBanks,
|
|
42
|
-
'mainnet-beta': MainnetBanks,
|
|
43
|
-
};
|
package/src/math/bankBalance.ts
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import { BankAccount, BankBalanceType, isVariant } from '../types';
|
|
2
|
-
import { BN } from '@project-serum/anchor';
|
|
3
|
-
import {
|
|
4
|
-
BANK_UTILIZATION_PRECISION,
|
|
5
|
-
ONE,
|
|
6
|
-
TEN,
|
|
7
|
-
ZERO,
|
|
8
|
-
BANK_INTEREST_PRECISION,
|
|
9
|
-
ONE_YEAR,
|
|
10
|
-
} from '../constants/numericConstants';
|
|
11
|
-
|
|
12
|
-
export function getBalance(
|
|
13
|
-
tokenAmount: BN,
|
|
14
|
-
bank: BankAccount,
|
|
15
|
-
balanceType: BankBalanceType
|
|
16
|
-
): BN {
|
|
17
|
-
const precisionIncrease = TEN.pow(new BN(16 - bank.decimals));
|
|
18
|
-
|
|
19
|
-
const cumulativeInterest = isVariant(balanceType, 'deposit')
|
|
20
|
-
? bank.cumulativeDepositInterest
|
|
21
|
-
: bank.cumulativeBorrowInterest;
|
|
22
|
-
|
|
23
|
-
let balance = tokenAmount.mul(precisionIncrease).div(cumulativeInterest);
|
|
24
|
-
|
|
25
|
-
if (!balance.eq(ZERO) && isVariant(balanceType, 'borrow')) {
|
|
26
|
-
balance = balance.add(ONE);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return balance;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function getTokenAmount(
|
|
33
|
-
balanceAmount: BN,
|
|
34
|
-
bank: BankAccount,
|
|
35
|
-
balanceType: BankBalanceType
|
|
36
|
-
): BN {
|
|
37
|
-
const precisionDecrease = TEN.pow(new BN(16 - bank.decimals));
|
|
38
|
-
|
|
39
|
-
const cumulativeInterest = isVariant(balanceType, 'deposit')
|
|
40
|
-
? bank.cumulativeDepositInterest
|
|
41
|
-
: bank.cumulativeBorrowInterest;
|
|
42
|
-
|
|
43
|
-
return balanceAmount.mul(cumulativeInterest).div(precisionDecrease);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function calculateInterestAccumulated(
|
|
47
|
-
bank: BankAccount,
|
|
48
|
-
now: BN
|
|
49
|
-
): { borrowInterest: BN; depositInterest: BN } {
|
|
50
|
-
const token_deposit_amount = getTokenAmount(
|
|
51
|
-
bank.depositBalance,
|
|
52
|
-
bank,
|
|
53
|
-
BankBalanceType.DEPOSIT
|
|
54
|
-
);
|
|
55
|
-
const token_borrow_amount = getTokenAmount(
|
|
56
|
-
bank.borrowBalance,
|
|
57
|
-
bank,
|
|
58
|
-
BankBalanceType.BORROW
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
let utilization: BN;
|
|
62
|
-
if (token_borrow_amount.eq(ZERO) && token_deposit_amount.eq(ZERO)) {
|
|
63
|
-
utilization = ZERO;
|
|
64
|
-
} else if (token_deposit_amount.eq(ZERO)) {
|
|
65
|
-
utilization = BANK_UTILIZATION_PRECISION;
|
|
66
|
-
} else {
|
|
67
|
-
utilization = token_borrow_amount
|
|
68
|
-
.mul(BANK_UTILIZATION_PRECISION)
|
|
69
|
-
.div(token_deposit_amount);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
let interest_rate: BN;
|
|
73
|
-
if (utilization.gt(bank.optimalUtilization)) {
|
|
74
|
-
const surplusUtilization = utilization.sub(bank.optimalUtilization);
|
|
75
|
-
const borrowRateSlope = bank.maxBorrowRate
|
|
76
|
-
.sub(bank.optimalBorrowRate)
|
|
77
|
-
.mul(BANK_UTILIZATION_PRECISION)
|
|
78
|
-
.div(BANK_UTILIZATION_PRECISION.sub(bank.optimalUtilization));
|
|
79
|
-
|
|
80
|
-
interest_rate = bank.optimalBorrowRate.add(
|
|
81
|
-
surplusUtilization.mul(borrowRateSlope).div(BANK_UTILIZATION_PRECISION)
|
|
82
|
-
);
|
|
83
|
-
} else {
|
|
84
|
-
const borrowRateSlope = bank.optimalBorrowRate
|
|
85
|
-
.mul(BANK_UTILIZATION_PRECISION)
|
|
86
|
-
.div(BANK_UTILIZATION_PRECISION.sub(bank.optimalUtilization));
|
|
87
|
-
|
|
88
|
-
interest_rate = utilization
|
|
89
|
-
.mul(borrowRateSlope)
|
|
90
|
-
.div(BANK_UTILIZATION_PRECISION);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const timeSinceLastUpdate = now.sub(bank.lastUpdated);
|
|
94
|
-
|
|
95
|
-
const modifiedBorrowRate = interest_rate.mul(timeSinceLastUpdate);
|
|
96
|
-
|
|
97
|
-
const modifiedDepositRate = modifiedBorrowRate
|
|
98
|
-
.mul(utilization)
|
|
99
|
-
.div(BANK_UTILIZATION_PRECISION);
|
|
100
|
-
|
|
101
|
-
const borrowInterest = bank.cumulativeBorrowInterest
|
|
102
|
-
.mul(modifiedBorrowRate)
|
|
103
|
-
.div(ONE_YEAR)
|
|
104
|
-
.div(BANK_INTEREST_PRECISION)
|
|
105
|
-
.add(ONE);
|
|
106
|
-
const depositInterest = bank.cumulativeDepositInterest
|
|
107
|
-
.mul(modifiedDepositRate)
|
|
108
|
-
.div(ONE_YEAR)
|
|
109
|
-
.div(BANK_INTEREST_PRECISION);
|
|
110
|
-
|
|
111
|
-
return { borrowInterest, depositInterest };
|
|
112
|
-
}
|
package/src/math/state.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { StateAccount } from '../types';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Get the clearing house percent fee charged on notional of taking trades
|
|
5
|
-
*
|
|
6
|
-
* @param state
|
|
7
|
-
* @returns Precision : basis points (bps)
|
|
8
|
-
*/
|
|
9
|
-
export function getExchangeFee(state: StateAccount): number {
|
|
10
|
-
const exchangeFee =
|
|
11
|
-
state.feeStructure.feeNumerator.toNumber() /
|
|
12
|
-
state.feeStructure.feeDenominator.toNumber();
|
|
13
|
-
return exchangeFee;
|
|
14
|
-
}
|
package/src/math/utils.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.squareRootBN = void 0;
|
|
4
|
-
const __1 = require("../");
|
|
5
|
-
const squareRootBN = (n, closeness = new __1.BN(1)) => {
|
|
6
|
-
// Assuming the sqrt of n as n only
|
|
7
|
-
let x = n;
|
|
8
|
-
// The closed guess will be stored in the root
|
|
9
|
-
let root;
|
|
10
|
-
// To count the number of iterations
|
|
11
|
-
let count = 0;
|
|
12
|
-
const TWO = new __1.BN(2);
|
|
13
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
14
|
-
while (count < Number.MAX_SAFE_INTEGER) {
|
|
15
|
-
count++;
|
|
16
|
-
// Calculate more closed x
|
|
17
|
-
root = x.add(n.div(x)).div(TWO);
|
|
18
|
-
// Check for closeness
|
|
19
|
-
if (x.sub(root).abs().lte(closeness))
|
|
20
|
-
break;
|
|
21
|
-
// Update root
|
|
22
|
-
x = root;
|
|
23
|
-
}
|
|
24
|
-
return root;
|
|
25
|
-
};
|
|
26
|
-
exports.squareRootBN = squareRootBN;
|
|
27
|
-
//# sourceMappingURL=utils.js.map
|
package/src/math/utils.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["utils.ts"],"names":[],"mappings":";;;AAAA,2BAAyB;AAElB,MAAM,YAAY,GAAG,CAAC,CAAC,EAAE,SAAS,GAAG,IAAI,MAAE,CAAC,CAAC,CAAC,EAAE,EAAE;IACxD,mCAAmC;IACnC,IAAI,CAAC,GAAG,CAAC,CAAC;IAEV,8CAA8C;IAC9C,IAAI,IAAI,CAAC;IAET,oCAAoC;IACpC,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,MAAM,GAAG,GAAG,IAAI,MAAE,CAAC,CAAC,CAAC,CAAC;IAEtB,6DAA6D;IAC7D,OAAO,KAAK,GAAG,MAAM,CAAC,gBAAgB,EAAE;QACvC,KAAK,EAAE,CAAC;QAER,0BAA0B;QAC1B,IAAI,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAEhC,sBAAsB;QACtB,IAAI,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,SAAS,CAAC;YAAE,MAAM;QAE5C,cAAc;QACd,CAAC,GAAG,IAAI,CAAC;KACT;IAED,OAAO,IAAI,CAAC;AACb,CAAC,CAAC;AA1BW,QAAA,YAAY,gBA0BvB"}
|