@gvnrdao/dh-mcp 0.5.7 → 0.5.8

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/boundary.js CHANGED
@@ -17342,11 +17342,11 @@ ${auth.clientId}`;
17342
17342
  );
17343
17343
  const prices = await Promise.race([fetchAll, timeoutPromise]);
17344
17344
  const sorted = [...prices].sort((a, b) => a - b);
17345
- let median;
17345
+ let median2;
17346
17346
  if (this.devSingleProvider) {
17347
- median = sorted[0];
17347
+ median2 = sorted[0];
17348
17348
  } else {
17349
- median = sorted[1];
17349
+ median2 = sorted[1];
17350
17350
  const spread3 = (sorted[2] - sorted[0]) / sorted[0];
17351
17351
  if (spread3 > PRICE_ORACLE_TOLERANCE) {
17352
17352
  const names = this.sources.map((s) => s.name).join(", ");
@@ -17356,10 +17356,10 @@ ${auth.clientId}`;
17356
17356
  );
17357
17357
  }
17358
17358
  }
17359
- const priceCents = Math.round(median * 100);
17359
+ const priceCents = Math.round(median2 * 100);
17360
17360
  const priceWith8Decimals = BigInt(priceCents) * 1000000n;
17361
17361
  const totalElapsed = Date.now() - startTime;
17362
- console.log(`[Price Oracle] Median price: $${median.toLocaleString()} \u2014 total time: ${totalElapsed}ms`);
17362
+ console.log(`[Price Oracle] Median price: $${median2.toLocaleString()} \u2014 total time: ${totalElapsed}ms`);
17363
17363
  console.log(`[Price Oracle] Price with 8 decimals: ${priceWith8Decimals}`);
17364
17364
  return priceWith8Decimals;
17365
17365
  }
@@ -55394,11 +55394,11 @@ ${auth.clientId}`;
55394
55394
  );
55395
55395
  const prices = await Promise.race([fetchAll, timeoutPromise]);
55396
55396
  const sorted = [...prices].sort((a, b) => a - b);
55397
- let median;
55397
+ let median2;
55398
55398
  if (this.devSingleProvider) {
55399
- median = sorted[0];
55399
+ median2 = sorted[0];
55400
55400
  } else {
55401
- median = sorted[1];
55401
+ median2 = sorted[1];
55402
55402
  const spread3 = (sorted[2] - sorted[0]) / sorted[0];
55403
55403
  if (spread3 > PRICE_ORACLE_TOLERANCE) {
55404
55404
  const names = this.sources.map((s) => s.name).join(", ");
@@ -55408,10 +55408,10 @@ ${auth.clientId}`;
55408
55408
  );
55409
55409
  }
55410
55410
  }
55411
- const priceCents = Math.round(median * 100);
55411
+ const priceCents = Math.round(median2 * 100);
55412
55412
  const priceWith8Decimals = BigInt(priceCents) * 1000000n;
55413
55413
  const totalElapsed = Date.now() - startTime;
55414
- console.log(`[Price Oracle] Median price: $${median.toLocaleString()} \u2014 total time: ${totalElapsed}ms`);
55414
+ console.log(`[Price Oracle] Median price: $${median2.toLocaleString()} \u2014 total time: ${totalElapsed}ms`);
55415
55415
  console.log(`[Price Oracle] Price with 8 decimals: ${priceWith8Decimals}`);
55416
55416
  return priceWith8Decimals;
55417
55417
  }
@@ -57176,26 +57176,83 @@ var MAKE_PAYMENT_GAS_CEILING = BigInt(1e6);
57176
57176
  var EXTEND_POSITION_GAS_CEILING = BigInt(1e6);
57177
57177
  var LIQUIDATION_COMMIT_GAS_CEILING = BigInt(1e6);
57178
57178
  var LIQUIDATION_REVEAL_GAS_CEILING = BigInt(2e6);
57179
- async function resolveEip1559FeeFields(provider) {
57180
- const feeData = await provider.getFeeData();
57181
- let maxFeePerGas = feeData.maxFeePerGas ?? void 0;
57182
- let maxPriorityFeePerGas = feeData.maxPriorityFeePerGas ?? void 0;
57183
- if (!maxFeePerGas || !maxPriorityFeePerGas) {
57184
- const gasPrice = (await provider.getFeeData()).gasPrice ?? (0, import_ethers14.parseUnits)("1", "gwei");
57185
- maxFeePerGas = maxFeePerGas || gasPrice * 2n;
57186
- maxPriorityFeePerGas = maxPriorityFeePerGas || (0, import_ethers14.parseUnits)("1.5", "gwei");
57187
- }
57188
- if (maxFeePerGas < maxPriorityFeePerGas) {
57189
- maxFeePerGas = maxPriorityFeePerGas;
57179
+ var PRIORITY_FEE_FLOOR_WEI = (0, import_ethers14.parseUnits)("0.1", "gwei");
57180
+ var PRIORITY_FEE_CAP_WEI = (0, import_ethers14.parseUnits)("3", "gwei");
57181
+ var FEE_HISTORY_BLOCK_COUNT = 20;
57182
+ var FEE_HISTORY_REWARD_PERCENTILE = 75;
57183
+ function isJsonRpcSendable(provider) {
57184
+ return typeof provider.send === "function";
57185
+ }
57186
+ function parseFeeHistory(raw) {
57187
+ if (typeof raw !== "object" || raw === null) {
57188
+ throw new Error(`eth_feeHistory returned a non-object result: ${JSON.stringify(raw)}`);
57190
57189
  }
57190
+ const { baseFeePerGas, reward } = raw;
57191
+ if (!Array.isArray(baseFeePerGas) || baseFeePerGas.length === 0) {
57192
+ throw new Error(
57193
+ `eth_feeHistory returned no baseFeePerGas \u2014 the RPC does not support EIP-1559 fee history: ${JSON.stringify(raw)}`
57194
+ );
57195
+ }
57196
+ if (!Array.isArray(reward) || reward.length === 0) {
57197
+ throw new Error(
57198
+ `eth_feeHistory returned no reward percentiles \u2014 the RPC ignored the rewardPercentiles argument: ${JSON.stringify(raw)}`
57199
+ );
57200
+ }
57201
+ const nextBaseFee = (0, import_ethers14.getBigInt)(baseFeePerGas[baseFeePerGas.length - 1], "baseFeePerGas");
57202
+ const tips = reward.map((perBlock, i) => {
57203
+ if (!Array.isArray(perBlock) || perBlock.length === 0) {
57204
+ throw new Error(`eth_feeHistory reward row ${i} is empty: ${JSON.stringify(raw)}`);
57205
+ }
57206
+ return (0, import_ethers14.getBigInt)(perBlock[0], `reward[${i}]`);
57207
+ });
57208
+ return { nextBaseFee, tips };
57209
+ }
57210
+ function median(values) {
57211
+ const sorted = [...values].sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
57212
+ const mid = sorted.length >> 1;
57213
+ const hi = sorted[mid];
57214
+ if (hi === void 0) {
57215
+ throw new Error("median: no values");
57216
+ }
57217
+ if (sorted.length % 2 === 1)
57218
+ return hi;
57219
+ const lo = sorted[mid - 1];
57220
+ if (lo === void 0) {
57221
+ throw new Error("median: no values");
57222
+ }
57223
+ return (lo + hi) / 2n;
57224
+ }
57225
+ function clamp(value, floor, cap) {
57226
+ if (value < floor)
57227
+ return floor;
57228
+ if (value > cap)
57229
+ return cap;
57230
+ return value;
57231
+ }
57232
+ async function resolveEip1559FeeFields(feeProvider) {
57233
+ if (!isJsonRpcSendable(feeProvider)) {
57234
+ throw new Error(
57235
+ "resolveEip1559FeeFields: feeProvider must be a JSON-RPC provider exposing send() (the SDK read provider), not a wallet BrowserProvider \u2014 eth_feeHistory is required"
57236
+ );
57237
+ }
57238
+ const history = parseFeeHistory(
57239
+ await feeProvider.send("eth_feeHistory", [
57240
+ (0, import_ethers14.toQuantity)(FEE_HISTORY_BLOCK_COUNT),
57241
+ "latest",
57242
+ [FEE_HISTORY_REWARD_PERCENTILE]
57243
+ ])
57244
+ );
57245
+ const marketTip = median(history.tips);
57246
+ const maxPriorityFeePerGas = clamp(marketTip, PRIORITY_FEE_FLOOR_WEI, PRIORITY_FEE_CAP_WEI);
57247
+ const maxFeePerGas = history.nextBaseFee * 2n + maxPriorityFeePerGas;
57191
57248
  return { maxFeePerGas, maxPriorityFeePerGas };
57192
57249
  }
57193
57250
  async function sendEip1559Transaction(params) {
57194
- const { signer, to, data, gasLimit } = params;
57251
+ const { signer, feeProvider, to, data, gasLimit } = params;
57195
57252
  if (!signer.provider) {
57196
57253
  throw new Error("Signer must have a provider attached");
57197
57254
  }
57198
- const fees = await resolveEip1559FeeFields(signer.provider);
57255
+ const fees = await resolveEip1559FeeFields(feeProvider);
57199
57256
  return signer.sendTransaction({
57200
57257
  to,
57201
57258
  data,
@@ -71999,6 +72056,7 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
71999
72056
  }
72000
72057
  const tx2 = await sendEip1559Transaction({
72001
72058
  signer,
72059
+ feeProvider: this.getProviderOrThrow(),
72002
72060
  to: positionManagerAddress,
72003
72061
  data: mintCalldata,
72004
72062
  gasLimit: MINT_UCD_GAS_CEILING
@@ -73225,6 +73283,7 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
73225
73283
  }
73226
73284
  const tx2 = await sendEip1559Transaction({
73227
73285
  signer: extendSigner,
73286
+ feeProvider: this.getProviderOrThrow(),
73228
73287
  to: extendTo,
73229
73288
  data: extendCalldata,
73230
73289
  gasLimit: EXTEND_POSITION_GAS_CEILING
@@ -74159,6 +74218,7 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
74159
74218
  }
74160
74219
  const tx2 = await sendEip1559Transaction({
74161
74220
  signer: paymentSigner,
74221
+ feeProvider: this.getProviderOrThrow(),
74162
74222
  to: paymentContractAddress,
74163
74223
  data: paymentCalldata,
74164
74224
  gasLimit: MAKE_PAYMENT_GAS_CEILING
@@ -74924,6 +74984,7 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
74924
74984
  });
74925
74985
  tx2 = await sendEip1559Transaction({
74926
74986
  signer: withdrawSigner,
74987
+ feeProvider: this.getProviderOrThrow(),
74927
74988
  to: positionManagerAddress,
74928
74989
  data: withdrawCalldata,
74929
74990
  gasLimit: WITHDRAW_BTC_GAS_CEILING
@@ -17461,11 +17461,11 @@ ${auth.clientId}`;
17461
17461
  );
17462
17462
  const prices = await Promise.race([fetchAll, timeoutPromise]);
17463
17463
  const sorted = [...prices].sort((a, b) => a - b);
17464
- let median;
17464
+ let median2;
17465
17465
  if (this.devSingleProvider) {
17466
- median = sorted[0];
17466
+ median2 = sorted[0];
17467
17467
  } else {
17468
- median = sorted[1];
17468
+ median2 = sorted[1];
17469
17469
  const spread3 = (sorted[2] - sorted[0]) / sorted[0];
17470
17470
  if (spread3 > PRICE_ORACLE_TOLERANCE) {
17471
17471
  const names = this.sources.map((s) => s.name).join(", ");
@@ -17475,10 +17475,10 @@ ${auth.clientId}`;
17475
17475
  );
17476
17476
  }
17477
17477
  }
17478
- const priceCents = Math.round(median * 100);
17478
+ const priceCents = Math.round(median2 * 100);
17479
17479
  const priceWith8Decimals = BigInt(priceCents) * 1000000n;
17480
17480
  const totalElapsed = Date.now() - startTime;
17481
- console.log(`[Price Oracle] Median price: $${median.toLocaleString()} \u2014 total time: ${totalElapsed}ms`);
17481
+ console.log(`[Price Oracle] Median price: $${median2.toLocaleString()} \u2014 total time: ${totalElapsed}ms`);
17482
17482
  console.log(`[Price Oracle] Price with 8 decimals: ${priceWith8Decimals}`);
17483
17483
  return priceWith8Decimals;
17484
17484
  }
@@ -56043,11 +56043,11 @@ ${auth.clientId}`;
56043
56043
  );
56044
56044
  const prices = await Promise.race([fetchAll, timeoutPromise]);
56045
56045
  const sorted = [...prices].sort((a, b) => a - b);
56046
- let median;
56046
+ let median2;
56047
56047
  if (this.devSingleProvider) {
56048
- median = sorted[0];
56048
+ median2 = sorted[0];
56049
56049
  } else {
56050
- median = sorted[1];
56050
+ median2 = sorted[1];
56051
56051
  const spread3 = (sorted[2] - sorted[0]) / sorted[0];
56052
56052
  if (spread3 > PRICE_ORACLE_TOLERANCE) {
56053
56053
  const names = this.sources.map((s) => s.name).join(", ");
@@ -56057,10 +56057,10 @@ ${auth.clientId}`;
56057
56057
  );
56058
56058
  }
56059
56059
  }
56060
- const priceCents = Math.round(median * 100);
56060
+ const priceCents = Math.round(median2 * 100);
56061
56061
  const priceWith8Decimals = BigInt(priceCents) * 1000000n;
56062
56062
  const totalElapsed = Date.now() - startTime;
56063
- console.log(`[Price Oracle] Median price: $${median.toLocaleString()} \u2014 total time: ${totalElapsed}ms`);
56063
+ console.log(`[Price Oracle] Median price: $${median2.toLocaleString()} \u2014 total time: ${totalElapsed}ms`);
56064
56064
  console.log(`[Price Oracle] Price with 8 decimals: ${priceWith8Decimals}`);
56065
56065
  return priceWith8Decimals;
56066
56066
  }
@@ -57825,26 +57825,83 @@ var MAKE_PAYMENT_GAS_CEILING = BigInt(1e6);
57825
57825
  var EXTEND_POSITION_GAS_CEILING = BigInt(1e6);
57826
57826
  var LIQUIDATION_COMMIT_GAS_CEILING = BigInt(1e6);
57827
57827
  var LIQUIDATION_REVEAL_GAS_CEILING = BigInt(2e6);
57828
- async function resolveEip1559FeeFields(provider) {
57829
- const feeData = await provider.getFeeData();
57830
- let maxFeePerGas = feeData.maxFeePerGas ?? void 0;
57831
- let maxPriorityFeePerGas = feeData.maxPriorityFeePerGas ?? void 0;
57832
- if (!maxFeePerGas || !maxPriorityFeePerGas) {
57833
- const gasPrice = (await provider.getFeeData()).gasPrice ?? (0, import_ethers15.parseUnits)("1", "gwei");
57834
- maxFeePerGas = maxFeePerGas || gasPrice * 2n;
57835
- maxPriorityFeePerGas = maxPriorityFeePerGas || (0, import_ethers15.parseUnits)("1.5", "gwei");
57836
- }
57837
- if (maxFeePerGas < maxPriorityFeePerGas) {
57838
- maxFeePerGas = maxPriorityFeePerGas;
57828
+ var PRIORITY_FEE_FLOOR_WEI = (0, import_ethers15.parseUnits)("0.1", "gwei");
57829
+ var PRIORITY_FEE_CAP_WEI = (0, import_ethers15.parseUnits)("3", "gwei");
57830
+ var FEE_HISTORY_BLOCK_COUNT = 20;
57831
+ var FEE_HISTORY_REWARD_PERCENTILE = 75;
57832
+ function isJsonRpcSendable(provider) {
57833
+ return typeof provider.send === "function";
57834
+ }
57835
+ function parseFeeHistory(raw) {
57836
+ if (typeof raw !== "object" || raw === null) {
57837
+ throw new Error(`eth_feeHistory returned a non-object result: ${JSON.stringify(raw)}`);
57838
+ }
57839
+ const { baseFeePerGas, reward } = raw;
57840
+ if (!Array.isArray(baseFeePerGas) || baseFeePerGas.length === 0) {
57841
+ throw new Error(
57842
+ `eth_feeHistory returned no baseFeePerGas \u2014 the RPC does not support EIP-1559 fee history: ${JSON.stringify(raw)}`
57843
+ );
57844
+ }
57845
+ if (!Array.isArray(reward) || reward.length === 0) {
57846
+ throw new Error(
57847
+ `eth_feeHistory returned no reward percentiles \u2014 the RPC ignored the rewardPercentiles argument: ${JSON.stringify(raw)}`
57848
+ );
57849
+ }
57850
+ const nextBaseFee = (0, import_ethers15.getBigInt)(baseFeePerGas[baseFeePerGas.length - 1], "baseFeePerGas");
57851
+ const tips = reward.map((perBlock, i) => {
57852
+ if (!Array.isArray(perBlock) || perBlock.length === 0) {
57853
+ throw new Error(`eth_feeHistory reward row ${i} is empty: ${JSON.stringify(raw)}`);
57854
+ }
57855
+ return (0, import_ethers15.getBigInt)(perBlock[0], `reward[${i}]`);
57856
+ });
57857
+ return { nextBaseFee, tips };
57858
+ }
57859
+ function median(values) {
57860
+ const sorted = [...values].sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
57861
+ const mid = sorted.length >> 1;
57862
+ const hi = sorted[mid];
57863
+ if (hi === void 0) {
57864
+ throw new Error("median: no values");
57865
+ }
57866
+ if (sorted.length % 2 === 1)
57867
+ return hi;
57868
+ const lo = sorted[mid - 1];
57869
+ if (lo === void 0) {
57870
+ throw new Error("median: no values");
57871
+ }
57872
+ return (lo + hi) / 2n;
57873
+ }
57874
+ function clamp(value, floor, cap) {
57875
+ if (value < floor)
57876
+ return floor;
57877
+ if (value > cap)
57878
+ return cap;
57879
+ return value;
57880
+ }
57881
+ async function resolveEip1559FeeFields(feeProvider) {
57882
+ if (!isJsonRpcSendable(feeProvider)) {
57883
+ throw new Error(
57884
+ "resolveEip1559FeeFields: feeProvider must be a JSON-RPC provider exposing send() (the SDK read provider), not a wallet BrowserProvider \u2014 eth_feeHistory is required"
57885
+ );
57839
57886
  }
57887
+ const history = parseFeeHistory(
57888
+ await feeProvider.send("eth_feeHistory", [
57889
+ (0, import_ethers15.toQuantity)(FEE_HISTORY_BLOCK_COUNT),
57890
+ "latest",
57891
+ [FEE_HISTORY_REWARD_PERCENTILE]
57892
+ ])
57893
+ );
57894
+ const marketTip = median(history.tips);
57895
+ const maxPriorityFeePerGas = clamp(marketTip, PRIORITY_FEE_FLOOR_WEI, PRIORITY_FEE_CAP_WEI);
57896
+ const maxFeePerGas = history.nextBaseFee * 2n + maxPriorityFeePerGas;
57840
57897
  return { maxFeePerGas, maxPriorityFeePerGas };
57841
57898
  }
57842
57899
  async function sendEip1559Transaction(params) {
57843
- const { signer, to, data, gasLimit } = params;
57900
+ const { signer, feeProvider, to, data, gasLimit } = params;
57844
57901
  if (!signer.provider) {
57845
57902
  throw new Error("Signer must have a provider attached");
57846
57903
  }
57847
- const fees = await resolveEip1559FeeFields(signer.provider);
57904
+ const fees = await resolveEip1559FeeFields(feeProvider);
57848
57905
  return signer.sendTransaction({
57849
57906
  to,
57850
57907
  data,
@@ -72648,6 +72705,7 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
72648
72705
  }
72649
72706
  const tx2 = await sendEip1559Transaction({
72650
72707
  signer,
72708
+ feeProvider: this.getProviderOrThrow(),
72651
72709
  to: positionManagerAddress,
72652
72710
  data: mintCalldata,
72653
72711
  gasLimit: MINT_UCD_GAS_CEILING
@@ -73874,6 +73932,7 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
73874
73932
  }
73875
73933
  const tx2 = await sendEip1559Transaction({
73876
73934
  signer: extendSigner,
73935
+ feeProvider: this.getProviderOrThrow(),
73877
73936
  to: extendTo,
73878
73937
  data: extendCalldata,
73879
73938
  gasLimit: EXTEND_POSITION_GAS_CEILING
@@ -74808,6 +74867,7 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
74808
74867
  }
74809
74868
  const tx2 = await sendEip1559Transaction({
74810
74869
  signer: paymentSigner,
74870
+ feeProvider: this.getProviderOrThrow(),
74811
74871
  to: paymentContractAddress,
74812
74872
  data: paymentCalldata,
74813
74873
  gasLimit: MAKE_PAYMENT_GAS_CEILING
@@ -75573,6 +75633,7 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
75573
75633
  });
75574
75634
  tx2 = await sendEip1559Transaction({
75575
75635
  signer: withdrawSigner,
75636
+ feeProvider: this.getProviderOrThrow(),
75576
75637
  to: positionManagerAddress,
75577
75638
  data: withdrawCalldata,
75578
75639
  gasLimit: WITHDRAW_BTC_GAS_CEILING
package/dist/connector.js CHANGED
@@ -17461,11 +17461,11 @@ ${auth.clientId}`;
17461
17461
  );
17462
17462
  const prices = await Promise.race([fetchAll, timeoutPromise]);
17463
17463
  const sorted = [...prices].sort((a, b) => a - b);
17464
- let median;
17464
+ let median2;
17465
17465
  if (this.devSingleProvider) {
17466
- median = sorted[0];
17466
+ median2 = sorted[0];
17467
17467
  } else {
17468
- median = sorted[1];
17468
+ median2 = sorted[1];
17469
17469
  const spread3 = (sorted[2] - sorted[0]) / sorted[0];
17470
17470
  if (spread3 > PRICE_ORACLE_TOLERANCE) {
17471
17471
  const names = this.sources.map((s) => s.name).join(", ");
@@ -17475,10 +17475,10 @@ ${auth.clientId}`;
17475
17475
  );
17476
17476
  }
17477
17477
  }
17478
- const priceCents = Math.round(median * 100);
17478
+ const priceCents = Math.round(median2 * 100);
17479
17479
  const priceWith8Decimals = BigInt(priceCents) * 1000000n;
17480
17480
  const totalElapsed = Date.now() - startTime;
17481
- console.log(`[Price Oracle] Median price: $${median.toLocaleString()} \u2014 total time: ${totalElapsed}ms`);
17481
+ console.log(`[Price Oracle] Median price: $${median2.toLocaleString()} \u2014 total time: ${totalElapsed}ms`);
17482
17482
  console.log(`[Price Oracle] Price with 8 decimals: ${priceWith8Decimals}`);
17483
17483
  return priceWith8Decimals;
17484
17484
  }
@@ -56037,11 +56037,11 @@ ${auth.clientId}`;
56037
56037
  );
56038
56038
  const prices = await Promise.race([fetchAll, timeoutPromise]);
56039
56039
  const sorted = [...prices].sort((a, b) => a - b);
56040
- let median;
56040
+ let median2;
56041
56041
  if (this.devSingleProvider) {
56042
- median = sorted[0];
56042
+ median2 = sorted[0];
56043
56043
  } else {
56044
- median = sorted[1];
56044
+ median2 = sorted[1];
56045
56045
  const spread3 = (sorted[2] - sorted[0]) / sorted[0];
56046
56046
  if (spread3 > PRICE_ORACLE_TOLERANCE) {
56047
56047
  const names = this.sources.map((s) => s.name).join(", ");
@@ -56051,10 +56051,10 @@ ${auth.clientId}`;
56051
56051
  );
56052
56052
  }
56053
56053
  }
56054
- const priceCents = Math.round(median * 100);
56054
+ const priceCents = Math.round(median2 * 100);
56055
56055
  const priceWith8Decimals = BigInt(priceCents) * 1000000n;
56056
56056
  const totalElapsed = Date.now() - startTime;
56057
- console.log(`[Price Oracle] Median price: $${median.toLocaleString()} \u2014 total time: ${totalElapsed}ms`);
56057
+ console.log(`[Price Oracle] Median price: $${median2.toLocaleString()} \u2014 total time: ${totalElapsed}ms`);
56058
56058
  console.log(`[Price Oracle] Price with 8 decimals: ${priceWith8Decimals}`);
56059
56059
  return priceWith8Decimals;
56060
56060
  }
@@ -57819,26 +57819,83 @@ var MAKE_PAYMENT_GAS_CEILING = BigInt(1e6);
57819
57819
  var EXTEND_POSITION_GAS_CEILING = BigInt(1e6);
57820
57820
  var LIQUIDATION_COMMIT_GAS_CEILING = BigInt(1e6);
57821
57821
  var LIQUIDATION_REVEAL_GAS_CEILING = BigInt(2e6);
57822
- async function resolveEip1559FeeFields(provider) {
57823
- const feeData = await provider.getFeeData();
57824
- let maxFeePerGas = feeData.maxFeePerGas ?? void 0;
57825
- let maxPriorityFeePerGas = feeData.maxPriorityFeePerGas ?? void 0;
57826
- if (!maxFeePerGas || !maxPriorityFeePerGas) {
57827
- const gasPrice = (await provider.getFeeData()).gasPrice ?? (0, import_ethers15.parseUnits)("1", "gwei");
57828
- maxFeePerGas = maxFeePerGas || gasPrice * 2n;
57829
- maxPriorityFeePerGas = maxPriorityFeePerGas || (0, import_ethers15.parseUnits)("1.5", "gwei");
57830
- }
57831
- if (maxFeePerGas < maxPriorityFeePerGas) {
57832
- maxFeePerGas = maxPriorityFeePerGas;
57822
+ var PRIORITY_FEE_FLOOR_WEI = (0, import_ethers15.parseUnits)("0.1", "gwei");
57823
+ var PRIORITY_FEE_CAP_WEI = (0, import_ethers15.parseUnits)("3", "gwei");
57824
+ var FEE_HISTORY_BLOCK_COUNT = 20;
57825
+ var FEE_HISTORY_REWARD_PERCENTILE = 75;
57826
+ function isJsonRpcSendable(provider) {
57827
+ return typeof provider.send === "function";
57828
+ }
57829
+ function parseFeeHistory(raw) {
57830
+ if (typeof raw !== "object" || raw === null) {
57831
+ throw new Error(`eth_feeHistory returned a non-object result: ${JSON.stringify(raw)}`);
57832
+ }
57833
+ const { baseFeePerGas, reward } = raw;
57834
+ if (!Array.isArray(baseFeePerGas) || baseFeePerGas.length === 0) {
57835
+ throw new Error(
57836
+ `eth_feeHistory returned no baseFeePerGas \u2014 the RPC does not support EIP-1559 fee history: ${JSON.stringify(raw)}`
57837
+ );
57838
+ }
57839
+ if (!Array.isArray(reward) || reward.length === 0) {
57840
+ throw new Error(
57841
+ `eth_feeHistory returned no reward percentiles \u2014 the RPC ignored the rewardPercentiles argument: ${JSON.stringify(raw)}`
57842
+ );
57843
+ }
57844
+ const nextBaseFee = (0, import_ethers15.getBigInt)(baseFeePerGas[baseFeePerGas.length - 1], "baseFeePerGas");
57845
+ const tips = reward.map((perBlock, i) => {
57846
+ if (!Array.isArray(perBlock) || perBlock.length === 0) {
57847
+ throw new Error(`eth_feeHistory reward row ${i} is empty: ${JSON.stringify(raw)}`);
57848
+ }
57849
+ return (0, import_ethers15.getBigInt)(perBlock[0], `reward[${i}]`);
57850
+ });
57851
+ return { nextBaseFee, tips };
57852
+ }
57853
+ function median(values) {
57854
+ const sorted = [...values].sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
57855
+ const mid = sorted.length >> 1;
57856
+ const hi = sorted[mid];
57857
+ if (hi === void 0) {
57858
+ throw new Error("median: no values");
57859
+ }
57860
+ if (sorted.length % 2 === 1)
57861
+ return hi;
57862
+ const lo = sorted[mid - 1];
57863
+ if (lo === void 0) {
57864
+ throw new Error("median: no values");
57865
+ }
57866
+ return (lo + hi) / 2n;
57867
+ }
57868
+ function clamp(value, floor, cap) {
57869
+ if (value < floor)
57870
+ return floor;
57871
+ if (value > cap)
57872
+ return cap;
57873
+ return value;
57874
+ }
57875
+ async function resolveEip1559FeeFields(feeProvider) {
57876
+ if (!isJsonRpcSendable(feeProvider)) {
57877
+ throw new Error(
57878
+ "resolveEip1559FeeFields: feeProvider must be a JSON-RPC provider exposing send() (the SDK read provider), not a wallet BrowserProvider \u2014 eth_feeHistory is required"
57879
+ );
57833
57880
  }
57881
+ const history = parseFeeHistory(
57882
+ await feeProvider.send("eth_feeHistory", [
57883
+ (0, import_ethers15.toQuantity)(FEE_HISTORY_BLOCK_COUNT),
57884
+ "latest",
57885
+ [FEE_HISTORY_REWARD_PERCENTILE]
57886
+ ])
57887
+ );
57888
+ const marketTip = median(history.tips);
57889
+ const maxPriorityFeePerGas = clamp(marketTip, PRIORITY_FEE_FLOOR_WEI, PRIORITY_FEE_CAP_WEI);
57890
+ const maxFeePerGas = history.nextBaseFee * 2n + maxPriorityFeePerGas;
57834
57891
  return { maxFeePerGas, maxPriorityFeePerGas };
57835
57892
  }
57836
57893
  async function sendEip1559Transaction(params) {
57837
- const { signer, to, data, gasLimit } = params;
57894
+ const { signer, feeProvider, to, data, gasLimit } = params;
57838
57895
  if (!signer.provider) {
57839
57896
  throw new Error("Signer must have a provider attached");
57840
57897
  }
57841
- const fees = await resolveEip1559FeeFields(signer.provider);
57898
+ const fees = await resolveEip1559FeeFields(feeProvider);
57842
57899
  return signer.sendTransaction({
57843
57900
  to,
57844
57901
  data,
@@ -72642,6 +72699,7 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
72642
72699
  }
72643
72700
  const tx2 = await sendEip1559Transaction({
72644
72701
  signer,
72702
+ feeProvider: this.getProviderOrThrow(),
72645
72703
  to: positionManagerAddress,
72646
72704
  data: mintCalldata,
72647
72705
  gasLimit: MINT_UCD_GAS_CEILING
@@ -73868,6 +73926,7 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
73868
73926
  }
73869
73927
  const tx2 = await sendEip1559Transaction({
73870
73928
  signer: extendSigner,
73929
+ feeProvider: this.getProviderOrThrow(),
73871
73930
  to: extendTo,
73872
73931
  data: extendCalldata,
73873
73932
  gasLimit: EXTEND_POSITION_GAS_CEILING
@@ -74802,6 +74861,7 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
74802
74861
  }
74803
74862
  const tx2 = await sendEip1559Transaction({
74804
74863
  signer: paymentSigner,
74864
+ feeProvider: this.getProviderOrThrow(),
74805
74865
  to: paymentContractAddress,
74806
74866
  data: paymentCalldata,
74807
74867
  gasLimit: MAKE_PAYMENT_GAS_CEILING
@@ -75567,6 +75627,7 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
75567
75627
  });
75568
75628
  tx2 = await sendEip1559Transaction({
75569
75629
  signer: withdrawSigner,
75630
+ feeProvider: this.getProviderOrThrow(),
75570
75631
  to: positionManagerAddress,
75571
75632
  data: withdrawCalldata,
75572
75633
  gasLimit: WITHDRAW_BTC_GAS_CEILING
package/dist/index.js CHANGED
@@ -17342,11 +17342,11 @@ ${auth.clientId}`;
17342
17342
  );
17343
17343
  const prices = await Promise.race([fetchAll, timeoutPromise]);
17344
17344
  const sorted = [...prices].sort((a, b) => a - b);
17345
- let median;
17345
+ let median2;
17346
17346
  if (this.devSingleProvider) {
17347
- median = sorted[0];
17347
+ median2 = sorted[0];
17348
17348
  } else {
17349
- median = sorted[1];
17349
+ median2 = sorted[1];
17350
17350
  const spread3 = (sorted[2] - sorted[0]) / sorted[0];
17351
17351
  if (spread3 > PRICE_ORACLE_TOLERANCE) {
17352
17352
  const names = this.sources.map((s) => s.name).join(", ");
@@ -17356,10 +17356,10 @@ ${auth.clientId}`;
17356
17356
  );
17357
17357
  }
17358
17358
  }
17359
- const priceCents = Math.round(median * 100);
17359
+ const priceCents = Math.round(median2 * 100);
17360
17360
  const priceWith8Decimals = BigInt(priceCents) * 1000000n;
17361
17361
  const totalElapsed = Date.now() - startTime;
17362
- console.log(`[Price Oracle] Median price: $${median.toLocaleString()} \u2014 total time: ${totalElapsed}ms`);
17362
+ console.log(`[Price Oracle] Median price: $${median2.toLocaleString()} \u2014 total time: ${totalElapsed}ms`);
17363
17363
  console.log(`[Price Oracle] Price with 8 decimals: ${priceWith8Decimals}`);
17364
17364
  return priceWith8Decimals;
17365
17365
  }
@@ -55596,11 +55596,11 @@ ${auth.clientId}`;
55596
55596
  );
55597
55597
  const prices = await Promise.race([fetchAll, timeoutPromise]);
55598
55598
  const sorted = [...prices].sort((a, b) => a - b);
55599
- let median;
55599
+ let median2;
55600
55600
  if (this.devSingleProvider) {
55601
- median = sorted[0];
55601
+ median2 = sorted[0];
55602
55602
  } else {
55603
- median = sorted[1];
55603
+ median2 = sorted[1];
55604
55604
  const spread3 = (sorted[2] - sorted[0]) / sorted[0];
55605
55605
  if (spread3 > PRICE_ORACLE_TOLERANCE) {
55606
55606
  const names = this.sources.map((s) => s.name).join(", ");
@@ -55610,10 +55610,10 @@ ${auth.clientId}`;
55610
55610
  );
55611
55611
  }
55612
55612
  }
55613
- const priceCents = Math.round(median * 100);
55613
+ const priceCents = Math.round(median2 * 100);
55614
55614
  const priceWith8Decimals = BigInt(priceCents) * 1000000n;
55615
55615
  const totalElapsed = Date.now() - startTime;
55616
- console.log(`[Price Oracle] Median price: $${median.toLocaleString()} \u2014 total time: ${totalElapsed}ms`);
55616
+ console.log(`[Price Oracle] Median price: $${median2.toLocaleString()} \u2014 total time: ${totalElapsed}ms`);
55617
55617
  console.log(`[Price Oracle] Price with 8 decimals: ${priceWith8Decimals}`);
55618
55618
  return priceWith8Decimals;
55619
55619
  }
@@ -57378,26 +57378,83 @@ var MAKE_PAYMENT_GAS_CEILING = BigInt(1e6);
57378
57378
  var EXTEND_POSITION_GAS_CEILING = BigInt(1e6);
57379
57379
  var LIQUIDATION_COMMIT_GAS_CEILING = BigInt(1e6);
57380
57380
  var LIQUIDATION_REVEAL_GAS_CEILING = BigInt(2e6);
57381
- async function resolveEip1559FeeFields(provider) {
57382
- const feeData = await provider.getFeeData();
57383
- let maxFeePerGas = feeData.maxFeePerGas ?? void 0;
57384
- let maxPriorityFeePerGas = feeData.maxPriorityFeePerGas ?? void 0;
57385
- if (!maxFeePerGas || !maxPriorityFeePerGas) {
57386
- const gasPrice = (await provider.getFeeData()).gasPrice ?? (0, import_ethers14.parseUnits)("1", "gwei");
57387
- maxFeePerGas = maxFeePerGas || gasPrice * 2n;
57388
- maxPriorityFeePerGas = maxPriorityFeePerGas || (0, import_ethers14.parseUnits)("1.5", "gwei");
57389
- }
57390
- if (maxFeePerGas < maxPriorityFeePerGas) {
57391
- maxFeePerGas = maxPriorityFeePerGas;
57381
+ var PRIORITY_FEE_FLOOR_WEI = (0, import_ethers14.parseUnits)("0.1", "gwei");
57382
+ var PRIORITY_FEE_CAP_WEI = (0, import_ethers14.parseUnits)("3", "gwei");
57383
+ var FEE_HISTORY_BLOCK_COUNT = 20;
57384
+ var FEE_HISTORY_REWARD_PERCENTILE = 75;
57385
+ function isJsonRpcSendable(provider) {
57386
+ return typeof provider.send === "function";
57387
+ }
57388
+ function parseFeeHistory(raw) {
57389
+ if (typeof raw !== "object" || raw === null) {
57390
+ throw new Error(`eth_feeHistory returned a non-object result: ${JSON.stringify(raw)}`);
57391
+ }
57392
+ const { baseFeePerGas, reward } = raw;
57393
+ if (!Array.isArray(baseFeePerGas) || baseFeePerGas.length === 0) {
57394
+ throw new Error(
57395
+ `eth_feeHistory returned no baseFeePerGas \u2014 the RPC does not support EIP-1559 fee history: ${JSON.stringify(raw)}`
57396
+ );
57397
+ }
57398
+ if (!Array.isArray(reward) || reward.length === 0) {
57399
+ throw new Error(
57400
+ `eth_feeHistory returned no reward percentiles \u2014 the RPC ignored the rewardPercentiles argument: ${JSON.stringify(raw)}`
57401
+ );
57402
+ }
57403
+ const nextBaseFee = (0, import_ethers14.getBigInt)(baseFeePerGas[baseFeePerGas.length - 1], "baseFeePerGas");
57404
+ const tips = reward.map((perBlock, i) => {
57405
+ if (!Array.isArray(perBlock) || perBlock.length === 0) {
57406
+ throw new Error(`eth_feeHistory reward row ${i} is empty: ${JSON.stringify(raw)}`);
57407
+ }
57408
+ return (0, import_ethers14.getBigInt)(perBlock[0], `reward[${i}]`);
57409
+ });
57410
+ return { nextBaseFee, tips };
57411
+ }
57412
+ function median(values) {
57413
+ const sorted = [...values].sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
57414
+ const mid = sorted.length >> 1;
57415
+ const hi = sorted[mid];
57416
+ if (hi === void 0) {
57417
+ throw new Error("median: no values");
57418
+ }
57419
+ if (sorted.length % 2 === 1)
57420
+ return hi;
57421
+ const lo = sorted[mid - 1];
57422
+ if (lo === void 0) {
57423
+ throw new Error("median: no values");
57424
+ }
57425
+ return (lo + hi) / 2n;
57426
+ }
57427
+ function clamp(value, floor, cap) {
57428
+ if (value < floor)
57429
+ return floor;
57430
+ if (value > cap)
57431
+ return cap;
57432
+ return value;
57433
+ }
57434
+ async function resolveEip1559FeeFields(feeProvider) {
57435
+ if (!isJsonRpcSendable(feeProvider)) {
57436
+ throw new Error(
57437
+ "resolveEip1559FeeFields: feeProvider must be a JSON-RPC provider exposing send() (the SDK read provider), not a wallet BrowserProvider \u2014 eth_feeHistory is required"
57438
+ );
57392
57439
  }
57440
+ const history = parseFeeHistory(
57441
+ await feeProvider.send("eth_feeHistory", [
57442
+ (0, import_ethers14.toQuantity)(FEE_HISTORY_BLOCK_COUNT),
57443
+ "latest",
57444
+ [FEE_HISTORY_REWARD_PERCENTILE]
57445
+ ])
57446
+ );
57447
+ const marketTip = median(history.tips);
57448
+ const maxPriorityFeePerGas = clamp(marketTip, PRIORITY_FEE_FLOOR_WEI, PRIORITY_FEE_CAP_WEI);
57449
+ const maxFeePerGas = history.nextBaseFee * 2n + maxPriorityFeePerGas;
57393
57450
  return { maxFeePerGas, maxPriorityFeePerGas };
57394
57451
  }
57395
57452
  async function sendEip1559Transaction(params) {
57396
- const { signer, to, data, gasLimit } = params;
57453
+ const { signer, feeProvider, to, data, gasLimit } = params;
57397
57454
  if (!signer.provider) {
57398
57455
  throw new Error("Signer must have a provider attached");
57399
57456
  }
57400
- const fees = await resolveEip1559FeeFields(signer.provider);
57457
+ const fees = await resolveEip1559FeeFields(feeProvider);
57401
57458
  return signer.sendTransaction({
57402
57459
  to,
57403
57460
  data,
@@ -72201,6 +72258,7 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
72201
72258
  }
72202
72259
  const tx2 = await sendEip1559Transaction({
72203
72260
  signer,
72261
+ feeProvider: this.getProviderOrThrow(),
72204
72262
  to: positionManagerAddress,
72205
72263
  data: mintCalldata,
72206
72264
  gasLimit: MINT_UCD_GAS_CEILING
@@ -73427,6 +73485,7 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
73427
73485
  }
73428
73486
  const tx2 = await sendEip1559Transaction({
73429
73487
  signer: extendSigner,
73488
+ feeProvider: this.getProviderOrThrow(),
73430
73489
  to: extendTo,
73431
73490
  data: extendCalldata,
73432
73491
  gasLimit: EXTEND_POSITION_GAS_CEILING
@@ -74361,6 +74420,7 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
74361
74420
  }
74362
74421
  const tx2 = await sendEip1559Transaction({
74363
74422
  signer: paymentSigner,
74423
+ feeProvider: this.getProviderOrThrow(),
74364
74424
  to: paymentContractAddress,
74365
74425
  data: paymentCalldata,
74366
74426
  gasLimit: MAKE_PAYMENT_GAS_CEILING
@@ -75126,6 +75186,7 @@ Message: ${causeMessage}${quantumContext}${mintDebtDiagnostics}`
75126
75186
  });
75127
75187
  tx2 = await sendEip1559Transaction({
75128
75188
  signer: withdrawSigner,
75189
+ feeProvider: this.getProviderOrThrow(),
75129
75190
  to: positionManagerAddress,
75130
75191
  data: withdrawCalldata,
75131
75192
  gasLimit: WITHDRAW_BTC_GAS_CEILING
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gvnrdao/dh-mcp",
3
- "version": "0.5.7",
3
+ "version": "0.5.8",
4
4
  "description": "MCP server for the Diamond Hands protocol — Bitcoin-collateralized UCD lending with human-approved signing (sepolia practice by default; mainnet fully enabled)",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -53,7 +53,7 @@
53
53
  },
54
54
  "devDependencies": {
55
55
  "@anthropic-ai/mcpb": "2.1.2",
56
- "@gvnrdao/dh-sdk": "^0.0.333",
56
+ "@gvnrdao/dh-sdk": "^0.0.334",
57
57
  "@types/express": "5.0.3",
58
58
  "@types/jest": "29.5.12",
59
59
  "@types/node": "20.14.10",