@drift-labs/sdk 0.2.0-master.4 → 0.2.0-master.41
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 +27 -27
- package/lib/accounts/bulkAccountLoader.d.ts +2 -0
- package/lib/accounts/bulkAccountLoader.js +36 -29
- package/lib/accounts/bulkUserStatsSubscription.d.ts +7 -0
- package/lib/accounts/bulkUserStatsSubscription.js +21 -0
- package/lib/accounts/bulkUserSubscription.d.ts +2 -2
- 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 → pollingDriftClientAccountSubscriber.d.ts} +20 -25
- package/lib/accounts/{pollingClearingHouseAccountSubscriber.js → pollingDriftClientAccountSubscriber.js} +45 -49
- package/lib/accounts/{pollingOracleSubscriber.d.ts → pollingOracleAccountSubscriber.d.ts} +2 -2
- package/lib/accounts/{pollingOracleSubscriber.js → pollingOracleAccountSubscriber.js} +3 -3
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +1 -1
- package/lib/accounts/pollingUserAccountSubscriber.js +5 -11
- package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +107 -0
- package/lib/accounts/types.d.ts +26 -15
- package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +49 -0
- package/lib/accounts/{webSocketClearingHouseAccountSubscriber.js → webSocketDriftClientAccountSubscriber.js} +47 -45
- 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/marketAddresses.js +1 -1
- package/lib/addresses/pda.d.ts +15 -9
- package/lib/addresses/pda.js +73 -35
- package/lib/adminClient.d.ts +65 -0
- package/lib/adminClient.js +637 -0
- package/lib/config.d.ts +9 -9
- package/lib/config.js +25 -21
- package/lib/constants/numericConstants.d.ts +30 -12
- package/lib/constants/numericConstants.js +41 -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 +82 -0
- package/lib/dlob/DLOB.js +694 -0
- package/lib/dlob/DLOBNode.d.ts +54 -0
- package/lib/dlob/DLOBNode.js +77 -0
- package/lib/dlob/NodeList.d.ts +27 -0
- package/lib/dlob/NodeList.js +144 -0
- package/lib/driftClient.d.ts +234 -0
- package/lib/driftClient.js +2108 -0
- package/lib/{clearingHouseConfig.d.ts → driftClientConfig.d.ts} +9 -9
- package/lib/{clearingHouseConfig.js → driftClientConfig.js} +0 -0
- package/lib/events/eventList.js +3 -0
- package/lib/events/eventSubscriber.d.ts +5 -2
- package/lib/events/eventSubscriber.js +25 -11
- package/lib/events/fetchLogs.d.ts +13 -2
- package/lib/events/fetchLogs.js +40 -12
- package/lib/events/pollingLogProvider.d.ts +2 -1
- package/lib/events/pollingLogProvider.js +7 -3
- package/lib/events/sort.js +8 -11
- package/lib/events/types.d.ts +11 -3
- package/lib/events/types.js +8 -0
- package/lib/events/webSocketLogProvider.js +1 -1
- package/lib/examples/makeTradeExample.js +30 -18
- package/lib/factory/bigNum.d.ts +8 -4
- package/lib/factory/bigNum.js +109 -19
- package/lib/idl/drift.json +8386 -0
- package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/lib/index.d.ts +30 -13
- package/lib/index.js +30 -13
- package/lib/math/amm.d.ts +9 -6
- package/lib/math/amm.js +91 -38
- package/lib/math/conversion.js +1 -1
- package/lib/math/exchangeStatus.d.ts +4 -0
- package/lib/math/exchangeStatus.js +18 -0
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +23 -21
- 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 +82 -0
- package/lib/math/market.d.ts +14 -9
- package/lib/math/market.js +70 -10
- package/lib/math/oracles.d.ts +4 -0
- package/lib/math/oracles.js +36 -8
- package/lib/math/orders.d.ts +16 -6
- package/lib/math/orders.js +97 -17
- package/lib/math/position.d.ts +27 -13
- package/lib/math/position.js +92 -36
- package/lib/math/repeg.js +17 -8
- package/lib/math/spotBalance.d.ts +22 -0
- package/lib/math/spotBalance.js +192 -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 +27 -31
- 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 +4 -4
- package/lib/orderParams.js +12 -4
- package/lib/serum/serumFulfillmentConfigMap.d.ts +10 -0
- package/lib/serum/serumFulfillmentConfigMap.js +17 -0
- package/lib/serum/serumSubscriber.d.ts +27 -0
- package/lib/serum/serumSubscriber.js +56 -0
- package/lib/serum/types.d.ts +11 -0
- package/lib/{clearingHouseUserConfig.js → serum/types.js} +0 -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 +688 -216
- package/lib/types.js +153 -24
- package/lib/user.d.ts +228 -0
- package/lib/user.js +959 -0
- package/lib/userConfig.d.ts +14 -0
- package/lib/userConfig.js +2 -0
- package/lib/userMap/userMap.d.ts +41 -0
- package/lib/userMap/userMap.js +85 -0
- package/lib/userMap/userStatsMap.d.ts +19 -0
- package/lib/userMap/userStatsMap.js +68 -0
- package/lib/userName.d.ts +1 -0
- package/lib/userName.js +3 -2
- package/lib/userStats.d.ts +18 -0
- package/lib/userStats.js +49 -0
- package/lib/userStatsConfig.d.ts +14 -0
- package/lib/userStatsConfig.js +2 -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 +11 -4
- package/src/accounts/bulkAccountLoader.ts +44 -34
- package/src/accounts/bulkUserStatsSubscription.ts +33 -0
- package/src/accounts/bulkUserSubscription.ts +2 -3
- package/src/accounts/fetch.ts +27 -2
- package/src/accounts/{pollingClearingHouseAccountSubscriber.ts → pollingDriftClientAccountSubscriber.ts} +65 -75
- package/src/accounts/{pollingOracleSubscriber.ts → pollingOracleAccountSubscriber.ts} +2 -2
- package/src/accounts/pollingUserAccountSubscriber.ts +5 -12
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +166 -0
- package/src/accounts/types.ts +35 -15
- package/src/accounts/{webSocketClearingHouseAccountSubscriber.ts → webSocketDriftClientAccountSubscriber.ts} +78 -73
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
- package/src/addresses/marketAddresses.ts +3 -4
- package/src/addresses/pda.ts +105 -33
- package/src/adminClient.ts +1207 -0
- package/src/assert/assert.js +9 -0
- package/src/config.ts +37 -31
- package/src/constants/numericConstants.ts +58 -24
- package/src/constants/{markets.ts → perpMarkets.ts} +10 -10
- package/src/constants/spotMarkets.ts +73 -0
- package/src/dlob/DLOB.ts +1120 -0
- package/src/dlob/DLOBNode.ts +155 -0
- package/src/dlob/NodeList.ts +195 -0
- package/src/driftClient.ts +3594 -0
- package/src/{clearingHouseConfig.ts → driftClientConfig.ts} +9 -8
- package/src/events/eventList.js +77 -0
- package/src/events/eventList.ts +3 -0
- package/src/events/eventSubscriber.ts +36 -14
- package/src/events/fetchLogs.ts +55 -13
- package/src/events/pollingLogProvider.ts +11 -3
- package/src/events/sort.ts +11 -15
- package/src/events/types.ts +27 -2
- package/src/events/webSocketLogProvider.ts +1 -1
- package/src/examples/makeTradeExample.js +157 -0
- package/src/examples/makeTradeExample.ts +44 -28
- package/src/factory/bigNum.ts +150 -22
- package/src/idl/drift.json +8386 -0
- package/src/idl/pyth.json +98 -2
- package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/src/index.ts +30 -13
- package/src/math/amm.ts +161 -48
- package/src/math/conversion.ts +2 -2
- package/src/math/exchangeStatus.ts +31 -0
- package/src/math/funding.ts +41 -31
- package/src/math/insurance.ts +35 -0
- package/src/math/margin.ts +133 -0
- package/src/math/market.ts +143 -14
- package/src/math/oracles.ts +63 -9
- package/src/math/orders.ts +168 -26
- package/src/math/position.ts +136 -58
- package/src/math/repeg.ts +19 -9
- package/src/math/spotBalance.ts +319 -0
- package/src/math/spotMarket.ts +9 -0
- package/src/math/spotPosition.ts +47 -0
- package/src/math/trade.ts +33 -37
- 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 +16 -8
- package/src/serum/serumFulfillmentConfigMap.ts +26 -0
- package/src/serum/serumSubscriber.ts +99 -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 +650 -189
- package/src/user.ts +1599 -0
- package/src/{clearingHouseUserConfig.ts → userConfig.ts} +5 -5
- package/src/userMap/userMap.ts +124 -0
- package/src/userMap/userStatsMap.ts +108 -0
- package/src/userName.ts +2 -1
- package/src/userStats.ts +75 -0
- package/src/userStatsConfig.ts +18 -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 +46 -11
- package/tests/dlob/helpers.ts +620 -0
- package/tests/dlob/test.ts +4586 -0
- package/yarn-error.log +3160 -0
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +0 -49
- package/lib/admin.d.ts +0 -44
- package/lib/admin.js +0 -433
- package/lib/clearingHouse.d.ts +0 -133
- package/lib/clearingHouse.js +0 -931
- package/lib/clearingHouseUser.d.ts +0 -187
- package/lib/clearingHouseUser.js +0 -643
- package/lib/clearingHouseUserConfig.d.ts +0 -14
- 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/idl/clearing_house.json +0 -3998
- 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/admin.ts +0 -722
- package/src/clearingHouse.ts +0 -1451
- package/src/clearingHouseUser.ts +0 -989
- package/src/constants/banks.ts +0 -43
- package/src/idl/clearing_house.json +0 -3998
- 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/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"}
|
package/src/orders.ts
DELETED
|
@@ -1,244 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
isVariant,
|
|
3
|
-
MarketAccount,
|
|
4
|
-
Order,
|
|
5
|
-
PositionDirection,
|
|
6
|
-
UserAccount,
|
|
7
|
-
UserPosition,
|
|
8
|
-
} from './types';
|
|
9
|
-
import { BN, standardizeBaseAssetAmount } from '.';
|
|
10
|
-
import { calculateNewMarketAfterTrade } from './math/market';
|
|
11
|
-
import {
|
|
12
|
-
AMM_TO_QUOTE_PRECISION_RATIO,
|
|
13
|
-
PEG_PRECISION,
|
|
14
|
-
ZERO,
|
|
15
|
-
} from './constants/numericConstants';
|
|
16
|
-
import { calculateMaxBaseAssetAmountToTrade } from './math/amm';
|
|
17
|
-
import {
|
|
18
|
-
findDirectionToClose,
|
|
19
|
-
positionCurrentDirection,
|
|
20
|
-
} from './math/position';
|
|
21
|
-
import { OraclePriceData } from '.';
|
|
22
|
-
|
|
23
|
-
export function calculateNewStateAfterOrder(
|
|
24
|
-
userAccount: UserAccount,
|
|
25
|
-
userPosition: UserPosition,
|
|
26
|
-
market: MarketAccount,
|
|
27
|
-
order: Order
|
|
28
|
-
): [UserAccount, UserPosition, MarketAccount] | null {
|
|
29
|
-
if (isVariant(order.status, 'init')) {
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const baseAssetAmountToTrade = calculateBaseAssetAmountMarketCanExecute(
|
|
34
|
-
market,
|
|
35
|
-
order
|
|
36
|
-
);
|
|
37
|
-
if (baseAssetAmountToTrade.lt(market.amm.baseAssetAmountStepSize)) {
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const userAccountAfter = Object.assign({}, userAccount);
|
|
42
|
-
const userPositionAfter = Object.assign({}, userPosition);
|
|
43
|
-
|
|
44
|
-
const currentPositionDirection = positionCurrentDirection(userPosition);
|
|
45
|
-
const increasePosition =
|
|
46
|
-
userPosition.baseAssetAmount.eq(ZERO) ||
|
|
47
|
-
isSameDirection(order.direction, currentPositionDirection);
|
|
48
|
-
|
|
49
|
-
if (increasePosition) {
|
|
50
|
-
const marketAfter = calculateNewMarketAfterTrade(
|
|
51
|
-
baseAssetAmountToTrade,
|
|
52
|
-
order.direction,
|
|
53
|
-
market
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
const { quoteAssetAmountSwapped, baseAssetAmountSwapped } =
|
|
57
|
-
calculateAmountSwapped(market, marketAfter);
|
|
58
|
-
|
|
59
|
-
userPositionAfter.baseAssetAmount = userPositionAfter.baseAssetAmount.add(
|
|
60
|
-
baseAssetAmountSwapped
|
|
61
|
-
);
|
|
62
|
-
userPositionAfter.quoteAssetAmount = userPositionAfter.quoteAssetAmount.add(
|
|
63
|
-
quoteAssetAmountSwapped
|
|
64
|
-
);
|
|
65
|
-
|
|
66
|
-
return [userAccountAfter, userPositionAfter, marketAfter];
|
|
67
|
-
} else {
|
|
68
|
-
const reversePosition = baseAssetAmountToTrade.gt(
|
|
69
|
-
userPosition.baseAssetAmount.abs()
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
if (reversePosition) {
|
|
73
|
-
const intermediateMarket = calculateNewMarketAfterTrade(
|
|
74
|
-
userPosition.baseAssetAmount,
|
|
75
|
-
findDirectionToClose(userPosition),
|
|
76
|
-
market
|
|
77
|
-
);
|
|
78
|
-
|
|
79
|
-
const { quoteAssetAmountSwapped: baseAssetValue } =
|
|
80
|
-
calculateAmountSwapped(market, intermediateMarket);
|
|
81
|
-
|
|
82
|
-
let pnl;
|
|
83
|
-
if (isVariant(currentPositionDirection, 'long')) {
|
|
84
|
-
pnl = baseAssetValue.sub(userPosition.quoteAssetAmount);
|
|
85
|
-
} else {
|
|
86
|
-
pnl = userPosition.quoteAssetAmount.sub(baseAssetValue);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
userAccountAfter.collateral = userAccountAfter.collateral.add(pnl);
|
|
90
|
-
|
|
91
|
-
const baseAssetAmountLeft = baseAssetAmountToTrade.sub(
|
|
92
|
-
userPosition.baseAssetAmount.abs()
|
|
93
|
-
);
|
|
94
|
-
|
|
95
|
-
const marketAfter = calculateNewMarketAfterTrade(
|
|
96
|
-
baseAssetAmountLeft,
|
|
97
|
-
order.direction,
|
|
98
|
-
intermediateMarket
|
|
99
|
-
);
|
|
100
|
-
|
|
101
|
-
const { quoteAssetAmountSwapped, baseAssetAmountSwapped } =
|
|
102
|
-
calculateAmountSwapped(intermediateMarket, marketAfter);
|
|
103
|
-
|
|
104
|
-
userPositionAfter.quoteAssetAmount = quoteAssetAmountSwapped;
|
|
105
|
-
userPositionAfter.baseAssetAmount = baseAssetAmountSwapped;
|
|
106
|
-
|
|
107
|
-
return [userAccountAfter, userPositionAfter, marketAfter];
|
|
108
|
-
} else {
|
|
109
|
-
const marketAfter = calculateNewMarketAfterTrade(
|
|
110
|
-
baseAssetAmountToTrade,
|
|
111
|
-
order.direction,
|
|
112
|
-
market
|
|
113
|
-
);
|
|
114
|
-
|
|
115
|
-
const {
|
|
116
|
-
quoteAssetAmountSwapped: baseAssetValue,
|
|
117
|
-
baseAssetAmountSwapped,
|
|
118
|
-
} = calculateAmountSwapped(market, marketAfter);
|
|
119
|
-
|
|
120
|
-
const costBasisRealized = userPosition.quoteAssetAmount
|
|
121
|
-
.mul(baseAssetAmountSwapped.abs())
|
|
122
|
-
.div(userPosition.baseAssetAmount.abs());
|
|
123
|
-
|
|
124
|
-
let pnl;
|
|
125
|
-
if (isVariant(currentPositionDirection, 'long')) {
|
|
126
|
-
pnl = baseAssetValue.sub(costBasisRealized);
|
|
127
|
-
} else {
|
|
128
|
-
pnl = costBasisRealized.sub(baseAssetValue);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
userAccountAfter.collateral = userAccountAfter.collateral.add(pnl);
|
|
132
|
-
|
|
133
|
-
userPositionAfter.baseAssetAmount = userPositionAfter.baseAssetAmount.add(
|
|
134
|
-
baseAssetAmountSwapped
|
|
135
|
-
);
|
|
136
|
-
userPositionAfter.quoteAssetAmount =
|
|
137
|
-
userPositionAfter.quoteAssetAmount.sub(costBasisRealized);
|
|
138
|
-
|
|
139
|
-
return [userAccountAfter, userPositionAfter, marketAfter];
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
function calculateAmountSwapped(
|
|
145
|
-
marketBefore: MarketAccount,
|
|
146
|
-
marketAfter: MarketAccount
|
|
147
|
-
): { quoteAssetAmountSwapped: BN; baseAssetAmountSwapped: BN } {
|
|
148
|
-
return {
|
|
149
|
-
quoteAssetAmountSwapped: marketBefore.amm.quoteAssetReserve
|
|
150
|
-
.sub(marketAfter.amm.quoteAssetReserve)
|
|
151
|
-
.abs()
|
|
152
|
-
.mul(marketBefore.amm.pegMultiplier)
|
|
153
|
-
.div(PEG_PRECISION)
|
|
154
|
-
.div(AMM_TO_QUOTE_PRECISION_RATIO),
|
|
155
|
-
baseAssetAmountSwapped: marketBefore.amm.baseAssetReserve.sub(
|
|
156
|
-
marketAfter.amm.baseAssetReserve
|
|
157
|
-
),
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
export function calculateBaseAssetAmountMarketCanExecute(
|
|
162
|
-
market: MarketAccount,
|
|
163
|
-
order: Order,
|
|
164
|
-
oraclePriceData?: OraclePriceData
|
|
165
|
-
): BN {
|
|
166
|
-
if (isVariant(order.orderType, 'limit')) {
|
|
167
|
-
return calculateAmountToTradeForLimit(market, order, oraclePriceData);
|
|
168
|
-
} else if (isVariant(order.orderType, 'triggerLimit')) {
|
|
169
|
-
return calculateAmountToTradeForTriggerLimit(market, order);
|
|
170
|
-
} else if (isVariant(order.orderType, 'market')) {
|
|
171
|
-
return ZERO;
|
|
172
|
-
} else {
|
|
173
|
-
return calculateAmountToTradeForTriggerMarket(market, order);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
export function calculateAmountToTradeForLimit(
|
|
178
|
-
market: MarketAccount,
|
|
179
|
-
order: Order,
|
|
180
|
-
oraclePriceData?: OraclePriceData
|
|
181
|
-
): BN {
|
|
182
|
-
let limitPrice = order.price;
|
|
183
|
-
if (!order.oraclePriceOffset.eq(ZERO)) {
|
|
184
|
-
if (!oraclePriceData) {
|
|
185
|
-
throw Error(
|
|
186
|
-
'Cant calculate limit price for oracle offset oracle without OraclePriceData'
|
|
187
|
-
);
|
|
188
|
-
}
|
|
189
|
-
limitPrice = oraclePriceData.price.add(order.oraclePriceOffset);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
const [maxAmountToTrade, direction] = calculateMaxBaseAssetAmountToTrade(
|
|
193
|
-
market.amm,
|
|
194
|
-
limitPrice,
|
|
195
|
-
order.direction
|
|
196
|
-
);
|
|
197
|
-
|
|
198
|
-
const baseAssetAmount = standardizeBaseAssetAmount(
|
|
199
|
-
maxAmountToTrade,
|
|
200
|
-
market.amm.baseAssetAmountStepSize
|
|
201
|
-
);
|
|
202
|
-
|
|
203
|
-
// Check that directions are the same
|
|
204
|
-
const sameDirection = isSameDirection(direction, order.direction);
|
|
205
|
-
if (!sameDirection) {
|
|
206
|
-
return ZERO;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
return baseAssetAmount.gt(order.baseAssetAmount)
|
|
210
|
-
? order.baseAssetAmount
|
|
211
|
-
: baseAssetAmount;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
export function calculateAmountToTradeForTriggerLimit(
|
|
215
|
-
market: MarketAccount,
|
|
216
|
-
order: Order
|
|
217
|
-
): BN {
|
|
218
|
-
if (!order.triggered) {
|
|
219
|
-
return ZERO;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
return calculateAmountToTradeForLimit(market, order);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
function isSameDirection(
|
|
226
|
-
firstDirection: PositionDirection,
|
|
227
|
-
secondDirection: PositionDirection
|
|
228
|
-
): boolean {
|
|
229
|
-
return (
|
|
230
|
-
(isVariant(firstDirection, 'long') && isVariant(secondDirection, 'long')) ||
|
|
231
|
-
(isVariant(firstDirection, 'short') && isVariant(secondDirection, 'short'))
|
|
232
|
-
);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
function calculateAmountToTradeForTriggerMarket(
|
|
236
|
-
market: MarketAccount,
|
|
237
|
-
order: Order
|
|
238
|
-
): BN {
|
|
239
|
-
if (!order.triggered) {
|
|
240
|
-
return ZERO;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
return order.baseAssetAmount;
|
|
244
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"computeUnits.js","sourceRoot":"","sources":["computeUnits.ts"],"names":[],"mappings":";;;AAEO,KAAK,UAAU,0BAA0B,CAC/C,SAAoB,EACpB,UAAsB,EACtB,WAAmB,EACnB,aAAuB,WAAW;IAElC,MAAM,EAAE,GAAG,MAAM,UAAU,CAAC,cAAc,CAAC,WAAW,EAAE,EAAE,UAAU,EAAE,CAAC,CAAC;IACxE,MAAM,YAAY,GAAG,EAAE,CAAC;IACxB,MAAM,KAAK,GAAG,IAAI,MAAM,CACvB,WAAW,SAAS,CAAC,QAAQ,EAAE,gDAAgD,CAC/E,CAAC;IACF,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE;QAC1C,MAAM,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACtC,IAAI,KAAK,IAAI,KAAK,CAAC,CAAC,CAAC,EAAE;YACtB,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;SAC5B;IACF,CAAC,CAAC,CAAC;IACH,OAAO,YAAY,CAAC;AACrB,CAAC;AAlBD,gEAkBC"}
|