@velocity-exchange/sdk 0.2.2 → 0.2.3
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 +6 -0
- package/lib/browser/adminClient.d.ts +4 -2
- package/lib/browser/adminClient.js +21 -4
- package/lib/browser/constants/numericConstants.d.ts +1 -0
- package/lib/browser/constants/numericConstants.js +2 -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/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 +2 -0
- 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/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/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/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 +2 -0
- package/lib/node/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/adminClient.ts +53 -3
- package/src/constants/numericConstants.ts +1 -0
- 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/spotMarket.ts +28 -2
- package/src/tx/forwardOnlyTxSender.ts +145 -0
- package/src/types.ts +2 -0
- package/tests/amm/test.ts +2 -2
- package/tests/dlob/helpers.ts +2 -0
package/src/adminClient.ts
CHANGED
|
@@ -352,7 +352,9 @@ export class AdminClient extends VelocityClient {
|
|
|
352
352
|
curveUpdateIntensity = 0,
|
|
353
353
|
ammJitIntensity = 0,
|
|
354
354
|
name = DEFAULT_MARKET_NAME,
|
|
355
|
-
lpPoolId: number = 0
|
|
355
|
+
lpPoolId: number = 0,
|
|
356
|
+
fundingClampThreshold = 0,
|
|
357
|
+
fundingRampSlope = 0
|
|
356
358
|
): Promise<TransactionSignature> {
|
|
357
359
|
const currentPerpMarketIndex = this.getStateAccount().numberOfMarkets;
|
|
358
360
|
|
|
@@ -383,7 +385,9 @@ export class AdminClient extends VelocityClient {
|
|
|
383
385
|
curveUpdateIntensity,
|
|
384
386
|
ammJitIntensity,
|
|
385
387
|
name,
|
|
386
|
-
lpPoolId
|
|
388
|
+
lpPoolId,
|
|
389
|
+
fundingClampThreshold,
|
|
390
|
+
fundingRampSlope
|
|
387
391
|
);
|
|
388
392
|
const tx = await this.buildTransaction(initializeMarketIxs);
|
|
389
393
|
|
|
@@ -430,7 +434,9 @@ export class AdminClient extends VelocityClient {
|
|
|
430
434
|
curveUpdateIntensity = 0,
|
|
431
435
|
ammJitIntensity = 0,
|
|
432
436
|
name = DEFAULT_MARKET_NAME,
|
|
433
|
-
lpPoolId: number = 0
|
|
437
|
+
lpPoolId: number = 0,
|
|
438
|
+
fundingClampThreshold = 0,
|
|
439
|
+
fundingRampSlope = 0
|
|
434
440
|
): Promise<TransactionInstruction[]> {
|
|
435
441
|
const perpMarketPublicKey = await getPerpMarketPublicKey(
|
|
436
442
|
this.program.programId,
|
|
@@ -467,6 +473,8 @@ export class AdminClient extends VelocityClient {
|
|
|
467
473
|
ammJitIntensity,
|
|
468
474
|
nameBuffer,
|
|
469
475
|
lpPoolId,
|
|
476
|
+
fundingClampThreshold,
|
|
477
|
+
fundingRampSlope,
|
|
470
478
|
{
|
|
471
479
|
accounts: {
|
|
472
480
|
state: await this.getStatePublicKey(),
|
|
@@ -1540,6 +1548,48 @@ export class AdminClient extends VelocityClient {
|
|
|
1540
1548
|
);
|
|
1541
1549
|
}
|
|
1542
1550
|
|
|
1551
|
+
public async updatePerpMarketFundingDeadZone(
|
|
1552
|
+
perpMarketIndex: number,
|
|
1553
|
+
fundingClampThreshold: number,
|
|
1554
|
+
fundingRampSlope: number
|
|
1555
|
+
): Promise<TransactionSignature> {
|
|
1556
|
+
const updatePerpMarketFundingDeadZoneIx =
|
|
1557
|
+
await this.getUpdatePerpMarketFundingDeadZoneIx(
|
|
1558
|
+
perpMarketIndex,
|
|
1559
|
+
fundingClampThreshold,
|
|
1560
|
+
fundingRampSlope
|
|
1561
|
+
);
|
|
1562
|
+
|
|
1563
|
+
const tx = await this.buildTransaction(updatePerpMarketFundingDeadZoneIx);
|
|
1564
|
+
|
|
1565
|
+
const { txSig } = await this.sendTransaction(tx, [], this.opts);
|
|
1566
|
+
|
|
1567
|
+
return txSig;
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
public async getUpdatePerpMarketFundingDeadZoneIx(
|
|
1571
|
+
perpMarketIndex: number,
|
|
1572
|
+
fundingClampThreshold: number,
|
|
1573
|
+
fundingRampSlope: number
|
|
1574
|
+
): Promise<TransactionInstruction> {
|
|
1575
|
+
return await this.program.instruction.updatePerpMarketFundingDeadZone(
|
|
1576
|
+
fundingClampThreshold,
|
|
1577
|
+
fundingRampSlope,
|
|
1578
|
+
{
|
|
1579
|
+
accounts: {
|
|
1580
|
+
admin: this.isSubscribed
|
|
1581
|
+
? this.getStateAccount().coldAdmin
|
|
1582
|
+
: this.wallet.publicKey,
|
|
1583
|
+
state: await this.getStatePublicKey(),
|
|
1584
|
+
perpMarket: await getPerpMarketPublicKey(
|
|
1585
|
+
this.program.programId,
|
|
1586
|
+
perpMarketIndex
|
|
1587
|
+
),
|
|
1588
|
+
},
|
|
1589
|
+
}
|
|
1590
|
+
);
|
|
1591
|
+
}
|
|
1592
|
+
|
|
1543
1593
|
public async updatePerpMarketImfFactor(
|
|
1544
1594
|
perpMarketIndex: number,
|
|
1545
1595
|
imfFactor: number,
|
|
@@ -81,6 +81,7 @@ export const PRICE_TO_QUOTE_PRECISION = PRICE_PRECISION.div(QUOTE_PRECISION); //
|
|
|
81
81
|
export const AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO =
|
|
82
82
|
AMM_RESERVE_PRECISION.mul(PEG_PRECISION).div(QUOTE_PRECISION); // 10^9
|
|
83
83
|
export const MARGIN_PRECISION = TEN_THOUSAND;
|
|
84
|
+
export const BPS_PRECISION = TEN_THOUSAND; // 1 unit = 1bp
|
|
84
85
|
export const BID_ASK_SPREAD_PRECISION = new BN(1000000); // 10^6
|
|
85
86
|
export const LIQUIDATION_PCT_PRECISION = TEN_THOUSAND;
|
|
86
87
|
export const FUNDING_RATE_OFFSET_DENOMINATOR = new BN(3333);
|
package/src/idl/velocity.json
CHANGED
|
@@ -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/src/idl/velocity.ts
CHANGED
|
@@ -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",
|
package/src/index.ts
CHANGED
|
@@ -108,6 +108,7 @@ export * from './swift/swiftOrderSubscriber';
|
|
|
108
108
|
export * from './swift/signedMsgUserAccountSubscriber';
|
|
109
109
|
export * from './swift/grpcSignedMsgUserAccountSubscriber';
|
|
110
110
|
export * from './tx/fastSingleTxSender';
|
|
111
|
+
export * from './tx/forwardOnlyTxSender';
|
|
111
112
|
export * from './tx/retryTxSender';
|
|
112
113
|
export * from './tx/whileValidTxSender';
|
|
113
114
|
export * from './tx/priorityFeeCalculator';
|
package/src/math/funding.ts
CHANGED
|
@@ -6,7 +6,8 @@ import {
|
|
|
6
6
|
ZERO,
|
|
7
7
|
ONE,
|
|
8
8
|
FUNDING_RATE_OFFSET_DENOMINATOR,
|
|
9
|
-
|
|
9
|
+
BPS_PRECISION,
|
|
10
|
+
PERCENTAGE_PRECISION,
|
|
10
11
|
} from '../constants/numericConstants';
|
|
11
12
|
import { BigNum } from '../factory/bigNum';
|
|
12
13
|
import { PerpMarketAccount, isVariant } from '../types';
|
|
@@ -183,11 +184,28 @@ export function calculateAllEstimatedFundingRate(
|
|
|
183
184
|
|
|
184
185
|
const twapSpread = markTwap.sub(oracleTwap);
|
|
185
186
|
const offset = oracleTwap.abs().div(FUNDING_RATE_OFFSET_DENOMINATOR);
|
|
186
|
-
|
|
187
|
+
|
|
188
|
+
// dead-zone threshold (per-market bps) as a price delta off the oracle twap
|
|
189
|
+
const clampThreshold = oracleTwap
|
|
187
190
|
.abs()
|
|
188
|
-
.
|
|
189
|
-
|
|
190
|
-
|
|
191
|
+
.mul(new BN(market.fundingClampThreshold))
|
|
192
|
+
.div(BPS_PRECISION);
|
|
193
|
+
|
|
194
|
+
let twapSpreadWithOffset: BN;
|
|
195
|
+
if (twapSpread.abs().lte(clampThreshold)) {
|
|
196
|
+
// inside the band: noise, no premium, baseline offset only
|
|
197
|
+
twapSpreadWithOffset = offset;
|
|
198
|
+
} else {
|
|
199
|
+
// outside the band: shrink the spread toward zero by the band width
|
|
200
|
+
// (keeping its sign), scale by the per-market ramp slope, add the offset
|
|
201
|
+
const shrunk = twapSpread.isNeg()
|
|
202
|
+
? twapSpread.add(clampThreshold)
|
|
203
|
+
: twapSpread.sub(clampThreshold);
|
|
204
|
+
const ramped = shrunk
|
|
205
|
+
.mul(new BN(market.fundingRampSlope))
|
|
206
|
+
.div(PERCENTAGE_PRECISION);
|
|
207
|
+
twapSpreadWithOffset = ramped.add(offset);
|
|
208
|
+
}
|
|
191
209
|
|
|
192
210
|
const maxSpread = getMaxPriceDivergenceForFundingRate(market, oracleTwap);
|
|
193
211
|
|
package/src/math/spotMarket.ts
CHANGED
|
@@ -5,8 +5,12 @@ import {
|
|
|
5
5
|
SpotBalanceType,
|
|
6
6
|
SpotMarketAccount,
|
|
7
7
|
} from '../types';
|
|
8
|
-
import {
|
|
9
|
-
|
|
8
|
+
import {
|
|
9
|
+
calculateAssetWeight,
|
|
10
|
+
calculateLiabilityWeight,
|
|
11
|
+
getTokenAmount,
|
|
12
|
+
} from './spotBalance';
|
|
13
|
+
import { MARGIN_PRECISION, ZERO } from '../constants/numericConstants';
|
|
10
14
|
import { numberToSafeBN } from './utils';
|
|
11
15
|
|
|
12
16
|
export function castNumberToSpotPrecision(
|
|
@@ -54,3 +58,25 @@ export function calculateSpotMarketMarginRatio(
|
|
|
54
58
|
|
|
55
59
|
return marginRatio;
|
|
56
60
|
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 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)
|
|
64
|
+
* @param market
|
|
65
|
+
* @returns
|
|
66
|
+
*/
|
|
67
|
+
export function calculateMaxRemainingDeposit(market: SpotMarketAccount) {
|
|
68
|
+
const marketMaxTokenDeposits = market.maxTokenDeposits;
|
|
69
|
+
|
|
70
|
+
if (marketMaxTokenDeposits.eq(ZERO)) {
|
|
71
|
+
// 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.
|
|
72
|
+
return ZERO;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const totalDepositsTokenAmount = getTokenAmount(
|
|
76
|
+
market.depositBalance,
|
|
77
|
+
market,
|
|
78
|
+
SpotBalanceType.DEPOSIT
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
return BN.max(ZERO, marketMaxTokenDeposits.sub(totalDepositsTokenAmount));
|
|
82
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ConfirmOptions,
|
|
3
|
+
Connection,
|
|
4
|
+
VersionedTransaction,
|
|
5
|
+
} from '@solana/web3.js';
|
|
6
|
+
import bs58 from 'bs58';
|
|
7
|
+
import { BaseTxSender } from './baseTxSender';
|
|
8
|
+
import { ConfirmationStrategy, TxSigAndSlot } from './types';
|
|
9
|
+
import { TxHandler } from './txHandler';
|
|
10
|
+
import { IWallet } from '../types';
|
|
11
|
+
import { DEFAULT_CONFIRMATION_OPTS } from '../config';
|
|
12
|
+
|
|
13
|
+
const DEFAULT_TIMEOUT = 35000;
|
|
14
|
+
const DEFAULT_RETRY = 5000;
|
|
15
|
+
|
|
16
|
+
type ResolveReference = {
|
|
17
|
+
resolve?: () => void;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export class ForwardOnlyTxSender extends BaseTxSender {
|
|
21
|
+
connection: Connection;
|
|
22
|
+
wallet: IWallet;
|
|
23
|
+
opts: ConfirmOptions;
|
|
24
|
+
timeout: number;
|
|
25
|
+
retrySleep: number;
|
|
26
|
+
additionalConnections: Connection[];
|
|
27
|
+
timoutCount = 0;
|
|
28
|
+
|
|
29
|
+
public constructor({
|
|
30
|
+
connection,
|
|
31
|
+
wallet,
|
|
32
|
+
opts = { ...DEFAULT_CONFIRMATION_OPTS, maxRetries: 0 },
|
|
33
|
+
timeout = DEFAULT_TIMEOUT,
|
|
34
|
+
retrySleep = DEFAULT_RETRY,
|
|
35
|
+
confirmationStrategy = ConfirmationStrategy.Combo,
|
|
36
|
+
additionalTxSenderCallbacks = [],
|
|
37
|
+
txHandler,
|
|
38
|
+
trackTxLandRate,
|
|
39
|
+
txLandRateLookbackWindowMinutes,
|
|
40
|
+
landRateToFeeFunc,
|
|
41
|
+
throwOnTimeoutError = true,
|
|
42
|
+
}: {
|
|
43
|
+
connection: Connection;
|
|
44
|
+
wallet: IWallet;
|
|
45
|
+
opts?: ConfirmOptions;
|
|
46
|
+
timeout?: number;
|
|
47
|
+
retrySleep?: number;
|
|
48
|
+
confirmationStrategy?: ConfirmationStrategy;
|
|
49
|
+
additionalTxSenderCallbacks?: ((base58EncodedTx: string) => void)[];
|
|
50
|
+
txHandler?: TxHandler;
|
|
51
|
+
trackTxLandRate?: boolean;
|
|
52
|
+
txLandRateLookbackWindowMinutes?: number;
|
|
53
|
+
landRateToFeeFunc?: (landRate: number) => number;
|
|
54
|
+
throwOnTimeoutError?: boolean;
|
|
55
|
+
}) {
|
|
56
|
+
super({
|
|
57
|
+
connection,
|
|
58
|
+
wallet,
|
|
59
|
+
opts,
|
|
60
|
+
timeout,
|
|
61
|
+
additionalConnections: [],
|
|
62
|
+
confirmationStrategy,
|
|
63
|
+
additionalTxSenderCallbacks,
|
|
64
|
+
txHandler,
|
|
65
|
+
trackTxLandRate,
|
|
66
|
+
txLandRateLookbackWindowMinutes,
|
|
67
|
+
landRateToFeeFunc,
|
|
68
|
+
throwOnTimeoutError,
|
|
69
|
+
});
|
|
70
|
+
this.connection = connection;
|
|
71
|
+
this.wallet = wallet;
|
|
72
|
+
this.opts = opts;
|
|
73
|
+
this.timeout = timeout;
|
|
74
|
+
this.retrySleep = retrySleep;
|
|
75
|
+
this.additionalConnections = [];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async sleep(reference: ResolveReference): Promise<void> {
|
|
79
|
+
return new Promise((resolve) => {
|
|
80
|
+
reference.resolve = resolve;
|
|
81
|
+
setTimeout(resolve, this.retrySleep);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
sendToAdditionalConnections(
|
|
86
|
+
rawTx: Buffer | Uint8Array,
|
|
87
|
+
_opts: ConfirmOptions
|
|
88
|
+
): void {
|
|
89
|
+
this.additionalTxSenderCallbacks?.map((callback) => {
|
|
90
|
+
callback(bs58.encode(rawTx));
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async sendRawTransaction(
|
|
95
|
+
rawTransaction: Buffer | Uint8Array,
|
|
96
|
+
opts: ConfirmOptions
|
|
97
|
+
): Promise<TxSigAndSlot> {
|
|
98
|
+
const deserializedTx = VersionedTransaction.deserialize(rawTransaction);
|
|
99
|
+
|
|
100
|
+
const txSig = deserializedTx.signatures[0];
|
|
101
|
+
const encodedTxSig = bs58.encode(txSig);
|
|
102
|
+
|
|
103
|
+
const startTime = this.getTimestamp();
|
|
104
|
+
|
|
105
|
+
this.sendToAdditionalConnections(rawTransaction, opts);
|
|
106
|
+
this.txSigCache?.set(encodedTxSig, false);
|
|
107
|
+
|
|
108
|
+
let done = false;
|
|
109
|
+
const resolveReference: ResolveReference = {
|
|
110
|
+
resolve: undefined,
|
|
111
|
+
};
|
|
112
|
+
const stopWaiting = () => {
|
|
113
|
+
done = true;
|
|
114
|
+
if (resolveReference.resolve) {
|
|
115
|
+
resolveReference.resolve();
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
(async () => {
|
|
120
|
+
while (!done && this.getTimestamp() - startTime < this.timeout) {
|
|
121
|
+
await this.sleep(resolveReference);
|
|
122
|
+
if (!done) {
|
|
123
|
+
this.sendToAdditionalConnections(rawTransaction, opts);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
})();
|
|
127
|
+
|
|
128
|
+
let slot: number | undefined;
|
|
129
|
+
try {
|
|
130
|
+
const result = await this.confirmTransaction(
|
|
131
|
+
encodedTxSig,
|
|
132
|
+
opts.commitment
|
|
133
|
+
);
|
|
134
|
+
slot = result?.context?.slot;
|
|
135
|
+
this.txSigCache?.set(encodedTxSig, true);
|
|
136
|
+
// eslint-disable-next-line no-useless-catch
|
|
137
|
+
} catch (e) {
|
|
138
|
+
throw e;
|
|
139
|
+
} finally {
|
|
140
|
+
stopWaiting();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return { txSig: encodedTxSig, slot };
|
|
144
|
+
}
|
|
145
|
+
}
|
package/src/types.ts
CHANGED
package/tests/amm/test.ts
CHANGED
|
@@ -1566,8 +1566,8 @@ describe('AMM Tests', () => {
|
|
|
1566
1566
|
|
|
1567
1567
|
assert(markTwapLive.eq(new BN('1949826')));
|
|
1568
1568
|
assert(oracleTwapLive.eq(new BN('1942510')));
|
|
1569
|
-
assert(est1.eq(new BN('
|
|
1570
|
-
assert(est2.eq(new BN('
|
|
1569
|
+
assert(est1.eq(new BN('14858')));
|
|
1570
|
+
assert(est2.eq(new BN('14858')));
|
|
1571
1571
|
});
|
|
1572
1572
|
|
|
1573
1573
|
it('predicted funding rate mock2', () => {
|
package/tests/dlob/helpers.ts
CHANGED
|
@@ -208,6 +208,8 @@ function mockPerpMarketCommon(): Omit<
|
|
|
208
208
|
lastFundingRateShort: new BN(0),
|
|
209
209
|
lastFundingRateTs: new BN(0),
|
|
210
210
|
netUnsettledFundingPnl: new BN(0),
|
|
211
|
+
fundingClampThreshold: 5,
|
|
212
|
+
fundingRampSlope: 1000000,
|
|
211
213
|
orderStepSize: new BN(1),
|
|
212
214
|
orderTickSize: new BN(1),
|
|
213
215
|
};
|