@wopr-network/platform-core 1.48.0 → 1.49.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/billing/crypto/__tests__/key-server.test.js +1 -1
- package/dist/billing/crypto/__tests__/unified-checkout.test.d.ts +1 -0
- package/dist/billing/crypto/__tests__/unified-checkout.test.js +63 -0
- package/dist/billing/crypto/__tests__/watcher-service.test.d.ts +1 -0
- package/dist/billing/crypto/__tests__/watcher-service.test.js +174 -0
- package/dist/billing/crypto/__tests__/webhook-confirmations.test.d.ts +1 -0
- package/dist/billing/crypto/__tests__/webhook-confirmations.test.js +304 -0
- package/dist/billing/crypto/btc/__tests__/settler.test.js +1 -0
- package/dist/billing/crypto/btc/__tests__/watcher.test.d.ts +1 -0
- package/dist/billing/crypto/btc/__tests__/watcher.test.js +170 -0
- package/dist/billing/crypto/btc/types.d.ts +3 -1
- package/dist/billing/crypto/btc/watcher.d.ts +6 -1
- package/dist/billing/crypto/btc/watcher.js +24 -8
- package/dist/billing/crypto/charge-store.d.ts +27 -2
- package/dist/billing/crypto/charge-store.js +67 -1
- package/dist/billing/crypto/charge-store.test.js +180 -1
- package/dist/billing/crypto/client.d.ts +2 -0
- package/dist/billing/crypto/cursor-store.d.ts +10 -3
- package/dist/billing/crypto/cursor-store.js +21 -1
- package/dist/billing/crypto/evm/__tests__/eth-checkout.test.js +3 -3
- package/dist/billing/crypto/evm/__tests__/eth-settler.test.js +2 -0
- package/dist/billing/crypto/evm/__tests__/eth-watcher.test.js +33 -6
- package/dist/billing/crypto/evm/__tests__/settler.test.js +2 -0
- package/dist/billing/crypto/evm/__tests__/watcher-confirmations.test.d.ts +1 -0
- package/dist/billing/crypto/evm/__tests__/watcher-confirmations.test.js +144 -0
- package/dist/billing/crypto/evm/__tests__/watcher.test.js +6 -2
- package/dist/billing/crypto/evm/eth-checkout.d.ts +2 -2
- package/dist/billing/crypto/evm/eth-checkout.js +3 -3
- package/dist/billing/crypto/evm/eth-watcher.d.ts +11 -8
- package/dist/billing/crypto/evm/eth-watcher.js +29 -15
- package/dist/billing/crypto/evm/types.d.ts +5 -1
- package/dist/billing/crypto/evm/watcher.d.ts +9 -1
- package/dist/billing/crypto/evm/watcher.js +36 -13
- package/dist/billing/crypto/index.d.ts +3 -3
- package/dist/billing/crypto/index.js +1 -1
- package/dist/billing/crypto/key-server-entry.js +7 -2
- package/dist/billing/crypto/key-server-webhook.d.ts +17 -4
- package/dist/billing/crypto/key-server-webhook.js +76 -15
- package/dist/billing/crypto/key-server.js +18 -7
- package/dist/billing/crypto/oracle/__tests__/chainlink.test.js +4 -4
- package/dist/billing/crypto/oracle/__tests__/coingecko.test.d.ts +1 -0
- package/dist/billing/crypto/oracle/__tests__/coingecko.test.js +65 -0
- package/dist/billing/crypto/oracle/__tests__/composite.test.d.ts +1 -0
- package/dist/billing/crypto/oracle/__tests__/composite.test.js +48 -0
- package/dist/billing/crypto/oracle/__tests__/convert.test.js +27 -17
- package/dist/billing/crypto/oracle/__tests__/fixed.test.js +5 -5
- package/dist/billing/crypto/oracle/chainlink.d.ts +2 -2
- package/dist/billing/crypto/oracle/chainlink.js +11 -10
- package/dist/billing/crypto/oracle/coingecko.d.ts +22 -0
- package/dist/billing/crypto/oracle/coingecko.js +67 -0
- package/dist/billing/crypto/oracle/composite.d.ts +14 -0
- package/dist/billing/crypto/oracle/composite.js +34 -0
- package/dist/billing/crypto/oracle/convert.d.ts +17 -7
- package/dist/billing/crypto/oracle/convert.js +26 -13
- package/dist/billing/crypto/oracle/fixed.d.ts +2 -2
- package/dist/billing/crypto/oracle/fixed.js +9 -7
- package/dist/billing/crypto/oracle/index.d.ts +4 -0
- package/dist/billing/crypto/oracle/index.js +3 -0
- package/dist/billing/crypto/oracle/types.d.ts +12 -3
- package/dist/billing/crypto/oracle/types.js +7 -1
- package/dist/billing/crypto/types.d.ts +16 -0
- package/dist/billing/crypto/unified-checkout.d.ts +10 -19
- package/dist/billing/crypto/unified-checkout.js +17 -131
- package/dist/billing/crypto/watcher-service.d.ts +22 -2
- package/dist/billing/crypto/watcher-service.js +71 -30
- package/dist/db/schema/crypto.d.ts +68 -0
- package/dist/db/schema/crypto.js +8 -0
- package/dist/monetization/crypto/__tests__/webhook.test.js +2 -1
- package/drizzle/migrations/0016_charge_progress_columns.sql +4 -0
- package/drizzle/migrations/meta/_journal.json +7 -0
- package/package.json +1 -1
- package/src/billing/crypto/__tests__/key-server.test.ts +1 -1
- package/src/billing/crypto/__tests__/unified-checkout.test.ts +83 -0
- package/src/billing/crypto/__tests__/watcher-service.test.ts +242 -0
- package/src/billing/crypto/__tests__/webhook-confirmations.test.ts +367 -0
- package/src/billing/crypto/btc/__tests__/settler.test.ts +1 -0
- package/src/billing/crypto/btc/__tests__/watcher.test.ts +201 -0
- package/src/billing/crypto/btc/types.ts +3 -1
- package/src/billing/crypto/btc/watcher.ts +26 -8
- package/src/billing/crypto/charge-store.test.ts +204 -1
- package/src/billing/crypto/charge-store.ts +86 -2
- package/src/billing/crypto/client.ts +2 -0
- package/src/billing/crypto/cursor-store.ts +31 -3
- package/src/billing/crypto/evm/__tests__/eth-checkout.test.ts +3 -3
- package/src/billing/crypto/evm/__tests__/eth-settler.test.ts +2 -0
- package/src/billing/crypto/evm/__tests__/eth-watcher.test.ts +33 -6
- package/src/billing/crypto/evm/__tests__/settler.test.ts +2 -0
- package/src/billing/crypto/evm/__tests__/watcher-confirmations.test.ts +176 -0
- package/src/billing/crypto/evm/__tests__/watcher.test.ts +6 -2
- package/src/billing/crypto/evm/eth-checkout.ts +5 -5
- package/src/billing/crypto/evm/eth-watcher.ts +36 -16
- package/src/billing/crypto/evm/types.ts +5 -1
- package/src/billing/crypto/evm/watcher.ts +39 -13
- package/src/billing/crypto/index.ts +12 -3
- package/src/billing/crypto/key-server-entry.ts +7 -2
- package/src/billing/crypto/key-server-webhook.ts +92 -21
- package/src/billing/crypto/key-server.ts +17 -7
- package/src/billing/crypto/oracle/__tests__/chainlink.test.ts +4 -4
- package/src/billing/crypto/oracle/__tests__/coingecko.test.ts +75 -0
- package/src/billing/crypto/oracle/__tests__/composite.test.ts +61 -0
- package/src/billing/crypto/oracle/__tests__/convert.test.ts +29 -17
- package/src/billing/crypto/oracle/__tests__/fixed.test.ts +5 -5
- package/src/billing/crypto/oracle/chainlink.ts +11 -10
- package/src/billing/crypto/oracle/coingecko.ts +92 -0
- package/src/billing/crypto/oracle/composite.ts +35 -0
- package/src/billing/crypto/oracle/convert.ts +28 -13
- package/src/billing/crypto/oracle/fixed.ts +9 -7
- package/src/billing/crypto/oracle/index.ts +4 -0
- package/src/billing/crypto/oracle/types.ts +16 -3
- package/src/billing/crypto/types.ts +18 -0
- package/src/billing/crypto/unified-checkout.ts +22 -181
- package/src/billing/crypto/watcher-service.ts +85 -32
- package/src/db/schema/crypto.ts +8 -0
- package/src/monetization/crypto/__tests__/webhook.test.ts +2 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export * from "./btc/index.js";
|
|
2
|
-
export type { CryptoChargeRecord, CryptoDepositChargeInput, ICryptoChargeRepository } from "./charge-store.js";
|
|
2
|
+
export type { CryptoChargeProgressUpdate, CryptoChargeRecord, CryptoDepositChargeInput, ICryptoChargeRepository, } from "./charge-store.js";
|
|
3
3
|
export { CryptoChargeRepository, DrizzleCryptoChargeRepository } from "./charge-store.js";
|
|
4
4
|
export type { ChainInfo, ChargeStatus, CreateChargeResult, CryptoConfig, CryptoServiceConfig, DeriveAddressResult, } from "./client.js";
|
|
5
5
|
export { CryptoServiceClient, loadCryptoConfig } from "./client.js";
|
|
@@ -9,10 +9,10 @@ export * from "./evm/index.js";
|
|
|
9
9
|
export type { KeyServerDeps } from "./key-server.js";
|
|
10
10
|
export { createKeyServerApp } from "./key-server.js";
|
|
11
11
|
export type { KeyServerWebhookDeps as CryptoWebhookDeps, KeyServerWebhookPayload as CryptoWebhookPayload, KeyServerWebhookResult as CryptoWebhookResult, } from "./key-server-webhook.js";
|
|
12
|
-
export { handleKeyServerWebhook, handleKeyServerWebhook as handleCryptoWebhook } from "./key-server-webhook.js";
|
|
12
|
+
export { handleKeyServerWebhook, handleKeyServerWebhook as handleCryptoWebhook, normalizeStatus, } from "./key-server-webhook.js";
|
|
13
13
|
export * from "./oracle/index.js";
|
|
14
14
|
export type { IPaymentMethodStore, PaymentMethodRecord } from "./payment-method-store.js";
|
|
15
15
|
export { DrizzlePaymentMethodStore } from "./payment-method-store.js";
|
|
16
|
-
export type { CryptoPaymentState } from "./types.js";
|
|
16
|
+
export type { CryptoCharge, CryptoChargeStatus, CryptoPaymentState } from "./types.js";
|
|
17
17
|
export type { UnifiedCheckoutDeps, UnifiedCheckoutResult } from "./unified-checkout.js";
|
|
18
18
|
export { createUnifiedCheckout, MIN_CHECKOUT_USD as MIN_PAYMENT_USD, MIN_CHECKOUT_USD } from "./unified-checkout.js";
|
|
@@ -4,7 +4,7 @@ export { CryptoServiceClient, loadCryptoConfig } from "./client.js";
|
|
|
4
4
|
export { DrizzleWatcherCursorStore } from "./cursor-store.js";
|
|
5
5
|
export * from "./evm/index.js";
|
|
6
6
|
export { createKeyServerApp } from "./key-server.js";
|
|
7
|
-
export { handleKeyServerWebhook, handleKeyServerWebhook as handleCryptoWebhook } from "./key-server-webhook.js";
|
|
7
|
+
export { handleKeyServerWebhook, handleKeyServerWebhook as handleCryptoWebhook, normalizeStatus, } from "./key-server-webhook.js";
|
|
8
8
|
export * from "./oracle/index.js";
|
|
9
9
|
export { DrizzlePaymentMethodStore } from "./payment-method-store.js";
|
|
10
10
|
export { createUnifiedCheckout, MIN_CHECKOUT_USD as MIN_PAYMENT_USD, MIN_CHECKOUT_USD } from "./unified-checkout.js";
|
|
@@ -17,6 +17,8 @@ import { DrizzleWatcherCursorStore } from "./cursor-store.js";
|
|
|
17
17
|
import { createRpcCaller } from "./evm/watcher.js";
|
|
18
18
|
import { createKeyServerApp } from "./key-server.js";
|
|
19
19
|
import { ChainlinkOracle } from "./oracle/chainlink.js";
|
|
20
|
+
import { CoinGeckoOracle } from "./oracle/coingecko.js";
|
|
21
|
+
import { CompositeOracle } from "./oracle/composite.js";
|
|
20
22
|
import { FixedPriceOracle } from "./oracle/fixed.js";
|
|
21
23
|
import { DrizzlePaymentMethodStore } from "./payment-method-store.js";
|
|
22
24
|
import { startWatchers } from "./watcher-service.js";
|
|
@@ -41,10 +43,13 @@ async function main() {
|
|
|
41
43
|
const db = drizzle(pool, { schema });
|
|
42
44
|
const chargeStore = new DrizzleCryptoChargeRepository(db);
|
|
43
45
|
const methodStore = new DrizzlePaymentMethodStore(db);
|
|
44
|
-
// Chainlink on-chain
|
|
45
|
-
|
|
46
|
+
// Composite oracle: Chainlink on-chain (BTC, ETH on Base) + CoinGecko fallback (DOGE, LTC, etc.)
|
|
47
|
+
// Every volatile asset needs reliable USD pricing — the ledger credits nanodollars.
|
|
48
|
+
const chainlink = BASE_RPC_URL
|
|
46
49
|
? new ChainlinkOracle({ rpcCall: createRpcCaller(BASE_RPC_URL) })
|
|
47
50
|
: new FixedPriceOracle();
|
|
51
|
+
const coingecko = new CoinGeckoOracle();
|
|
52
|
+
const oracle = new CompositeOracle(chainlink, coingecko);
|
|
48
53
|
const app = createKeyServerApp({
|
|
49
54
|
db,
|
|
50
55
|
chargeStore,
|
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
import type { ILedger } from "../../credits/ledger.js";
|
|
2
2
|
import type { IWebhookSeenRepository } from "../webhook-seen-repository.js";
|
|
3
3
|
import type { ICryptoChargeRepository } from "./charge-store.js";
|
|
4
|
+
import type { CryptoChargeStatus } from "./types.js";
|
|
4
5
|
export interface KeyServerWebhookPayload {
|
|
5
6
|
chargeId: string;
|
|
6
7
|
chain: string;
|
|
7
8
|
address: string;
|
|
8
|
-
|
|
9
|
+
/** @deprecated Use amountReceivedCents instead. Kept for one release cycle. */
|
|
10
|
+
amountUsdCents?: number;
|
|
11
|
+
amountReceivedCents?: number;
|
|
9
12
|
status: string;
|
|
10
13
|
txHash?: string;
|
|
11
14
|
amountReceived?: string;
|
|
12
15
|
confirmations?: number;
|
|
16
|
+
confirmationsRequired?: number;
|
|
13
17
|
}
|
|
14
18
|
export interface KeyServerWebhookDeps {
|
|
15
19
|
chargeStore: ICryptoChargeRepository;
|
|
@@ -23,11 +27,20 @@ export interface KeyServerWebhookResult {
|
|
|
23
27
|
tenant?: string;
|
|
24
28
|
creditedCents?: number;
|
|
25
29
|
reactivatedBots?: string[];
|
|
30
|
+
status?: CryptoChargeStatus;
|
|
31
|
+
confirmations?: number;
|
|
32
|
+
confirmationsRequired?: number;
|
|
26
33
|
}
|
|
27
34
|
/**
|
|
28
|
-
*
|
|
35
|
+
* Map legacy/watcher status strings to canonical CryptoChargeStatus.
|
|
36
|
+
* Accepts both old BTCPay-style ("Settled", "Processing") and new canonical ("confirmed", "partial").
|
|
37
|
+
*/
|
|
38
|
+
export declare function normalizeStatus(raw: string): CryptoChargeStatus;
|
|
39
|
+
/**
|
|
40
|
+
* Process a payment webhook from the crypto key server.
|
|
29
41
|
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
42
|
+
* Idempotency: deduplicate by chargeId + status + confirmations so that
|
|
43
|
+
* multiple progress updates (0→1→2→...→6 confirmations) each get through,
|
|
44
|
+
* but exact duplicates are rejected.
|
|
32
45
|
*/
|
|
33
46
|
export declare function handleKeyServerWebhook(deps: KeyServerWebhookDeps, payload: KeyServerWebhookPayload): Promise<KeyServerWebhookResult>;
|
|
@@ -1,32 +1,76 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Key Server webhook handler — processes payment
|
|
2
|
+
* Key Server webhook handler — processes payment events from the
|
|
3
3
|
* centralized crypto key server.
|
|
4
4
|
*
|
|
5
|
+
* Called on EVERY status update (not just terminal):
|
|
6
|
+
* - "partial" / "Processing" → update progress, no credit
|
|
7
|
+
* - "confirmed" / "Settled" → update progress + credit ledger
|
|
8
|
+
* - "expired" / "failed" → update progress, no credit
|
|
9
|
+
*
|
|
5
10
|
* Payload shape (from watcher-service.ts):
|
|
6
11
|
* {
|
|
7
12
|
* chargeId: "btc:bc1q...",
|
|
8
13
|
* chain: "bitcoin",
|
|
9
14
|
* address: "bc1q...",
|
|
10
|
-
*
|
|
15
|
+
* amountReceivedCents: 5000,
|
|
11
16
|
* status: "confirmed",
|
|
12
17
|
* txHash: "abc123...",
|
|
13
18
|
* amountReceived: "50000 sats",
|
|
14
|
-
* confirmations: 6
|
|
19
|
+
* confirmations: 6,
|
|
20
|
+
* confirmationsRequired: 6
|
|
15
21
|
* }
|
|
16
22
|
*
|
|
17
23
|
* Replaces handleCryptoWebhook() for products using the key server.
|
|
18
24
|
*/
|
|
19
25
|
import { Credit } from "../../credits/credit.js";
|
|
20
26
|
/**
|
|
21
|
-
*
|
|
27
|
+
* Map legacy/watcher status strings to canonical CryptoChargeStatus.
|
|
28
|
+
* Accepts both old BTCPay-style ("Settled", "Processing") and new canonical ("confirmed", "partial").
|
|
29
|
+
*/
|
|
30
|
+
export function normalizeStatus(raw) {
|
|
31
|
+
switch (raw) {
|
|
32
|
+
case "confirmed":
|
|
33
|
+
case "Settled":
|
|
34
|
+
case "InvoiceSettled":
|
|
35
|
+
return "confirmed";
|
|
36
|
+
case "partial":
|
|
37
|
+
case "Processing":
|
|
38
|
+
case "InvoiceProcessing":
|
|
39
|
+
case "InvoiceReceivedPayment":
|
|
40
|
+
return "partial";
|
|
41
|
+
case "expired":
|
|
42
|
+
case "Expired":
|
|
43
|
+
case "InvoiceExpired":
|
|
44
|
+
return "expired";
|
|
45
|
+
case "failed":
|
|
46
|
+
case "Invalid":
|
|
47
|
+
case "InvoiceInvalid":
|
|
48
|
+
return "failed";
|
|
49
|
+
case "pending":
|
|
50
|
+
case "New":
|
|
51
|
+
case "InvoiceCreated":
|
|
52
|
+
return "pending";
|
|
53
|
+
default:
|
|
54
|
+
return "pending";
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Process a payment webhook from the crypto key server.
|
|
22
59
|
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
60
|
+
* Idempotency: deduplicate by chargeId + status + confirmations so that
|
|
61
|
+
* multiple progress updates (0→1→2→...→6 confirmations) each get through,
|
|
62
|
+
* but exact duplicates are rejected.
|
|
25
63
|
*/
|
|
26
64
|
export async function handleKeyServerWebhook(deps, payload) {
|
|
27
65
|
const { chargeStore, creditLedger } = deps;
|
|
28
|
-
|
|
29
|
-
const
|
|
66
|
+
const status = normalizeStatus(payload.status);
|
|
67
|
+
const confirmations = payload.confirmations ?? 0;
|
|
68
|
+
const confirmationsRequired = payload.confirmationsRequired ?? 1;
|
|
69
|
+
// Support deprecated amountUsdCents field as fallback
|
|
70
|
+
const amountReceivedCents = payload.amountReceivedCents ?? payload.amountUsdCents ?? 0;
|
|
71
|
+
// Replay guard: deduplicate by chargeId + status + confirmations
|
|
72
|
+
// This allows multiple progress updates for the same charge
|
|
73
|
+
const dedupeKey = `ks:${payload.chargeId}:${status}:${confirmations}`;
|
|
30
74
|
if (await deps.replayGuard.isDuplicate(dedupeKey, "crypto")) {
|
|
31
75
|
return { handled: true, duplicate: true };
|
|
32
76
|
}
|
|
@@ -35,14 +79,29 @@ export async function handleKeyServerWebhook(deps, payload) {
|
|
|
35
79
|
if (!charge) {
|
|
36
80
|
return { handled: false };
|
|
37
81
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
82
|
+
// Always update progress on every webhook
|
|
83
|
+
await chargeStore.updateProgress(payload.chargeId, {
|
|
84
|
+
status,
|
|
85
|
+
amountReceivedCents,
|
|
86
|
+
confirmations,
|
|
87
|
+
confirmationsRequired,
|
|
88
|
+
txHash: payload.txHash,
|
|
89
|
+
});
|
|
90
|
+
// Also call deprecated updateStatus for backward compat with downstream consumers
|
|
91
|
+
const legacyStatusMap = {
|
|
92
|
+
pending: "New",
|
|
93
|
+
partial: "Processing",
|
|
94
|
+
confirmed: "Settled",
|
|
95
|
+
expired: "Expired",
|
|
96
|
+
failed: "Invalid",
|
|
97
|
+
};
|
|
98
|
+
await chargeStore.updateStatus(payload.chargeId, legacyStatusMap[status], charge.token ?? undefined, payload.amountReceived);
|
|
99
|
+
if (status === "confirmed") {
|
|
41
100
|
// Idempotency: check ledger referenceId (atomic, same as BTCPay handler)
|
|
42
101
|
const creditRef = `crypto:${payload.chargeId}`;
|
|
43
102
|
if (await creditLedger.hasReferenceId(creditRef)) {
|
|
44
103
|
await deps.replayGuard.markSeen(dedupeKey, "crypto");
|
|
45
|
-
return { handled: true, duplicate: true, tenant: charge.tenantId };
|
|
104
|
+
return { handled: true, duplicate: true, tenant: charge.tenantId, status, confirmations, confirmationsRequired };
|
|
46
105
|
}
|
|
47
106
|
// Credit the original USD amount requested.
|
|
48
107
|
// charge.amountUsdCents is integer cents. Credit.fromCents() → nanodollars.
|
|
@@ -64,10 +123,12 @@ export async function handleKeyServerWebhook(deps, payload) {
|
|
|
64
123
|
tenant: charge.tenantId,
|
|
65
124
|
creditedCents: charge.amountUsdCents,
|
|
66
125
|
reactivatedBots,
|
|
126
|
+
status,
|
|
127
|
+
confirmations,
|
|
128
|
+
confirmationsRequired,
|
|
67
129
|
};
|
|
68
130
|
}
|
|
69
|
-
// Non-confirmed status —
|
|
70
|
-
await chargeStore.updateStatus(payload.chargeId, payload.status, charge.token ?? undefined, payload.amountReceived);
|
|
131
|
+
// Non-confirmed status — progress already updated above, no credit
|
|
71
132
|
await deps.replayGuard.markSeen(dedupeKey, "crypto");
|
|
72
|
-
return { handled: true, tenant: charge.tenantId };
|
|
133
|
+
return { handled: true, tenant: charge.tenantId, status, confirmations, confirmationsRequired };
|
|
73
134
|
}
|
|
@@ -13,6 +13,7 @@ import { derivedAddresses, pathAllocations, paymentMethods } from "../../db/sche
|
|
|
13
13
|
import { deriveAddress, deriveP2pkhAddress } from "./btc/address-gen.js";
|
|
14
14
|
import { deriveDepositAddress } from "./evm/address-gen.js";
|
|
15
15
|
import { centsToNative } from "./oracle/convert.js";
|
|
16
|
+
import { AssetNotSupportedError } from "./oracle/types.js";
|
|
16
17
|
/**
|
|
17
18
|
* Derive the next unused address for a chain.
|
|
18
19
|
* Atomically increments next_index and records address in a single transaction.
|
|
@@ -120,14 +121,24 @@ export function createKeyServerApp(deps) {
|
|
|
120
121
|
// Compute expected crypto amount in native base units.
|
|
121
122
|
// Price is locked NOW — this is what the user must send.
|
|
122
123
|
let expectedAmount;
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
124
|
+
const feedAddress = method.oracleAddress ? method.oracleAddress : undefined;
|
|
125
|
+
try {
|
|
126
|
+
// Try oracle pricing (Chainlink for BTC/ETH, CoinGecko for DOGE/LTC).
|
|
127
|
+
// feedAddress is a hint for Chainlink — undefined is fine, CompositeOracle
|
|
128
|
+
// falls through to CoinGecko or built-in feed maps.
|
|
129
|
+
const { priceMicros } = await deps.oracle.getPrice(token, feedAddress);
|
|
130
|
+
expectedAmount = centsToNative(amountUsdCents, priceMicros, method.decimals);
|
|
127
131
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
132
|
+
catch (err) {
|
|
133
|
+
if (err instanceof AssetNotSupportedError) {
|
|
134
|
+
// No oracle knows this token (e.g. USDC, DAI) — stablecoin 1:1 USD.
|
|
135
|
+
expectedAmount = (BigInt(amountUsdCents) * 10n ** BigInt(method.decimals)) / 100n;
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
// Transient oracle failure (network, rate limit, stale feed).
|
|
139
|
+
// Reject the charge — silently pricing BTC at $1 would be catastrophic.
|
|
140
|
+
return c.json({ error: `Price oracle unavailable for ${token}: ${err.message}` }, 503);
|
|
141
|
+
}
|
|
131
142
|
}
|
|
132
143
|
const referenceId = `${token.toLowerCase()}:${address.toLowerCase()}`;
|
|
133
144
|
await deps.chargeStore.createStablecoinCharge({
|
|
@@ -22,7 +22,7 @@ describe("ChainlinkOracle", () => {
|
|
|
22
22
|
const rpc = vi.fn().mockResolvedValue(encodeRoundData(350000000000n, nowSec));
|
|
23
23
|
const oracle = new ChainlinkOracle({ rpcCall: rpc });
|
|
24
24
|
const result = await oracle.getPrice("ETH");
|
|
25
|
-
expect(result.
|
|
25
|
+
expect(result.priceMicros).toBe(3_500_000_000); // $3,500.00
|
|
26
26
|
expect(result.updatedAt).toBeInstanceOf(Date);
|
|
27
27
|
expect(rpc).toHaveBeenCalledWith("eth_call", [
|
|
28
28
|
{ to: "0x71041dddad3595F9CEd3DcCFBe3D1F4b0a16Bb70", data: "0xfeaf968c" },
|
|
@@ -34,14 +34,14 @@ describe("ChainlinkOracle", () => {
|
|
|
34
34
|
const rpc = vi.fn().mockResolvedValue(encodeRoundData(6500000000000n, nowSec));
|
|
35
35
|
const oracle = new ChainlinkOracle({ rpcCall: rpc });
|
|
36
36
|
const result = await oracle.getPrice("BTC");
|
|
37
|
-
expect(result.
|
|
37
|
+
expect(result.priceMicros).toBe(65_000_000_000); // $65,000.00
|
|
38
38
|
});
|
|
39
39
|
it("handles fractional dollar prices correctly", async () => {
|
|
40
40
|
// ETH at $3,456.78 → answer = 345_678_000_000
|
|
41
41
|
const rpc = vi.fn().mockResolvedValue(encodeRoundData(345678000000n, nowSec));
|
|
42
42
|
const oracle = new ChainlinkOracle({ rpcCall: rpc });
|
|
43
43
|
const result = await oracle.getPrice("ETH");
|
|
44
|
-
expect(result.
|
|
44
|
+
expect(result.priceMicros).toBe(3_456_780_000); // $3,456.78
|
|
45
45
|
});
|
|
46
46
|
it("rejects stale prices", async () => {
|
|
47
47
|
const staleTime = nowSec - 7200; // 2 hours ago
|
|
@@ -78,6 +78,6 @@ describe("ChainlinkOracle", () => {
|
|
|
78
78
|
// 60-minute threshold → fresh
|
|
79
79
|
const relaxed = new ChainlinkOracle({ rpcCall: rpc, maxStalenessMs: 60 * 60 * 1000 });
|
|
80
80
|
const result = await relaxed.getPrice("ETH");
|
|
81
|
-
expect(result.
|
|
81
|
+
expect(result.priceMicros).toBe(3_500_000_000);
|
|
82
82
|
});
|
|
83
83
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { CoinGeckoOracle } from "../coingecko.js";
|
|
3
|
+
describe("CoinGeckoOracle", () => {
|
|
4
|
+
const mockFetch = (price) => vi.fn().mockResolvedValue({
|
|
5
|
+
ok: true,
|
|
6
|
+
json: () => Promise.resolve({ bitcoin: { usd: price } }),
|
|
7
|
+
});
|
|
8
|
+
it("returns price in microdollars from CoinGecko API", async () => {
|
|
9
|
+
const oracle = new CoinGeckoOracle({ fetchFn: mockFetch(84_532.17) });
|
|
10
|
+
const result = await oracle.getPrice("BTC");
|
|
11
|
+
expect(result.priceMicros).toBe(84_532_170_000);
|
|
12
|
+
expect(result.updatedAt).toBeInstanceOf(Date);
|
|
13
|
+
});
|
|
14
|
+
it("caches prices within TTL", async () => {
|
|
15
|
+
const fn = mockFetch(84_532.17);
|
|
16
|
+
const oracle = new CoinGeckoOracle({ fetchFn: fn, cacheTtlMs: 60_000 });
|
|
17
|
+
await oracle.getPrice("BTC");
|
|
18
|
+
await oracle.getPrice("BTC");
|
|
19
|
+
expect(fn).toHaveBeenCalledTimes(1);
|
|
20
|
+
});
|
|
21
|
+
it("re-fetches after cache expires", async () => {
|
|
22
|
+
const fn = mockFetch(84_532.17);
|
|
23
|
+
const oracle = new CoinGeckoOracle({ fetchFn: fn, cacheTtlMs: 0 });
|
|
24
|
+
await oracle.getPrice("BTC");
|
|
25
|
+
await oracle.getPrice("BTC");
|
|
26
|
+
expect(fn).toHaveBeenCalledTimes(2);
|
|
27
|
+
});
|
|
28
|
+
it("throws for unknown asset", async () => {
|
|
29
|
+
const oracle = new CoinGeckoOracle({ fetchFn: mockFetch(100) });
|
|
30
|
+
await expect(oracle.getPrice("UNKNOWN")).rejects.toThrow("No price oracle supports asset: UNKNOWN");
|
|
31
|
+
});
|
|
32
|
+
it("throws on API error", async () => {
|
|
33
|
+
const fn = vi.fn().mockResolvedValue({ ok: false, status: 429, statusText: "Too Many Requests" });
|
|
34
|
+
const oracle = new CoinGeckoOracle({ fetchFn: fn });
|
|
35
|
+
await expect(oracle.getPrice("BTC")).rejects.toThrow("CoinGecko API error");
|
|
36
|
+
});
|
|
37
|
+
it("throws on zero price", async () => {
|
|
38
|
+
const fn = vi.fn().mockResolvedValue({
|
|
39
|
+
ok: true,
|
|
40
|
+
json: () => Promise.resolve({ bitcoin: { usd: 0 } }),
|
|
41
|
+
});
|
|
42
|
+
const oracle = new CoinGeckoOracle({ fetchFn: fn });
|
|
43
|
+
await expect(oracle.getPrice("BTC")).rejects.toThrow("Invalid CoinGecko price");
|
|
44
|
+
});
|
|
45
|
+
it("resolves DOGE via coingecko ID mapping", async () => {
|
|
46
|
+
const fn = vi.fn().mockResolvedValue({
|
|
47
|
+
ok: true,
|
|
48
|
+
json: () => Promise.resolve({ dogecoin: { usd: 0.1742 } }),
|
|
49
|
+
});
|
|
50
|
+
const oracle = new CoinGeckoOracle({ fetchFn: fn });
|
|
51
|
+
const result = await oracle.getPrice("DOGE");
|
|
52
|
+
expect(result.priceMicros).toBe(174_200);
|
|
53
|
+
expect(fn).toHaveBeenCalledWith(expect.stringContaining("ids=dogecoin"));
|
|
54
|
+
});
|
|
55
|
+
it("resolves LTC via coingecko ID mapping", async () => {
|
|
56
|
+
const fn = vi.fn().mockResolvedValue({
|
|
57
|
+
ok: true,
|
|
58
|
+
json: () => Promise.resolve({ litecoin: { usd: 92.45 } }),
|
|
59
|
+
});
|
|
60
|
+
const oracle = new CoinGeckoOracle({ fetchFn: fn });
|
|
61
|
+
const result = await oracle.getPrice("LTC");
|
|
62
|
+
expect(result.priceMicros).toBe(92_450_000);
|
|
63
|
+
expect(fn).toHaveBeenCalledWith(expect.stringContaining("ids=litecoin"));
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { CompositeOracle } from "../composite.js";
|
|
3
|
+
function mockOracle(priceMicros) {
|
|
4
|
+
return { getPrice: vi.fn().mockResolvedValue({ priceMicros, updatedAt: new Date() }) };
|
|
5
|
+
}
|
|
6
|
+
function failingOracle(msg = "no feed") {
|
|
7
|
+
return { getPrice: vi.fn().mockRejectedValue(new Error(msg)) };
|
|
8
|
+
}
|
|
9
|
+
describe("CompositeOracle", () => {
|
|
10
|
+
it("uses primary when feedAddress is provided and primary succeeds", async () => {
|
|
11
|
+
const primary = mockOracle(8_500_000);
|
|
12
|
+
const fallback = mockOracle(8_400_000);
|
|
13
|
+
const oracle = new CompositeOracle(primary, fallback);
|
|
14
|
+
const result = await oracle.getPrice("BTC", "0x64c911996D3c6aC71f9b455B1E8E7266BcbD848F");
|
|
15
|
+
expect(result.priceMicros).toBe(8_500_000);
|
|
16
|
+
expect(primary.getPrice).toHaveBeenCalledWith("BTC", "0x64c911996D3c6aC71f9b455B1E8E7266BcbD848F");
|
|
17
|
+
expect(fallback.getPrice).not.toHaveBeenCalled();
|
|
18
|
+
});
|
|
19
|
+
it("falls back when primary fails with feedAddress", async () => {
|
|
20
|
+
const primary = failingOracle("stale");
|
|
21
|
+
const fallback = mockOracle(8_400_000);
|
|
22
|
+
const oracle = new CompositeOracle(primary, fallback);
|
|
23
|
+
const result = await oracle.getPrice("BTC", "0x64c911996D3c6aC71f9b455B1E8E7266BcbD848F");
|
|
24
|
+
expect(result.priceMicros).toBe(8_400_000);
|
|
25
|
+
});
|
|
26
|
+
it("tries primary without feed, then falls back for unknown assets", async () => {
|
|
27
|
+
const primary = failingOracle("No price feed for asset: DOGE");
|
|
28
|
+
const fallback = mockOracle(17);
|
|
29
|
+
const oracle = new CompositeOracle(primary, fallback);
|
|
30
|
+
const result = await oracle.getPrice("DOGE");
|
|
31
|
+
expect(result.priceMicros).toBe(17);
|
|
32
|
+
});
|
|
33
|
+
it("uses primary built-in feeds for BTC/ETH without explicit feedAddress", async () => {
|
|
34
|
+
const primary = mockOracle(8_500_000);
|
|
35
|
+
const fallback = mockOracle(8_400_000);
|
|
36
|
+
const oracle = new CompositeOracle(primary, fallback);
|
|
37
|
+
const result = await oracle.getPrice("BTC");
|
|
38
|
+
expect(result.priceMicros).toBe(8_500_000);
|
|
39
|
+
expect(primary.getPrice).toHaveBeenCalledWith("BTC");
|
|
40
|
+
expect(fallback.getPrice).not.toHaveBeenCalled();
|
|
41
|
+
});
|
|
42
|
+
it("propagates fallback errors when both fail", async () => {
|
|
43
|
+
const primary = failingOracle("no chainlink");
|
|
44
|
+
const fallback = failingOracle("no coingecko");
|
|
45
|
+
const oracle = new CompositeOracle(primary, fallback);
|
|
46
|
+
await expect(oracle.getPrice("UNKNOWN")).rejects.toThrow("no coingecko");
|
|
47
|
+
});
|
|
48
|
+
});
|
|
@@ -2,50 +2,60 @@ import { describe, expect, it } from "vitest";
|
|
|
2
2
|
import { centsToNative, nativeToCents } from "../convert.js";
|
|
3
3
|
describe("centsToNative", () => {
|
|
4
4
|
it("converts $50 to ETH wei at $3,500", () => {
|
|
5
|
-
// 5000 cents × 10^18 /
|
|
6
|
-
const wei = centsToNative(5000,
|
|
5
|
+
// 5000 cents × 10000 × 10^18 / 3_500_000_000 micros = 14285714285714285n wei
|
|
6
|
+
const wei = centsToNative(5000, 3_500_000_000, 18);
|
|
7
7
|
expect(wei).toBe(14285714285714285n);
|
|
8
8
|
});
|
|
9
9
|
it("converts $50 to BTC sats at $65,000", () => {
|
|
10
|
-
// 5000 cents × 10^8 /
|
|
11
|
-
const sats = centsToNative(5000,
|
|
10
|
+
// 5000 cents × 10000 × 10^8 / 65_000_000_000 micros = 76923n sats
|
|
11
|
+
const sats = centsToNative(5000, 65_000_000_000, 8);
|
|
12
12
|
expect(sats).toBe(76923n);
|
|
13
13
|
});
|
|
14
|
+
it("converts $50 to DOGE at $0.094147", () => {
|
|
15
|
+
// 5000 cents × 10000 × 10^8 / 94_147 micros = 53_107_898_982n base units (531.08 DOGE)
|
|
16
|
+
const dogeUnits = centsToNative(5000, 94_147, 8);
|
|
17
|
+
expect(Number(dogeUnits) / 1e8).toBeCloseTo(531.08, 0);
|
|
18
|
+
});
|
|
19
|
+
it("converts $50 to LTC at $55.79", () => {
|
|
20
|
+
// 5000 cents × 10000 × 10^8 / 55_790_000 micros = 89_622_512n base units (0.896 LTC)
|
|
21
|
+
const ltcUnits = centsToNative(5000, 55_790_000, 8);
|
|
22
|
+
expect(Number(ltcUnits) / 1e8).toBeCloseTo(0.896, 2);
|
|
23
|
+
});
|
|
14
24
|
it("converts $100 to ETH wei at $2,000", () => {
|
|
15
|
-
// 10000 cents × 10^18 /
|
|
16
|
-
const wei = centsToNative(10_000,
|
|
25
|
+
// 10000 cents × 10000 × 10^18 / 2_000_000_000 micros = 50_000_000_000_000_000n (0.05 ETH)
|
|
26
|
+
const wei = centsToNative(10_000, 2_000_000_000, 18);
|
|
17
27
|
expect(wei).toBe(50000000000000000n);
|
|
18
28
|
});
|
|
19
29
|
it("rejects non-integer amountCents", () => {
|
|
20
|
-
expect(() => centsToNative(50.5,
|
|
30
|
+
expect(() => centsToNative(50.5, 3_500_000_000, 18)).toThrow("positive integer");
|
|
21
31
|
});
|
|
22
32
|
it("rejects zero amountCents", () => {
|
|
23
|
-
expect(() => centsToNative(0,
|
|
33
|
+
expect(() => centsToNative(0, 3_500_000_000, 18)).toThrow("positive integer");
|
|
24
34
|
});
|
|
25
|
-
it("rejects zero
|
|
35
|
+
it("rejects zero priceMicros", () => {
|
|
26
36
|
expect(() => centsToNative(5000, 0, 18)).toThrow("positive integer");
|
|
27
37
|
});
|
|
28
38
|
it("rejects negative decimals", () => {
|
|
29
|
-
expect(() => centsToNative(5000,
|
|
39
|
+
expect(() => centsToNative(5000, 3_500_000_000, -1)).toThrow("non-negative integer");
|
|
30
40
|
});
|
|
31
41
|
});
|
|
32
42
|
describe("nativeToCents", () => {
|
|
33
43
|
it("converts ETH wei back to cents at $3,500", () => {
|
|
34
|
-
// 14285714285714285n
|
|
35
|
-
const cents = nativeToCents(14285714285714285n,
|
|
36
|
-
expect(cents).toBe(4999);
|
|
44
|
+
// 14285714285714285n × 3_500_000_000 / (10000 × 10^18) = 4999 cents (truncated)
|
|
45
|
+
const cents = nativeToCents(14285714285714285n, 3_500_000_000, 18);
|
|
46
|
+
expect(cents).toBe(4999);
|
|
37
47
|
});
|
|
38
48
|
it("converts BTC sats back to cents at $65,000", () => {
|
|
39
|
-
// 76923n
|
|
40
|
-
const cents = nativeToCents(76923n,
|
|
49
|
+
// 76923n × 65_000_000_000 / (10000 × 10^8) = 4999 cents
|
|
50
|
+
const cents = nativeToCents(76923n, 65_000_000_000, 8);
|
|
41
51
|
expect(cents).toBe(4999);
|
|
42
52
|
});
|
|
43
53
|
it("exact round-trip for clean division", () => {
|
|
44
54
|
// 0.05 ETH at $2,000 = $100
|
|
45
|
-
const cents = nativeToCents(50000000000000000n,
|
|
55
|
+
const cents = nativeToCents(50000000000000000n, 2_000_000_000, 18);
|
|
46
56
|
expect(cents).toBe(10_000); // $100.00
|
|
47
57
|
});
|
|
48
58
|
it("rejects negative rawAmount", () => {
|
|
49
|
-
expect(() => nativeToCents(-1n,
|
|
59
|
+
expect(() => nativeToCents(-1n, 3_500_000_000, 18)).toThrow("non-negative");
|
|
50
60
|
});
|
|
51
61
|
});
|
|
@@ -4,17 +4,17 @@ describe("FixedPriceOracle", () => {
|
|
|
4
4
|
it("returns default ETH price", async () => {
|
|
5
5
|
const oracle = new FixedPriceOracle();
|
|
6
6
|
const result = await oracle.getPrice("ETH");
|
|
7
|
-
expect(result.
|
|
7
|
+
expect(result.priceMicros).toBe(3_500_000_000); // $3,500
|
|
8
8
|
expect(result.updatedAt).toBeInstanceOf(Date);
|
|
9
9
|
});
|
|
10
10
|
it("returns default BTC price", async () => {
|
|
11
11
|
const oracle = new FixedPriceOracle();
|
|
12
12
|
const result = await oracle.getPrice("BTC");
|
|
13
|
-
expect(result.
|
|
13
|
+
expect(result.priceMicros).toBe(65_000_000_000); // $65,000
|
|
14
14
|
});
|
|
15
15
|
it("accepts custom prices", async () => {
|
|
16
|
-
const oracle = new FixedPriceOracle({ ETH:
|
|
17
|
-
expect((await oracle.getPrice("ETH")).
|
|
18
|
-
expect((await oracle.getPrice("BTC")).
|
|
16
|
+
const oracle = new FixedPriceOracle({ ETH: 2_000_000_000, BTC: 50_000_000_000 });
|
|
17
|
+
expect((await oracle.getPrice("ETH")).priceMicros).toBe(2_000_000_000);
|
|
18
|
+
expect((await oracle.getPrice("BTC")).priceMicros).toBe(50_000_000_000);
|
|
19
19
|
});
|
|
20
20
|
});
|
|
@@ -14,13 +14,13 @@ export interface ChainlinkOracleOpts {
|
|
|
14
14
|
* No API key, no rate limits — just an RPC call to our own node.
|
|
15
15
|
*
|
|
16
16
|
* Chainlink USD feeds use 8 decimals. We convert to integer USD cents:
|
|
17
|
-
*
|
|
17
|
+
* priceMicros = answer / 100 (i.e. answer / 10^8 * 10^6)
|
|
18
18
|
*/
|
|
19
19
|
export declare class ChainlinkOracle implements IPriceOracle {
|
|
20
20
|
private readonly rpc;
|
|
21
21
|
private readonly feeds;
|
|
22
22
|
private readonly maxStalenessMs;
|
|
23
23
|
constructor(opts: ChainlinkOracleOpts);
|
|
24
|
-
getPrice(asset: PriceAsset): Promise<PriceResult>;
|
|
24
|
+
getPrice(asset: PriceAsset, feedAddress?: `0x${string}`): Promise<PriceResult>;
|
|
25
25
|
}
|
|
26
26
|
export {};
|
|
@@ -17,7 +17,7 @@ const DEFAULT_MAX_STALENESS_MS = 60 * 60 * 1000;
|
|
|
17
17
|
* No API key, no rate limits — just an RPC call to our own node.
|
|
18
18
|
*
|
|
19
19
|
* Chainlink USD feeds use 8 decimals. We convert to integer USD cents:
|
|
20
|
-
*
|
|
20
|
+
* priceMicros = answer / 100 (i.e. answer / 10^8 * 10^6)
|
|
21
21
|
*/
|
|
22
22
|
export class ChainlinkOracle {
|
|
23
23
|
rpc;
|
|
@@ -28,11 +28,11 @@ export class ChainlinkOracle {
|
|
|
28
28
|
this.feeds = new Map(Object.entries({ ...FEED_ADDRESSES, ...opts.feedAddresses }));
|
|
29
29
|
this.maxStalenessMs = opts.maxStalenessMs ?? DEFAULT_MAX_STALENESS_MS;
|
|
30
30
|
}
|
|
31
|
-
async getPrice(asset) {
|
|
32
|
-
const
|
|
33
|
-
if (!
|
|
31
|
+
async getPrice(asset, feedAddress) {
|
|
32
|
+
const resolvedFeed = feedAddress ?? this.feeds.get(asset);
|
|
33
|
+
if (!resolvedFeed)
|
|
34
34
|
throw new Error(`No price feed for asset: ${asset}`);
|
|
35
|
-
const result = (await this.rpc("eth_call", [{ to:
|
|
35
|
+
const result = (await this.rpc("eth_call", [{ to: resolvedFeed, data: LATEST_ROUND_DATA }, "latest"]));
|
|
36
36
|
// ABI decode latestRoundData() return:
|
|
37
37
|
// [0] roundId (uint80) — skip
|
|
38
38
|
// [1] answer (int256) — price × 10^8
|
|
@@ -51,11 +51,12 @@ export class ChainlinkOracle {
|
|
|
51
51
|
if (ageMs > this.maxStalenessMs) {
|
|
52
52
|
throw new Error(`Price feed for ${asset} is stale (${Math.round(ageMs / 1000)}s old, max ${Math.round(this.maxStalenessMs / 1000)}s)`);
|
|
53
53
|
}
|
|
54
|
-
// Chainlink USD feeds: 8 decimals. answer /
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
// Chainlink USD feeds: 8 decimals. answer / 100 = microdollars (10^-6 USD).
|
|
55
|
+
// e.g. BTC at $70,315 → answer = 7_031_500_000_000 → 70_315_000_000 microdollars
|
|
56
|
+
const priceMicros = Number(answer / 100n);
|
|
57
|
+
if (priceMicros <= 0) {
|
|
58
|
+
throw new Error(`Invalid price for ${asset}: ${priceMicros} microdollars`);
|
|
58
59
|
}
|
|
59
|
-
return {
|
|
60
|
+
return { priceMicros, updatedAt };
|
|
60
61
|
}
|
|
61
62
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { IPriceOracle, PriceAsset, PriceResult } from "./types.js";
|
|
2
|
+
export interface CoinGeckoOracleOpts {
|
|
3
|
+
/** Override token→id mapping. */
|
|
4
|
+
tokenIds?: Record<string, string>;
|
|
5
|
+
/** Cache TTL in ms. Default: 60s. */
|
|
6
|
+
cacheTtlMs?: number;
|
|
7
|
+
/** Custom fetch function (for testing). */
|
|
8
|
+
fetchFn?: typeof fetch;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* CoinGecko price oracle — free API, no key required.
|
|
12
|
+
* Used for assets without Chainlink on-chain feeds (DOGE, LTC).
|
|
13
|
+
* Caches prices to stay within rate limits.
|
|
14
|
+
*/
|
|
15
|
+
export declare class CoinGeckoOracle implements IPriceOracle {
|
|
16
|
+
private readonly ids;
|
|
17
|
+
private readonly cacheTtlMs;
|
|
18
|
+
private readonly fetchFn;
|
|
19
|
+
private readonly cache;
|
|
20
|
+
constructor(opts?: CoinGeckoOracleOpts);
|
|
21
|
+
getPrice(asset: PriceAsset, _feedAddress?: `0x${string}`): Promise<PriceResult>;
|
|
22
|
+
}
|