@d8x/perpetuals-sdk 2.1.0-alpha2 → 2.1.1-alpha2
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/dist/cjs/abi/IPerpetualManager.json +30 -1
- package/dist/cjs/accountTrade.d.ts +3 -3
- package/dist/cjs/accountTrade.js +1 -1
- package/dist/cjs/accountTrade.js.map +1 -1
- package/dist/cjs/brokerTool.js +7 -7
- package/dist/cjs/brokerTool.js.map +1 -1
- package/dist/cjs/config/defaultConfig.json +0 -12
- package/dist/cjs/config/priceFeedConfig.json +1 -1
- package/dist/cjs/constants.d.ts +0 -1
- package/dist/cjs/constants.js +2 -3
- package/dist/cjs/constants.js.map +1 -1
- package/dist/cjs/contracts/IPerpetualManager.d.ts +20 -4
- package/dist/cjs/contracts/factories/IPerpetualManager__factory.d.ts +23 -1
- package/dist/cjs/contracts/factories/IPerpetualManager__factory.js +30 -1
- package/dist/cjs/contracts/factories/IPerpetualManager__factory.js.map +1 -1
- package/dist/cjs/d8XMath.d.ts +16 -1
- package/dist/cjs/d8XMath.js +28 -5
- package/dist/cjs/d8XMath.js.map +1 -1
- package/dist/cjs/liquidatorTool.js +2 -2
- package/dist/cjs/liquidatorTool.js.map +1 -1
- package/dist/cjs/marketData.d.ts +3 -3
- package/dist/cjs/marketData.js +38 -16
- package/dist/cjs/marketData.js.map +1 -1
- package/dist/cjs/nodeSDKTypes.d.ts +3 -2
- package/dist/cjs/nodeSDKTypes.js.map +1 -1
- package/dist/cjs/perpetualDataHandler.d.ts +9 -8
- package/dist/cjs/perpetualDataHandler.js +17 -6
- package/dist/cjs/perpetualDataHandler.js.map +1 -1
- package/dist/cjs/polyMktsPxFeed.d.ts +1 -1
- package/dist/cjs/polyMktsPxFeed.js +15 -4
- package/dist/cjs/polyMktsPxFeed.js.map +1 -1
- package/dist/cjs/priceFeeds.js +2 -2
- package/dist/cjs/priceFeeds.js.map +1 -1
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/cjs/writeAccessHandler.js +1 -1
- package/dist/cjs/writeAccessHandler.js.map +1 -1
- package/dist/esm/abi/IPerpetualManager.json +30 -1
- package/dist/esm/accountTrade.d.ts +3 -3
- package/dist/esm/accountTrade.js +1 -1
- package/dist/esm/accountTrade.js.map +1 -1
- package/dist/esm/brokerTool.js +7 -7
- package/dist/esm/brokerTool.js.map +1 -1
- package/dist/esm/config/defaultConfig.json +0 -12
- package/dist/esm/config/priceFeedConfig.json +1 -1
- package/dist/esm/constants.d.ts +0 -1
- package/dist/esm/constants.js +1 -2
- package/dist/esm/constants.js.map +1 -1
- package/dist/esm/contracts/IPerpetualManager.d.ts +20 -4
- package/dist/esm/contracts/factories/IPerpetualManager__factory.d.ts +23 -1
- package/dist/esm/contracts/factories/IPerpetualManager__factory.js +30 -1
- package/dist/esm/contracts/factories/IPerpetualManager__factory.js.map +1 -1
- package/dist/esm/d8XMath.d.ts +16 -1
- package/dist/esm/d8XMath.js +26 -4
- package/dist/esm/d8XMath.js.map +1 -1
- package/dist/esm/liquidatorTool.js +2 -2
- package/dist/esm/liquidatorTool.js.map +1 -1
- package/dist/esm/marketData.d.ts +3 -3
- package/dist/esm/marketData.js +39 -17
- package/dist/esm/marketData.js.map +1 -1
- package/dist/esm/nodeSDKTypes.d.ts +3 -2
- package/dist/esm/nodeSDKTypes.js.map +1 -1
- package/dist/esm/perpetualDataHandler.d.ts +9 -8
- package/dist/esm/perpetualDataHandler.js +19 -8
- package/dist/esm/perpetualDataHandler.js.map +1 -1
- package/dist/esm/polyMktsPxFeed.d.ts +1 -1
- package/dist/esm/polyMktsPxFeed.js +15 -4
- package/dist/esm/polyMktsPxFeed.js.map +1 -1
- package/dist/esm/priceFeeds.js +2 -2
- package/dist/esm/priceFeeds.js.map +1 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/esm/writeAccessHandler.js +3 -3
- package/dist/esm/writeAccessHandler.js.map +1 -1
- package/doc/brokerTool.md +3 -1
- package/doc/d8x-perpetuals-sdk.md +804 -132
- package/doc/marketData.md +813 -0
- package/doc/perpetualDataHandler.md +76 -7
- package/package.json +1 -1
- package/src/abi/IPerpetualManager.json +30 -1
- package/src/accountTrade.ts +3 -3
- package/src/brokerTool.ts +7 -7
- package/src/config/defaultConfig.json +0 -13
- package/src/config/priceFeedConfig.json +1 -1
- package/src/constants.ts +1 -2
- package/src/contracts/IPerpetualManager.ts +33 -3
- package/src/contracts/factories/IPerpetualManager__factory.ts +30 -1
- package/src/d8XMath.ts +27 -4
- package/src/liquidatorTool.ts +2 -2
- package/src/marketData.ts +41 -20
- package/src/nodeSDKTypes.ts +3 -2
- package/src/perpetualDataHandler.ts +26 -15
- package/src/polyMktsPxFeed.ts +15 -4
- package/src/priceFeeds.ts +2 -2
- package/src/version.ts +1 -1
- package/src/writeAccessHandler.ts +2 -2
- package/src/abi-zkevm/IPerpetualManager.json +0 -5366
package/src/marketData.ts
CHANGED
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
} from "./constants";
|
|
17
17
|
import {
|
|
18
18
|
ERC20__factory,
|
|
19
|
+
IPerpetualManager__factory,
|
|
19
20
|
LimitOrderBook__factory,
|
|
20
21
|
Multicall3__factory,
|
|
21
22
|
type LimitOrderBook,
|
|
@@ -114,7 +115,7 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
114
115
|
const mktData = providerOrMarketData;
|
|
115
116
|
this.nodeURL = mktData.config.nodeURL;
|
|
116
117
|
this.provider = new JsonRpcProvider(mktData.config.nodeURL, mktData.network, { staticNetwork: true });
|
|
117
|
-
this.proxyContract =
|
|
118
|
+
this.proxyContract = IPerpetualManager__factory.connect(mktData.getProxyAddress(), this.provider);
|
|
118
119
|
this.multicall = Multicall3__factory.connect(this.config.multicall ?? MULTICALL_ADDRESS, this.provider);
|
|
119
120
|
({
|
|
120
121
|
nestedPerpetualIDs: this.nestedPerpetualIDs,
|
|
@@ -179,9 +180,9 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
179
180
|
* }
|
|
180
181
|
* main();
|
|
181
182
|
*
|
|
182
|
-
* @returns
|
|
183
|
+
* @returns read-only proxy instance
|
|
183
184
|
*/
|
|
184
|
-
public getReadOnlyProxyInstance()
|
|
185
|
+
public getReadOnlyProxyInstance() {
|
|
185
186
|
if (this.proxyContract == null) {
|
|
186
187
|
throw Error("no proxy contract initialized. Use createProxyInstance().");
|
|
187
188
|
}
|
|
@@ -478,7 +479,7 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
478
479
|
const isPredMkt = this.isPredictionMarket(symbol);
|
|
479
480
|
// create all calls
|
|
480
481
|
const perpId = PerpetualDataHandler.symbolToPerpetualId(symbol, this.symbolToPerpStaticInfo);
|
|
481
|
-
const [fS2, fS3, fEma] = [indexPriceInfo.s2, indexPriceInfo.s3, indexPriceInfo.ema].map((x) =>
|
|
482
|
+
const [fS2, fS3, fEma] = [indexPriceInfo.s2, indexPriceInfo.s3 ?? 0, indexPriceInfo.ema].map((x) =>
|
|
482
483
|
floatToABK64x64(x)
|
|
483
484
|
) as [bigint, bigint, bigint];
|
|
484
485
|
const proxyCalls: Multicall3.Call3Struct[] = [
|
|
@@ -556,7 +557,7 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
556
557
|
fPrice = await this.proxyContract.queryPerpetualPrice(
|
|
557
558
|
perpId,
|
|
558
559
|
floatToABK64x64(tradeAmountBC),
|
|
559
|
-
[indexPriceInfo.s2, indexPriceInfo.s3],
|
|
560
|
+
[floatToABK64x64(indexPriceInfo.s2), floatToABK64x64(indexPriceInfo.s3 ?? 0)],
|
|
560
561
|
indexPriceInfo.conf,
|
|
561
562
|
indexPriceInfo.predMktCLOBParams
|
|
562
563
|
);
|
|
@@ -748,12 +749,20 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
748
749
|
let newMarginCashCC = currentMarginCashCC + deltaCashCC + traderDepositCC;
|
|
749
750
|
let newEntryPrice = newPositionBC == 0 ? 0 : Math.abs(newLockedInValueQC / newPositionBC);
|
|
750
751
|
let newMarginBalanceCC = newMarginCashCC + (newPositionBC * Sm - newLockedInValueQC) / S3;
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
752
|
+
|
|
753
|
+
let newLeverage: number;
|
|
754
|
+
if (newPositionBC === 0) {
|
|
755
|
+
newLeverage = 0;
|
|
756
|
+
} else if (newMarginBalanceCC <= 0) {
|
|
757
|
+
newLeverage = Infinity;
|
|
758
|
+
} else {
|
|
759
|
+
let p = Sm;
|
|
760
|
+
if (isPredMkt) {
|
|
761
|
+
p -= 1;
|
|
762
|
+
p = newPositionBC > 0 ? p : 1 - p;
|
|
763
|
+
}
|
|
764
|
+
newLeverage = Math.abs(newPositionBC * p) / S3 / newMarginBalanceCC;
|
|
765
|
+
}
|
|
757
766
|
|
|
758
767
|
// Liquidation params
|
|
759
768
|
let [S2Liq, S3Liq, tau] = MarketData._getLiquidationParams(
|
|
@@ -1185,7 +1194,7 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
1185
1194
|
const perpId = PerpetualDataHandler.symbolToPerpetualId(symbol, this.symbolToPerpStaticInfo);
|
|
1186
1195
|
const poolInfo = this.poolStaticInfos[this.getPoolStaticInfoIndexFromSymbol(symbol)];
|
|
1187
1196
|
const indexPriceInfo = await this.priceFeedGetter.fetchPricesForPerpetual(symbol);
|
|
1188
|
-
const [fS2, fS3, fEma] = [indexPriceInfo.s2, indexPriceInfo.s3, indexPriceInfo.ema].map((x) =>
|
|
1197
|
+
const [fS2, fS3, fEma] = [indexPriceInfo.s2, indexPriceInfo.s3 ?? 0, indexPriceInfo.ema].map((x) =>
|
|
1189
1198
|
floatToABK64x64(x)
|
|
1190
1199
|
) as [bigint, bigint, bigint];
|
|
1191
1200
|
const proxyCalls: Multicall3.Call3Struct[] = [
|
|
@@ -1258,20 +1267,20 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
1258
1267
|
account.entryPrice * currentPositionBC,
|
|
1259
1268
|
Sm,
|
|
1260
1269
|
Sm,
|
|
1261
|
-
indexPriceInfo.s3,
|
|
1270
|
+
indexPriceInfo.s3 ?? 0,
|
|
1262
1271
|
totLong,
|
|
1263
1272
|
totShort,
|
|
1264
1273
|
maxShortPosPerp,
|
|
1265
1274
|
maxLongPosPerp
|
|
1266
1275
|
);
|
|
1267
1276
|
const maxLong = pmFindMaxTradeSize(
|
|
1268
|
-
|
|
1277
|
+
1,
|
|
1269
1278
|
currentPositionBC,
|
|
1270
1279
|
availCashCC,
|
|
1271
1280
|
account.entryPrice * currentPositionBC,
|
|
1272
1281
|
Sm,
|
|
1273
1282
|
Sm,
|
|
1274
|
-
indexPriceInfo.s3,
|
|
1283
|
+
indexPriceInfo.s3 ?? 0,
|
|
1275
1284
|
totLong,
|
|
1276
1285
|
totShort,
|
|
1277
1286
|
maxShortPosPerp,
|
|
@@ -1355,7 +1364,7 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
1355
1364
|
const isPredMkt = false;
|
|
1356
1365
|
const indexPriceInfo = await this.priceFeedGetter.fetchPricesForPerpetual(symbol);
|
|
1357
1366
|
let coll2SettlePromise = this.fetchCollateralToSettlementConversion(symbol);
|
|
1358
|
-
const [fS2, fS3, fEma] = [indexPriceInfo.s2, indexPriceInfo.s3, indexPriceInfo.ema].map((x) =>
|
|
1367
|
+
const [fS2, fS3, fEma] = [indexPriceInfo.s2, indexPriceInfo.s3 ?? 0, indexPriceInfo.ema].map((x) =>
|
|
1359
1368
|
floatToABK64x64(x)
|
|
1360
1369
|
) as [bigint, bigint, bigint];
|
|
1361
1370
|
if (!this.proxyContract || !this.multicall) {
|
|
@@ -1516,12 +1525,11 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
1516
1525
|
*
|
|
1517
1526
|
* @returns {number} Price of index in given currency.
|
|
1518
1527
|
*/
|
|
1519
|
-
public async getOraclePrice(base: string, quote: string, overrides?: Overrides)
|
|
1528
|
+
public async getOraclePrice(base: string, quote: string, overrides?: Overrides) {
|
|
1520
1529
|
if (!this.proxyContract) {
|
|
1521
1530
|
throw Error("no proxy contract initialized. Use createProxyInstance().");
|
|
1522
1531
|
}
|
|
1523
|
-
|
|
1524
|
-
return px == undefined ? undefined : ABK64x64ToFloat(px);
|
|
1532
|
+
return await this.proxyContract.getOraclePrice([toBytes4(base), toBytes4(quote)], overrides || {});
|
|
1525
1533
|
}
|
|
1526
1534
|
|
|
1527
1535
|
/**
|
|
@@ -1669,7 +1677,7 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
1669
1677
|
quantity,
|
|
1670
1678
|
this.symbolToPerpStaticInfo,
|
|
1671
1679
|
this.proxyContract,
|
|
1672
|
-
[priceInfo.s2, priceInfo.s3], //s2,s3
|
|
1680
|
+
[priceInfo.s2, priceInfo.s3 ?? 0], //s2,s3
|
|
1673
1681
|
priceInfo.conf, //conf
|
|
1674
1682
|
priceInfo.predMktCLOBParams, //params
|
|
1675
1683
|
overrides
|
|
@@ -2293,6 +2301,7 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
2293
2301
|
indexPrice: 0, //fill later
|
|
2294
2302
|
collToQuoteIndexPrice: 0, //fill later
|
|
2295
2303
|
markPremium: ABK64x64ToFloat(perp.currentMarkPremiumRate!.fPrice),
|
|
2304
|
+
markPrice: 0, //fill later
|
|
2296
2305
|
midPrice: 0, // fill later
|
|
2297
2306
|
currentFundingRateBps: 1e4 * ABK64x64ToFloat(perp.fCurrentFundingRate!),
|
|
2298
2307
|
openInterestBC: ABK64x64ToFloat(perp.fOpenInterest!),
|
|
@@ -2376,6 +2385,18 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
2376
2385
|
indexS3 = idxPriceS3Pair[0];
|
|
2377
2386
|
}
|
|
2378
2387
|
perp.collToQuoteIndexPrice = indexS3;
|
|
2388
|
+
|
|
2389
|
+
const emaKey = info!.S2Symbol + ":ema";
|
|
2390
|
+
let markPrice: number;
|
|
2391
|
+
if (idxPriceMap.has(emaKey)) {
|
|
2392
|
+
let ema: number;
|
|
2393
|
+
let res = idxPriceMap.get(emaKey);
|
|
2394
|
+
ema = res![0];
|
|
2395
|
+
markPrice = ema + perp.markPremium;
|
|
2396
|
+
} else {
|
|
2397
|
+
markPrice = perp.indexPrice * (1 + perp.markPremium);
|
|
2398
|
+
}
|
|
2399
|
+
perp.markPrice = markPrice;
|
|
2379
2400
|
perp.midPrice = midPriceMap.get(symbol3s!)!;
|
|
2380
2401
|
// which pool?
|
|
2381
2402
|
const poolId = info!.poolId;
|
package/src/nodeSDKTypes.ts
CHANGED
|
@@ -147,6 +147,7 @@ export interface PerpetualState {
|
|
|
147
147
|
indexPrice: number;
|
|
148
148
|
collToQuoteIndexPrice: number;
|
|
149
149
|
markPremium: number;
|
|
150
|
+
markPrice: number;
|
|
150
151
|
midPrice: number;
|
|
151
152
|
currentFundingRateBps: number;
|
|
152
153
|
openInterestBC: number;
|
|
@@ -539,10 +540,10 @@ export const referralTypes = {
|
|
|
539
540
|
// prediction markets and regular markets
|
|
540
541
|
export interface IdxPriceInfo {
|
|
541
542
|
s2: number;
|
|
542
|
-
s3
|
|
543
|
+
s3?: number;
|
|
543
544
|
ema: number;
|
|
544
545
|
s2MktClosed: boolean;
|
|
545
|
-
s3MktClosed
|
|
546
|
+
s3MktClosed?: boolean;
|
|
546
547
|
conf: bigint;
|
|
547
548
|
predMktCLOBParams: bigint;
|
|
548
549
|
}
|
|
@@ -37,6 +37,7 @@ import {
|
|
|
37
37
|
} from "./constants";
|
|
38
38
|
import {
|
|
39
39
|
ERC20__factory,
|
|
40
|
+
IPerpetualManager__factory,
|
|
40
41
|
LimitOrderBook,
|
|
41
42
|
LimitOrderBookFactory,
|
|
42
43
|
LimitOrderBookFactory__factory,
|
|
@@ -45,7 +46,7 @@ import {
|
|
|
45
46
|
Multicall3__factory,
|
|
46
47
|
OracleFactory__factory,
|
|
47
48
|
} from "./contracts";
|
|
48
|
-
import { IPerpetualInfo } from "./contracts/IPerpetualManager";
|
|
49
|
+
import { IPerpetualInfo, IPerpetualManager } from "./contracts/IPerpetualManager";
|
|
49
50
|
import { IClientOrder, IPerpetualOrder } from "./contracts/LimitOrderBook";
|
|
50
51
|
|
|
51
52
|
import {
|
|
@@ -111,7 +112,7 @@ export default class PerpetualDataHandler {
|
|
|
111
112
|
protected symbolToTokenAddrMap: Map<string, string>;
|
|
112
113
|
public chainId: bigint;
|
|
113
114
|
public network: Network;
|
|
114
|
-
protected proxyContract:
|
|
115
|
+
protected proxyContract: IPerpetualManager | null = null;
|
|
115
116
|
protected proxyABI: Interface;
|
|
116
117
|
protected proxyAddr: string;
|
|
117
118
|
// oracle
|
|
@@ -181,7 +182,7 @@ export default class PerpetualDataHandler {
|
|
|
181
182
|
if (network.chainId !== this.chainId) {
|
|
182
183
|
throw new Error(`Provider: chain id ${network.chainId} does not match config (${this.chainId})`);
|
|
183
184
|
}
|
|
184
|
-
this.proxyContract =
|
|
185
|
+
this.proxyContract = IPerpetualManager__factory.connect(this.proxyAddr, signerOrProvider);
|
|
185
186
|
this.multicall = Multicall3__factory.connect(this.config.multicall ?? MULTICALL_ADDRESS, this.signerOrProvider);
|
|
186
187
|
await this._fillSymbolMaps(overrides);
|
|
187
188
|
}
|
|
@@ -616,7 +617,7 @@ export default class PerpetualDataHandler {
|
|
|
616
617
|
* @returns array with PerpetualStaticInfo for each perpetual
|
|
617
618
|
*/
|
|
618
619
|
public static async getPerpetualStaticInfo(
|
|
619
|
-
_proxyContract:
|
|
620
|
+
_proxyContract: IPerpetualManager,
|
|
620
621
|
nestedPerpetualIDs: Array<Array<number>>,
|
|
621
622
|
symbolList: Map<string, string>,
|
|
622
623
|
overrides?: Overrides
|
|
@@ -695,7 +696,7 @@ export default class PerpetualDataHandler {
|
|
|
695
696
|
public static async _getLiquidityPools(
|
|
696
697
|
fromIdx: number,
|
|
697
698
|
toIdx: number,
|
|
698
|
-
_proxyContract:
|
|
699
|
+
_proxyContract: IPerpetualManager,
|
|
699
700
|
_symbolList: Map<string, string>,
|
|
700
701
|
overrides?: Overrides
|
|
701
702
|
): Promise<LiquidityPoolData[]> {
|
|
@@ -742,7 +743,7 @@ export default class PerpetualDataHandler {
|
|
|
742
743
|
*/
|
|
743
744
|
public static async _getPerpetuals(
|
|
744
745
|
ids: number[],
|
|
745
|
-
_proxyContract:
|
|
746
|
+
_proxyContract: IPerpetualManager,
|
|
746
747
|
_symbolList: Map<string, string>,
|
|
747
748
|
overrides?: Overrides
|
|
748
749
|
): Promise<PerpetualData[]> {
|
|
@@ -813,7 +814,7 @@ export default class PerpetualDataHandler {
|
|
|
813
814
|
}
|
|
814
815
|
|
|
815
816
|
public static async getPoolStaticInfo(
|
|
816
|
-
_proxyContract:
|
|
817
|
+
_proxyContract: IPerpetualManager,
|
|
817
818
|
overrides?: Overrides
|
|
818
819
|
): Promise<{
|
|
819
820
|
nestedPerpetualIDs: Array<Array<number>>;
|
|
@@ -932,7 +933,7 @@ export default class PerpetualDataHandler {
|
|
|
932
933
|
let traderState = await _proxyContract.getTraderState(
|
|
933
934
|
perpId,
|
|
934
935
|
traderAddr,
|
|
935
|
-
[_pxInfo.ema, _pxInfo.s3].map((x) => floatToABK64x64(x)) as [bigint, bigint],
|
|
936
|
+
[_pxInfo.ema, _pxInfo.s3 ?? 0].map((x) => floatToABK64x64(x)) as [bigint, bigint],
|
|
936
937
|
overrides || {}
|
|
937
938
|
);
|
|
938
939
|
return PerpetualDataHandler.buildMarginAccountFromState(
|
|
@@ -1132,7 +1133,7 @@ export default class PerpetualDataHandler {
|
|
|
1132
1133
|
callData: _proxyContract.interface.encodeFunctionData("getTraderState", [
|
|
1133
1134
|
PerpetualDataHandler.symbolToPerpetualId(symbols[i], symbolToPerpStaticInfo),
|
|
1134
1135
|
_addr,
|
|
1135
|
-
[_pxInfo[i].ema, _pxInfo[i].s3].map((x) => floatToABK64x64(x)) as [bigint, bigint],
|
|
1136
|
+
[_pxInfo[i].ema, _pxInfo[i].s3 ?? 0].map((x) => floatToABK64x64(x)) as [bigint, bigint],
|
|
1136
1137
|
]),
|
|
1137
1138
|
}));
|
|
1138
1139
|
const encodedResults = await _multicall.aggregate3.staticCall(proxyCalls, overrides || {});
|
|
@@ -1155,7 +1156,7 @@ export default class PerpetualDataHandler {
|
|
|
1155
1156
|
symbol: string,
|
|
1156
1157
|
tradeAmount: number,
|
|
1157
1158
|
symbolToPerpStaticInfo: Map<string, PerpetualStaticInfo>,
|
|
1158
|
-
_proxyContract:
|
|
1159
|
+
_proxyContract: IPerpetualManager,
|
|
1159
1160
|
indexPrices: [number, number],
|
|
1160
1161
|
conf: bigint,
|
|
1161
1162
|
params: bigint,
|
|
@@ -1187,7 +1188,7 @@ export default class PerpetualDataHandler {
|
|
|
1187
1188
|
protected static async _queryPerpetualMarkPrice(
|
|
1188
1189
|
symbol: string,
|
|
1189
1190
|
symbolToPerpStaticInfo: Map<string, PerpetualStaticInfo>,
|
|
1190
|
-
_proxyContract:
|
|
1191
|
+
_proxyContract: IPerpetualManager,
|
|
1191
1192
|
indexPrices: IdxPriceInfo,
|
|
1192
1193
|
isPredMkt: boolean,
|
|
1193
1194
|
overrides?: Overrides
|
|
@@ -1207,7 +1208,7 @@ export default class PerpetualDataHandler {
|
|
|
1207
1208
|
protected static async _queryPerpetualState(
|
|
1208
1209
|
symbol: string,
|
|
1209
1210
|
symbolToPerpStaticInfo: Map<string, PerpetualStaticInfo>,
|
|
1210
|
-
_proxyContract:
|
|
1211
|
+
_proxyContract: IPerpetualManager,
|
|
1211
1212
|
_multicall: Multicall3,
|
|
1212
1213
|
indexPrices: IdxPriceInfo,
|
|
1213
1214
|
overrides?: Overrides
|
|
@@ -1226,7 +1227,7 @@ export default class PerpetualDataHandler {
|
|
|
1226
1227
|
allowFailure: false,
|
|
1227
1228
|
callData: _proxyContract.interface.encodeFunctionData("getAMMState", [
|
|
1228
1229
|
perpId,
|
|
1229
|
-
[indexPrices.s2, indexPrices.s3].map(floatToABK64x64) as [bigint, bigint],
|
|
1230
|
+
[indexPrices.s2, indexPrices.s3 ?? 0].map(floatToABK64x64) as [bigint, bigint],
|
|
1230
1231
|
]),
|
|
1231
1232
|
},
|
|
1232
1233
|
{
|
|
@@ -1281,18 +1282,28 @@ export default class PerpetualDataHandler {
|
|
|
1281
1282
|
} else if (staticInfo.collateralCurrencyType == CollaterlCCY.QUOTE) {
|
|
1282
1283
|
S3 = 1;
|
|
1283
1284
|
}
|
|
1285
|
+
const isPred = PerpetualDataHandler.isPredictionMarketStatic(staticInfo);
|
|
1286
|
+
let markPrice: number;
|
|
1287
|
+
if (isPred) {
|
|
1288
|
+
// ema + premium
|
|
1289
|
+
markPrice = indexPrices.ema + ABK64x64ToFloat(ammState[8]);
|
|
1290
|
+
} else {
|
|
1291
|
+
// S2 * (1+premium)
|
|
1292
|
+
markPrice = indexPrices.s2 * (1 + ABK64x64ToFloat(ammState[8]));
|
|
1293
|
+
}
|
|
1284
1294
|
let state: PerpetualState = {
|
|
1285
1295
|
id: perpId,
|
|
1286
1296
|
state: PERP_STATE_STR[Number(ammState[13])],
|
|
1287
1297
|
baseCurrency: ccy[0],
|
|
1288
1298
|
quoteCurrency: ccy[1],
|
|
1289
1299
|
indexPrice: S2,
|
|
1290
|
-
collToQuoteIndexPrice: S3,
|
|
1300
|
+
collToQuoteIndexPrice: S3 ?? (ccy[0] === ccy[1] ? S2 : 1),
|
|
1291
1301
|
markPremium: ABK64x64ToFloat(ammState[8]),
|
|
1302
|
+
markPrice: markPrice,
|
|
1292
1303
|
midPrice: ABK64x64ToFloat(ammState[10]),
|
|
1293
1304
|
currentFundingRateBps: ABK64x64ToFloat(ammState[14]) * 1e4,
|
|
1294
1305
|
openInterestBC: ABK64x64ToFloat(ammState[11]),
|
|
1295
|
-
isMarketClosed: indexPrices.s2MktClosed || indexPrices.s3MktClosed,
|
|
1306
|
+
isMarketClosed: indexPrices.s2MktClosed || (indexPrices.s3MktClosed !== undefined && indexPrices.s3MktClosed),
|
|
1296
1307
|
longBC: ABK64x64ToFloat(longShort[0]),
|
|
1297
1308
|
shortBC: ABK64x64ToFloat(longShort[1]),
|
|
1298
1309
|
};
|
package/src/polyMktsPxFeed.ts
CHANGED
|
@@ -16,8 +16,19 @@ export default class PolyMktsPxFeed {
|
|
|
16
16
|
private ids: Map<string, PolyConfig>;
|
|
17
17
|
private oracleEndpoint: string;
|
|
18
18
|
|
|
19
|
-
constructor(config: PriceFeedConfig
|
|
19
|
+
constructor(config: PriceFeedConfig) {
|
|
20
20
|
this.ids = new Map<string, PolyConfig>();
|
|
21
|
+
this.oracleEndpoint = "";
|
|
22
|
+
for (let k = 0; k < config.endpoints.length; k++) {
|
|
23
|
+
if (config.endpoints[k].type == "polymarket") {
|
|
24
|
+
let endp = config.endpoints[k].writeEndpoints;
|
|
25
|
+
this.oracleEndpoint = endp[Math.floor(Math.random() * endp.length)];
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (this.oracleEndpoint == "") {
|
|
30
|
+
throw Error("no polymarkets write endpoint defined in priceFeedConfig");
|
|
31
|
+
}
|
|
21
32
|
for (let k = 0; k < config.ids.length; k++) {
|
|
22
33
|
if (config.ids[k].type == "polymarket") {
|
|
23
34
|
const sym = config.ids[k].symbol;
|
|
@@ -31,7 +42,7 @@ export default class PolyMktsPxFeed {
|
|
|
31
42
|
}
|
|
32
43
|
}
|
|
33
44
|
|
|
34
|
-
this.oracleEndpoint = oracleEndpoint.replace(/\/$/, "") + "v2/updates/price/latest?encoding=base64&ids[]=";
|
|
45
|
+
this.oracleEndpoint = this.oracleEndpoint.replace(/\/$/, "") + "/v2/updates/price/latest?encoding=base64&ids[]=";
|
|
35
46
|
}
|
|
36
47
|
|
|
37
48
|
public isPolyMktsSym(sym: string) {
|
|
@@ -58,8 +69,8 @@ export default class PolyMktsPxFeed {
|
|
|
58
69
|
throw new Error(`failed to fetch posts (${response.status}): ${response.statusText} ${query}`);
|
|
59
70
|
}
|
|
60
71
|
const data = await response.json();
|
|
61
|
-
const emaObj = data.parsed.ema_price as PriceFeedJson;
|
|
62
|
-
const pxObj = data.parsed.price as PriceFeedJson;
|
|
72
|
+
const emaObj = data.parsed[0].ema_price as PriceFeedJson;
|
|
73
|
+
const pxObj = data.parsed[0].price as PriceFeedJson;
|
|
63
74
|
|
|
64
75
|
const px = Number(pxObj.price) * Math.pow(10, pxObj.expo);
|
|
65
76
|
const ema = Number(emaObj.price) * Math.pow(10, emaObj.expo);
|
package/src/priceFeeds.ts
CHANGED
|
@@ -68,8 +68,7 @@ export default class PriceFeeds {
|
|
|
68
68
|
if (this.writeFeedEndpoints.length == 0) {
|
|
69
69
|
throw new Error("PriceFeeds: no writeEndpoints provided in config");
|
|
70
70
|
}
|
|
71
|
-
|
|
72
|
-
this.polyMktsPxFeed = new PolyMktsPxFeed(this.config, this.writeFeedEndpoints[idx]);
|
|
71
|
+
this.polyMktsPxFeed = new PolyMktsPxFeed(this.config);
|
|
73
72
|
this.dataHandler = dataHandler;
|
|
74
73
|
this.triangulations = new Map<string, [string[], boolean[]]>();
|
|
75
74
|
}
|
|
@@ -352,6 +351,7 @@ export default class PriceFeeds {
|
|
|
352
351
|
prices.push(info);
|
|
353
352
|
} catch (error) {
|
|
354
353
|
console.log("fetchPriceForSym failed for " + symbols[k]);
|
|
354
|
+
console.log(error);
|
|
355
355
|
prices.push(undefined);
|
|
356
356
|
}
|
|
357
357
|
if (k > 0) {
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const D8X_SDK_VERSION = "2.1.
|
|
1
|
+
export const D8X_SDK_VERSION = "2.1.1-alpha2";
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
Wallet,
|
|
10
10
|
} from "ethers";
|
|
11
11
|
import { MAX_UINT_256, MULTICALL_ADDRESS } from "./constants";
|
|
12
|
-
import { ERC20__factory, MockTokenSwap__factory, Multicall3__factory } from "./contracts";
|
|
12
|
+
import { ERC20__factory, IPerpetualManager__factory, MockTokenSwap__factory, Multicall3__factory } from "./contracts";
|
|
13
13
|
import { PayableOverrides } from "./contracts/common";
|
|
14
14
|
import { floatToDecN } from "./d8XMath";
|
|
15
15
|
import MarketData from "./marketData";
|
|
@@ -66,7 +66,7 @@ export default class WriteAccessHandler extends PerpetualDataHandler {
|
|
|
66
66
|
const mktData = providerOrMarketData;
|
|
67
67
|
this.nodeURL = mktData.config.nodeURL;
|
|
68
68
|
this.provider = new JsonRpcProvider(mktData.config.nodeURL, mktData.network, { staticNetwork: true });
|
|
69
|
-
this.proxyContract =
|
|
69
|
+
this.proxyContract = IPerpetualManager__factory.connect(mktData.getProxyAddress(), this.provider);
|
|
70
70
|
this.multicall = Multicall3__factory.connect(this.config.multicall ?? MULTICALL_ADDRESS, this.provider);
|
|
71
71
|
({
|
|
72
72
|
nestedPerpetualIDs: this.nestedPerpetualIDs,
|