Two hackathon integrations spanning the whole credit stack, sim-first (green with no keys) with real integrations behind env flags. KeeperHub — the on-chain execution & reliability layer (lib/keeperhub/): - OnchainExecutor: pay-per-execution (x402/MPP) -> simulate -> smart gas with exponential backoff -> MEV-protected private routing -> audit trail; real MCP client to app.keeperhub.com; deterministic sim fallback. - Autonomous Credit Keeper: FTSO position health -> check->execute deleverage. - Credit Automations: declarative price/health/schedule rules as workflows. - Autonomous Scheduler: KeeperHub-cron loop running keeper + automations. Flare — interoperable + confidential primitives (packages/.../flare, lib/flare): - FXRP credit satellite: CAN-gated, FTSO-priced, FDC-attested; Coston2 Solidity vault (contracts/flare/). - Confidential Compute credit scoring (TEE-attested, private inputs). - FTSO-priced multi-asset collateral; FAssets mint->collateralize lifecycle. x402 Credit-Service Marketplace: sells cred402's intelligence pay-per-call over x402 (402->pay->200); paid receipts feed the revenue->reputation loop. Wiring: 78 MCP tools, /v1 + /api routes, console Flare + x402 tabs, 11 demo CLIs, 8 feature docs + docs/SUBMISSIONS.md, recorded demo video (media/). 346 tests pass; 7 adversarial verification workflows caught + fixed ~37 correctness/security issues. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
79 lines
2.9 KiB
TypeScript
79 lines
2.9 KiB
TypeScript
import type { GasEstimate } from "./types.js";
|
||
|
||
/**
|
||
* SmartGasEstimator — KeeperHub's "transactions execute instead of getting stuck"
|
||
* guarantee, modeled honestly. It prices EIP-1559 fees off the current base fee and,
|
||
* on congestion / cold-start retries, applies EXPONENTIAL BACKOFF to the fee cap so a
|
||
* resubmission clears the mempool instead of stalling. This mirrors what KeeperHub's
|
||
* smart-gas engine does server-side; on the real path we seed it from KeeperHub's
|
||
* `simulate` gas estimate, on the sim path it stands alone.
|
||
*/
|
||
export interface GasInputs {
|
||
/** Current network base fee (wei). */
|
||
base_fee_wei: bigint;
|
||
/** Miner tip (wei). */
|
||
priority_fee_wei: bigint;
|
||
/** Gas limit from simulation (or a safe default). */
|
||
gas_limit: number;
|
||
}
|
||
|
||
export interface SmartGasOptions {
|
||
/** Fee multiplier applied per backoff attempt (>1). Default 1.25 (25% bumps). */
|
||
backoffFactor?: number;
|
||
/** Max resubmission attempts before giving up. Default 5. */
|
||
maxAttempts?: number;
|
||
/** Base delay (ms) for the exponential retry schedule. Default 500ms. */
|
||
baseDelayMs?: number;
|
||
/** Headroom over base fee for the max-fee cap. Default 2× base + tip. */
|
||
baseFeeHeadroom?: number;
|
||
}
|
||
|
||
const DEFAULTS: Required<SmartGasOptions> = {
|
||
backoffFactor: 1.25,
|
||
maxAttempts: 5,
|
||
baseDelayMs: 500,
|
||
baseFeeHeadroom: 2,
|
||
};
|
||
|
||
export class SmartGasEstimator {
|
||
private readonly opts: Required<SmartGasOptions>;
|
||
|
||
constructor(opts: SmartGasOptions = {}) {
|
||
this.opts = { ...DEFAULTS, ...opts };
|
||
if (this.opts.backoffFactor <= 1) throw new Error("gas: backoffFactor must be > 1");
|
||
if (this.opts.maxAttempts < 1) throw new Error("gas: maxAttempts must be >= 1");
|
||
}
|
||
|
||
/**
|
||
* Price fees for a given attempt (0-based). Attempt 0 is the initial submit; each
|
||
* subsequent attempt multiplies both fee components by backoffFactor^attempt so a
|
||
* stuck tx is replaced by one the network will actually include.
|
||
*/
|
||
estimate(inputs: GasInputs, attempt = 0): GasEstimate {
|
||
if (attempt >= this.opts.maxAttempts) {
|
||
throw new Error(`gas: exhausted ${this.opts.maxAttempts} backoff attempts`);
|
||
}
|
||
const bump = this.opts.backoffFactor ** attempt;
|
||
const scale = (v: bigint) => (v * BigInt(Math.round(bump * 1000))) / 1000n;
|
||
const priority = scale(inputs.priority_fee_wei);
|
||
// max_fee = headroom × base + priority, all backoff-scaled.
|
||
const maxFee = scale(inputs.base_fee_wei * BigInt(this.opts.baseFeeHeadroom)) + priority;
|
||
return {
|
||
max_fee_per_gas: maxFee.toString(),
|
||
max_priority_fee_per_gas: priority.toString(),
|
||
gas_limit: inputs.gas_limit,
|
||
attempts: attempt + 1,
|
||
strategy: "eip1559-exponential-backoff",
|
||
};
|
||
}
|
||
|
||
/** Exponential backoff delay (ms) before resubmitting attempt N. */
|
||
retryDelayMs(attempt: number): number {
|
||
return Math.round(this.opts.baseDelayMs * this.opts.backoffFactor ** attempt);
|
||
}
|
||
|
||
get maxAttempts(): number {
|
||
return this.opts.maxAttempts;
|
||
}
|
||
}
|