@velocity-exchange/sdk 0.2.2 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/lib/browser/adminClient.d.ts +4 -2
- package/lib/browser/adminClient.js +21 -4
- package/lib/browser/config.js +1 -1
- package/lib/browser/constants/numericConstants.d.ts +1 -0
- package/lib/browser/constants/numericConstants.js +2 -1
- package/lib/browser/constants/spotMarkets.js +1 -1
- package/lib/browser/idl/velocity.d.ts +61 -9
- package/lib/browser/idl/velocity.json +61 -9
- package/lib/browser/index.d.ts +1 -0
- package/lib/browser/index.js +1 -0
- package/lib/browser/math/funding.js +20 -4
- package/lib/browser/math/liquidation.d.ts +14 -0
- package/lib/browser/math/liquidation.js +14 -0
- package/lib/browser/math/spotMarket.d.ts +6 -0
- package/lib/browser/math/spotMarket.js +16 -1
- package/lib/browser/tx/forwardOnlyTxSender.d.ts +37 -0
- package/lib/browser/tx/forwardOnlyTxSender.js +92 -0
- package/lib/browser/types.d.ts +77 -9
- package/lib/browser/userMap/userMap.js +5 -1
- package/lib/node/adminClient.d.ts +4 -2
- package/lib/node/adminClient.d.ts.map +1 -1
- package/lib/node/adminClient.js +21 -4
- package/lib/node/config.js +1 -1
- package/lib/node/constants/numericConstants.d.ts +1 -0
- package/lib/node/constants/numericConstants.d.ts.map +1 -1
- package/lib/node/constants/numericConstants.js +2 -1
- package/lib/node/constants/spotMarkets.js +1 -1
- package/lib/node/idl/velocity.d.ts +61 -9
- package/lib/node/idl/velocity.d.ts.map +1 -1
- package/lib/node/idl/velocity.json +61 -9
- package/lib/node/index.d.ts +1 -0
- package/lib/node/index.d.ts.map +1 -1
- package/lib/node/index.js +1 -0
- package/lib/node/math/funding.d.ts.map +1 -1
- package/lib/node/math/funding.js +20 -4
- package/lib/node/math/liquidation.d.ts +14 -0
- package/lib/node/math/liquidation.d.ts.map +1 -1
- package/lib/node/math/liquidation.js +14 -0
- package/lib/node/math/spotMarket.d.ts +6 -0
- package/lib/node/math/spotMarket.d.ts.map +1 -1
- package/lib/node/math/spotMarket.js +16 -1
- package/lib/node/tx/forwardOnlyTxSender.d.ts +38 -0
- package/lib/node/tx/forwardOnlyTxSender.d.ts.map +1 -0
- package/lib/node/tx/forwardOnlyTxSender.js +92 -0
- package/lib/node/types.d.ts +77 -9
- package/lib/node/types.d.ts.map +1 -1
- package/lib/node/userMap/userMap.d.ts.map +1 -1
- package/lib/node/userMap/userMap.js +5 -1
- package/package.json +1 -1
- package/src/adminClient.ts +53 -3
- package/src/config.ts +1 -1
- package/src/constants/numericConstants.ts +1 -0
- package/src/constants/spotMarkets.ts +1 -1
- package/src/idl/velocity.json +61 -9
- package/src/idl/velocity.ts +61 -9
- package/src/index.ts +1 -0
- package/src/math/funding.ts +23 -5
- package/src/math/liquidation.ts +14 -0
- package/src/math/spotMarket.ts +28 -2
- package/src/tx/forwardOnlyTxSender.ts +145 -0
- package/src/types.ts +84 -9
- package/src/userMap/userMap.ts +5 -1
- package/tests/amm/test.ts +2 -2
- package/tests/dlob/helpers.ts +26 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @velocity-exchange/sdk
|
|
2
2
|
|
|
3
|
+
## 0.2.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#127](https://github.com/drift-labs/protocol-v2-shadow/pull/127) [`4f8e7aa`](https://github.com/drift-labs/protocol-v2-shadow/commit/4f8e7aaef0e35b190fc0b91cd29314d902d1ccab) Thanks [@ChesterSim](https://github.com/ChesterSim)! - reflect Typescript types on IDL changes
|
|
8
|
+
|
|
9
|
+
## 0.2.3
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#100](https://github.com/drift-labs/protocol-v2-shadow/pull/100) [`ae78769`](https://github.com/drift-labs/protocol-v2-shadow/commit/ae78769ef58355202c030435c2796ef045fe30a0) Thanks [@ChesterSim](https://github.com/ChesterSim)! - add back ForwardOnlyTxSender and calculateMaxRemainingDeposit
|
|
14
|
+
|
|
3
15
|
## 0.2.2
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
|
@@ -22,8 +22,8 @@ export declare class AdminClient extends VelocityClient {
|
|
|
22
22
|
}, scaleInitialAssetWeightStart?: BN, withdrawGuardThreshold?: BN, orderTickSize?: BN, orderStepSize?: BN, ifTotalFactor?: number, name?: string, marketIndex?: number): Promise<TransactionInstruction>;
|
|
23
23
|
deleteInitializedSpotMarket(marketIndex: number): Promise<TransactionSignature>;
|
|
24
24
|
getDeleteInitializedSpotMarketIx(marketIndex: number): Promise<TransactionInstruction>;
|
|
25
|
-
initializePerpMarket(marketIndex: number, priceOracle: PublicKey, baseAssetReserve: BN, quoteAssetReserve: BN, periodicity: BN, pegMultiplier?: BN, oracleSource?: OracleSource, contractTier?: ContractTier, marginRatioInitial?: number, marginRatioMaintenance?: number, liquidatorFee?: number, ifLiquidatorFee?: number, imfFactor?: number, activeStatus?: boolean, baseSpread?: number, maxSpread?: number, maxOpenInterest?: BN, maxRevenueWithdrawPerPeriod?: BN, quoteMaxInsurance?: BN, orderStepSize?: BN, orderTickSize?: BN, minOrderSize?: BN, concentrationCoefScale?: BN, curveUpdateIntensity?: number, ammJitIntensity?: number, name?: string, lpPoolId?: number): Promise<TransactionSignature>;
|
|
26
|
-
getInitializePerpMarketIx(marketIndex: number, priceOracle: PublicKey, baseAssetReserve: BN, quoteAssetReserve: BN, periodicity: BN, pegMultiplier?: BN, oracleSource?: OracleSource, contractTier?: ContractTier, marginRatioInitial?: number, marginRatioMaintenance?: number, liquidatorFee?: number, ifLiquidatorFee?: number, imfFactor?: number, activeStatus?: boolean, baseSpread?: number, maxSpread?: number, maxOpenInterest?: BN, maxRevenueWithdrawPerPeriod?: BN, quoteMaxInsurance?: BN, orderStepSize?: BN, orderTickSize?: BN, minOrderSize?: BN, concentrationCoefScale?: BN, curveUpdateIntensity?: number, ammJitIntensity?: number, name?: string, lpPoolId?: number): Promise<TransactionInstruction[]>;
|
|
25
|
+
initializePerpMarket(marketIndex: number, priceOracle: PublicKey, baseAssetReserve: BN, quoteAssetReserve: BN, periodicity: BN, pegMultiplier?: BN, oracleSource?: OracleSource, contractTier?: ContractTier, marginRatioInitial?: number, marginRatioMaintenance?: number, liquidatorFee?: number, ifLiquidatorFee?: number, imfFactor?: number, activeStatus?: boolean, baseSpread?: number, maxSpread?: number, maxOpenInterest?: BN, maxRevenueWithdrawPerPeriod?: BN, quoteMaxInsurance?: BN, orderStepSize?: BN, orderTickSize?: BN, minOrderSize?: BN, concentrationCoefScale?: BN, curveUpdateIntensity?: number, ammJitIntensity?: number, name?: string, lpPoolId?: number, fundingClampThreshold?: number, fundingRampSlope?: number): Promise<TransactionSignature>;
|
|
26
|
+
getInitializePerpMarketIx(marketIndex: number, priceOracle: PublicKey, baseAssetReserve: BN, quoteAssetReserve: BN, periodicity: BN, pegMultiplier?: BN, oracleSource?: OracleSource, contractTier?: ContractTier, marginRatioInitial?: number, marginRatioMaintenance?: number, liquidatorFee?: number, ifLiquidatorFee?: number, imfFactor?: number, activeStatus?: boolean, baseSpread?: number, maxSpread?: number, maxOpenInterest?: BN, maxRevenueWithdrawPerPeriod?: BN, quoteMaxInsurance?: BN, orderStepSize?: BN, orderTickSize?: BN, minOrderSize?: BN, concentrationCoefScale?: BN, curveUpdateIntensity?: number, ammJitIntensity?: number, name?: string, lpPoolId?: number, fundingClampThreshold?: number, fundingRampSlope?: number): Promise<TransactionInstruction[]>;
|
|
27
27
|
initializeAmmCache(txParams?: TxParams): Promise<TransactionSignature>;
|
|
28
28
|
getInitializeAmmCacheIx(): Promise<TransactionInstruction>;
|
|
29
29
|
addMarketToAmmCache(perpMarketIndex: number, txParams?: TxParams): Promise<TransactionSignature>;
|
|
@@ -92,6 +92,8 @@ export declare class AdminClient extends VelocityClient {
|
|
|
92
92
|
getUpdatePerpMarketTargetBaseAssetAmountPerLpIx(perpMarketIndex: number, targetBaseAssetAmountPerLP: number): Promise<TransactionInstruction>;
|
|
93
93
|
updatePerpMarketMarginRatio(perpMarketIndex: number, marginRatioInitial: number, marginRatioMaintenance: number): Promise<TransactionSignature>;
|
|
94
94
|
getUpdatePerpMarketMarginRatioIx(perpMarketIndex: number, marginRatioInitial: number, marginRatioMaintenance: number): Promise<TransactionInstruction>;
|
|
95
|
+
updatePerpMarketFundingDeadZone(perpMarketIndex: number, fundingClampThreshold: number, fundingRampSlope: number): Promise<TransactionSignature>;
|
|
96
|
+
getUpdatePerpMarketFundingDeadZoneIx(perpMarketIndex: number, fundingClampThreshold: number, fundingRampSlope: number): Promise<TransactionInstruction>;
|
|
95
97
|
updatePerpMarketImfFactor(perpMarketIndex: number, imfFactor: number, unrealizedPnlImfFactor: number): Promise<TransactionSignature>;
|
|
96
98
|
getUpdatePerpMarketImfFactorIx(perpMarketIndex: number, imfFactor: number, unrealizedPnlImfFactor: number): Promise<TransactionInstruction>;
|
|
97
99
|
updatePerpMarketBaseSpread(perpMarketIndex: number, baseSpread: number): Promise<TransactionSignature>;
|
|
@@ -137,9 +137,9 @@ class AdminClient extends velocityClient_1.VelocityClient {
|
|
|
137
137
|
},
|
|
138
138
|
});
|
|
139
139
|
}
|
|
140
|
-
async initializePerpMarket(marketIndex, priceOracle, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier = numericConstants_1.PEG_PRECISION, oracleSource = types_1.OracleSource.PYTH_LAZER, contractTier = types_1.ContractTier.SPECULATIVE, marginRatioInitial = 2000, marginRatioMaintenance = 500, liquidatorFee = 0, ifLiquidatorFee = 10000, imfFactor = 0, activeStatus = true, baseSpread = 0, maxSpread = 142500, maxOpenInterest = numericConstants_1.ZERO, maxRevenueWithdrawPerPeriod = numericConstants_1.ZERO, quoteMaxInsurance = numericConstants_1.ZERO, orderStepSize = numericConstants_1.BASE_PRECISION.divn(10000), orderTickSize = numericConstants_1.PRICE_PRECISION.divn(100000), minOrderSize = numericConstants_1.BASE_PRECISION.divn(10000), concentrationCoefScale = numericConstants_1.ONE, curveUpdateIntensity = 0, ammJitIntensity = 0, name = userName_1.DEFAULT_MARKET_NAME, lpPoolId = 0) {
|
|
140
|
+
async initializePerpMarket(marketIndex, priceOracle, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier = numericConstants_1.PEG_PRECISION, oracleSource = types_1.OracleSource.PYTH_LAZER, contractTier = types_1.ContractTier.SPECULATIVE, marginRatioInitial = 2000, marginRatioMaintenance = 500, liquidatorFee = 0, ifLiquidatorFee = 10000, imfFactor = 0, activeStatus = true, baseSpread = 0, maxSpread = 142500, maxOpenInterest = numericConstants_1.ZERO, maxRevenueWithdrawPerPeriod = numericConstants_1.ZERO, quoteMaxInsurance = numericConstants_1.ZERO, orderStepSize = numericConstants_1.BASE_PRECISION.divn(10000), orderTickSize = numericConstants_1.PRICE_PRECISION.divn(100000), minOrderSize = numericConstants_1.BASE_PRECISION.divn(10000), concentrationCoefScale = numericConstants_1.ONE, curveUpdateIntensity = 0, ammJitIntensity = 0, name = userName_1.DEFAULT_MARKET_NAME, lpPoolId = 0, fundingClampThreshold = 0, fundingRampSlope = 0) {
|
|
141
141
|
const currentPerpMarketIndex = this.getStateAccount().numberOfMarkets;
|
|
142
|
-
const initializeMarketIxs = await this.getInitializePerpMarketIx(marketIndex, priceOracle, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier, oracleSource, contractTier, marginRatioInitial, marginRatioMaintenance, liquidatorFee, ifLiquidatorFee, imfFactor, activeStatus, baseSpread, maxSpread, maxOpenInterest, maxRevenueWithdrawPerPeriod, quoteMaxInsurance, orderStepSize, orderTickSize, minOrderSize, concentrationCoefScale, curveUpdateIntensity, ammJitIntensity, name, lpPoolId);
|
|
142
|
+
const initializeMarketIxs = await this.getInitializePerpMarketIx(marketIndex, priceOracle, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier, oracleSource, contractTier, marginRatioInitial, marginRatioMaintenance, liquidatorFee, ifLiquidatorFee, imfFactor, activeStatus, baseSpread, maxSpread, maxOpenInterest, maxRevenueWithdrawPerPeriod, quoteMaxInsurance, orderStepSize, orderTickSize, minOrderSize, concentrationCoefScale, curveUpdateIntensity, ammJitIntensity, name, lpPoolId, fundingClampThreshold, fundingRampSlope);
|
|
143
143
|
const tx = await this.buildTransaction(initializeMarketIxs);
|
|
144
144
|
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
145
145
|
while (this.getStateAccount().numberOfMarkets <= currentPerpMarketIndex) {
|
|
@@ -153,11 +153,11 @@ class AdminClient extends velocityClient_1.VelocityClient {
|
|
|
153
153
|
await this.accountSubscriber.setPerpOracleMap();
|
|
154
154
|
return txSig;
|
|
155
155
|
}
|
|
156
|
-
async getInitializePerpMarketIx(marketIndex, priceOracle, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier = numericConstants_1.PEG_PRECISION, oracleSource = types_1.OracleSource.PYTH_LAZER, contractTier = types_1.ContractTier.SPECULATIVE, marginRatioInitial = 2000, marginRatioMaintenance = 500, liquidatorFee = 0, ifLiquidatorFee = 10000, imfFactor = 0, activeStatus = true, baseSpread = 0, maxSpread = 142500, maxOpenInterest = numericConstants_1.ZERO, maxRevenueWithdrawPerPeriod = numericConstants_1.ZERO, quoteMaxInsurance = numericConstants_1.ZERO, orderStepSize = numericConstants_1.BASE_PRECISION.divn(10000), orderTickSize = numericConstants_1.PRICE_PRECISION.divn(100000), minOrderSize = numericConstants_1.BASE_PRECISION.divn(10000), concentrationCoefScale = numericConstants_1.ONE, curveUpdateIntensity = 0, ammJitIntensity = 0, name = userName_1.DEFAULT_MARKET_NAME, lpPoolId = 0) {
|
|
156
|
+
async getInitializePerpMarketIx(marketIndex, priceOracle, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier = numericConstants_1.PEG_PRECISION, oracleSource = types_1.OracleSource.PYTH_LAZER, contractTier = types_1.ContractTier.SPECULATIVE, marginRatioInitial = 2000, marginRatioMaintenance = 500, liquidatorFee = 0, ifLiquidatorFee = 10000, imfFactor = 0, activeStatus = true, baseSpread = 0, maxSpread = 142500, maxOpenInterest = numericConstants_1.ZERO, maxRevenueWithdrawPerPeriod = numericConstants_1.ZERO, quoteMaxInsurance = numericConstants_1.ZERO, orderStepSize = numericConstants_1.BASE_PRECISION.divn(10000), orderTickSize = numericConstants_1.PRICE_PRECISION.divn(100000), minOrderSize = numericConstants_1.BASE_PRECISION.divn(10000), concentrationCoefScale = numericConstants_1.ONE, curveUpdateIntensity = 0, ammJitIntensity = 0, name = userName_1.DEFAULT_MARKET_NAME, lpPoolId = 0, fundingClampThreshold = 0, fundingRampSlope = 0) {
|
|
157
157
|
const perpMarketPublicKey = await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, marketIndex);
|
|
158
158
|
const ixs = [];
|
|
159
159
|
const nameBuffer = (0, userName_1.encodeName)(name);
|
|
160
|
-
const initPerpIx = await this.program.instruction.initializePerpMarket(marketIndex, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier, oracleSource, contractTier, marginRatioInitial, marginRatioMaintenance, liquidatorFee, ifLiquidatorFee, imfFactor, activeStatus, baseSpread, maxSpread, maxOpenInterest, maxRevenueWithdrawPerPeriod, quoteMaxInsurance, orderStepSize, orderTickSize, minOrderSize, concentrationCoefScale, curveUpdateIntensity, ammJitIntensity, nameBuffer, lpPoolId, {
|
|
160
|
+
const initPerpIx = await this.program.instruction.initializePerpMarket(marketIndex, baseAssetReserve, quoteAssetReserve, periodicity, pegMultiplier, oracleSource, contractTier, marginRatioInitial, marginRatioMaintenance, liquidatorFee, ifLiquidatorFee, imfFactor, activeStatus, baseSpread, maxSpread, maxOpenInterest, maxRevenueWithdrawPerPeriod, quoteMaxInsurance, orderStepSize, orderTickSize, minOrderSize, concentrationCoefScale, curveUpdateIntensity, ammJitIntensity, nameBuffer, lpPoolId, fundingClampThreshold, fundingRampSlope, {
|
|
161
161
|
accounts: {
|
|
162
162
|
state: await this.getStatePublicKey(),
|
|
163
163
|
admin: this.useHotWalletAdmin
|
|
@@ -711,6 +711,23 @@ class AdminClient extends velocityClient_1.VelocityClient {
|
|
|
711
711
|
},
|
|
712
712
|
});
|
|
713
713
|
}
|
|
714
|
+
async updatePerpMarketFundingDeadZone(perpMarketIndex, fundingClampThreshold, fundingRampSlope) {
|
|
715
|
+
const updatePerpMarketFundingDeadZoneIx = await this.getUpdatePerpMarketFundingDeadZoneIx(perpMarketIndex, fundingClampThreshold, fundingRampSlope);
|
|
716
|
+
const tx = await this.buildTransaction(updatePerpMarketFundingDeadZoneIx);
|
|
717
|
+
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
718
|
+
return txSig;
|
|
719
|
+
}
|
|
720
|
+
async getUpdatePerpMarketFundingDeadZoneIx(perpMarketIndex, fundingClampThreshold, fundingRampSlope) {
|
|
721
|
+
return await this.program.instruction.updatePerpMarketFundingDeadZone(fundingClampThreshold, fundingRampSlope, {
|
|
722
|
+
accounts: {
|
|
723
|
+
admin: this.isSubscribed
|
|
724
|
+
? this.getStateAccount().coldAdmin
|
|
725
|
+
: this.wallet.publicKey,
|
|
726
|
+
state: await this.getStatePublicKey(),
|
|
727
|
+
perpMarket: await (0, pda_1.getPerpMarketPublicKey)(this.program.programId, perpMarketIndex),
|
|
728
|
+
},
|
|
729
|
+
});
|
|
730
|
+
}
|
|
714
731
|
async updatePerpMarketImfFactor(perpMarketIndex, imfFactor, unrealizedPnlImfFactor) {
|
|
715
732
|
const updatePerpMarketImfFactorIx = await this.getUpdatePerpMarketImfFactorIx(perpMarketIndex, imfFactor, unrealizedPnlImfFactor);
|
|
716
733
|
const tx = await this.buildTransaction(updatePerpMarketImfFactorIx);
|
package/lib/browser/config.js
CHANGED
|
@@ -22,7 +22,7 @@ exports.configs = {
|
|
|
22
22
|
PYTH_ORACLE_MAPPING_ADDRESS: 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2',
|
|
23
23
|
VELOCITY_PROGRAM_ID: exports.VELOCITY_DEVNET_PROGRAM_ID,
|
|
24
24
|
JIT_PROXY_PROGRAM_ID: 'J1TnP8zvVxbtF5KFp5xRmWuvG9McnhzmBd9XGfCyuxFP',
|
|
25
|
-
QUOTE_MINT_ADDRESS: '
|
|
25
|
+
QUOTE_MINT_ADDRESS: 'GqmEqYsy8EyvofDpmtFxK8zhYrgWgNokAtYoduQdL7v6',
|
|
26
26
|
V2_ALPHA_TICKET_MINT_ADDRESS: 'DeEiGWfCMP9psnLGkxGrBBMEAW5Jv8bBGMN8DCtFRCyB',
|
|
27
27
|
PERP_MARKETS: perpMarkets_1.DevnetPerpMarkets,
|
|
28
28
|
SPOT_MARKETS: spotMarkets_1.DevnetSpotMarkets,
|
|
@@ -49,6 +49,7 @@ export declare const PRICE_DIV_PEG: BN;
|
|
|
49
49
|
export declare const PRICE_TO_QUOTE_PRECISION: BN;
|
|
50
50
|
export declare const AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO: BN;
|
|
51
51
|
export declare const MARGIN_PRECISION: BN;
|
|
52
|
+
export declare const BPS_PRECISION: BN;
|
|
52
53
|
export declare const BID_ASK_SPREAD_PRECISION: BN;
|
|
53
54
|
export declare const LIQUIDATION_PCT_PRECISION: BN;
|
|
54
55
|
export declare const FUNDING_RATE_OFFSET_DENOMINATOR: BN;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MARGIN_PRECISION = exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.PRICE_TO_QUOTE_PRECISION = exports.PRICE_DIV_PEG = exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.BASE_PRECISION_EXP = exports.BASE_PRECISION = exports.AMM_RESERVE_PRECISION = exports.PEG_PRECISION = exports.FUNDING_RATE_BUFFER_PRECISION = exports.FUNDING_RATE_PRECISION = exports.PRICE_PRECISION = exports.QUOTE_PRECISION = exports.LIQUIDATION_FEE_PRECISION = exports.SPOT_MARKET_IMF_PRECISION = exports.SPOT_MARKET_IMF_PRECISION_EXP = exports.SPOT_MARKET_BALANCE_PRECISION = exports.SPOT_MARKET_BALANCE_PRECISION_EXP = exports.SPOT_MARKET_WEIGHT_PRECISION = exports.SPOT_MARKET_UTILIZATION_PRECISION = exports.SPOT_MARKET_UTILIZATION_PRECISION_EXP = exports.SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION = exports.SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION_EXP = exports.SPOT_MARKET_RATE_PRECISION = exports.SPOT_MARKET_RATE_PRECISION_EXP = exports.AMM_RESERVE_PRECISION_EXP = exports.PEG_PRECISION_EXP = exports.FUNDING_RATE_PRECISION_EXP = exports.PRICE_PRECISION_EXP = exports.FUNDING_RATE_BUFFER_PRECISION_EXP = exports.QUOTE_PRECISION_EXP = exports.CONCENTRATION_PRECISION = exports.PERCENTAGE_PRECISION = exports.PERCENTAGE_PRECISION_EXP = exports.MAX_LEVERAGE_ORDER_SIZE = exports.MAX_LEVERAGE = exports.TEN_MILLION = exports.BN_MAX = exports.TEN_THOUSAND = exports.TEN = exports.NINE = exports.EIGHT = exports.SEVEN = exports.SIX = exports.FIVE = exports.FOUR = exports.THREE = exports.TWO = exports.ONE = exports.ZERO = void 0;
|
|
4
|
-
exports.MIN_I64 = exports.MAX_I64 = exports.GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE = exports.DUST_POSITION_SIZE = exports.SLOT_TIME_ESTIMATE_MS = exports.IDLE_TIME_SLOTS = exports.ACCOUNT_AGE_DELETION_CUTOFF_SECONDS = exports.DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT = exports.OPEN_ORDER_MARGIN_REQUIREMENT = exports.LAMPORTS_EXP = exports.LAMPORTS_PRECISION = exports.QUOTE_SPOT_MARKET_INDEX = exports.ONE_YEAR = exports.ONE_HOUR = exports.FIVE_MINUTE = exports.PRICE_TIMES_AMM_TO_QUOTE_PRECISION_RATIO = exports.FUNDING_RATE_CLAMP_DENOMINATOR = exports.FUNDING_RATE_OFFSET_PERCENTAGE = exports.FUNDING_RATE_OFFSET_DENOMINATOR = exports.LIQUIDATION_PCT_PRECISION = exports.BID_ASK_SPREAD_PRECISION = void 0;
|
|
4
|
+
exports.MIN_I64 = exports.MAX_I64 = exports.GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE = exports.DUST_POSITION_SIZE = exports.SLOT_TIME_ESTIMATE_MS = exports.IDLE_TIME_SLOTS = exports.ACCOUNT_AGE_DELETION_CUTOFF_SECONDS = exports.DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT = exports.OPEN_ORDER_MARGIN_REQUIREMENT = exports.LAMPORTS_EXP = exports.LAMPORTS_PRECISION = exports.QUOTE_SPOT_MARKET_INDEX = exports.ONE_YEAR = exports.ONE_HOUR = exports.FIVE_MINUTE = exports.PRICE_TIMES_AMM_TO_QUOTE_PRECISION_RATIO = exports.FUNDING_RATE_CLAMP_DENOMINATOR = exports.FUNDING_RATE_OFFSET_PERCENTAGE = exports.FUNDING_RATE_OFFSET_DENOMINATOR = exports.LIQUIDATION_PCT_PRECISION = exports.BID_ASK_SPREAD_PRECISION = exports.BPS_PRECISION = void 0;
|
|
5
5
|
const web3_js_1 = require("@solana/web3.js");
|
|
6
6
|
const anchor_1 = require("../isomorphic/anchor");
|
|
7
7
|
exports.ZERO = new anchor_1.BN(0);
|
|
@@ -54,6 +54,7 @@ exports.PRICE_DIV_PEG = exports.PRICE_PRECISION.div(exports.PEG_PRECISION); //10
|
|
|
54
54
|
exports.PRICE_TO_QUOTE_PRECISION = exports.PRICE_PRECISION.div(exports.QUOTE_PRECISION); // 10^1
|
|
55
55
|
exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.AMM_RESERVE_PRECISION.mul(exports.PEG_PRECISION).div(exports.QUOTE_PRECISION); // 10^9
|
|
56
56
|
exports.MARGIN_PRECISION = exports.TEN_THOUSAND;
|
|
57
|
+
exports.BPS_PRECISION = exports.TEN_THOUSAND; // 1 unit = 1bp
|
|
57
58
|
exports.BID_ASK_SPREAD_PRECISION = new anchor_1.BN(1000000); // 10^6
|
|
58
59
|
exports.LIQUIDATION_PCT_PRECISION = exports.TEN_THOUSAND;
|
|
59
60
|
exports.FUNDING_RATE_OFFSET_DENOMINATOR = new anchor_1.BN(3333);
|
|
@@ -15,7 +15,7 @@ exports.DevnetSpotMarkets = [
|
|
|
15
15
|
poolId: 0,
|
|
16
16
|
oracle: new web3_js_1.PublicKey('Dai8hT1YRBBm5rBSJUSKcdR11psM55LVAkshbypfC4k4'),
|
|
17
17
|
oracleSource: types_1.OracleSource.PYTH_LAZER_STABLE_COIN,
|
|
18
|
-
mint: new web3_js_1.PublicKey('
|
|
18
|
+
mint: new web3_js_1.PublicKey('GqmEqYsy8EyvofDpmtFxK8zhYrgWgNokAtYoduQdL7v6'),
|
|
19
19
|
precision: new anchor_1.BN(10).pow(numericConstants_1.SIX),
|
|
20
20
|
precisionExp: numericConstants_1.SIX,
|
|
21
21
|
pythLazerId: 8,
|
|
@@ -3317,6 +3317,14 @@ export type Velocity = {
|
|
|
3317
3317
|
{
|
|
3318
3318
|
"name": "lpPoolId";
|
|
3319
3319
|
"type": "u8";
|
|
3320
|
+
},
|
|
3321
|
+
{
|
|
3322
|
+
"name": "fundingClampThreshold";
|
|
3323
|
+
"type": "u32";
|
|
3324
|
+
},
|
|
3325
|
+
{
|
|
3326
|
+
"name": "fundingRampSlope";
|
|
3327
|
+
"type": "u32";
|
|
3320
3328
|
}
|
|
3321
3329
|
];
|
|
3322
3330
|
},
|
|
@@ -9895,6 +9903,42 @@ export type Velocity = {
|
|
|
9895
9903
|
}
|
|
9896
9904
|
];
|
|
9897
9905
|
},
|
|
9906
|
+
{
|
|
9907
|
+
"name": "updatePerpMarketFundingDeadZone";
|
|
9908
|
+
"discriminator": [
|
|
9909
|
+
249,
|
|
9910
|
+
58,
|
|
9911
|
+
136,
|
|
9912
|
+
96,
|
|
9913
|
+
2,
|
|
9914
|
+
116,
|
|
9915
|
+
111,
|
|
9916
|
+
127
|
|
9917
|
+
];
|
|
9918
|
+
"accounts": [
|
|
9919
|
+
{
|
|
9920
|
+
"name": "admin";
|
|
9921
|
+
"signer": true;
|
|
9922
|
+
},
|
|
9923
|
+
{
|
|
9924
|
+
"name": "state";
|
|
9925
|
+
},
|
|
9926
|
+
{
|
|
9927
|
+
"name": "perpMarket";
|
|
9928
|
+
"writable": true;
|
|
9929
|
+
}
|
|
9930
|
+
];
|
|
9931
|
+
"args": [
|
|
9932
|
+
{
|
|
9933
|
+
"name": "fundingClampThreshold";
|
|
9934
|
+
"type": "u32";
|
|
9935
|
+
},
|
|
9936
|
+
{
|
|
9937
|
+
"name": "fundingRampSlope";
|
|
9938
|
+
"type": "u32";
|
|
9939
|
+
}
|
|
9940
|
+
];
|
|
9941
|
+
},
|
|
9898
9942
|
{
|
|
9899
9943
|
"name": "updatePerpMarketFundingPeriod";
|
|
9900
9944
|
"discriminator": [
|
|
@@ -20656,17 +20700,25 @@ export type Velocity = {
|
|
|
20656
20700
|
"type": "i64";
|
|
20657
20701
|
},
|
|
20658
20702
|
{
|
|
20659
|
-
"name": "
|
|
20703
|
+
"name": "fundingClampThreshold";
|
|
20660
20704
|
"docs": [
|
|
20661
|
-
"
|
|
20662
|
-
"
|
|
20705
|
+
"dead-zone threshold for the funding premium. mark/oracle twap spreads",
|
|
20706
|
+
"within +/- this band are treated as noise and add no premium; spreads",
|
|
20707
|
+
"past it are shrunk toward zero by this amount so funding stays continuous",
|
|
20708
|
+
"across the boundary. fit per market post-launch",
|
|
20709
|
+
"precision: BPS_PRECISION"
|
|
20663
20710
|
];
|
|
20664
|
-
"type":
|
|
20665
|
-
|
|
20666
|
-
|
|
20667
|
-
|
|
20668
|
-
|
|
20669
|
-
|
|
20711
|
+
"type": "u32";
|
|
20712
|
+
},
|
|
20713
|
+
{
|
|
20714
|
+
"name": "fundingRampSlope";
|
|
20715
|
+
"docs": [
|
|
20716
|
+
"slope of the funding premium ramp above the dead zone. 1.0x passes the",
|
|
20717
|
+
"shrunk spread through unchanged; higher leans into the premium harder",
|
|
20718
|
+
"fit per market post-launch",
|
|
20719
|
+
"precision: PERCENTAGE_PRECISION"
|
|
20720
|
+
];
|
|
20721
|
+
"type": "u32";
|
|
20670
20722
|
},
|
|
20671
20723
|
{
|
|
20672
20724
|
"name": "orderStepSize";
|
|
@@ -3311,6 +3311,14 @@
|
|
|
3311
3311
|
{
|
|
3312
3312
|
"name": "lp_pool_id",
|
|
3313
3313
|
"type": "u8"
|
|
3314
|
+
},
|
|
3315
|
+
{
|
|
3316
|
+
"name": "funding_clamp_threshold",
|
|
3317
|
+
"type": "u32"
|
|
3318
|
+
},
|
|
3319
|
+
{
|
|
3320
|
+
"name": "funding_ramp_slope",
|
|
3321
|
+
"type": "u32"
|
|
3314
3322
|
}
|
|
3315
3323
|
]
|
|
3316
3324
|
},
|
|
@@ -9889,6 +9897,42 @@
|
|
|
9889
9897
|
}
|
|
9890
9898
|
]
|
|
9891
9899
|
},
|
|
9900
|
+
{
|
|
9901
|
+
"name": "update_perp_market_funding_dead_zone",
|
|
9902
|
+
"discriminator": [
|
|
9903
|
+
249,
|
|
9904
|
+
58,
|
|
9905
|
+
136,
|
|
9906
|
+
96,
|
|
9907
|
+
2,
|
|
9908
|
+
116,
|
|
9909
|
+
111,
|
|
9910
|
+
127
|
|
9911
|
+
],
|
|
9912
|
+
"accounts": [
|
|
9913
|
+
{
|
|
9914
|
+
"name": "admin",
|
|
9915
|
+
"signer": true
|
|
9916
|
+
},
|
|
9917
|
+
{
|
|
9918
|
+
"name": "state"
|
|
9919
|
+
},
|
|
9920
|
+
{
|
|
9921
|
+
"name": "perp_market",
|
|
9922
|
+
"writable": true
|
|
9923
|
+
}
|
|
9924
|
+
],
|
|
9925
|
+
"args": [
|
|
9926
|
+
{
|
|
9927
|
+
"name": "funding_clamp_threshold",
|
|
9928
|
+
"type": "u32"
|
|
9929
|
+
},
|
|
9930
|
+
{
|
|
9931
|
+
"name": "funding_ramp_slope",
|
|
9932
|
+
"type": "u32"
|
|
9933
|
+
}
|
|
9934
|
+
]
|
|
9935
|
+
},
|
|
9892
9936
|
{
|
|
9893
9937
|
"name": "update_perp_market_funding_period",
|
|
9894
9938
|
"discriminator": [
|
|
@@ -20650,17 +20694,25 @@
|
|
|
20650
20694
|
"type": "i64"
|
|
20651
20695
|
},
|
|
20652
20696
|
{
|
|
20653
|
-
"name": "
|
|
20697
|
+
"name": "funding_clamp_threshold",
|
|
20654
20698
|
"docs": [
|
|
20655
|
-
"
|
|
20656
|
-
"
|
|
20699
|
+
"dead-zone threshold for the funding premium. mark/oracle twap spreads",
|
|
20700
|
+
"within +/- this band are treated as noise and add no premium; spreads",
|
|
20701
|
+
"past it are shrunk toward zero by this amount so funding stays continuous",
|
|
20702
|
+
"across the boundary. fit per market post-launch",
|
|
20703
|
+
"precision: BPS_PRECISION"
|
|
20657
20704
|
],
|
|
20658
|
-
"type":
|
|
20659
|
-
|
|
20660
|
-
|
|
20661
|
-
|
|
20662
|
-
|
|
20663
|
-
|
|
20705
|
+
"type": "u32"
|
|
20706
|
+
},
|
|
20707
|
+
{
|
|
20708
|
+
"name": "funding_ramp_slope",
|
|
20709
|
+
"docs": [
|
|
20710
|
+
"slope of the funding premium ramp above the dead zone. 1.0x passes the",
|
|
20711
|
+
"shrunk spread through unchanged; higher leans into the premium harder",
|
|
20712
|
+
"fit per market post-launch",
|
|
20713
|
+
"precision: PERCENTAGE_PRECISION"
|
|
20714
|
+
],
|
|
20715
|
+
"type": "u32"
|
|
20664
20716
|
},
|
|
20665
20717
|
{
|
|
20666
20718
|
"name": "order_step_size",
|
package/lib/browser/index.d.ts
CHANGED
|
@@ -102,6 +102,7 @@ export * from './swift/swiftOrderSubscriber';
|
|
|
102
102
|
export * from './swift/signedMsgUserAccountSubscriber';
|
|
103
103
|
export * from './swift/grpcSignedMsgUserAccountSubscriber';
|
|
104
104
|
export * from './tx/fastSingleTxSender';
|
|
105
|
+
export * from './tx/forwardOnlyTxSender';
|
|
105
106
|
export * from './tx/retryTxSender';
|
|
106
107
|
export * from './tx/whileValidTxSender';
|
|
107
108
|
export * from './tx/priorityFeeCalculator';
|
package/lib/browser/index.js
CHANGED
|
@@ -133,6 +133,7 @@ __exportStar(require("./swift/swiftOrderSubscriber"), exports);
|
|
|
133
133
|
__exportStar(require("./swift/signedMsgUserAccountSubscriber"), exports);
|
|
134
134
|
__exportStar(require("./swift/grpcSignedMsgUserAccountSubscriber"), exports);
|
|
135
135
|
__exportStar(require("./tx/fastSingleTxSender"), exports);
|
|
136
|
+
__exportStar(require("./tx/forwardOnlyTxSender"), exports);
|
|
136
137
|
__exportStar(require("./tx/retryTxSender"), exports);
|
|
137
138
|
__exportStar(require("./tx/whileValidTxSender"), exports);
|
|
138
139
|
__exportStar(require("./tx/priorityFeeCalculator"), exports);
|
|
@@ -79,11 +79,27 @@ function calculateAllEstimatedFundingRate(market, mmOraclePriceData, oraclePrice
|
|
|
79
79
|
// }
|
|
80
80
|
const twapSpread = markTwap.sub(oracleTwap);
|
|
81
81
|
const offset = oracleTwap.abs().div(numericConstants_1.FUNDING_RATE_OFFSET_DENOMINATOR);
|
|
82
|
-
|
|
82
|
+
// dead-zone threshold (per-market bps) as a price delta off the oracle twap
|
|
83
|
+
const clampThreshold = oracleTwap
|
|
83
84
|
.abs()
|
|
84
|
-
.
|
|
85
|
-
|
|
86
|
-
|
|
85
|
+
.mul(new anchor_1.BN(market.fundingClampThreshold))
|
|
86
|
+
.div(numericConstants_1.BPS_PRECISION);
|
|
87
|
+
let twapSpreadWithOffset;
|
|
88
|
+
if (twapSpread.abs().lte(clampThreshold)) {
|
|
89
|
+
// inside the band: noise, no premium, baseline offset only
|
|
90
|
+
twapSpreadWithOffset = offset;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
// outside the band: shrink the spread toward zero by the band width
|
|
94
|
+
// (keeping its sign), scale by the per-market ramp slope, add the offset
|
|
95
|
+
const shrunk = twapSpread.isNeg()
|
|
96
|
+
? twapSpread.add(clampThreshold)
|
|
97
|
+
: twapSpread.sub(clampThreshold);
|
|
98
|
+
const ramped = shrunk
|
|
99
|
+
.mul(new anchor_1.BN(market.fundingRampSlope))
|
|
100
|
+
.div(numericConstants_1.PERCENTAGE_PRECISION);
|
|
101
|
+
twapSpreadWithOffset = ramped.add(offset);
|
|
102
|
+
}
|
|
87
103
|
const maxSpread = getMaxPriceDivergenceForFundingRate(market, oracleTwap);
|
|
88
104
|
const clampedSpreadWithOffset = (0, utils_1.clampBN)(twapSpreadWithOffset, maxSpread.mul(new anchor_1.BN(-1)), maxSpread);
|
|
89
105
|
const twapSpreadPct = clampedSpreadWithOffset
|
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
import { BN } from '../isomorphic/anchor';
|
|
2
|
+
/**
|
|
3
|
+
* @param ifLiquidationFee Since PR#75 the program sizes liquidations against the
|
|
4
|
+
* COMBINED insurance-fund + protocol liquidation fee budget:
|
|
5
|
+
* `market.ifLiquidationFee + market.protocolLiquidationFee` (split IF-first
|
|
6
|
+
* on-chain). Pass that sum here to match on-chain sizing; passing
|
|
7
|
+
* `market.ifLiquidationFee` alone under-sizes the estimate.
|
|
8
|
+
*/
|
|
2
9
|
export declare function calculateBaseAssetAmountToCoverMarginShortage(marginShortage: BN, marginRatio: number, liquidationFee: number, ifLiquidationFee: number, oraclePrice: BN, quoteOraclePrice: BN): BN | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* @param ifLiquidationFee Since PR#75 the program sizes liquidations against the
|
|
12
|
+
* COMBINED insurance-fund + protocol liquidation fee budget:
|
|
13
|
+
* `market.ifLiquidationFee + market.protocolLiquidationFee` (split IF-first
|
|
14
|
+
* on-chain). Pass that sum here to match on-chain sizing; passing
|
|
15
|
+
* `market.ifLiquidationFee` alone under-sizes the estimate.
|
|
16
|
+
*/
|
|
3
17
|
export declare function calculateLiabilityTransferToCoverMarginShortage(marginShortage: BN, assetWeight: number, assetLiquidationMultiplier: number, liabilityWeight: number, liabilityLiquidationMultiplier: number, liabilityDecimals: number, liabilityPrice: BN, ifLiquidationFee: number): BN | undefined;
|
|
4
18
|
export declare function calculateAssetTransferForLiabilityTransfer(assetAmount: BN, assetLiquidationMultiplier: number, assetDecimals: number, assetPrice: BN, liabilityAmount: BN, liabilityLiquidationMultiplier: number, liabilityDecimals: number, liabilityPrice: BN): BN | undefined;
|
|
5
19
|
export declare function calculateMaxPctToLiquidate(userLastActiveSlot: BN, userLiquidationMarginFreed: BN, marginShortage: BN, slot: BN, initialPctToLiquidate: BN, liquidationDuration: BN): BN;
|
|
@@ -3,6 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getMarginShortage = exports.calculateMaxPctToLiquidate = exports.calculateAssetTransferForLiabilityTransfer = exports.calculateLiabilityTransferToCoverMarginShortage = exports.calculateBaseAssetAmountToCoverMarginShortage = void 0;
|
|
4
4
|
const anchor_1 = require("../isomorphic/anchor");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
|
+
/**
|
|
7
|
+
* @param ifLiquidationFee Since PR#75 the program sizes liquidations against the
|
|
8
|
+
* COMBINED insurance-fund + protocol liquidation fee budget:
|
|
9
|
+
* `market.ifLiquidationFee + market.protocolLiquidationFee` (split IF-first
|
|
10
|
+
* on-chain). Pass that sum here to match on-chain sizing; passing
|
|
11
|
+
* `market.ifLiquidationFee` alone under-sizes the estimate.
|
|
12
|
+
*/
|
|
6
13
|
function calculateBaseAssetAmountToCoverMarginShortage(marginShortage, marginRatio, liquidationFee, ifLiquidationFee, oraclePrice, quoteOraclePrice) {
|
|
7
14
|
const marginRatioBN = new anchor_1.BN(marginRatio)
|
|
8
15
|
.mul(numericConstants_1.LIQUIDATION_FEE_PRECISION)
|
|
@@ -20,6 +27,13 @@ function calculateBaseAssetAmountToCoverMarginShortage(marginShortage, marginRat
|
|
|
20
27
|
.sub(oraclePrice.mul(new anchor_1.BN(ifLiquidationFee)).div(numericConstants_1.LIQUIDATION_FEE_PRECISION)));
|
|
21
28
|
}
|
|
22
29
|
exports.calculateBaseAssetAmountToCoverMarginShortage = calculateBaseAssetAmountToCoverMarginShortage;
|
|
30
|
+
/**
|
|
31
|
+
* @param ifLiquidationFee Since PR#75 the program sizes liquidations against the
|
|
32
|
+
* COMBINED insurance-fund + protocol liquidation fee budget:
|
|
33
|
+
* `market.ifLiquidationFee + market.protocolLiquidationFee` (split IF-first
|
|
34
|
+
* on-chain). Pass that sum here to match on-chain sizing; passing
|
|
35
|
+
* `market.ifLiquidationFee` alone under-sizes the estimate.
|
|
36
|
+
*/
|
|
23
37
|
function calculateLiabilityTransferToCoverMarginShortage(marginShortage, assetWeight, assetLiquidationMultiplier, liabilityWeight, liabilityLiquidationMultiplier, liabilityDecimals, liabilityPrice, ifLiquidationFee) {
|
|
24
38
|
if (assetWeight >= liabilityWeight) {
|
|
25
39
|
// undefined is max
|
|
@@ -2,3 +2,9 @@ import { BN } from '../isomorphic/anchor';
|
|
|
2
2
|
import { MarginCategory, SpotBalanceType, SpotMarketAccount } from '../types';
|
|
3
3
|
export declare function castNumberToSpotPrecision(value: number | BN, spotMarket: SpotMarketAccount): BN;
|
|
4
4
|
export declare function calculateSpotMarketMarginRatio(market: SpotMarketAccount, oraclePrice: BN, marginCategory: MarginCategory, size: BN, balanceType: SpotBalanceType, customMarginRatio?: number): number;
|
|
5
|
+
/**
|
|
6
|
+
* Returns the maximum remaining deposit that can be made to the spot market. If the maxTokenDeposits on the market is zero then there is no limit and this function will also return zero. (so that needs to be checked)
|
|
7
|
+
* @param market
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
export declare function calculateMaxRemainingDeposit(market: SpotMarketAccount): BN;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateSpotMarketMarginRatio = exports.castNumberToSpotPrecision = void 0;
|
|
3
|
+
exports.calculateMaxRemainingDeposit = exports.calculateSpotMarketMarginRatio = exports.castNumberToSpotPrecision = void 0;
|
|
4
4
|
const anchor_1 = require("../isomorphic/anchor");
|
|
5
5
|
const types_1 = require("../types");
|
|
6
6
|
const spotBalance_1 = require("./spotBalance");
|
|
@@ -32,3 +32,18 @@ function calculateSpotMarketMarginRatio(market, oraclePrice, marginCategory, siz
|
|
|
32
32
|
return marginRatio;
|
|
33
33
|
}
|
|
34
34
|
exports.calculateSpotMarketMarginRatio = calculateSpotMarketMarginRatio;
|
|
35
|
+
/**
|
|
36
|
+
* Returns the maximum remaining deposit that can be made to the spot market. If the maxTokenDeposits on the market is zero then there is no limit and this function will also return zero. (so that needs to be checked)
|
|
37
|
+
* @param market
|
|
38
|
+
* @returns
|
|
39
|
+
*/
|
|
40
|
+
function calculateMaxRemainingDeposit(market) {
|
|
41
|
+
const marketMaxTokenDeposits = market.maxTokenDeposits;
|
|
42
|
+
if (marketMaxTokenDeposits.eq(numericConstants_1.ZERO)) {
|
|
43
|
+
// If the maxTokenDeposits is set to zero then that means there is no limit. Return the largest number we can to represent infinite available deposit.
|
|
44
|
+
return numericConstants_1.ZERO;
|
|
45
|
+
}
|
|
46
|
+
const totalDepositsTokenAmount = (0, spotBalance_1.getTokenAmount)(market.depositBalance, market, types_1.SpotBalanceType.DEPOSIT);
|
|
47
|
+
return anchor_1.BN.max(numericConstants_1.ZERO, marketMaxTokenDeposits.sub(totalDepositsTokenAmount));
|
|
48
|
+
}
|
|
49
|
+
exports.calculateMaxRemainingDeposit = calculateMaxRemainingDeposit;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { ConfirmOptions, Connection } from '@solana/web3.js';
|
|
4
|
+
import { BaseTxSender } from './baseTxSender';
|
|
5
|
+
import { ConfirmationStrategy, TxSigAndSlot } from './types';
|
|
6
|
+
import { TxHandler } from './txHandler';
|
|
7
|
+
import { IWallet } from '../types';
|
|
8
|
+
type ResolveReference = {
|
|
9
|
+
resolve?: () => void;
|
|
10
|
+
};
|
|
11
|
+
export declare class ForwardOnlyTxSender extends BaseTxSender {
|
|
12
|
+
connection: Connection;
|
|
13
|
+
wallet: IWallet;
|
|
14
|
+
opts: ConfirmOptions;
|
|
15
|
+
timeout: number;
|
|
16
|
+
retrySleep: number;
|
|
17
|
+
additionalConnections: Connection[];
|
|
18
|
+
timoutCount: number;
|
|
19
|
+
constructor({ connection, wallet, opts, timeout, retrySleep, confirmationStrategy, additionalTxSenderCallbacks, txHandler, trackTxLandRate, txLandRateLookbackWindowMinutes, landRateToFeeFunc, throwOnTimeoutError, }: {
|
|
20
|
+
connection: Connection;
|
|
21
|
+
wallet: IWallet;
|
|
22
|
+
opts?: ConfirmOptions;
|
|
23
|
+
timeout?: number;
|
|
24
|
+
retrySleep?: number;
|
|
25
|
+
confirmationStrategy?: ConfirmationStrategy;
|
|
26
|
+
additionalTxSenderCallbacks?: ((base58EncodedTx: string) => void)[];
|
|
27
|
+
txHandler?: TxHandler;
|
|
28
|
+
trackTxLandRate?: boolean;
|
|
29
|
+
txLandRateLookbackWindowMinutes?: number;
|
|
30
|
+
landRateToFeeFunc?: (landRate: number) => number;
|
|
31
|
+
throwOnTimeoutError?: boolean;
|
|
32
|
+
});
|
|
33
|
+
sleep(reference: ResolveReference): Promise<void>;
|
|
34
|
+
sendToAdditionalConnections(rawTx: Buffer | Uint8Array, _opts: ConfirmOptions): void;
|
|
35
|
+
sendRawTransaction(rawTransaction: Buffer | Uint8Array, opts: ConfirmOptions): Promise<TxSigAndSlot>;
|
|
36
|
+
}
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,92 @@
|
|
|
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.ForwardOnlyTxSender = void 0;
|
|
7
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
8
|
+
const bs58_1 = __importDefault(require("bs58"));
|
|
9
|
+
const baseTxSender_1 = require("./baseTxSender");
|
|
10
|
+
const types_1 = require("./types");
|
|
11
|
+
const config_1 = require("../config");
|
|
12
|
+
const DEFAULT_TIMEOUT = 35000;
|
|
13
|
+
const DEFAULT_RETRY = 5000;
|
|
14
|
+
class ForwardOnlyTxSender extends baseTxSender_1.BaseTxSender {
|
|
15
|
+
constructor({ connection, wallet, opts = { ...config_1.DEFAULT_CONFIRMATION_OPTS, maxRetries: 0 }, timeout = DEFAULT_TIMEOUT, retrySleep = DEFAULT_RETRY, confirmationStrategy = types_1.ConfirmationStrategy.Combo, additionalTxSenderCallbacks = [], txHandler, trackTxLandRate, txLandRateLookbackWindowMinutes, landRateToFeeFunc, throwOnTimeoutError = true, }) {
|
|
16
|
+
super({
|
|
17
|
+
connection,
|
|
18
|
+
wallet,
|
|
19
|
+
opts,
|
|
20
|
+
timeout,
|
|
21
|
+
additionalConnections: [],
|
|
22
|
+
confirmationStrategy,
|
|
23
|
+
additionalTxSenderCallbacks,
|
|
24
|
+
txHandler,
|
|
25
|
+
trackTxLandRate,
|
|
26
|
+
txLandRateLookbackWindowMinutes,
|
|
27
|
+
landRateToFeeFunc,
|
|
28
|
+
throwOnTimeoutError,
|
|
29
|
+
});
|
|
30
|
+
this.timoutCount = 0;
|
|
31
|
+
this.connection = connection;
|
|
32
|
+
this.wallet = wallet;
|
|
33
|
+
this.opts = opts;
|
|
34
|
+
this.timeout = timeout;
|
|
35
|
+
this.retrySleep = retrySleep;
|
|
36
|
+
this.additionalConnections = [];
|
|
37
|
+
}
|
|
38
|
+
async sleep(reference) {
|
|
39
|
+
return new Promise((resolve) => {
|
|
40
|
+
reference.resolve = resolve;
|
|
41
|
+
setTimeout(resolve, this.retrySleep);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
sendToAdditionalConnections(rawTx, _opts) {
|
|
45
|
+
var _a;
|
|
46
|
+
(_a = this.additionalTxSenderCallbacks) === null || _a === void 0 ? void 0 : _a.map((callback) => {
|
|
47
|
+
callback(bs58_1.default.encode(rawTx));
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
async sendRawTransaction(rawTransaction, opts) {
|
|
51
|
+
var _a, _b, _c;
|
|
52
|
+
const deserializedTx = web3_js_1.VersionedTransaction.deserialize(rawTransaction);
|
|
53
|
+
const txSig = deserializedTx.signatures[0];
|
|
54
|
+
const encodedTxSig = bs58_1.default.encode(txSig);
|
|
55
|
+
const startTime = this.getTimestamp();
|
|
56
|
+
this.sendToAdditionalConnections(rawTransaction, opts);
|
|
57
|
+
(_a = this.txSigCache) === null || _a === void 0 ? void 0 : _a.set(encodedTxSig, false);
|
|
58
|
+
let done = false;
|
|
59
|
+
const resolveReference = {
|
|
60
|
+
resolve: undefined,
|
|
61
|
+
};
|
|
62
|
+
const stopWaiting = () => {
|
|
63
|
+
done = true;
|
|
64
|
+
if (resolveReference.resolve) {
|
|
65
|
+
resolveReference.resolve();
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
(async () => {
|
|
69
|
+
while (!done && this.getTimestamp() - startTime < this.timeout) {
|
|
70
|
+
await this.sleep(resolveReference);
|
|
71
|
+
if (!done) {
|
|
72
|
+
this.sendToAdditionalConnections(rawTransaction, opts);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
})();
|
|
76
|
+
let slot;
|
|
77
|
+
try {
|
|
78
|
+
const result = await this.confirmTransaction(encodedTxSig, opts.commitment);
|
|
79
|
+
slot = (_b = result === null || result === void 0 ? void 0 : result.context) === null || _b === void 0 ? void 0 : _b.slot;
|
|
80
|
+
(_c = this.txSigCache) === null || _c === void 0 ? void 0 : _c.set(encodedTxSig, true);
|
|
81
|
+
// eslint-disable-next-line no-useless-catch
|
|
82
|
+
}
|
|
83
|
+
catch (e) {
|
|
84
|
+
throw e;
|
|
85
|
+
}
|
|
86
|
+
finally {
|
|
87
|
+
stopWaiting();
|
|
88
|
+
}
|
|
89
|
+
return { txSig: encodedTxSig, slot };
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.ForwardOnlyTxSender = ForwardOnlyTxSender;
|