@wopr-network/platform-core 1.20.0 → 1.22.0
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/btc/settler.js +1 -1
- package/dist/billing/crypto/btc/watcher.d.ts +5 -3
- package/dist/billing/crypto/btc/watcher.js +9 -8
- package/dist/billing/crypto/evm/__tests__/eth-checkout.test.d.ts +1 -0
- package/dist/billing/crypto/evm/__tests__/eth-checkout.test.js +49 -0
- package/dist/billing/crypto/evm/__tests__/eth-settler.test.d.ts +1 -0
- package/dist/billing/crypto/evm/__tests__/eth-settler.test.js +80 -0
- package/dist/billing/crypto/evm/__tests__/eth-watcher.test.d.ts +1 -0
- package/dist/billing/crypto/evm/__tests__/eth-watcher.test.js +134 -0
- package/dist/billing/crypto/evm/eth-checkout.d.ts +34 -0
- package/dist/billing/crypto/evm/eth-checkout.js +53 -0
- package/dist/billing/crypto/evm/eth-settler.d.ts +23 -0
- package/dist/billing/crypto/evm/eth-settler.js +52 -0
- package/dist/billing/crypto/evm/eth-watcher.d.ts +53 -0
- package/dist/billing/crypto/evm/eth-watcher.js +83 -0
- package/dist/billing/crypto/evm/index.d.ts +6 -0
- package/dist/billing/crypto/evm/index.js +3 -0
- package/dist/billing/crypto/evm/settler.js +1 -1
- package/dist/billing/crypto/index.d.ts +1 -0
- package/dist/billing/crypto/index.js +1 -0
- package/dist/billing/crypto/oracle/__tests__/chainlink.test.d.ts +1 -0
- package/dist/billing/crypto/oracle/__tests__/chainlink.test.js +83 -0
- package/dist/billing/crypto/oracle/__tests__/convert.test.d.ts +1 -0
- package/dist/billing/crypto/oracle/__tests__/convert.test.js +51 -0
- package/dist/billing/crypto/oracle/__tests__/fixed.test.d.ts +1 -0
- package/dist/billing/crypto/oracle/__tests__/fixed.test.js +20 -0
- package/dist/billing/crypto/oracle/chainlink.d.ts +26 -0
- package/dist/billing/crypto/oracle/chainlink.js +61 -0
- package/dist/billing/crypto/oracle/convert.d.ts +20 -0
- package/dist/billing/crypto/oracle/convert.js +38 -0
- package/dist/billing/crypto/oracle/fixed.d.ts +10 -0
- package/dist/billing/crypto/oracle/fixed.js +20 -0
- package/dist/billing/crypto/oracle/index.d.ts +5 -0
- package/dist/billing/crypto/oracle/index.js +3 -0
- package/dist/billing/crypto/oracle/types.d.ts +13 -0
- package/dist/billing/crypto/oracle/types.js +1 -0
- package/package.json +1 -1
- package/src/billing/crypto/btc/settler.ts +1 -1
- package/src/billing/crypto/btc/watcher.ts +12 -11
- package/src/billing/crypto/evm/__tests__/eth-checkout.test.ts +60 -0
- package/src/billing/crypto/evm/__tests__/eth-settler.test.ts +98 -0
- package/src/billing/crypto/evm/__tests__/eth-watcher.test.ts +157 -0
- package/src/billing/crypto/evm/eth-checkout.ts +84 -0
- package/src/billing/crypto/evm/eth-settler.ts +71 -0
- package/src/billing/crypto/evm/eth-watcher.ts +129 -0
- package/src/billing/crypto/evm/index.ts +6 -0
- package/src/billing/crypto/evm/settler.ts +1 -1
- package/src/billing/crypto/index.ts +1 -0
- package/src/billing/crypto/oracle/__tests__/chainlink.test.ts +107 -0
- package/src/billing/crypto/oracle/__tests__/convert.test.ts +62 -0
- package/src/billing/crypto/oracle/__tests__/fixed.test.ts +23 -0
- package/src/billing/crypto/oracle/chainlink.ts +85 -0
- package/src/billing/crypto/oracle/convert.ts +38 -0
- package/src/billing/crypto/oracle/fixed.ts +23 -0
- package/src/billing/crypto/oracle/index.ts +5 -0
- package/src/billing/crypto/oracle/types.ts +15 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { centsToNative, nativeToCents } from "../convert.js";
|
|
3
|
+
|
|
4
|
+
describe("centsToNative", () => {
|
|
5
|
+
it("converts $50 to ETH wei at $3,500", () => {
|
|
6
|
+
// 5000 cents × 10^18 / 350000 cents = 14285714285714285n wei
|
|
7
|
+
const wei = centsToNative(5000, 350_000, 18);
|
|
8
|
+
expect(wei).toBe(14_285_714_285_714_285n);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("converts $50 to BTC sats at $65,000", () => {
|
|
12
|
+
// 5000 cents × 10^8 / 6500000 cents = 76923n sats
|
|
13
|
+
const sats = centsToNative(5000, 6_500_000, 8);
|
|
14
|
+
expect(sats).toBe(76_923n);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("converts $100 to ETH wei at $2,000", () => {
|
|
18
|
+
// 10000 cents × 10^18 / 200000 cents = 50000000000000000n wei (0.05 ETH)
|
|
19
|
+
const wei = centsToNative(10_000, 200_000, 18);
|
|
20
|
+
expect(wei).toBe(50_000_000_000_000_000n);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("rejects non-integer amountCents", () => {
|
|
24
|
+
expect(() => centsToNative(50.5, 350_000, 18)).toThrow("positive integer");
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("rejects zero amountCents", () => {
|
|
28
|
+
expect(() => centsToNative(0, 350_000, 18)).toThrow("positive integer");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("rejects zero priceCents", () => {
|
|
32
|
+
expect(() => centsToNative(5000, 0, 18)).toThrow("positive integer");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("rejects negative decimals", () => {
|
|
36
|
+
expect(() => centsToNative(5000, 350_000, -1)).toThrow("non-negative integer");
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe("nativeToCents", () => {
|
|
41
|
+
it("converts ETH wei back to cents at $3,500", () => {
|
|
42
|
+
// 14285714285714285n wei × 350000 / 10^18 = 4999 cents (truncated)
|
|
43
|
+
const cents = nativeToCents(14_285_714_285_714_285n, 350_000, 18);
|
|
44
|
+
expect(cents).toBe(4999); // truncation from integer division
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("converts BTC sats back to cents at $65,000", () => {
|
|
48
|
+
// 76923n sats × 6500000 / 10^8 = 4999 cents (truncated)
|
|
49
|
+
const cents = nativeToCents(76_923n, 6_500_000, 8);
|
|
50
|
+
expect(cents).toBe(4999);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("exact round-trip for clean division", () => {
|
|
54
|
+
// 0.05 ETH at $2,000 = $100
|
|
55
|
+
const cents = nativeToCents(50_000_000_000_000_000n, 200_000, 18);
|
|
56
|
+
expect(cents).toBe(10_000); // $100.00
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("rejects negative rawAmount", () => {
|
|
60
|
+
expect(() => nativeToCents(-1n, 350_000, 18)).toThrow("non-negative");
|
|
61
|
+
});
|
|
62
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { FixedPriceOracle } from "../fixed.js";
|
|
3
|
+
|
|
4
|
+
describe("FixedPriceOracle", () => {
|
|
5
|
+
it("returns default ETH price", async () => {
|
|
6
|
+
const oracle = new FixedPriceOracle();
|
|
7
|
+
const result = await oracle.getPrice("ETH");
|
|
8
|
+
expect(result.priceCents).toBe(350_000); // $3,500
|
|
9
|
+
expect(result.updatedAt).toBeInstanceOf(Date);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("returns default BTC price", async () => {
|
|
13
|
+
const oracle = new FixedPriceOracle();
|
|
14
|
+
const result = await oracle.getPrice("BTC");
|
|
15
|
+
expect(result.priceCents).toBe(6_500_000); // $65,000
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("accepts custom prices", async () => {
|
|
19
|
+
const oracle = new FixedPriceOracle({ ETH: 200_000, BTC: 5_000_000 });
|
|
20
|
+
expect((await oracle.getPrice("ETH")).priceCents).toBe(200_000);
|
|
21
|
+
expect((await oracle.getPrice("BTC")).priceCents).toBe(5_000_000);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { IPriceOracle, PriceAsset, PriceResult } from "./types.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Chainlink price feed addresses on Base mainnet.
|
|
5
|
+
* These are ERC-1967 proxy contracts — addresses are stable.
|
|
6
|
+
*/
|
|
7
|
+
const FEED_ADDRESSES: Record<PriceAsset, `0x${string}`> = {
|
|
8
|
+
ETH: "0x71041dddad3595F9CEd3DcCFBe3D1F4b0a16Bb70",
|
|
9
|
+
BTC: "0x64c911996D3c6aC71f9b455B1E8E7266BcbD848F",
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/** Function selector for latestRoundData(). */
|
|
13
|
+
const LATEST_ROUND_DATA = "0xfeaf968c";
|
|
14
|
+
|
|
15
|
+
/** Default max staleness: 1 hour. */
|
|
16
|
+
const DEFAULT_MAX_STALENESS_MS = 60 * 60 * 1000;
|
|
17
|
+
|
|
18
|
+
type RpcCall = (method: string, params: unknown[]) => Promise<unknown>;
|
|
19
|
+
|
|
20
|
+
export interface ChainlinkOracleOpts {
|
|
21
|
+
rpcCall: RpcCall;
|
|
22
|
+
/** Override feed addresses (e.g. for testnet or Anvil forks). */
|
|
23
|
+
feedAddresses?: Partial<Record<PriceAsset, `0x${string}`>>;
|
|
24
|
+
/** Maximum age of price data before rejecting (ms). Default: 1 hour. */
|
|
25
|
+
maxStalenessMs?: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* On-chain Chainlink price oracle.
|
|
30
|
+
*
|
|
31
|
+
* Reads latestRoundData() from Chainlink aggregator contracts via eth_call.
|
|
32
|
+
* No API key, no rate limits — just an RPC call to our own node.
|
|
33
|
+
*
|
|
34
|
+
* Chainlink USD feeds use 8 decimals. We convert to integer USD cents:
|
|
35
|
+
* priceCents = answer / 10^6 (i.e. answer / 10^8 * 100)
|
|
36
|
+
*/
|
|
37
|
+
export class ChainlinkOracle implements IPriceOracle {
|
|
38
|
+
private readonly rpc: RpcCall;
|
|
39
|
+
private readonly feeds: Record<PriceAsset, `0x${string}`>;
|
|
40
|
+
private readonly maxStalenessMs: number;
|
|
41
|
+
|
|
42
|
+
constructor(opts: ChainlinkOracleOpts) {
|
|
43
|
+
this.rpc = opts.rpcCall;
|
|
44
|
+
this.feeds = { ...FEED_ADDRESSES, ...opts.feedAddresses };
|
|
45
|
+
this.maxStalenessMs = opts.maxStalenessMs ?? DEFAULT_MAX_STALENESS_MS;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async getPrice(asset: PriceAsset): Promise<PriceResult> {
|
|
49
|
+
const feedAddress = this.feeds[asset];
|
|
50
|
+
if (!feedAddress) throw new Error(`No price feed for asset: ${asset}`);
|
|
51
|
+
|
|
52
|
+
const result = (await this.rpc("eth_call", [{ to: feedAddress, data: LATEST_ROUND_DATA }, "latest"])) as string;
|
|
53
|
+
|
|
54
|
+
// ABI decode latestRoundData() return:
|
|
55
|
+
// [0] roundId (uint80) — skip
|
|
56
|
+
// [1] answer (int256) — price × 10^8
|
|
57
|
+
// [2] startedAt (uint256) — skip
|
|
58
|
+
// [3] updatedAt (uint256) — unix seconds
|
|
59
|
+
// [4] answeredInRound (uint80) — skip
|
|
60
|
+
const hex = result.slice(2);
|
|
61
|
+
if (hex.length < 320) {
|
|
62
|
+
throw new Error(`Malformed Chainlink response for ${asset}: expected 320 hex chars, got ${hex.length}`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const answer = BigInt(`0x${hex.slice(64, 128)}`);
|
|
66
|
+
const updatedAtSec = Number(BigInt(`0x${hex.slice(192, 256)}`));
|
|
67
|
+
const updatedAt = new Date(updatedAtSec * 1000);
|
|
68
|
+
|
|
69
|
+
// Staleness guard.
|
|
70
|
+
const ageMs = Date.now() - updatedAt.getTime();
|
|
71
|
+
if (ageMs > this.maxStalenessMs) {
|
|
72
|
+
throw new Error(
|
|
73
|
+
`Price feed for ${asset} is stale (${Math.round(ageMs / 1000)}s old, max ${Math.round(this.maxStalenessMs / 1000)}s)`,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Chainlink USD feeds: 8 decimals. answer / 10^6 = cents (integer).
|
|
78
|
+
const priceCents = Number(answer / 1_000_000n);
|
|
79
|
+
if (priceCents <= 0) {
|
|
80
|
+
throw new Error(`Invalid price for ${asset}: ${priceCents} cents`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return { priceCents, updatedAt };
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert USD cents to native token amount using a price in cents.
|
|
3
|
+
*
|
|
4
|
+
* Formula: rawAmount = amountCents × 10^decimals / priceCents
|
|
5
|
+
*
|
|
6
|
+
* Examples:
|
|
7
|
+
* $50 in ETH at $3,500: centsToNative(5000, 350000, 18) = 14285714285714285n (≈0.01429 ETH)
|
|
8
|
+
* $50 in BTC at $65,000: centsToNative(5000, 6500000, 8) = 76923n (76,923 sats ≈ 0.00077 BTC)
|
|
9
|
+
*
|
|
10
|
+
* Integer math only. No floating point.
|
|
11
|
+
*/
|
|
12
|
+
export function centsToNative(amountCents: number, priceCents: number, decimals: number): bigint {
|
|
13
|
+
if (!Number.isInteger(amountCents) || amountCents <= 0) {
|
|
14
|
+
throw new Error(`amountCents must be a positive integer, got ${amountCents}`);
|
|
15
|
+
}
|
|
16
|
+
if (!Number.isInteger(priceCents) || priceCents <= 0) {
|
|
17
|
+
throw new Error(`priceCents must be a positive integer, got ${priceCents}`);
|
|
18
|
+
}
|
|
19
|
+
if (!Number.isInteger(decimals) || decimals < 0) {
|
|
20
|
+
throw new Error(`decimals must be a non-negative integer, got ${decimals}`);
|
|
21
|
+
}
|
|
22
|
+
return (BigInt(amountCents) * 10n ** BigInt(decimals)) / BigInt(priceCents);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Convert native token amount back to USD cents using a price in cents.
|
|
27
|
+
*
|
|
28
|
+
* Inverse of centsToNative. Truncates fractional cents.
|
|
29
|
+
*
|
|
30
|
+
* Integer math only.
|
|
31
|
+
*/
|
|
32
|
+
export function nativeToCents(rawAmount: bigint, priceCents: number, decimals: number): number {
|
|
33
|
+
if (rawAmount < 0n) throw new Error("rawAmount must be non-negative");
|
|
34
|
+
if (!Number.isInteger(priceCents) || priceCents <= 0) {
|
|
35
|
+
throw new Error(`priceCents must be a positive integer, got ${priceCents}`);
|
|
36
|
+
}
|
|
37
|
+
return Number((rawAmount * BigInt(priceCents)) / 10n ** BigInt(decimals));
|
|
38
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { IPriceOracle, PriceAsset, PriceResult } from "./types.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Fixed-price oracle for testing and local dev (Anvil, regtest).
|
|
5
|
+
* Returns hardcoded prices — no RPC calls.
|
|
6
|
+
*/
|
|
7
|
+
export class FixedPriceOracle implements IPriceOracle {
|
|
8
|
+
private readonly prices: Record<string, number>;
|
|
9
|
+
|
|
10
|
+
constructor(prices: Partial<Record<PriceAsset, number>> = {}) {
|
|
11
|
+
this.prices = {
|
|
12
|
+
ETH: 350_000, // $3,500
|
|
13
|
+
BTC: 6_500_000, // $65,000
|
|
14
|
+
...prices,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async getPrice(asset: PriceAsset): Promise<PriceResult> {
|
|
19
|
+
const priceCents = this.prices[asset];
|
|
20
|
+
if (priceCents === undefined) throw new Error(`No fixed price for ${asset}`);
|
|
21
|
+
return { priceCents, updatedAt: new Date() };
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export type { ChainlinkOracleOpts } from "./chainlink.js";
|
|
2
|
+
export { ChainlinkOracle } from "./chainlink.js";
|
|
3
|
+
export { centsToNative, nativeToCents } from "./convert.js";
|
|
4
|
+
export { FixedPriceOracle } from "./fixed.js";
|
|
5
|
+
export type { IPriceOracle, PriceAsset, PriceResult } from "./types.js";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** Assets with Chainlink price feeds. */
|
|
2
|
+
export type PriceAsset = "ETH" | "BTC";
|
|
3
|
+
|
|
4
|
+
/** Result from a price oracle query. */
|
|
5
|
+
export interface PriceResult {
|
|
6
|
+
/** USD cents per 1 unit of asset (integer). */
|
|
7
|
+
priceCents: number;
|
|
8
|
+
/** When the price was last updated on-chain. */
|
|
9
|
+
updatedAt: Date;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Read-only price oracle. */
|
|
13
|
+
export interface IPriceOracle {
|
|
14
|
+
getPrice(asset: PriceAsset): Promise<PriceResult>;
|
|
15
|
+
}
|