@strobelabs/perpcity-sdk 0.4.3 → 0.5.1
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/index.d.mts +118 -126
- package/dist/index.d.ts +118 -126
- package/dist/index.js +242 -165
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +236 -165
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -36,6 +36,10 @@ __export(index_exports, {
|
|
|
36
36
|
ErrorCategory: () => ErrorCategory,
|
|
37
37
|
ErrorSource: () => ErrorSource,
|
|
38
38
|
InsufficientFundsError: () => InsufficientFundsError,
|
|
39
|
+
MAX_PRICE: () => MAX_PRICE,
|
|
40
|
+
MAX_TICK: () => MAX_TICK,
|
|
41
|
+
MIN_PRICE: () => MIN_PRICE,
|
|
42
|
+
MIN_TICK: () => MIN_TICK,
|
|
39
43
|
NUMBER_1E6: () => NUMBER_1E6,
|
|
40
44
|
OpenPosition: () => OpenPosition,
|
|
41
45
|
PERP_MANAGER_ABI: () => PERP_MANAGER_ABI,
|
|
@@ -46,6 +50,7 @@ __export(index_exports, {
|
|
|
46
50
|
TransactionRejectedError: () => TransactionRejectedError,
|
|
47
51
|
ValidationError: () => ValidationError,
|
|
48
52
|
approveUsdc: () => approveUsdc,
|
|
53
|
+
calculateAlignedTicks: () => calculateAlignedTicks,
|
|
49
54
|
calculateEntryPrice: () => calculateEntryPrice,
|
|
50
55
|
calculateLeverage: () => calculateLeverage,
|
|
51
56
|
calculateLiquidationPrice: () => calculateLiquidationPrice,
|
|
@@ -80,6 +85,7 @@ __export(index_exports, {
|
|
|
80
85
|
parseContractError: () => parseContractError,
|
|
81
86
|
priceToSqrtPriceX96: () => priceToSqrtPriceX96,
|
|
82
87
|
priceToTick: () => priceToTick,
|
|
88
|
+
quoteOpenMakerPosition: () => quoteOpenMakerPosition,
|
|
83
89
|
scale6Decimals: () => scale6Decimals,
|
|
84
90
|
scaleFrom6Decimals: () => scaleFrom6Decimals,
|
|
85
91
|
scaleFromX96: () => scaleFromX96,
|
|
@@ -668,26 +674,19 @@ var PERP_MANAGER_ABI = [
|
|
|
668
674
|
anonymous: false,
|
|
669
675
|
inputs: [
|
|
670
676
|
{
|
|
671
|
-
indexed:
|
|
672
|
-
internalType: "
|
|
673
|
-
name: "
|
|
674
|
-
type: "
|
|
675
|
-
}
|
|
676
|
-
],
|
|
677
|
-
name: "FeesModuleRegistered",
|
|
678
|
-
type: "event"
|
|
679
|
-
},
|
|
680
|
-
{
|
|
681
|
-
anonymous: false,
|
|
682
|
-
inputs: [
|
|
677
|
+
indexed: true,
|
|
678
|
+
internalType: "uint8",
|
|
679
|
+
name: "moduleType",
|
|
680
|
+
type: "uint8"
|
|
681
|
+
},
|
|
683
682
|
{
|
|
684
|
-
indexed:
|
|
685
|
-
internalType: "
|
|
686
|
-
name: "
|
|
683
|
+
indexed: true,
|
|
684
|
+
internalType: "address",
|
|
685
|
+
name: "module",
|
|
687
686
|
type: "address"
|
|
688
687
|
}
|
|
689
688
|
],
|
|
690
|
-
name: "
|
|
689
|
+
name: "ModuleRegistered",
|
|
691
690
|
type: "event"
|
|
692
691
|
},
|
|
693
692
|
{
|
|
@@ -715,19 +714,6 @@ var PERP_MANAGER_ABI = [
|
|
|
715
714
|
name: "MarginAdjusted",
|
|
716
715
|
type: "event"
|
|
717
716
|
},
|
|
718
|
-
{
|
|
719
|
-
anonymous: false,
|
|
720
|
-
inputs: [
|
|
721
|
-
{
|
|
722
|
-
indexed: false,
|
|
723
|
-
internalType: "contract IMarginRatios",
|
|
724
|
-
name: "marginRatiosModule",
|
|
725
|
-
type: "address"
|
|
726
|
-
}
|
|
727
|
-
],
|
|
728
|
-
name: "MarginRatiosModuleRegistered",
|
|
729
|
-
type: "event"
|
|
730
|
-
},
|
|
731
717
|
{
|
|
732
718
|
anonymous: false,
|
|
733
719
|
inputs: [
|
|
@@ -901,13 +887,13 @@ var PERP_MANAGER_ABI = [
|
|
|
901
887
|
{
|
|
902
888
|
indexed: false,
|
|
903
889
|
internalType: "int256",
|
|
904
|
-
name: "
|
|
890
|
+
name: "exitPerpDelta",
|
|
905
891
|
type: "int256"
|
|
906
892
|
},
|
|
907
893
|
{
|
|
908
894
|
indexed: false,
|
|
909
895
|
internalType: "int256",
|
|
910
|
-
name: "
|
|
896
|
+
name: "exitUsdDelta",
|
|
911
897
|
type: "int256"
|
|
912
898
|
},
|
|
913
899
|
{
|
|
@@ -921,6 +907,42 @@ var PERP_MANAGER_ABI = [
|
|
|
921
907
|
internalType: "int24",
|
|
922
908
|
name: "tickUpper",
|
|
923
909
|
type: "int24"
|
|
910
|
+
},
|
|
911
|
+
{
|
|
912
|
+
indexed: false,
|
|
913
|
+
internalType: "int256",
|
|
914
|
+
name: "netUsdDelta",
|
|
915
|
+
type: "int256"
|
|
916
|
+
},
|
|
917
|
+
{
|
|
918
|
+
indexed: false,
|
|
919
|
+
internalType: "int256",
|
|
920
|
+
name: "funding",
|
|
921
|
+
type: "int256"
|
|
922
|
+
},
|
|
923
|
+
{
|
|
924
|
+
indexed: false,
|
|
925
|
+
internalType: "uint256",
|
|
926
|
+
name: "utilizationFee",
|
|
927
|
+
type: "uint256"
|
|
928
|
+
},
|
|
929
|
+
{
|
|
930
|
+
indexed: false,
|
|
931
|
+
internalType: "uint256",
|
|
932
|
+
name: "adl",
|
|
933
|
+
type: "uint256"
|
|
934
|
+
},
|
|
935
|
+
{
|
|
936
|
+
indexed: false,
|
|
937
|
+
internalType: "uint256",
|
|
938
|
+
name: "liquidationFee",
|
|
939
|
+
type: "uint256"
|
|
940
|
+
},
|
|
941
|
+
{
|
|
942
|
+
indexed: false,
|
|
943
|
+
internalType: "int256",
|
|
944
|
+
name: "netMargin",
|
|
945
|
+
type: "int256"
|
|
924
946
|
}
|
|
925
947
|
],
|
|
926
948
|
name: "PositionClosed",
|
|
@@ -993,19 +1015,6 @@ var PERP_MANAGER_ABI = [
|
|
|
993
1015
|
name: "PositionOpened",
|
|
994
1016
|
type: "event"
|
|
995
1017
|
},
|
|
996
|
-
{
|
|
997
|
-
anonymous: false,
|
|
998
|
-
inputs: [
|
|
999
|
-
{
|
|
1000
|
-
indexed: false,
|
|
1001
|
-
internalType: "contract ISqrtPriceImpactLimit",
|
|
1002
|
-
name: "sqrtPriceImpactLimitModule",
|
|
1003
|
-
type: "address"
|
|
1004
|
-
}
|
|
1005
|
-
],
|
|
1006
|
-
name: "SqrtPriceImpactLimitModuleRegistered",
|
|
1007
|
-
type: "event"
|
|
1008
|
-
},
|
|
1009
1018
|
{
|
|
1010
1019
|
anonymous: false,
|
|
1011
1020
|
inputs: [
|
|
@@ -1132,12 +1141,12 @@ var PERP_MANAGER_ABI = [
|
|
|
1132
1141
|
},
|
|
1133
1142
|
{
|
|
1134
1143
|
internalType: "int256",
|
|
1135
|
-
name: "
|
|
1144
|
+
name: "usdDelta",
|
|
1136
1145
|
type: "int256"
|
|
1137
1146
|
},
|
|
1138
1147
|
{
|
|
1139
1148
|
internalType: "uint128",
|
|
1140
|
-
name: "
|
|
1149
|
+
name: "perpLimit",
|
|
1141
1150
|
type: "uint128"
|
|
1142
1151
|
}
|
|
1143
1152
|
],
|
|
@@ -1384,11 +1393,6 @@ var PERP_MANAGER_ABI = [
|
|
|
1384
1393
|
internalType: "contract ISqrtPriceImpactLimit",
|
|
1385
1394
|
name: "sqrtPriceImpactLimit",
|
|
1386
1395
|
type: "address"
|
|
1387
|
-
},
|
|
1388
|
-
{
|
|
1389
|
-
internalType: "uint160",
|
|
1390
|
-
name: "startingSqrtPriceX96",
|
|
1391
|
-
type: "uint160"
|
|
1392
1396
|
}
|
|
1393
1397
|
],
|
|
1394
1398
|
internalType: "struct IPerpManager.CreatePerpParams",
|
|
@@ -1509,69 +1513,17 @@ var PERP_MANAGER_ABI = [
|
|
|
1509
1513
|
{
|
|
1510
1514
|
inputs: [
|
|
1511
1515
|
{
|
|
1512
|
-
internalType: "
|
|
1513
|
-
name: "",
|
|
1514
|
-
type: "
|
|
1515
|
-
}
|
|
1516
|
-
],
|
|
1517
|
-
name: "isFeesRegistered",
|
|
1518
|
-
outputs: [
|
|
1519
|
-
{
|
|
1520
|
-
internalType: "bool",
|
|
1521
|
-
name: "",
|
|
1522
|
-
type: "bool"
|
|
1523
|
-
}
|
|
1524
|
-
],
|
|
1525
|
-
stateMutability: "view",
|
|
1526
|
-
type: "function"
|
|
1527
|
-
},
|
|
1528
|
-
{
|
|
1529
|
-
inputs: [
|
|
1530
|
-
{
|
|
1531
|
-
internalType: "contract ILockupPeriod",
|
|
1532
|
-
name: "",
|
|
1533
|
-
type: "address"
|
|
1534
|
-
}
|
|
1535
|
-
],
|
|
1536
|
-
name: "isLockupPeriodRegistered",
|
|
1537
|
-
outputs: [
|
|
1538
|
-
{
|
|
1539
|
-
internalType: "bool",
|
|
1540
|
-
name: "",
|
|
1541
|
-
type: "bool"
|
|
1542
|
-
}
|
|
1543
|
-
],
|
|
1544
|
-
stateMutability: "view",
|
|
1545
|
-
type: "function"
|
|
1546
|
-
},
|
|
1547
|
-
{
|
|
1548
|
-
inputs: [
|
|
1549
|
-
{
|
|
1550
|
-
internalType: "contract IMarginRatios",
|
|
1551
|
-
name: "",
|
|
1552
|
-
type: "address"
|
|
1553
|
-
}
|
|
1554
|
-
],
|
|
1555
|
-
name: "isMarginRatiosRegistered",
|
|
1556
|
-
outputs: [
|
|
1557
|
-
{
|
|
1558
|
-
internalType: "bool",
|
|
1559
|
-
name: "",
|
|
1560
|
-
type: "bool"
|
|
1561
|
-
}
|
|
1562
|
-
],
|
|
1563
|
-
stateMutability: "view",
|
|
1564
|
-
type: "function"
|
|
1565
|
-
},
|
|
1566
|
-
{
|
|
1567
|
-
inputs: [
|
|
1516
|
+
internalType: "uint8",
|
|
1517
|
+
name: "moduleType",
|
|
1518
|
+
type: "uint8"
|
|
1519
|
+
},
|
|
1568
1520
|
{
|
|
1569
|
-
internalType: "
|
|
1570
|
-
name: "",
|
|
1521
|
+
internalType: "address",
|
|
1522
|
+
name: "module",
|
|
1571
1523
|
type: "address"
|
|
1572
1524
|
}
|
|
1573
1525
|
],
|
|
1574
|
-
name: "
|
|
1526
|
+
name: "isModuleRegistered",
|
|
1575
1527
|
outputs: [
|
|
1576
1528
|
{
|
|
1577
1529
|
internalType: "bool",
|
|
@@ -1923,14 +1875,19 @@ var PERP_MANAGER_ABI = [
|
|
|
1923
1875
|
type: "int256"
|
|
1924
1876
|
},
|
|
1925
1877
|
{
|
|
1926
|
-
internalType: "
|
|
1878
|
+
internalType: "int256",
|
|
1927
1879
|
name: "netMargin",
|
|
1928
|
-
type: "
|
|
1880
|
+
type: "int256"
|
|
1929
1881
|
},
|
|
1930
1882
|
{
|
|
1931
1883
|
internalType: "bool",
|
|
1932
1884
|
name: "wasLiquidated",
|
|
1933
1885
|
type: "bool"
|
|
1886
|
+
},
|
|
1887
|
+
{
|
|
1888
|
+
internalType: "uint256",
|
|
1889
|
+
name: "notional",
|
|
1890
|
+
type: "uint256"
|
|
1934
1891
|
}
|
|
1935
1892
|
],
|
|
1936
1893
|
stateMutability: "nonpayable",
|
|
@@ -2071,51 +2028,66 @@ var PERP_MANAGER_ABI = [
|
|
|
2071
2028
|
{
|
|
2072
2029
|
inputs: [
|
|
2073
2030
|
{
|
|
2074
|
-
internalType: "
|
|
2075
|
-
name: "
|
|
2076
|
-
type: "
|
|
2077
|
-
}
|
|
2078
|
-
],
|
|
2079
|
-
name: "registerFeesModule",
|
|
2080
|
-
outputs: [],
|
|
2081
|
-
stateMutability: "nonpayable",
|
|
2082
|
-
type: "function"
|
|
2083
|
-
},
|
|
2084
|
-
{
|
|
2085
|
-
inputs: [
|
|
2031
|
+
internalType: "PoolId",
|
|
2032
|
+
name: "perpId",
|
|
2033
|
+
type: "bytes32"
|
|
2034
|
+
},
|
|
2086
2035
|
{
|
|
2087
|
-
internalType: "
|
|
2088
|
-
name: "
|
|
2089
|
-
type: "
|
|
2036
|
+
internalType: "bool",
|
|
2037
|
+
name: "zeroForOne",
|
|
2038
|
+
type: "bool"
|
|
2039
|
+
},
|
|
2040
|
+
{
|
|
2041
|
+
internalType: "bool",
|
|
2042
|
+
name: "isExactIn",
|
|
2043
|
+
type: "bool"
|
|
2044
|
+
},
|
|
2045
|
+
{
|
|
2046
|
+
internalType: "uint256",
|
|
2047
|
+
name: "amount",
|
|
2048
|
+
type: "uint256"
|
|
2049
|
+
},
|
|
2050
|
+
{
|
|
2051
|
+
internalType: "uint160",
|
|
2052
|
+
name: "sqrtPriceLimitX96",
|
|
2053
|
+
type: "uint160"
|
|
2090
2054
|
}
|
|
2091
2055
|
],
|
|
2092
|
-
name: "
|
|
2093
|
-
outputs: [
|
|
2094
|
-
stateMutability: "nonpayable",
|
|
2095
|
-
type: "function"
|
|
2096
|
-
},
|
|
2097
|
-
{
|
|
2098
|
-
inputs: [
|
|
2056
|
+
name: "quoteSwap",
|
|
2057
|
+
outputs: [
|
|
2099
2058
|
{
|
|
2100
|
-
internalType: "
|
|
2101
|
-
name: "
|
|
2102
|
-
type: "
|
|
2059
|
+
internalType: "bytes",
|
|
2060
|
+
name: "unexpectedReason",
|
|
2061
|
+
type: "bytes"
|
|
2062
|
+
},
|
|
2063
|
+
{
|
|
2064
|
+
internalType: "int256",
|
|
2065
|
+
name: "perpDelta",
|
|
2066
|
+
type: "int256"
|
|
2067
|
+
},
|
|
2068
|
+
{
|
|
2069
|
+
internalType: "int256",
|
|
2070
|
+
name: "usdDelta",
|
|
2071
|
+
type: "int256"
|
|
2103
2072
|
}
|
|
2104
2073
|
],
|
|
2105
|
-
name: "registerMarginRatiosModule",
|
|
2106
|
-
outputs: [],
|
|
2107
2074
|
stateMutability: "nonpayable",
|
|
2108
2075
|
type: "function"
|
|
2109
2076
|
},
|
|
2110
2077
|
{
|
|
2111
2078
|
inputs: [
|
|
2112
2079
|
{
|
|
2113
|
-
internalType: "
|
|
2114
|
-
name: "
|
|
2080
|
+
internalType: "uint8",
|
|
2081
|
+
name: "moduleType",
|
|
2082
|
+
type: "uint8"
|
|
2083
|
+
},
|
|
2084
|
+
{
|
|
2085
|
+
internalType: "address",
|
|
2086
|
+
name: "module",
|
|
2115
2087
|
type: "address"
|
|
2116
2088
|
}
|
|
2117
2089
|
],
|
|
2118
|
-
name: "
|
|
2090
|
+
name: "registerModule",
|
|
2119
2091
|
outputs: [],
|
|
2120
2092
|
stateMutability: "nonpayable",
|
|
2121
2093
|
type: "function"
|
|
@@ -2496,6 +2468,10 @@ async function approveUsdc(context, amount, confirmations = DEFAULT_CONFIRMATION
|
|
|
2496
2468
|
var NUMBER_1E6 = 1e6;
|
|
2497
2469
|
var BIGINT_1E6 = 1000000n;
|
|
2498
2470
|
var Q96 = 79228162514264337593543950336n;
|
|
2471
|
+
var MIN_TICK = -887272;
|
|
2472
|
+
var MAX_TICK = 887272;
|
|
2473
|
+
var MIN_PRICE = 1e-6;
|
|
2474
|
+
var MAX_PRICE = 1e6;
|
|
2499
2475
|
|
|
2500
2476
|
// src/utils/conversions.ts
|
|
2501
2477
|
function priceToSqrtPriceX96(price) {
|
|
@@ -2528,6 +2504,11 @@ function priceToTick(price, roundDown) {
|
|
|
2528
2504
|
if (price <= 0) {
|
|
2529
2505
|
throw new Error("Price must be positive");
|
|
2530
2506
|
}
|
|
2507
|
+
if (price < MIN_PRICE || price > MAX_PRICE) {
|
|
2508
|
+
throw new Error(
|
|
2509
|
+
`Price ${price} is outside the representable range [${MIN_PRICE}, ${MAX_PRICE}]`
|
|
2510
|
+
);
|
|
2511
|
+
}
|
|
2531
2512
|
const logPrice = Math.log(price) / Math.log(1.0001);
|
|
2532
2513
|
return roundDown ? Math.floor(logPrice) : Math.ceil(logPrice);
|
|
2533
2514
|
}
|
|
@@ -3566,7 +3547,6 @@ function getPerpTickSpacing(perpData) {
|
|
|
3566
3547
|
var import_viem5 = require("viem");
|
|
3567
3548
|
async function createPerp(context, params) {
|
|
3568
3549
|
return withErrorHandling(async () => {
|
|
3569
|
-
const sqrtPriceX96 = priceToSqrtPriceX96(params.startingPrice);
|
|
3570
3550
|
const deployments = context.deployments();
|
|
3571
3551
|
const fees = params.fees ?? deployments.feesModule;
|
|
3572
3552
|
const marginRatios = params.marginRatios ?? deployments.marginRatiosModule;
|
|
@@ -3580,8 +3560,7 @@ async function createPerp(context, params) {
|
|
|
3580
3560
|
fees,
|
|
3581
3561
|
marginRatios,
|
|
3582
3562
|
lockupPeriod,
|
|
3583
|
-
sqrtPriceImpactLimit
|
|
3584
|
-
startingSqrtPriceX96: sqrtPriceX96
|
|
3563
|
+
sqrtPriceImpactLimit
|
|
3585
3564
|
};
|
|
3586
3565
|
const { request } = await context.publicClient.simulateContract({
|
|
3587
3566
|
address: context.deployments().perpManager,
|
|
@@ -3591,7 +3570,9 @@ async function createPerp(context, params) {
|
|
|
3591
3570
|
account: context.walletClient.account
|
|
3592
3571
|
});
|
|
3593
3572
|
const txHash = await context.walletClient.writeContract(request);
|
|
3594
|
-
const receipt = await context.publicClient.waitForTransactionReceipt({
|
|
3573
|
+
const receipt = await context.publicClient.waitForTransactionReceipt({
|
|
3574
|
+
hash: txHash
|
|
3575
|
+
});
|
|
3595
3576
|
if (receipt.status === "reverted") {
|
|
3596
3577
|
throw new Error(`Transaction reverted. Hash: ${txHash}`);
|
|
3597
3578
|
}
|
|
@@ -3658,7 +3639,9 @@ async function openTakerPosition(context, perpId, params) {
|
|
|
3658
3639
|
account: context.walletClient.account
|
|
3659
3640
|
});
|
|
3660
3641
|
const txHash = await context.walletClient.writeContract(request);
|
|
3661
|
-
const receipt = await context.publicClient.waitForTransactionReceipt({
|
|
3642
|
+
const receipt = await context.publicClient.waitForTransactionReceipt({
|
|
3643
|
+
hash: txHash
|
|
3644
|
+
});
|
|
3662
3645
|
if (receipt.status === "reverted") {
|
|
3663
3646
|
throw new Error(`Transaction reverted. Hash: ${txHash}`);
|
|
3664
3647
|
}
|
|
@@ -3683,6 +3666,82 @@ async function openTakerPosition(context, perpId, params) {
|
|
|
3683
3666
|
return new OpenPosition(context, perpId, takerPosId, params.isLong, false, txHash);
|
|
3684
3667
|
}, "openTakerPosition");
|
|
3685
3668
|
}
|
|
3669
|
+
function buildMakerContractParams(context, marginScaled, params, alignedTickLower, alignedTickUpper, maxAmt0In, maxAmt1In) {
|
|
3670
|
+
return {
|
|
3671
|
+
holder: context.walletClient.account.address,
|
|
3672
|
+
margin: marginScaled,
|
|
3673
|
+
liquidity: params.liquidity,
|
|
3674
|
+
tickLower: alignedTickLower,
|
|
3675
|
+
tickUpper: alignedTickUpper,
|
|
3676
|
+
maxAmt0In,
|
|
3677
|
+
maxAmt1In
|
|
3678
|
+
};
|
|
3679
|
+
}
|
|
3680
|
+
function calculateAlignedTicks(priceLower, priceUpper, tickSpacing) {
|
|
3681
|
+
const tickLower = priceToTick(priceLower, true);
|
|
3682
|
+
const tickUpper = priceToTick(priceUpper, false);
|
|
3683
|
+
const alignedTickLower = Math.floor(tickLower / tickSpacing) * tickSpacing;
|
|
3684
|
+
const alignedTickUpper = Math.ceil(tickUpper / tickSpacing) * tickSpacing;
|
|
3685
|
+
if (alignedTickLower < MIN_TICK) {
|
|
3686
|
+
throw new Error(
|
|
3687
|
+
`Lower tick ${alignedTickLower} is below MIN_TICK (${MIN_TICK}). Increase priceLower.`
|
|
3688
|
+
);
|
|
3689
|
+
}
|
|
3690
|
+
if (alignedTickUpper > MAX_TICK) {
|
|
3691
|
+
throw new Error(
|
|
3692
|
+
`Upper tick ${alignedTickUpper} exceeds MAX_TICK (${MAX_TICK}). Decrease priceUpper.`
|
|
3693
|
+
);
|
|
3694
|
+
}
|
|
3695
|
+
if (alignedTickLower === alignedTickUpper) {
|
|
3696
|
+
throw new Error(
|
|
3697
|
+
"Price range too narrow: lower and upper ticks are equal after alignment. Widen the range."
|
|
3698
|
+
);
|
|
3699
|
+
}
|
|
3700
|
+
return { alignedTickLower, alignedTickUpper };
|
|
3701
|
+
}
|
|
3702
|
+
function alignMakerTicks(params, tickSpacing) {
|
|
3703
|
+
return calculateAlignedTicks(params.priceLower, params.priceUpper, tickSpacing);
|
|
3704
|
+
}
|
|
3705
|
+
var DEFAULT_MAKER_SLIPPAGE_TOLERANCE = 0.01;
|
|
3706
|
+
var MAX_UINT128 = 2n ** 128n - 1n;
|
|
3707
|
+
async function quoteOpenMakerPosition(context, perpId, params) {
|
|
3708
|
+
return withErrorHandling(async () => {
|
|
3709
|
+
if (params.margin <= 0) {
|
|
3710
|
+
throw new Error("Margin must be greater than 0");
|
|
3711
|
+
}
|
|
3712
|
+
if (params.priceLower >= params.priceUpper) {
|
|
3713
|
+
throw new Error("priceLower must be less than priceUpper");
|
|
3714
|
+
}
|
|
3715
|
+
const marginScaled = scale6Decimals(params.margin);
|
|
3716
|
+
const perpData = await context.getPerpData(perpId);
|
|
3717
|
+
const { alignedTickLower, alignedTickUpper } = alignMakerTicks(params, perpData.tickSpacing);
|
|
3718
|
+
const contractParams = buildMakerContractParams(
|
|
3719
|
+
context,
|
|
3720
|
+
marginScaled,
|
|
3721
|
+
params,
|
|
3722
|
+
alignedTickLower,
|
|
3723
|
+
alignedTickUpper,
|
|
3724
|
+
MAX_UINT128,
|
|
3725
|
+
MAX_UINT128
|
|
3726
|
+
);
|
|
3727
|
+
const [unexpectedReason, perpDelta, usdDelta] = await context.publicClient.readContract({
|
|
3728
|
+
address: context.deployments().perpManager,
|
|
3729
|
+
abi: PERP_MANAGER_ABI,
|
|
3730
|
+
functionName: "quoteOpenMakerPosition",
|
|
3731
|
+
args: [perpId, contractParams]
|
|
3732
|
+
});
|
|
3733
|
+
if (unexpectedReason !== "0x") {
|
|
3734
|
+
throw new Error(`Quote failed: ${unexpectedReason}`);
|
|
3735
|
+
}
|
|
3736
|
+
return { perpDelta, usdDelta };
|
|
3737
|
+
}, "quoteOpenMakerPosition");
|
|
3738
|
+
}
|
|
3739
|
+
function applySlippage(delta, slippageTolerance) {
|
|
3740
|
+
if (delta >= 0n) return 0n;
|
|
3741
|
+
const absDelta = -delta;
|
|
3742
|
+
const slippageBps = BigInt(Math.ceil(slippageTolerance * 1e4));
|
|
3743
|
+
return absDelta + absDelta * slippageBps / 10000n;
|
|
3744
|
+
}
|
|
3686
3745
|
async function openMakerPosition(context, perpId, params) {
|
|
3687
3746
|
return withErrorHandling(async () => {
|
|
3688
3747
|
if (params.margin <= 0) {
|
|
@@ -3692,8 +3751,24 @@ async function openMakerPosition(context, perpId, params) {
|
|
|
3692
3751
|
throw new Error("priceLower must be less than priceUpper");
|
|
3693
3752
|
}
|
|
3694
3753
|
const marginScaled = scale6Decimals(params.margin);
|
|
3695
|
-
const
|
|
3696
|
-
const
|
|
3754
|
+
const perpData = await context.getPerpData(perpId);
|
|
3755
|
+
const { alignedTickLower, alignedTickUpper } = alignMakerTicks(params, perpData.tickSpacing);
|
|
3756
|
+
let maxAmt0In;
|
|
3757
|
+
let maxAmt1In;
|
|
3758
|
+
if (params.maxAmt0In === void 0 !== (params.maxAmt1In === void 0)) {
|
|
3759
|
+
throw new Error(
|
|
3760
|
+
"Both maxAmt0In and maxAmt1In must be provided together or neither. Omit both to use automatic quote-based slippage calculation."
|
|
3761
|
+
);
|
|
3762
|
+
}
|
|
3763
|
+
if (params.maxAmt0In !== void 0 && params.maxAmt1In !== void 0) {
|
|
3764
|
+
maxAmt0In = typeof params.maxAmt0In === "bigint" ? params.maxAmt0In : scale6Decimals(params.maxAmt0In);
|
|
3765
|
+
maxAmt1In = typeof params.maxAmt1In === "bigint" ? params.maxAmt1In : scale6Decimals(params.maxAmt1In);
|
|
3766
|
+
} else {
|
|
3767
|
+
const quote = await quoteOpenMakerPosition(context, perpId, params);
|
|
3768
|
+
const slippage = params.slippageTolerance ?? DEFAULT_MAKER_SLIPPAGE_TOLERANCE;
|
|
3769
|
+
maxAmt0In = applySlippage(quote.perpDelta, slippage);
|
|
3770
|
+
maxAmt1In = applySlippage(quote.usdDelta, slippage);
|
|
3771
|
+
}
|
|
3697
3772
|
const currentAllowance = await context.publicClient.readContract({
|
|
3698
3773
|
address: context.deployments().usdc,
|
|
3699
3774
|
abi: import_viem5.erc20Abi,
|
|
@@ -3704,21 +3779,15 @@ async function openMakerPosition(context, perpId, params) {
|
|
|
3704
3779
|
if (currentAllowance < marginScaled) {
|
|
3705
3780
|
await approveUsdc(context, marginScaled);
|
|
3706
3781
|
}
|
|
3707
|
-
const
|
|
3708
|
-
|
|
3709
|
-
|
|
3710
|
-
|
|
3711
|
-
|
|
3712
|
-
|
|
3713
|
-
|
|
3714
|
-
|
|
3715
|
-
|
|
3716
|
-
liquidity: params.liquidity,
|
|
3717
|
-
tickLower: alignedTickLower,
|
|
3718
|
-
tickUpper: alignedTickUpper,
|
|
3719
|
-
maxAmt0In: maxAmt0InScaled,
|
|
3720
|
-
maxAmt1In: maxAmt1InScaled
|
|
3721
|
-
};
|
|
3782
|
+
const contractParams = buildMakerContractParams(
|
|
3783
|
+
context,
|
|
3784
|
+
marginScaled,
|
|
3785
|
+
params,
|
|
3786
|
+
alignedTickLower,
|
|
3787
|
+
alignedTickUpper,
|
|
3788
|
+
maxAmt0In,
|
|
3789
|
+
maxAmt1In
|
|
3790
|
+
);
|
|
3722
3791
|
const { request } = await context.publicClient.simulateContract({
|
|
3723
3792
|
address: context.deployments().perpManager,
|
|
3724
3793
|
abi: PERP_MANAGER_ABI,
|
|
@@ -3727,7 +3796,9 @@ async function openMakerPosition(context, perpId, params) {
|
|
|
3727
3796
|
account: context.walletClient.account
|
|
3728
3797
|
});
|
|
3729
3798
|
const txHash = await context.walletClient.writeContract(request);
|
|
3730
|
-
const receipt = await context.publicClient.waitForTransactionReceipt({
|
|
3799
|
+
const receipt = await context.publicClient.waitForTransactionReceipt({
|
|
3800
|
+
hash: txHash
|
|
3801
|
+
});
|
|
3731
3802
|
if (receipt.status === "reverted") {
|
|
3732
3803
|
throw new Error(`Transaction reverted. Hash: ${txHash}`);
|
|
3733
3804
|
}
|
|
@@ -3915,6 +3986,10 @@ function getUserWalletAddress(userData) {
|
|
|
3915
3986
|
ErrorCategory,
|
|
3916
3987
|
ErrorSource,
|
|
3917
3988
|
InsufficientFundsError,
|
|
3989
|
+
MAX_PRICE,
|
|
3990
|
+
MAX_TICK,
|
|
3991
|
+
MIN_PRICE,
|
|
3992
|
+
MIN_TICK,
|
|
3918
3993
|
NUMBER_1E6,
|
|
3919
3994
|
OpenPosition,
|
|
3920
3995
|
PERP_MANAGER_ABI,
|
|
@@ -3925,6 +4000,7 @@ function getUserWalletAddress(userData) {
|
|
|
3925
4000
|
TransactionRejectedError,
|
|
3926
4001
|
ValidationError,
|
|
3927
4002
|
approveUsdc,
|
|
4003
|
+
calculateAlignedTicks,
|
|
3928
4004
|
calculateEntryPrice,
|
|
3929
4005
|
calculateLeverage,
|
|
3930
4006
|
calculateLiquidationPrice,
|
|
@@ -3959,6 +4035,7 @@ function getUserWalletAddress(userData) {
|
|
|
3959
4035
|
parseContractError,
|
|
3960
4036
|
priceToSqrtPriceX96,
|
|
3961
4037
|
priceToTick,
|
|
4038
|
+
quoteOpenMakerPosition,
|
|
3962
4039
|
scale6Decimals,
|
|
3963
4040
|
scaleFrom6Decimals,
|
|
3964
4041
|
scaleFromX96,
|