@rhinestone/service-sdk 0.2.14 → 0.2.16
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/README.md +197 -14
- package/dist/chains.d.ts +48 -0
- package/dist/chains.d.ts.map +1 -1
- package/dist/chains.js +68 -16
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,22 +1,205 @@
|
|
|
1
|
-
# service-sdk
|
|
1
|
+
# @rhinestone/service-sdk
|
|
2
|
+
|
|
3
|
+
Shared TypeScript utilities for Rhinestone backend services — the pieces every service
|
|
4
|
+
needs and none of them should reimplement: OpenTelemetry setup and helpers, chain-aware gas
|
|
5
|
+
fee estimation, and BigInt-safe JSON serialization.
|
|
6
|
+
|
|
7
|
+
Published to npm and consumed by `orchestrator`, `relayer`, `deposit-service-processor`,
|
|
8
|
+
`route-stats`, `back-office` (api), and `1auth` (passkey, recovery-signer).
|
|
9
|
+
|
|
10
|
+
Deliberately not a home for domain logic. The one module that encodes real judgement —
|
|
11
|
+
`chains` — earns it by being pure pricing arithmetic that several services must agree on;
|
|
12
|
+
everything else is glue.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
bun add @rhinestone/service-sdk
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
`viem` is a peer dependency (`^2.40.1`), needed only for the `chains` entry point.
|
|
21
|
+
|
|
22
|
+
## Entry points
|
|
23
|
+
|
|
24
|
+
| Import | What |
|
|
25
|
+
|---|---|
|
|
26
|
+
| `@rhinestone/service-sdk` | Re-exports `./serialization` only |
|
|
27
|
+
| `@rhinestone/service-sdk/chains` | Chain-aware gas fee estimation |
|
|
28
|
+
| `@rhinestone/service-sdk/opentelemetry` | SDK setup, tracing/metrics helpers, propagation, integrations |
|
|
29
|
+
| `@rhinestone/service-sdk/serialization` | BigInt-safe JSON serialization |
|
|
30
|
+
|
|
31
|
+
---
|
|
2
32
|
|
|
3
33
|
## Gas fee estimation
|
|
4
34
|
|
|
5
35
|
```ts
|
|
6
36
|
import { estimateGasFees } from '@rhinestone/service-sdk/chains'
|
|
7
37
|
|
|
8
|
-
const
|
|
9
|
-
|
|
38
|
+
const { maxFeePerGas, maxPriorityFeePerGas, baseFeePerGas } =
|
|
39
|
+
await estimateGasFees(client, chainId)
|
|
10
40
|
```
|
|
11
41
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
`
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
42
|
+
`estimateGasFees` returns EIP-1559-shaped fees for every chain and every mode. The whole
|
|
43
|
+
module exists because viem's default estimator systematically mis-prices our fills: the
|
|
44
|
+
orchestrator **bills** `maxFeePerGas`, but on-chain a transaction only ever pays
|
|
45
|
+
`base + tip`, so unused headroom is charged to the user rather than spent.
|
|
46
|
+
|
|
47
|
+
**Three estimation strategies**, selected per chain:
|
|
48
|
+
|
|
49
|
+
- **Windowed `eth_feeHistory`** (mainnet, sepolia, plasma) — pools tips across a 20-block
|
|
50
|
+
window and prices the base from the *next* block, escalating to a higher reward
|
|
51
|
+
percentile when the window's mean `gasUsedRatio` shows congestion. Chains land here
|
|
52
|
+
either for a volatile base fee (the L1s) or a broken tip oracle (plasma, whose
|
|
53
|
+
`eth_maxPriorityFeePerGas` reports ~3.5x what fills actually clear at).
|
|
54
|
+
- **viem's estimate with a tip floor** (base, bsc, polygon, gnosis, hyperEVM, monad) — for
|
|
55
|
+
chains whose base fee is stable but whose tip oracle *under*-reports. Polygon and base
|
|
56
|
+
additionally window the feeHistory tip, since their real tip drifts above a static floor.
|
|
57
|
+
- **viem's estimate plain** — everything else.
|
|
58
|
+
|
|
59
|
+
On both viem paths a per-chain tip **cap** also applies where the oracle *over*-reports
|
|
60
|
+
(optimism, soneium, sonic, avalanche, bsc). Cap and floor can both be set for one chain —
|
|
61
|
+
bsc is — in which case the cap applies first and the floor last, so a floor is never
|
|
62
|
+
undercut.
|
|
63
|
+
|
|
64
|
+
`maxFeePerGas` is rebuilt as `nextBlockBaseFee * multiplier + tip` — the next block's base,
|
|
65
|
+
not the latest mined one, which is where most of the over-charge disappears. The multiplier
|
|
66
|
+
is per-chain and each path has its own table: 1.05x by default on the viem paths (overridden
|
|
67
|
+
for hyperEVM, gnosis, avalanche), and part of the per-chain fee config on the windowed path
|
|
68
|
+
(1.30x sepolia, 1.60x mainnet, 2.00x plasma).
|
|
69
|
+
|
|
70
|
+
**`baseFeePerGas` in the result** lets a caller derive the expected price without a second
|
|
71
|
+
`eth_feeHistory` round trip. It matters because `maxFeePerGas` is a submission *cap* — right
|
|
72
|
+
to bill, wrong to report to a consumer comparing relayers. It is `null` — never `0n` — when
|
|
73
|
+
the base was unavailable (non-1559 chain, legacy mode, or a degraded read). Treat `null` as
|
|
74
|
+
unknown and fall back; treating it as zero would price a cap at the tip alone.
|
|
75
|
+
|
|
76
|
+
### Legacy mode
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
const fees = await estimateGasFees(client, chainId, { mode: 'legacy' })
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Takes precedence over all chain-specific logic and calls
|
|
83
|
+
`client.estimateFeesPerGas({ type: 'legacy' })` without requesting fee history. The legacy
|
|
84
|
+
`gasPrice` is normalized to `{ maxFeePerGas: gasPrice, maxPriorityFeePerGas: gasPrice }` —
|
|
85
|
+
equal caps preserve the legacy effective-price ceiling for EIP-1559 consumers. On a
|
|
86
|
+
no-priority local fork where `baseFee == gasPrice`, the max-fee cap keeps the paid tip at
|
|
87
|
+
zero. The mode selects the estimation *source* only; it does not select a transaction type.
|
|
88
|
+
|
|
89
|
+
### When the RPC degrades
|
|
90
|
+
|
|
91
|
+
Every fallback here is silent by design — an `eth_feeHistory` outage must never become a
|
|
92
|
+
quote outage. Because that would otherwise revert a chain to exactly the pricing this module
|
|
93
|
+
removes, with nothing to show for it, the degraded paths annotate the **active span**
|
|
94
|
+
(`gas.estimate.degraded`, `gas.estimate.degraded_reason`, `gas.estimate.chain_id`) rather
|
|
95
|
+
than a logger. `@opentelemetry/api` is a no-op without a registered SDK, so this costs
|
|
96
|
+
nothing for uninstrumented consumers.
|
|
97
|
+
|
|
98
|
+
### Changing the per-chain numbers
|
|
99
|
+
|
|
100
|
+
Every floor, cap and multiplier in `src/chains.ts` is annotated with the evidence it came
|
|
101
|
+
from — measured clearing prices, block replays, dated probes — and several encode a
|
|
102
|
+
deliberate trade rather than an optimum. **Read the comment before changing the number**;
|
|
103
|
+
these values price real fills on seven services, and the reasoning is not reconstructible
|
|
104
|
+
from the value alone.
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
## OpenTelemetry
|
|
109
|
+
|
|
110
|
+
### Setup
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
import { setupSDK, configFromEnv, shutdownOpentelemetry } from '@rhinestone/service-sdk/opentelemetry'
|
|
114
|
+
|
|
115
|
+
setupSDK({ serviceName: 'my-service', ...configFromEnv() })
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
`setupSDK` is idempotent and wires the whole stack in one call: an `AsyncLocalStorage`
|
|
119
|
+
context manager, gzip'd OTLP/gRPC trace and metric exporters, a tuned `BatchSpanProcessor`,
|
|
120
|
+
host metrics, and a `startup_time` gauge used as a deploy marker. It also registers a
|
|
121
|
+
`beforeExit` flush, so a clean shutdown exports what is still queued.
|
|
122
|
+
|
|
123
|
+
`configFromEnv()` reads `VERSION` and `DEPLOYMENT_ENV`. The OTLP endpoint comes from the
|
|
124
|
+
standard `OTEL_EXPORTER_OTLP_ENDPOINT`.
|
|
125
|
+
|
|
126
|
+
**Bundled instrumentations**, all enabled by `setupSDK`: `http` (WebSocket upgrades ignored,
|
|
127
|
+
so a long-lived connection can't produce an enormous span), `express` (captures referer and
|
|
128
|
+
request body on handler spans), `amqplib`, `prisma`, `pino` (log *correlation* only —
|
|
129
|
+
sending is disabled), and `viem` (parent-span-gated, capturing operation results).
|
|
130
|
+
|
|
131
|
+
### Tracing
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
import { withSpan, currentSpan, currentTraceId, addSpanAttributes, recordSpanError, CreateSpan } from '@rhinestone/service-sdk/opentelemetry'
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
`withSpan(name, fn, options?)` runs `fn` in an active span, handling sync and async
|
|
138
|
+
identically: it records the exception, sets `ERROR` status and ends the span on a throw or
|
|
139
|
+
rejection, then rethrows. `CreateSpan(name?)` is the decorator form for class methods.
|
|
140
|
+
`withRootContext(fn)` detaches from the ambient context to start a fresh trace.
|
|
141
|
+
`currentSpan`, `currentContext`, `currentTraceId`, `addSpanAttributes` and `recordSpanError`
|
|
142
|
+
operate on whatever span is active.
|
|
143
|
+
|
|
144
|
+
### Metrics
|
|
145
|
+
|
|
146
|
+
`createCounter`, `createHistogram`, `createGauge` and `createObservableGauge`, each taking
|
|
147
|
+
an optional `MetricOptions`. `SpanStatusCode`, `ValueType` and the OTel API types are
|
|
148
|
+
re-exported so consumers need no direct `@opentelemetry/api` dependency.
|
|
149
|
+
|
|
150
|
+
### Propagation
|
|
151
|
+
|
|
152
|
+
`getTraceHeaders()` returns W3C `traceparent`/`tracestate` as a plain object for axios or
|
|
153
|
+
generic HTTP. `injectTraceHeaders` is the viem `onFetchRequest` hook form, propagating trace
|
|
154
|
+
context into outgoing RPC calls.
|
|
155
|
+
|
|
156
|
+
---
|
|
157
|
+
|
|
158
|
+
## Serialization
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
import { convertBigIntFields, stringifyBigIntFields, bigintReplacer } from '@rhinestone/service-sdk/serialization'
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
`BigInt` has no JSON representation, so an un-converted payload makes `JSON.stringify`
|
|
165
|
+
throw. These convert it to decimal strings.
|
|
166
|
+
|
|
167
|
+
- `convertBigIntFields(value, options?)` — returns a converted copy.
|
|
168
|
+
- `stringifyBigIntFields(value, options?)` — stringifies directly, preserving
|
|
169
|
+
`JSON.stringify`'s `toJSON` semantics (so a `Date` still becomes an ISO string).
|
|
170
|
+
- `bigintReplacer` — the bare replacer, with no guards, for use with `JSON.stringify`.
|
|
171
|
+
|
|
172
|
+
Both guarded functions **throw** on circular references and past `maxDepth`
|
|
173
|
+
(`DEFAULT_BIGINT_CONVERSION_MAX_DEPTH`, 100) rather than emitting lossy placeholders.
|
|
174
|
+
`convertFiniteNumbers: true` additionally encodes finite numbers as strings, for
|
|
175
|
+
canonical-signing payloads.
|
|
176
|
+
|
|
177
|
+
---
|
|
178
|
+
|
|
179
|
+
## Development
|
|
180
|
+
|
|
181
|
+
```sh
|
|
182
|
+
bun install
|
|
183
|
+
bun run build # clean + tsc
|
|
184
|
+
bun run test
|
|
185
|
+
bun run check # biome (src only — see below)
|
|
186
|
+
bun run check:fix
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
Dependency installs are gated on a 3-day release age (`bunfig.toml`,
|
|
190
|
+
`minimumReleaseAge`) as a supply-chain mitigation, so a freshly published — or freshly
|
|
191
|
+
compromised — version is not picked up immediately.
|
|
192
|
+
|
|
193
|
+
Note that biome is scoped to `src/**`, so `test/**` is neither linted nor format-checked.
|
|
194
|
+
|
|
195
|
+
## Releasing
|
|
196
|
+
|
|
197
|
+
Publishing is automated. Cut a **GitHub Release** with tag `vX.Y.Z`; `release.yml` builds and
|
|
198
|
+
publishes to npm via OIDC trusted publishing.
|
|
199
|
+
|
|
200
|
+
The **tag is the version**. `package.json` says `0.0.1` and is never bumped — the workflow
|
|
201
|
+
overwrites it at publish time from the tag name. Don't try to keep it in sync.
|
|
202
|
+
|
|
203
|
+
Consumers pin a mix of exact versions and caret ranges, so **a `0.2.x` release reaches
|
|
204
|
+
every caret consumer on its next install, with no PR in that service**. Treat any change to
|
|
205
|
+
gas pricing as shipping to production on that basis.
|
package/dist/chains.d.ts
CHANGED
|
@@ -12,6 +12,54 @@ export type EstimateGasFeesOptions = {
|
|
|
12
12
|
* Omit this option to retain the chain-aware EIP-1559 estimation strategy.
|
|
13
13
|
*/
|
|
14
14
|
mode?: 'legacy';
|
|
15
|
+
/**
|
|
16
|
+
* Samples the per-block tip at this percentile instead of the chain's
|
|
17
|
+
* configured `tipPercentile`/`congestedTipPercentile`, for a caller that must
|
|
18
|
+
* deliberately price against something other than our own calibration.
|
|
19
|
+
*
|
|
20
|
+
* The one caller today is the Safe GTF gas-price feed (RHI-4992). Safe
|
|
21
|
+
* benchmarks that feed against Gelato's `relayer_getFeeData` and asked us to
|
|
22
|
+
* match its mainnet buffer, having previously used Gelato for the same
|
|
23
|
+
* self-pay flow. Gelato fits to `base + tip`, 20-block window, NO multiplier —
|
|
24
|
+
* the same shape as our windowed path, so tracking them is a percentile and
|
|
25
|
+
* nothing else. Which percentile, measured on 314 paired live samples over 95
|
|
26
|
+
* minutes (2026-08-10, mainnet):
|
|
27
|
+
*
|
|
28
|
+
* pctl model/gelato p10 median p90 mean|err| gwei
|
|
29
|
+
* p50 0.479 0.669 0.889 0.15124
|
|
30
|
+
* p60 0.624 0.879 1.085 0.10221
|
|
31
|
+
* p65 0.725 1.045 1.264 0.08906 <- parity
|
|
32
|
+
* p70 0.985 1.411 1.793 0.16159
|
|
33
|
+
*
|
|
34
|
+
* MEASURE IT OVER HOURS, NOT MINUTES. Two short fits of this same quantity, on
|
|
35
|
+
* the same day, gave p60 at 0.982 (25 samples / 100s) and 0.785 (40 samples /
|
|
36
|
+
* 4 min) — and the 314-sample answer is 0.879. Even the CUMULATIVE median
|
|
37
|
+
* inside the long run wandered 1.006 -> 0.844 -> 0.929 -> 0.881 as it filled
|
|
38
|
+
* up. A 5-percentile step moves the median 19-35%, so a short window can
|
|
39
|
+
* "prove" almost any percentile. An older fit also needed a x1.17 multiplier;
|
|
40
|
+
* that is gone. Re-fit against live data before trusting any number here.
|
|
41
|
+
*
|
|
42
|
+
* DELIBERATELY NOT the chain default. Our own mainnet calibration is p25 plus
|
|
43
|
+
* a realized-clearing floor. p65 sits on the steep face of mainnet's tip cliff
|
|
44
|
+
* and prices at ~1.5x what a transaction actually costs (Gelato itself: 1.494x
|
|
45
|
+
* median, p10 1.125 / p90 2.089 — they swing because they stand on the same
|
|
46
|
+
* cliff). This override exists so ONE surface can opt into a competitor's
|
|
47
|
+
* pricing without moving intent pricing, which shares this function.
|
|
48
|
+
*
|
|
49
|
+
* The cliff MOVES, which is why it is a cliff and not a constant: a 120-block
|
|
50
|
+
* replay read a realized p50 tip of 0.0094 gwei (39,312 txs) in the morning
|
|
51
|
+
* and 0.148 gwei (34,314 txs) a few hours later — 16x — with p75 going
|
|
52
|
+
* 0.25 -> 1.00 gwei over the same span.
|
|
53
|
+
*
|
|
54
|
+
* Applies to the windowed feeHistory path only (`HISTORICAL_FEE_CHAINS`).
|
|
55
|
+
* Ignored on the viem path, where the tip does not come from a percentile we
|
|
56
|
+
* choose. Mainnet — the only chain that needs it — is on the windowed path.
|
|
57
|
+
*
|
|
58
|
+
* Requests a single reward column and pins the read to it, so congestion
|
|
59
|
+
* escalation cannot silently move the sampled percentile somewhere else — the
|
|
60
|
+
* caller asked to track one number.
|
|
61
|
+
*/
|
|
62
|
+
tipPercentileOverride?: number;
|
|
15
63
|
};
|
|
16
64
|
/**
|
|
17
65
|
* {@link estimateGasFees}' result: viem's EIP-1559 fee values plus the next
|
package/dist/chains.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"chains.d.ts","sourceRoot":"","sources":["../src/chains.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAmB,YAAY,EAAE,MAAM,MAAM,CAAA;AAmB3E,oDAAoD;AAMpD,MAAM,MAAM,mBAAmB,GAAG,IAAI,CACpC,YAAY,EACZ,oBAAoB,GAAG,eAAe,CACvC,GACC,OAAO,CAAC,IAAI,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC,CAAA;AAEtC,2CAA2C;AAC3C,MAAM,MAAM,sBAAsB,GAAG;IACnC;;;;;;;OAOG;IACH,IAAI,CAAC,EAAE,QAAQ,CAAA;
|
|
1
|
+
{"version":3,"file":"chains.d.ts","sourceRoot":"","sources":["../src/chains.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAmB,YAAY,EAAE,MAAM,MAAM,CAAA;AAmB3E,oDAAoD;AAMpD,MAAM,MAAM,mBAAmB,GAAG,IAAI,CACpC,YAAY,EACZ,oBAAoB,GAAG,eAAe,CACvC,GACC,OAAO,CAAC,IAAI,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC,CAAA;AAEtC,2CAA2C;AAC3C,MAAM,MAAM,sBAAsB,GAAG;IACnC;;;;;;;OAOG;IACH,IAAI,CAAC,EAAE,QAAQ,CAAA;IACf;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8CG;IACH,qBAAqB,CAAC,EAAE,MAAM,CAAA;CAC/B,CAAA;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,MAAM,gBAAgB,GAAG,gBAAgB,GAAG;IAChD,aAAa,EAAE,MAAM,GAAG,IAAI,CAAA;CAC7B,CAAA;AAyWD;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,eAAe,CACnC,MAAM,EAAE,mBAAmB,EAC3B,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,sBAAsB,GAC/B,OAAO,CAAC,gBAAgB,CAAC,CAqE3B"}
|
package/dist/chains.js
CHANGED
|
@@ -41,19 +41,51 @@ const DEFAULT_FEE_CONFIG = {
|
|
|
41
41
|
// Per-chain overrides for the windowed feeHistory path; any omitted field falls
|
|
42
42
|
// back to DEFAULT_FEE_CONFIG.
|
|
43
43
|
const FEE_CONFIG_OVERRIDES = {
|
|
44
|
-
//
|
|
45
|
-
//
|
|
46
|
-
//
|
|
47
|
-
//
|
|
48
|
-
//
|
|
49
|
-
//
|
|
50
|
-
//
|
|
51
|
-
//
|
|
44
|
+
// Sepolia's reward distribution is a STEP FUNCTION, not a gradient. Replayed 300
|
|
45
|
+
// blocks / 38,746 txs on 2026-08-10:
|
|
46
|
+
//
|
|
47
|
+
// realised priority fee p1-p10 0.001 p25 0.0012 p50 0.354 p75 1.000
|
|
48
|
+
// per-block MINIMUM included tip: 0.001 gwei in essentially every block
|
|
49
|
+
//
|
|
50
|
+
// So a mass sits at 0.001 and another at 1.0 with almost nothing between, and the
|
|
51
|
+
// percentile choice is the whole price. p50 lands ON the cliff (0.22 gwei live) and
|
|
52
|
+
// p75 lands past it (1.16) — against a price that demonstrably clears at 0.001.
|
|
53
|
+
//
|
|
54
|
+
// This chain was billing ~1000x that clearing price. Paired against live feeHistory
|
|
55
|
+
// 12 times, our tip tracked p75 at ratio 0.98 while the median block was only 36%
|
|
56
|
+
// full: sepolia has 4-6 completely full blocks per 20-block window, and the
|
|
57
|
+
// congestion predicate takes the MEAN gasUsedRatio, so those outliers alone hold it
|
|
58
|
+
// on the 0.5 threshold (sampled means 0.499-0.541, escalating in 5 of 8 samples).
|
|
59
|
+
// The quote therefore oscillated between a 0.22 and a 1.16 gwei tip minute to minute.
|
|
60
|
+
//
|
|
61
|
+
// MEASURED effect, simulated against live feeHistory 10x with both configs side by
|
|
62
|
+
// side rather than derived on paper (the paper number was wrong):
|
|
63
|
+
//
|
|
64
|
+
// tip 1.1124 -> 0.1300 gwei (9x lower)
|
|
65
|
+
// cap 2.49 -> 1.51 gwei (1.68x cheaper, at a ~1.0 gwei base fee)
|
|
66
|
+
//
|
|
67
|
+
// Note WHICH knob does the work. Congestion fired in 10 of 10 samples, so sepolia
|
|
68
|
+
// effectively never reads `tipPercentile` at all — `congestedTipPercentile` is the
|
|
69
|
+
// live lever, and 75 -> 50 is what buys the 9x. The 50 -> 25 only starts to bite once
|
|
70
|
+
// the mean-based predicate below is fixed, at which point this gets cheaper again
|
|
71
|
+
// (p25 reads 0.0011 gwei). Reported by an external consumer as 3.3x over spot
|
|
72
|
+
// (RHI-5618).
|
|
73
|
+
//
|
|
74
|
+
// NOTE the mean-based congestion predicate is a SEPARATE defect and is deliberately
|
|
75
|
+
// NOT fixed here: mainnet sits on the same 0.5 knife-edge (measured mean 0.475-0.502,
|
|
76
|
+
// flipping), so switching it to a median would silently re-tune mainnet's tip too.
|
|
77
|
+
// That needs its own A/C measurement — see RHI-5618.
|
|
52
78
|
[chains_1.sepolia.id]: {
|
|
79
|
+
// Unchanged. The base multiplier was already lowered 2.0x -> 1.3x and was never
|
|
80
|
+
// the dominant term here; the tip was.
|
|
53
81
|
baseFeeMultiplierPct: 130n,
|
|
54
|
-
tipPercentile:
|
|
55
|
-
congestedTipPercentile:
|
|
56
|
-
|
|
82
|
+
tipPercentile: 25,
|
|
83
|
+
congestedTipPercentile: 50,
|
|
84
|
+
// 0.001 gwei — the price sepolia inclusion demonstrably clears at, from the replay
|
|
85
|
+
// above. Was 0n: a zero floor lets a percentile read of 0 price the tip at nothing,
|
|
86
|
+
// and testnet inclusion is already flaky, so the floor is doing real work now that
|
|
87
|
+
// we sample low enough to hit it.
|
|
88
|
+
tipFloorWei: 1000000n,
|
|
57
89
|
},
|
|
58
90
|
// Mainnet's reward distribution is inflated by MEV-boost blocks — searchers
|
|
59
91
|
// pay enormous priority fees, so the feeHistory p50 prices our plain fills
|
|
@@ -303,6 +335,16 @@ async function nextBlockBaseFee(client, chainId) {
|
|
|
303
335
|
* type.
|
|
304
336
|
*/
|
|
305
337
|
async function estimateGasFees(client, chainId, options) {
|
|
338
|
+
// Validated HERE, at the public entry, rather than where it is used. A bad value
|
|
339
|
+
// otherwise surfaces as an opaque provider error from eth_feeHistory — and only on the
|
|
340
|
+
// chains that take the windowed path, so the same argument would throw on mainnet and
|
|
341
|
+
// be silently ignored on Base. This is a locally decidable input; fail locally and say
|
|
342
|
+
// what was wrong. (Copilot, PR #24.)
|
|
343
|
+
const override = options?.tipPercentileOverride;
|
|
344
|
+
if (override !== undefined &&
|
|
345
|
+
(!Number.isFinite(override) || override < 0 || override > 100)) {
|
|
346
|
+
throw new Error(`tipPercentileOverride must be a finite number in [0, 100], got ${override}`);
|
|
347
|
+
}
|
|
306
348
|
if (options?.mode === 'legacy') {
|
|
307
349
|
const legacyClient = client;
|
|
308
350
|
const { gasPrice } = await legacyClient.estimateFeesPerGas({
|
|
@@ -320,7 +362,7 @@ async function estimateGasFees(client, chainId, options) {
|
|
|
320
362
|
}
|
|
321
363
|
if (HISTORICAL_FEE_CHAINS.has(chainId)) {
|
|
322
364
|
const config = { ...DEFAULT_FEE_CONFIG, ...FEE_CONFIG_OVERRIDES[chainId] };
|
|
323
|
-
return estimateFeeHistoryGasFees(client, chainId, config);
|
|
365
|
+
return estimateFeeHistoryGasFees(client, chainId, config, options?.tipPercentileOverride);
|
|
324
366
|
}
|
|
325
367
|
const tipFloorWei = TIP_FLOOR_WEI[chainId];
|
|
326
368
|
if (tipFloorWei !== undefined) {
|
|
@@ -526,17 +568,27 @@ async function viemFallbackCap(client, config, priorityFee) {
|
|
|
526
568
|
}
|
|
527
569
|
return cap > priorityFee ? cap : priorityFee;
|
|
528
570
|
}
|
|
529
|
-
async function estimateFeeHistoryGasFees(client, chainId, config) {
|
|
571
|
+
async function estimateFeeHistoryGasFees(client, chainId, config, tipPercentileOverride) {
|
|
572
|
+
// An override asks for ONE percentile, so ask the RPC for one column. It
|
|
573
|
+
// cannot be requested as [p, p]: geth rejects a reward-percentile list that is
|
|
574
|
+
// not strictly increasing — "invalid reward percentile: #0:60.000000 >=
|
|
575
|
+
// #1:60.000000", verified live 2026-08-10 against publicnode and 1rpc — so the
|
|
576
|
+
// duplicate form is an RPC error, not a pricing choice.
|
|
577
|
+
const overridden = tipPercentileOverride !== undefined;
|
|
530
578
|
const feeHistory = await client.getFeeHistory({
|
|
531
579
|
blockCount: config.historyBlocks,
|
|
532
|
-
rewardPercentiles:
|
|
580
|
+
rewardPercentiles: overridden
|
|
581
|
+
? [tipPercentileOverride]
|
|
582
|
+
: [config.tipPercentile, config.congestedTipPercentile],
|
|
533
583
|
});
|
|
534
584
|
const ratios = feeHistory.gasUsedRatio ?? [];
|
|
535
585
|
const meanRatio = ratios.length > 0
|
|
536
586
|
? ratios.reduce((sum, r) => sum + r, 0) / ratios.length
|
|
537
587
|
: 0;
|
|
538
|
-
// Under load, read the per-block tip from the higher percentile column.
|
|
539
|
-
|
|
588
|
+
// Under load, read the per-block tip from the higher percentile column. With
|
|
589
|
+
// an override there is only column 0, and escalating is not wanted anyway —
|
|
590
|
+
// the caller asked to track one percentile, so congestion must not move it.
|
|
591
|
+
const column = !overridden && meanRatio > config.congestionRatio ? 1 : 0;
|
|
540
592
|
const perBlockTips = (feeHistory.reward ?? []).map((r) => r?.[column] ?? 0n);
|
|
541
593
|
const windowedTip = percentileBigInt(perBlockTips, config.windowTipPercentile);
|
|
542
594
|
const priorityFee = windowedTip > config.tipFloorWei ? windowedTip : config.tipFloorWei;
|