@panoptic-eng/sdk 1.0.37 → 1.0.39

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.
Files changed (74) hide show
  1. package/dist/cow/index.d.ts +804 -6
  2. package/dist/cow/index.d.ts.map +1 -0
  3. package/dist/cow/index.js +5268 -5
  4. package/dist/cow/index.js.map +1 -0
  5. package/dist/{cow-aX6of9TS.js → cow-tRUtAUZy.js} +2 -3
  6. package/dist/deployments.d.ts +3 -2
  7. package/dist/deployments.d.ts.map +1 -1
  8. package/dist/deployments.js +4 -0
  9. package/dist/deployments.js.map +1 -1
  10. package/dist/{position-C8rkkKhi.js → getTrackedPositionIds-ms3W9dYB.js} +1085 -34
  11. package/dist/greeks-GysWXct-.js +1 -2
  12. package/dist/index.d.ts +5907 -39
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +12192 -2117
  15. package/dist/index.js.map +1 -1
  16. package/dist/{irm-DnM96-X4.js → irm-SPC2KxLA.js} +6 -3
  17. package/dist/panoptic/v2/greeks/index.d.ts +277 -3
  18. package/dist/panoptic/v2/greeks/index.d.ts.map +1 -0
  19. package/dist/panoptic/v2/greeks/index.js +620 -2
  20. package/dist/panoptic/v2/greeks/index.js.map +1 -0
  21. package/dist/panoptic/v2/index.d.ts +14633 -8
  22. package/dist/panoptic/v2/index.d.ts.map +1 -0
  23. package/dist/panoptic/v2/index.js +27875 -8
  24. package/dist/panoptic/v2/index.js.map +1 -0
  25. package/dist/panoptic/v2/react-public.d.ts +14797 -17
  26. package/dist/panoptic/v2/react-public.d.ts.map +1 -1
  27. package/dist/panoptic/v2/react-public.js +29503 -89
  28. package/dist/panoptic/v2/react-public.js.map +1 -1
  29. package/dist/{router-DcoU2KmV.js → router-BTfbaRfg.js} +2 -3
  30. package/dist/{router-CS86ptMu.js → router-mLWy0MD9.js} +3 -4
  31. package/dist/test/index.d.ts +1 -1
  32. package/dist/test/index.js +2 -2
  33. package/dist/transactionFees-C_Qx07mx.js +174 -0
  34. package/dist/uniswap/index.d.ts +813 -79
  35. package/dist/uniswap/index.d.ts.map +1 -1
  36. package/dist/uniswap/index.js +5884 -13
  37. package/dist/uniswap/index.js.map +1 -1
  38. package/dist/{v2-D0dEatKb.js → v2-DG7qG6di.js} +3266 -2675
  39. package/dist/vault-transaction-fees.d.ts +63 -0
  40. package/dist/vault-transaction-fees.d.ts.map +1 -0
  41. package/dist/vault-transaction-fees.js +175 -0
  42. package/dist/vault-transaction-fees.js.map +1 -0
  43. package/dist/{writes-AupZCK4M.js → writes-Xp9Wa8GQ.js} +325 -1292
  44. package/package.json +8 -3
  45. package/dist/cow/types.d.ts +0 -3
  46. package/dist/cow/types.js +0 -0
  47. package/dist/cow-aX6of9TS.js.map +0 -1
  48. package/dist/greeks-GysWXct-.js.map +0 -1
  49. package/dist/index-CURXeKNy.d.ts +0 -9706
  50. package/dist/index-CURXeKNy.d.ts.map +0 -1
  51. package/dist/index-Cp-nCeV2.d.ts +0 -276
  52. package/dist/index-Cp-nCeV2.d.ts.map +0 -1
  53. package/dist/index-Zvu-rJpV.d.ts +0 -261
  54. package/dist/index-Zvu-rJpV.d.ts.map +0 -1
  55. package/dist/irm-CmgC1Mqi.d.ts +0 -5694
  56. package/dist/irm-CmgC1Mqi.d.ts.map +0 -1
  57. package/dist/irm-DnM96-X4.js.map +0 -1
  58. package/dist/position-BqIgubs5.d.ts +0 -199
  59. package/dist/position-BqIgubs5.d.ts.map +0 -1
  60. package/dist/position-C8rkkKhi.js.map +0 -1
  61. package/dist/quote-BxQkPBRg.d.ts +0 -718
  62. package/dist/quote-BxQkPBRg.d.ts.map +0 -1
  63. package/dist/router-CS86ptMu.js.map +0 -1
  64. package/dist/router-DcoU2KmV.js.map +0 -1
  65. package/dist/simulation-zM6-YUaw.d.ts +0 -258
  66. package/dist/simulation-zM6-YUaw.d.ts.map +0 -1
  67. package/dist/tx-CbDQ58Io.d.ts +0 -367
  68. package/dist/tx-CbDQ58Io.d.ts.map +0 -1
  69. package/dist/types-BHdnurYr.d.ts +0 -128
  70. package/dist/types-BHdnurYr.d.ts.map +0 -1
  71. package/dist/types-D_jcYss-.d.ts +0 -248
  72. package/dist/types-D_jcYss-.d.ts.map +0 -1
  73. package/dist/v2-D0dEatKb.js.map +0 -1
  74. package/dist/writes-AupZCK4M.js.map +0 -1
@@ -0,0 +1,63 @@
1
+ import { Chain, Client, Transport } from "viem";
2
+
3
+ //#region src/hypoVault/transactionFees.d.ts
4
+ declare const MIN_VAULT_PRIORITY_FEE_PER_GAS = 100000000n;
5
+ declare const MAX_VAULT_PRIORITY_FEE_PER_GAS = 3000000000n;
6
+ declare const MAX_VAULT_TRANSACTION_GAS_COST = 15000000000000000n;
7
+ type VaultTransactionFeeQuote = {
8
+ maxFeePerGas: bigint;
9
+ maxPriorityFeePerGas: bigint;
10
+ minimumMaxFeePerGas: bigint;
11
+ source: 'fee_history' | 'viem_fallback';
12
+ };
13
+ type VaultSignedTransactionFeeCaps = {
14
+ gasLimit?: bigint | null;
15
+ maxFeePerGas: bigint | null;
16
+ maxPriorityFeePerGas: bigint | null;
17
+ };
18
+ type VaultSignedTransactionFeeValidationResult = {
19
+ valid: true;
20
+ } | {
21
+ valid: false;
22
+ code: 'MissingEip1559Fees' | 'InvalidFeeCaps' | 'PriorityFeeTooLow' | 'PriorityFeeTooHigh' | 'MaxFeeTooLow' | 'GasCostTooHigh';
23
+ reason: string;
24
+ };
25
+ declare class VaultTransactionFeeEstimationError extends Error {
26
+ readonly feeHistoryError: unknown;
27
+ readonly fallbackError: unknown;
28
+ constructor({
29
+ feeHistoryError,
30
+ fallbackError
31
+ }: {
32
+ feeHistoryError: unknown;
33
+ fallbackError: unknown;
34
+ });
35
+ }
36
+ declare class VaultTransactionGasCostLimitError extends Error {
37
+ readonly gasLimit: bigint;
38
+ readonly maximumAffordableFeePerGas: bigint;
39
+ readonly minimumRequiredFeePerGas: bigint;
40
+ constructor({
41
+ gasLimit,
42
+ maximumAffordableFeePerGas,
43
+ minimumRequiredFeePerGas
44
+ }: {
45
+ gasLimit: bigint;
46
+ maximumAffordableFeePerGas: bigint;
47
+ minimumRequiredFeePerGas: bigint;
48
+ });
49
+ }
50
+ declare function applyVaultTransactionGasCostLimit(quote: VaultTransactionFeeQuote, gasLimit: bigint): VaultTransactionFeeQuote;
51
+ declare function bufferVaultTransactionGasEstimate(gasEstimate: bigint): bigint;
52
+ declare function validateVaultSignedTransactionFeeCaps(feeCaps: VaultSignedTransactionFeeCaps, quote?: VaultTransactionFeeQuote): VaultSignedTransactionFeeValidationResult;
53
+ /**
54
+ * Resolve explicit EIP-1559 fees for operator-controlled vault transactions.
55
+ *
56
+ * The normal path follows the median p90 priority fee from the last 20 blocks,
57
+ * with a 0.1 gwei floor and 3 gwei ceiling. If fee history is unavailable,
58
+ * Viem's estimate is used while preserving those bounds and its original
59
+ * base-fee allowance.
60
+ */
61
+ declare function getVaultTransactionFeeQuote<chain extends Chain | undefined>(client: Client<Transport, chain>): Promise<VaultTransactionFeeQuote>; //#endregion
62
+ export { MAX_VAULT_PRIORITY_FEE_PER_GAS, MAX_VAULT_TRANSACTION_GAS_COST, MIN_VAULT_PRIORITY_FEE_PER_GAS, VaultSignedTransactionFeeCaps, VaultSignedTransactionFeeValidationResult, VaultTransactionFeeEstimationError, VaultTransactionFeeQuote, VaultTransactionGasCostLimitError, applyVaultTransactionGasCostLimit, bufferVaultTransactionGasEstimate, getVaultTransactionFeeQuote, validateVaultSignedTransactionFeeCaps };
63
+ //# sourceMappingURL=vault-transaction-fees.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vault-transaction-fees.d.ts","names":["Chain","Client","Transport","MIN_VAULT_PRIORITY_FEE_PER_GAS","MAX_VAULT_PRIORITY_FEE_PER_GAS","MAX_VAULT_TRANSACTION_GAS_COST","VaultTransactionFeeQuote","VaultSignedTransactionFeeCaps","VaultSignedTransactionFeeValidationResult","VaultTransactionFeeEstimationError","feeHistoryError","fallbackError","Error","VaultTransactionGasCostLimitError","gasLimit","maximumAffordableFeePerGas","minimumRequiredFeePerGas","FeeHistorySnapshot","FallbackFeeEstimate","resolveFeeHistoryQuote","baseFeePerGas","reward","resolveFallbackQuote","estimatedMaxFeePerGas","estimatedPriorityFeePerGas","applyVaultTransactionGasCostLimit","bufferVaultTransactionGasEstimate","validateVaultSignedTransactionFeeCaps","resolveVaultTransactionFeeQuote","readFeeHistory","readFallbackEstimate","Promise","getVaultTransactionFeeQuote","chain","__transactionFeeTestUtils"],"sources":["../src/hypoVault/transactionFees.d.ts"],"sourcesContent":null,"mappings":";;;cACqBG,8BAAAA;cACAC,8BAAAA;AADAD,cAEAE,8BAAAA,GAF8B,kBAAA;AAC9BD,KAETE,wBAAAA,GAFuC;EAC9BD,YAAAA,EAAAA,MAAAA;EACTC,oBAAAA,EAAAA,MAAwB;EAMxBC,mBAAAA,EAAAA,MAAAA;EAKAC,MAAAA,EAAAA,aAAAA,GAAAA,eAAAA;AAOZ,CAAA;AAAuD,KAZ3CD,6BAAAA,GAY2C;EAAA,QAGrCG,CAAAA,EAAAA,MAAAA,GAAAA,IAAAA;EAAe,YAAEC,EAAAA,MAAAA,GAAAA,IAAAA;EAAa,oBAHgBC,EAAAA,MAAAA,GAAAA,IAAAA;AAAK,CAAA;AAQhDC,KAfTL,yCAAAA,GAe0C;EAAA,KAAA,EAAA,IAAA;CAAA,GAAA;EAI5B,KAAEO,EAAAA,KAAAA;EAA0B,IAAEC,EAAAA,oBAAAA,GAAAA,gBAAAA,GAAAA,mBAAAA,GAAAA,oBAAAA,GAAAA,cAAAA,GAAAA,gBAAAA;EAAwB,MAJjBJ,EAAAA,MAAAA;AAAK,CAAA;AA0B5Ca,cAlCHhB,kCAAAA,SAA2CG,KAAAA,CAkCP;EAAA,SAAA,eAAA,EAAA,OAAA;EAAA,SAAQN,aAAAA,EAAAA,OAAAA;EAAwB,WAAqBA,CAAAA;IAAAA,eAAAA;IAAAA;EAEtFqB,CAFsFrB,EAAAA;IAAwB,eAAA,EAAA,OAAA;IAC9GoB,aAAAA,EAAAA,OAAAA;EACAC,CAAAA;;AAA+CpB,cA5BlDM,iCAAAA,SAA0CD,KAAAA,CA4BQL;EAA6B,SAAUD,QAAAA,EAAAA,MAAAA;EAAwB,SAAGE,0BAAAA,EAAAA,MAAAA;EAAyC,SAAA,wBAAA,EAAA,MAAA;;;;;;;;;;;iBAF1JiB,iCAAAA,QAAyCnB,6CAA6CA;iBACtFoB,iCAAAA;iBACAC,qCAAAA,UAA+CpB,uCAAuCD,2BAA2BE;;;;;;;;;iBAajHwB,0CAA0ChC,2BAA2BC,OAAOC,WAAW+B,SAASF,QAAQzB"}
@@ -0,0 +1,175 @@
1
+ import { estimateFeesPerGas, getFeeHistory } from "viem/actions";
2
+
3
+ //#region src/hypoVault/transactionFees.ts
4
+ const MIN_VAULT_PRIORITY_FEE_PER_GAS = 100000000n;
5
+ const MAX_VAULT_PRIORITY_FEE_PER_GAS = 3000000000n;
6
+ const MAX_VAULT_TRANSACTION_GAS_COST = 15000000000000000n;
7
+ const FEE_HISTORY_BLOCK_COUNT = 20;
8
+ const FEE_HISTORY_REWARD_PERCENTILES = [90];
9
+ const BASE_FEE_BUFFER_NUMERATOR = 1125n;
10
+ const BASE_FEE_BUFFER_DENOMINATOR = 1000n;
11
+ const GAS_ESTIMATE_BUFFER_NUMERATOR = 3n;
12
+ const GAS_ESTIMATE_BUFFER_DENOMINATOR = 2n;
13
+ var VaultTransactionFeeEstimationError = class extends Error {
14
+ feeHistoryError;
15
+ fallbackError;
16
+ constructor({ feeHistoryError, fallbackError }) {
17
+ super("Unable to estimate EIP-1559 fees for vault transaction");
18
+ this.name = "VaultTransactionFeeEstimationError";
19
+ this.feeHistoryError = feeHistoryError;
20
+ this.fallbackError = fallbackError;
21
+ }
22
+ };
23
+ var VaultTransactionGasCostLimitError = class extends Error {
24
+ gasLimit;
25
+ maximumAffordableFeePerGas;
26
+ minimumRequiredFeePerGas;
27
+ constructor({ gasLimit, maximumAffordableFeePerGas, minimumRequiredFeePerGas }) {
28
+ super(`Vault transaction cannot fit the ${MAX_VAULT_TRANSACTION_GAS_COST.toString()} wei gas-cost limit: gasLimit=${gasLimit.toString()}, maximumAffordableFeePerGas=${maximumAffordableFeePerGas.toString()}, minimumRequiredFeePerGas=${minimumRequiredFeePerGas.toString()}`);
29
+ this.name = "VaultTransactionGasCostLimitError";
30
+ this.gasLimit = gasLimit;
31
+ this.maximumAffordableFeePerGas = maximumAffordableFeePerGas;
32
+ this.minimumRequiredFeePerGas = minimumRequiredFeePerGas;
33
+ }
34
+ };
35
+ function ceilMultiplyFraction(value, numerator, denominator) {
36
+ return (value * numerator + denominator - 1n) / denominator;
37
+ }
38
+ function medianBigInt(values) {
39
+ if (values.length === 0) return void 0;
40
+ const sorted = [...values].sort((left, right) => left < right ? -1 : left > right ? 1 : 0);
41
+ const upperMiddleIndex = Math.floor(sorted.length / 2);
42
+ const upperMiddle = sorted[upperMiddleIndex];
43
+ if (upperMiddle === void 0) return void 0;
44
+ if (sorted.length % 2 === 1) return upperMiddle;
45
+ const lowerMiddle = sorted[upperMiddleIndex - 1];
46
+ return lowerMiddle === void 0 ? void 0 : (lowerMiddle + upperMiddle) / 2n;
47
+ }
48
+ function clampPriorityFee(priorityFee) {
49
+ if (priorityFee < MIN_VAULT_PRIORITY_FEE_PER_GAS) return MIN_VAULT_PRIORITY_FEE_PER_GAS;
50
+ if (priorityFee > MAX_VAULT_PRIORITY_FEE_PER_GAS) return MAX_VAULT_PRIORITY_FEE_PER_GAS;
51
+ return priorityFee;
52
+ }
53
+ function resolveFeeHistoryQuote({ baseFeePerGas, reward }) {
54
+ const latestBaseFeeIndex = baseFeePerGas.length - 2;
55
+ if (latestBaseFeeIndex < 0 || reward === void 0 || reward.length === 0) return null;
56
+ const p90Rewards = reward.flatMap((blockRewards) => {
57
+ const p90 = blockRewards[0];
58
+ return p90 === void 0 ? [] : [p90];
59
+ });
60
+ const sampledPriorityFee = medianBigInt(p90Rewards);
61
+ if (sampledPriorityFee === void 0) return null;
62
+ const maxPriorityFeePerGas = clampPriorityFee(sampledPriorityFee);
63
+ const bufferedBaseFee = ceilMultiplyFraction(baseFeePerGas[latestBaseFeeIndex], BASE_FEE_BUFFER_NUMERATOR, BASE_FEE_BUFFER_DENOMINATOR);
64
+ return {
65
+ maxFeePerGas: bufferedBaseFee + maxPriorityFeePerGas,
66
+ maxPriorityFeePerGas,
67
+ minimumMaxFeePerGas: bufferedBaseFee + MIN_VAULT_PRIORITY_FEE_PER_GAS,
68
+ source: "fee_history"
69
+ };
70
+ }
71
+ function resolveFallbackQuote({ maxFeePerGas: estimatedMaxFeePerGas, maxPriorityFeePerGas: estimatedPriorityFeePerGas }) {
72
+ const maxPriorityFeePerGas = clampPriorityFee(estimatedPriorityFeePerGas);
73
+ const estimatedBaseFeeAllowance = estimatedMaxFeePerGas > estimatedPriorityFeePerGas ? estimatedMaxFeePerGas - estimatedPriorityFeePerGas : 0n;
74
+ return {
75
+ maxFeePerGas: estimatedBaseFeeAllowance + maxPriorityFeePerGas,
76
+ maxPriorityFeePerGas,
77
+ minimumMaxFeePerGas: estimatedBaseFeeAllowance + MIN_VAULT_PRIORITY_FEE_PER_GAS,
78
+ source: "viem_fallback"
79
+ };
80
+ }
81
+ function applyVaultTransactionGasCostLimit(quote, gasLimit) {
82
+ if (gasLimit <= 0n) throw new Error(`Vault transaction gas limit must be positive, received ${gasLimit.toString()}`);
83
+ const maximumAffordableFeePerGas = MAX_VAULT_TRANSACTION_GAS_COST / gasLimit;
84
+ if (maximumAffordableFeePerGas < quote.minimumMaxFeePerGas) throw new VaultTransactionGasCostLimitError({
85
+ gasLimit,
86
+ maximumAffordableFeePerGas,
87
+ minimumRequiredFeePerGas: quote.minimumMaxFeePerGas
88
+ });
89
+ if (quote.maxFeePerGas <= maximumAffordableFeePerGas) return quote;
90
+ const bufferedBaseFee = quote.minimumMaxFeePerGas - MIN_VAULT_PRIORITY_FEE_PER_GAS;
91
+ const affordablePriorityFee = maximumAffordableFeePerGas - bufferedBaseFee;
92
+ return {
93
+ ...quote,
94
+ maxFeePerGas: maximumAffordableFeePerGas,
95
+ maxPriorityFeePerGas: quote.maxPriorityFeePerGas < affordablePriorityFee ? quote.maxPriorityFeePerGas : affordablePriorityFee
96
+ };
97
+ }
98
+ function bufferVaultTransactionGasEstimate(gasEstimate) {
99
+ if (gasEstimate <= 0n) throw new Error(`Vault transaction gas estimate must be positive, received ${gasEstimate.toString()}`);
100
+ return ceilMultiplyFraction(gasEstimate, GAS_ESTIMATE_BUFFER_NUMERATOR, GAS_ESTIMATE_BUFFER_DENOMINATOR);
101
+ }
102
+ function validateVaultSignedTransactionFeeCaps(feeCaps, quote) {
103
+ const { gasLimit, maxFeePerGas, maxPriorityFeePerGas } = feeCaps;
104
+ if (maxFeePerGas === null || maxPriorityFeePerGas === null) return {
105
+ valid: false,
106
+ code: "MissingEip1559Fees",
107
+ reason: "Signed transaction must include EIP-1559 max fee and priority fee caps."
108
+ };
109
+ if (maxPriorityFeePerGas > maxFeePerGas) return {
110
+ valid: false,
111
+ code: "InvalidFeeCaps",
112
+ reason: `Signed transaction maxPriorityFeePerGas (${maxPriorityFeePerGas.toString()} wei) cannot exceed maxFeePerGas (${maxFeePerGas.toString()} wei).`
113
+ };
114
+ if (maxPriorityFeePerGas < MIN_VAULT_PRIORITY_FEE_PER_GAS) return {
115
+ valid: false,
116
+ code: "PriorityFeeTooLow",
117
+ reason: `Signed transaction maxPriorityFeePerGas (${maxPriorityFeePerGas.toString()} wei) must be at least ${MIN_VAULT_PRIORITY_FEE_PER_GAS.toString()} wei (0.1 gwei).`
118
+ };
119
+ if (maxPriorityFeePerGas > MAX_VAULT_PRIORITY_FEE_PER_GAS) return {
120
+ valid: false,
121
+ code: "PriorityFeeTooHigh",
122
+ reason: `Signed transaction maxPriorityFeePerGas (${maxPriorityFeePerGas.toString()} wei) must not exceed ${MAX_VAULT_PRIORITY_FEE_PER_GAS.toString()} wei (3 gwei).`
123
+ };
124
+ if (gasLimit !== void 0 && gasLimit !== null && gasLimit * maxFeePerGas > MAX_VAULT_TRANSACTION_GAS_COST) return {
125
+ valid: false,
126
+ code: "GasCostTooHigh",
127
+ reason: `Signed transaction maximum gas cost (${(gasLimit * maxFeePerGas).toString()} wei) must not exceed ${MAX_VAULT_TRANSACTION_GAS_COST.toString()} wei (0.015 ETH).`
128
+ };
129
+ if (quote !== void 0 && maxFeePerGas < quote.minimumMaxFeePerGas) return {
130
+ valid: false,
131
+ code: "MaxFeeTooLow",
132
+ reason: `Signed transaction maxFeePerGas (${maxFeePerGas.toString()} wei) must be at least ${quote.minimumMaxFeePerGas.toString()} wei for the buffered next-block base fee and 0.1 gwei minimum priority fee.`
133
+ };
134
+ return { valid: true };
135
+ }
136
+ async function resolveVaultTransactionFeeQuote({ readFeeHistory, readFallbackEstimate }) {
137
+ let feeHistoryError;
138
+ try {
139
+ const quote = resolveFeeHistoryQuote(await readFeeHistory());
140
+ if (quote !== null) return quote;
141
+ feeHistoryError = new Error("eth_feeHistory returned incomplete base fee or reward data");
142
+ } catch (error) {
143
+ feeHistoryError = error;
144
+ }
145
+ try {
146
+ return resolveFallbackQuote(await readFallbackEstimate());
147
+ } catch (fallbackError) {
148
+ throw new VaultTransactionFeeEstimationError({
149
+ feeHistoryError,
150
+ fallbackError
151
+ });
152
+ }
153
+ }
154
+ /**
155
+ * Resolve explicit EIP-1559 fees for operator-controlled vault transactions.
156
+ *
157
+ * The normal path follows the median p90 priority fee from the last 20 blocks,
158
+ * with a 0.1 gwei floor and 3 gwei ceiling. If fee history is unavailable,
159
+ * Viem's estimate is used while preserving those bounds and its original
160
+ * base-fee allowance.
161
+ */
162
+ async function getVaultTransactionFeeQuote(client) {
163
+ return resolveVaultTransactionFeeQuote({
164
+ readFeeHistory: () => getFeeHistory(client, {
165
+ blockCount: FEE_HISTORY_BLOCK_COUNT,
166
+ blockTag: "latest",
167
+ rewardPercentiles: FEE_HISTORY_REWARD_PERCENTILES
168
+ }),
169
+ readFallbackEstimate: () => estimateFeesPerGas(client)
170
+ });
171
+ }
172
+
173
+ //#endregion
174
+ export { MAX_VAULT_PRIORITY_FEE_PER_GAS, MAX_VAULT_TRANSACTION_GAS_COST, MIN_VAULT_PRIORITY_FEE_PER_GAS, VaultTransactionFeeEstimationError, VaultTransactionGasCostLimitError, applyVaultTransactionGasCostLimit, bufferVaultTransactionGasEstimate, getVaultTransactionFeeQuote, validateVaultSignedTransactionFeeCaps };
175
+ //# sourceMappingURL=vault-transaction-fees.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vault-transaction-fees.js","names":["value: bigint","numerator: bigint","denominator: bigint","values: readonly bigint[]","priorityFee: bigint","quote: VaultTransactionFeeQuote","gasLimit: bigint","gasEstimate: bigint","feeCaps: VaultSignedTransactionFeeCaps","quote?: VaultTransactionFeeQuote","feeHistoryError: unknown","client: Client<Transport, chain>"],"sources":["../src/hypoVault/transactionFees.ts"],"sourcesContent":["import type { Chain, Client, Transport } from 'viem'\nimport { estimateFeesPerGas, getFeeHistory } from 'viem/actions'\n\nexport const MIN_VAULT_PRIORITY_FEE_PER_GAS = 100_000_000n // 0.1 gwei\nexport const MAX_VAULT_PRIORITY_FEE_PER_GAS = 3_000_000_000n // 3 gwei\nexport const MAX_VAULT_TRANSACTION_GAS_COST = 15_000_000_000_000_000n // 0.015 ETH\n\nconst FEE_HISTORY_BLOCK_COUNT = 20\nconst FEE_HISTORY_REWARD_PERCENTILES = [90]\nconst BASE_FEE_BUFFER_NUMERATOR = 1_125n\nconst BASE_FEE_BUFFER_DENOMINATOR = 1_000n\nconst GAS_ESTIMATE_BUFFER_NUMERATOR = 3n\nconst GAS_ESTIMATE_BUFFER_DENOMINATOR = 2n\n\nexport type VaultTransactionFeeQuote = {\n maxFeePerGas: bigint\n maxPriorityFeePerGas: bigint\n minimumMaxFeePerGas: bigint\n source: 'fee_history' | 'viem_fallback'\n}\n\nexport type VaultSignedTransactionFeeCaps = {\n gasLimit?: bigint | null\n maxFeePerGas: bigint | null\n maxPriorityFeePerGas: bigint | null\n}\n\nexport type VaultSignedTransactionFeeValidationResult =\n | { valid: true }\n | {\n valid: false\n code:\n | 'MissingEip1559Fees'\n | 'InvalidFeeCaps'\n | 'PriorityFeeTooLow'\n | 'PriorityFeeTooHigh'\n | 'MaxFeeTooLow'\n | 'GasCostTooHigh'\n reason: string\n }\n\nexport class VaultTransactionFeeEstimationError extends Error {\n readonly feeHistoryError: unknown\n readonly fallbackError: unknown\n\n constructor({\n feeHistoryError,\n fallbackError,\n }: {\n feeHistoryError: unknown\n fallbackError: unknown\n }) {\n super('Unable to estimate EIP-1559 fees for vault transaction')\n this.name = 'VaultTransactionFeeEstimationError'\n this.feeHistoryError = feeHistoryError\n this.fallbackError = fallbackError\n }\n}\n\nexport class VaultTransactionGasCostLimitError extends Error {\n readonly gasLimit: bigint\n readonly maximumAffordableFeePerGas: bigint\n readonly minimumRequiredFeePerGas: bigint\n\n constructor({\n gasLimit,\n maximumAffordableFeePerGas,\n minimumRequiredFeePerGas,\n }: {\n gasLimit: bigint\n maximumAffordableFeePerGas: bigint\n minimumRequiredFeePerGas: bigint\n }) {\n super(\n `Vault transaction cannot fit the ${MAX_VAULT_TRANSACTION_GAS_COST.toString()} wei gas-cost limit: ` +\n `gasLimit=${gasLimit.toString()}, maximumAffordableFeePerGas=${maximumAffordableFeePerGas.toString()}, ` +\n `minimumRequiredFeePerGas=${minimumRequiredFeePerGas.toString()}`,\n )\n this.name = 'VaultTransactionGasCostLimitError'\n this.gasLimit = gasLimit\n this.maximumAffordableFeePerGas = maximumAffordableFeePerGas\n this.minimumRequiredFeePerGas = minimumRequiredFeePerGas\n }\n}\n\ntype FeeHistorySnapshot = {\n baseFeePerGas: readonly bigint[]\n reward?: readonly (readonly bigint[])[] | undefined\n}\n\ntype FallbackFeeEstimate = {\n maxFeePerGas: bigint\n maxPriorityFeePerGas: bigint\n}\n\nfunction ceilMultiplyFraction(value: bigint, numerator: bigint, denominator: bigint): bigint {\n return (value * numerator + denominator - 1n) / denominator\n}\n\nfunction medianBigInt(values: readonly bigint[]): bigint | undefined {\n if (values.length === 0) return undefined\n const sorted = [...values].sort((left, right) => (left < right ? -1 : left > right ? 1 : 0))\n const upperMiddleIndex = Math.floor(sorted.length / 2)\n const upperMiddle = sorted[upperMiddleIndex]\n if (upperMiddle === undefined) return undefined\n if (sorted.length % 2 === 1) return upperMiddle\n const lowerMiddle = sorted[upperMiddleIndex - 1]\n return lowerMiddle === undefined ? undefined : (lowerMiddle + upperMiddle) / 2n\n}\n\nfunction clampPriorityFee(priorityFee: bigint): bigint {\n if (priorityFee < MIN_VAULT_PRIORITY_FEE_PER_GAS) return MIN_VAULT_PRIORITY_FEE_PER_GAS\n if (priorityFee > MAX_VAULT_PRIORITY_FEE_PER_GAS) return MAX_VAULT_PRIORITY_FEE_PER_GAS\n return priorityFee\n}\n\nfunction resolveFeeHistoryQuote({\n baseFeePerGas,\n reward,\n}: {\n baseFeePerGas: readonly bigint[]\n reward?: readonly (readonly bigint[])[] | undefined\n}): VaultTransactionFeeQuote | null {\n // feeHistory includes one additional base fee for the next block. The\n // second-to-last entry is therefore the latest mined block's base fee.\n const latestBaseFeeIndex = baseFeePerGas.length - 2\n if (latestBaseFeeIndex < 0 || reward === undefined || reward.length === 0) return null\n\n const p90Rewards = reward.flatMap((blockRewards) => {\n const p90 = blockRewards[0]\n return p90 === undefined ? [] : [p90]\n })\n const sampledPriorityFee = medianBigInt(p90Rewards)\n if (sampledPriorityFee === undefined) return null\n\n const maxPriorityFeePerGas = clampPriorityFee(sampledPriorityFee)\n const bufferedBaseFee = ceilMultiplyFraction(\n baseFeePerGas[latestBaseFeeIndex],\n BASE_FEE_BUFFER_NUMERATOR,\n BASE_FEE_BUFFER_DENOMINATOR,\n )\n\n return {\n maxFeePerGas: bufferedBaseFee + maxPriorityFeePerGas,\n maxPriorityFeePerGas,\n minimumMaxFeePerGas: bufferedBaseFee + MIN_VAULT_PRIORITY_FEE_PER_GAS,\n source: 'fee_history',\n }\n}\n\nfunction resolveFallbackQuote({\n maxFeePerGas: estimatedMaxFeePerGas,\n maxPriorityFeePerGas: estimatedPriorityFeePerGas,\n}: {\n maxFeePerGas: bigint\n maxPriorityFeePerGas: bigint\n}): VaultTransactionFeeQuote {\n const maxPriorityFeePerGas = clampPriorityFee(estimatedPriorityFeePerGas)\n const estimatedBaseFeeAllowance =\n estimatedMaxFeePerGas > estimatedPriorityFeePerGas\n ? estimatedMaxFeePerGas - estimatedPriorityFeePerGas\n : 0n\n\n return {\n maxFeePerGas: estimatedBaseFeeAllowance + maxPriorityFeePerGas,\n maxPriorityFeePerGas,\n minimumMaxFeePerGas: estimatedBaseFeeAllowance + MIN_VAULT_PRIORITY_FEE_PER_GAS,\n source: 'viem_fallback',\n }\n}\n\nexport function applyVaultTransactionGasCostLimit(\n quote: VaultTransactionFeeQuote,\n gasLimit: bigint,\n): VaultTransactionFeeQuote {\n if (gasLimit <= 0n) {\n throw new Error(`Vault transaction gas limit must be positive, received ${gasLimit.toString()}`)\n }\n\n const maximumAffordableFeePerGas = MAX_VAULT_TRANSACTION_GAS_COST / gasLimit\n if (maximumAffordableFeePerGas < quote.minimumMaxFeePerGas) {\n throw new VaultTransactionGasCostLimitError({\n gasLimit,\n maximumAffordableFeePerGas,\n minimumRequiredFeePerGas: quote.minimumMaxFeePerGas,\n })\n }\n\n if (quote.maxFeePerGas <= maximumAffordableFeePerGas) return quote\n\n const bufferedBaseFee = quote.minimumMaxFeePerGas - MIN_VAULT_PRIORITY_FEE_PER_GAS\n const affordablePriorityFee = maximumAffordableFeePerGas - bufferedBaseFee\n return {\n ...quote,\n maxFeePerGas: maximumAffordableFeePerGas,\n maxPriorityFeePerGas:\n quote.maxPriorityFeePerGas < affordablePriorityFee\n ? quote.maxPriorityFeePerGas\n : affordablePriorityFee,\n }\n}\n\nexport function bufferVaultTransactionGasEstimate(gasEstimate: bigint): bigint {\n if (gasEstimate <= 0n) {\n throw new Error(\n `Vault transaction gas estimate must be positive, received ${gasEstimate.toString()}`,\n )\n }\n return ceilMultiplyFraction(\n gasEstimate,\n GAS_ESTIMATE_BUFFER_NUMERATOR,\n GAS_ESTIMATE_BUFFER_DENOMINATOR,\n )\n}\n\nexport function validateVaultSignedTransactionFeeCaps(\n feeCaps: VaultSignedTransactionFeeCaps,\n quote?: VaultTransactionFeeQuote,\n): VaultSignedTransactionFeeValidationResult {\n const { gasLimit, maxFeePerGas, maxPriorityFeePerGas } = feeCaps\n if (maxFeePerGas === null || maxPriorityFeePerGas === null) {\n return {\n valid: false,\n code: 'MissingEip1559Fees',\n reason: 'Signed transaction must include EIP-1559 max fee and priority fee caps.',\n }\n }\n if (maxPriorityFeePerGas > maxFeePerGas) {\n return {\n valid: false,\n code: 'InvalidFeeCaps',\n reason:\n `Signed transaction maxPriorityFeePerGas (${maxPriorityFeePerGas.toString()} wei) ` +\n `cannot exceed maxFeePerGas (${maxFeePerGas.toString()} wei).`,\n }\n }\n if (maxPriorityFeePerGas < MIN_VAULT_PRIORITY_FEE_PER_GAS) {\n return {\n valid: false,\n code: 'PriorityFeeTooLow',\n reason:\n `Signed transaction maxPriorityFeePerGas (${maxPriorityFeePerGas.toString()} wei) ` +\n `must be at least ${MIN_VAULT_PRIORITY_FEE_PER_GAS.toString()} wei (0.1 gwei).`,\n }\n }\n if (maxPriorityFeePerGas > MAX_VAULT_PRIORITY_FEE_PER_GAS) {\n return {\n valid: false,\n code: 'PriorityFeeTooHigh',\n reason:\n `Signed transaction maxPriorityFeePerGas (${maxPriorityFeePerGas.toString()} wei) ` +\n `must not exceed ${MAX_VAULT_PRIORITY_FEE_PER_GAS.toString()} wei (3 gwei).`,\n }\n }\n if (\n gasLimit !== undefined &&\n gasLimit !== null &&\n gasLimit * maxFeePerGas > MAX_VAULT_TRANSACTION_GAS_COST\n ) {\n return {\n valid: false,\n code: 'GasCostTooHigh',\n reason:\n `Signed transaction maximum gas cost (${(gasLimit * maxFeePerGas).toString()} wei) ` +\n `must not exceed ${MAX_VAULT_TRANSACTION_GAS_COST.toString()} wei (0.015 ETH).`,\n }\n }\n if (quote !== undefined && maxFeePerGas < quote.minimumMaxFeePerGas) {\n return {\n valid: false,\n code: 'MaxFeeTooLow',\n reason:\n `Signed transaction maxFeePerGas (${maxFeePerGas.toString()} wei) must be at least ` +\n `${quote.minimumMaxFeePerGas.toString()} wei for the buffered next-block base fee ` +\n 'and 0.1 gwei minimum priority fee.',\n }\n }\n return { valid: true }\n}\n\nasync function resolveVaultTransactionFeeQuote({\n readFeeHistory,\n readFallbackEstimate,\n}: {\n readFeeHistory: () => Promise<FeeHistorySnapshot>\n readFallbackEstimate: () => Promise<FallbackFeeEstimate>\n}): Promise<VaultTransactionFeeQuote> {\n let feeHistoryError: unknown\n try {\n const quote = resolveFeeHistoryQuote(await readFeeHistory())\n if (quote !== null) return quote\n feeHistoryError = new Error('eth_feeHistory returned incomplete base fee or reward data')\n } catch (error) {\n feeHistoryError = error\n }\n\n try {\n return resolveFallbackQuote(await readFallbackEstimate())\n } catch (fallbackError) {\n throw new VaultTransactionFeeEstimationError({ feeHistoryError, fallbackError })\n }\n}\n\n/**\n * Resolve explicit EIP-1559 fees for operator-controlled vault transactions.\n *\n * The normal path follows the median p90 priority fee from the last 20 blocks,\n * with a 0.1 gwei floor and 3 gwei ceiling. If fee history is unavailable,\n * Viem's estimate is used while preserving those bounds and its original\n * base-fee allowance.\n */\nexport async function getVaultTransactionFeeQuote<chain extends Chain | undefined>(\n client: Client<Transport, chain>,\n): Promise<VaultTransactionFeeQuote> {\n return resolveVaultTransactionFeeQuote({\n readFeeHistory: () =>\n getFeeHistory(client, {\n blockCount: FEE_HISTORY_BLOCK_COUNT,\n blockTag: 'latest',\n rewardPercentiles: FEE_HISTORY_REWARD_PERCENTILES,\n }),\n readFallbackEstimate: () => estimateFeesPerGas(client),\n })\n}\n\nexport const __transactionFeeTestUtils = {\n resolveFallbackQuote,\n resolveFeeHistoryQuote,\n resolveVaultTransactionFeeQuote,\n}\n"],"mappings":";;;AAGA,MAAa,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