@pod-network/trade-sdk 0.3.0 → 0.4.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/README.md +99 -2
- package/dist/client.d.ts +45 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +63 -2
- package/dist/client.js.map +1 -1
- package/dist/codec/bridge.d.ts +82 -0
- package/dist/codec/bridge.d.ts.map +1 -0
- package/dist/codec/bridge.js +114 -0
- package/dist/codec/bridge.js.map +1 -0
- package/dist/codec/decode.d.ts +15 -2
- package/dist/codec/decode.d.ts.map +1 -1
- package/dist/codec/decode.js +39 -0
- package/dist/codec/decode.js.map +1 -1
- package/dist/codec/resolution.d.ts +4 -1
- package/dist/codec/resolution.d.ts.map +1 -1
- package/dist/codec/resolution.js +6 -3
- package/dist/codec/resolution.js.map +1 -1
- package/dist/codec/units.d.ts.map +1 -1
- package/dist/codec/units.js +9 -1
- package/dist/codec/units.js.map +1 -1
- package/dist/datafeed.d.ts.map +1 -1
- package/dist/datafeed.js +15 -9
- package/dist/datafeed.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/dist/stores/resource.d.ts +14 -1
- package/dist/stores/resource.d.ts.map +1 -1
- package/dist/stores/resource.js +7 -1
- package/dist/stores/resource.js.map +1 -1
- package/dist/sync/candles.d.ts +62 -4
- package/dist/sync/candles.d.ts.map +1 -1
- package/dist/sync/candles.js +259 -42
- package/dist/sync/candles.js.map +1 -1
- package/dist/sync/sources.d.ts +5 -1
- package/dist/sync/sources.d.ts.map +1 -1
- package/dist/sync/sources.js +16 -0
- package/dist/sync/sources.js.map +1 -1
- package/dist/sync/withdrawals.d.ts +5 -0
- package/dist/sync/withdrawals.d.ts.map +1 -0
- package/dist/sync/withdrawals.js +172 -0
- package/dist/sync/withdrawals.js.map +1 -0
- package/dist/transport/claim.d.ts +160 -0
- package/dist/transport/claim.d.ts.map +1 -0
- package/dist/transport/claim.js +341 -0
- package/dist/transport/claim.js.map +1 -0
- package/dist/transport/jsonrpc.d.ts +16 -0
- package/dist/transport/jsonrpc.d.ts.map +1 -0
- package/dist/transport/jsonrpc.js +42 -0
- package/dist/transport/jsonrpc.js.map +1 -0
- package/dist/transport/rest.d.ts +23 -1
- package/dist/transport/rest.d.ts.map +1 -1
- package/dist/transport/rest.js +33 -2
- package/dist/transport/rest.js.map +1 -1
- package/dist/transport/ws.d.ts +4 -1
- package/dist/transport/ws.d.ts.map +1 -1
- package/dist/transport/ws.js.map +1 -1
- package/dist/types/public.d.ts +87 -0
- package/dist/types/public.d.ts.map +1 -1
- package/dist/types/wire.d.ts +43 -1
- package/dist/types/wire.d.ts.map +1 -1
- package/dist/write/delegation.d.ts.map +1 -1
- package/dist/write/delegation.js +2 -12
- package/dist/write/delegation.js.map +1 -1
- package/dist/write/index.d.ts +25 -0
- package/dist/write/index.d.ts.map +1 -1
- package/dist/write/index.js +20 -13
- package/dist/write/index.js.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +77 -5
- package/src/codec/bridge.ts +138 -0
- package/src/codec/decode.ts +47 -4
- package/src/codec/resolution.ts +6 -3
- package/src/codec/units.ts +8 -1
- package/src/datafeed.ts +15 -9
- package/src/index.ts +16 -0
- package/src/stores/resource.ts +14 -1
- package/src/sync/candles.ts +297 -45
- package/src/sync/sources.ts +16 -1
- package/src/sync/withdrawals.ts +177 -0
- package/src/transport/claim.ts +434 -0
- package/src/transport/jsonrpc.ts +54 -0
- package/src/transport/rest.ts +50 -8
- package/src/transport/ws.ts +4 -1
- package/src/types/public.ts +94 -0
- package/src/types/wire.ts +47 -1
- package/src/write/delegation.ts +2 -12
- package/src/write/index.ts +58 -12
package/src/types/public.ts
CHANGED
|
@@ -465,3 +465,97 @@ export interface TriggersQuery {
|
|
|
465
465
|
orderbookId?: MarketId;
|
|
466
466
|
limit?: number;
|
|
467
467
|
}
|
|
468
|
+
|
|
469
|
+
// --- bridge / withdrawals (ADR 0033) ---
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* One bridged token, as `GET /v1/bridge/config` serves it.
|
|
473
|
+
*
|
|
474
|
+
* `min`, `max` and `decimals` are all on the **claim chain's** terms, not pod's
|
|
475
|
+
* 18 — see `codec/bridge.ts`, which owns that conversion.
|
|
476
|
+
*/
|
|
477
|
+
export interface BridgeToken {
|
|
478
|
+
podToken: Address;
|
|
479
|
+
l1Token: Address;
|
|
480
|
+
/** Decimals on the claim chain, and so the withdrawal granularity on pod. */
|
|
481
|
+
decimals: number;
|
|
482
|
+
/** Smallest admissible withdrawal, in claim-chain decimals. */
|
|
483
|
+
min: bigint;
|
|
484
|
+
/** Largest admissible withdrawal, in claim-chain decimals. */
|
|
485
|
+
max: bigint;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/** Static bridge configuration — the one chain pod withdrawals settle on. */
|
|
489
|
+
export interface BridgeConfig {
|
|
490
|
+
/** `0` when the node has no bridge configured, in which case `tokens` is
|
|
491
|
+
* empty and no withdrawal is admissible at all. */
|
|
492
|
+
claimChainId: number;
|
|
493
|
+
/** The bridge contract on the claim chain, where `Claim` is emitted. */
|
|
494
|
+
sourceContract: Address;
|
|
495
|
+
/** Bridge version; feeds the claim-hash domain separator. */
|
|
496
|
+
version: number;
|
|
497
|
+
tokens: BridgeToken[];
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Why a withdrawal produced no L1 claim. Any value here means **no L1 event will
|
|
502
|
+
* ever fire**, so a client watching only the claim chain would wait forever —
|
|
503
|
+
* this is the only place a reason exists.
|
|
504
|
+
*/
|
|
505
|
+
export type WithdrawalError =
|
|
506
|
+
/** The CLOB balance did not cover it at execution. Admission deliberately
|
|
507
|
+
* doesn't check the balance (pending fills can raise it), so this races that. */
|
|
508
|
+
| "insufficient_balance"
|
|
509
|
+
/** The solver never included the intent before its deadline. Funds are
|
|
510
|
+
* untouched and the withdrawal can simply be resubmitted. */
|
|
511
|
+
| "not_included"
|
|
512
|
+
/**
|
|
513
|
+
* Any other reason a node reports. This set is **open by construction**: the
|
|
514
|
+
* node's reason enum is explicitly extensible and an app ships ahead of the
|
|
515
|
+
* fleet, so a spelling this build predates has to arrive verbatim rather than
|
|
516
|
+
* be dropped. Dropping it makes a *failed* withdrawal read as claimable —
|
|
517
|
+
* the one direction it is unsafe to be lossy in, because the caller then
|
|
518
|
+
* reports success and waits for a claim that can never land.
|
|
519
|
+
*
|
|
520
|
+
* `(string & {})` keeps the two known values in editor completion while still
|
|
521
|
+
* admitting the rest. Treat an unknown reason exactly like a known one, and
|
|
522
|
+
* show it verbatim rather than guessing which known reason it resembles.
|
|
523
|
+
*/
|
|
524
|
+
| (string & {});
|
|
525
|
+
|
|
526
|
+
/** One terminal withdrawal outcome, from `pod_withdrawals` or its REST backfill. */
|
|
527
|
+
export interface Withdrawal {
|
|
528
|
+
/** `keccak(abi.encode(signer, nonce, sequence))` — derivable client-side
|
|
529
|
+
* before submitting, which is what lets a client match its own withdrawal. */
|
|
530
|
+
id: Hash;
|
|
531
|
+
/** The debited account: the master, for a delegated withdrawal. */
|
|
532
|
+
withdrawer: Address;
|
|
533
|
+
/** Recipient on the **claim chain**. Nothing is credited on pod. */
|
|
534
|
+
to: Address;
|
|
535
|
+
/** Pod-side token address. */
|
|
536
|
+
token: Address;
|
|
537
|
+
/** Pod's 18 decimals, as signed. The L1 claim carries this converted to the
|
|
538
|
+
* token's claim-chain decimals — never pass this value to `claim`. */
|
|
539
|
+
amount: bigint;
|
|
540
|
+
/** Absent when the withdrawal is claimable on L1. */
|
|
541
|
+
error?: WithdrawalError;
|
|
542
|
+
/**
|
|
543
|
+
* The tick's batch deadline, in **microseconds** — not the millisecond
|
|
544
|
+
* convention the rest of this surface uses.
|
|
545
|
+
*
|
|
546
|
+
* Deliberate: this value is also the resume cursor for `pod_withdrawals` and
|
|
547
|
+
* for the REST backfill, both of which compare it in micros. Narrowing it to
|
|
548
|
+
* ms and back would round a tick boundary and silently re-serve or skip a
|
|
549
|
+
* tick, so it stays in the unit the server compares.
|
|
550
|
+
*/
|
|
551
|
+
timeUs: number;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
export interface WithdrawalsQuery {
|
|
555
|
+
/** Batch deadline in microseconds; strictly greater. */
|
|
556
|
+
since?: number;
|
|
557
|
+
/** Last id already seen *within* the `since` tick, to resume mid-tick. */
|
|
558
|
+
sinceId?: Hash;
|
|
559
|
+
/** Server default 500, capped at 1000. */
|
|
560
|
+
limit?: number;
|
|
561
|
+
}
|
package/src/types/wire.ts
CHANGED
|
@@ -126,7 +126,6 @@ export interface WireOrdersPage {
|
|
|
126
126
|
|
|
127
127
|
export interface WireTickSnapshot {
|
|
128
128
|
volume: WireDecimal;
|
|
129
|
-
minimum_expiry?: number;
|
|
130
129
|
}
|
|
131
130
|
|
|
132
131
|
export interface WireOrderbook {
|
|
@@ -385,3 +384,50 @@ export interface WireTriggersPush {
|
|
|
385
384
|
total_count: number;
|
|
386
385
|
next_cursor: string | null;
|
|
387
386
|
}
|
|
387
|
+
|
|
388
|
+
// --- bridge / withdrawals (ADR 0033) ---
|
|
389
|
+
|
|
390
|
+
export interface WireBridgeToken {
|
|
391
|
+
pod_token: Hex;
|
|
392
|
+
l1_token: Hex;
|
|
393
|
+
decimals: number;
|
|
394
|
+
/** Hex-encoded U256, in claim-chain decimals. */
|
|
395
|
+
min: string;
|
|
396
|
+
max: string;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
export interface WireBridgeConfig {
|
|
400
|
+
claim_chain_id: number;
|
|
401
|
+
source_contract: Hex;
|
|
402
|
+
version: number;
|
|
403
|
+
tokens: WireBridgeToken[];
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/** `pod_withdrawals` delivers an ARRAY of these per tick; the REST backfill
|
|
407
|
+
* serves the identical shape, so a backfilled outcome is indistinguishable
|
|
408
|
+
* from one that arrived live. */
|
|
409
|
+
export interface WireWithdrawal {
|
|
410
|
+
withdrawal_id: Hex;
|
|
411
|
+
withdrawer: Hex;
|
|
412
|
+
to: Hex;
|
|
413
|
+
token: Hex;
|
|
414
|
+
/** Hex-encoded U256 in pod's 18 decimals — the amount as signed. */
|
|
415
|
+
amount: string;
|
|
416
|
+
/** Omitted (not null) when the withdrawal is claimable. */
|
|
417
|
+
error?: string | null;
|
|
418
|
+
timestamp_us: number;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/** `GET /v1/bridge/withdrawals/by-id/{id}`. Nested rather than flattened because
|
|
422
|
+
* `WithdrawalUpdate` carries a u128 and serde's `flatten` cannot round-trip one.
|
|
423
|
+
*
|
|
424
|
+
* `status` is always present, but it is NOT what a consumer decides on: `proof`
|
|
425
|
+
* takes precedence, including over a `status` this build has never seen, so an
|
|
426
|
+
* unfamiliar label cannot veto a certificate attached to the same response.
|
|
427
|
+
* `proof` is omitted (never null) until one can be assembled. `fetchClaimStatus`
|
|
428
|
+
* carries the full precedence. */
|
|
429
|
+
export interface WireWithdrawalDetail {
|
|
430
|
+
withdrawal: WireWithdrawal;
|
|
431
|
+
status: "claimable" | "pending" | "refused";
|
|
432
|
+
proof?: { claim_hash?: Hex | null };
|
|
433
|
+
}
|
package/src/write/delegation.ts
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
import { createWalletClient, defineChain, encodeFunctionData, http, parseAbi, recoverTypedDataAddress } from "viem";
|
|
14
14
|
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
|
|
15
15
|
import type { Address, Hash, Hex } from "../types/public.js";
|
|
16
|
+
import { rpc } from "../transport/jsonrpc.js";
|
|
16
17
|
import { sendRawTransaction, type PodTxRequest } from "./index.js";
|
|
17
18
|
|
|
18
19
|
/** EIP-712 payload the master signs (domain "pod delegation" v1). */
|
|
@@ -92,7 +93,7 @@ export async function createDelegatedWallet(p: CreateDelegatedWalletParams): Pro
|
|
|
92
93
|
functionName: "delegated",
|
|
93
94
|
args: [p.master, validUntil, signature, tx.data],
|
|
94
95
|
});
|
|
95
|
-
const maxFeePerGas = BigInt(await rpc(p.rpcUrl,
|
|
96
|
+
const maxFeePerGas = BigInt(await rpc<string>(p.rpcUrl, "eth_gasPrice", [], { fetch: doFetch }));
|
|
96
97
|
const prepared = await client.prepareTransactionRequest({ ...tx, data, account, chain, maxFeePerGas });
|
|
97
98
|
const serialized = await client.signTransaction(prepared as never);
|
|
98
99
|
return sendRawTransaction(p.rpcUrl, serialized, { fetch: doFetch });
|
|
@@ -100,14 +101,3 @@ export async function createDelegatedWallet(p: CreateDelegatedWalletParams): Pro
|
|
|
100
101
|
|
|
101
102
|
return Object.freeze({ master: p.master, delegate: account.address, validUntil, submit });
|
|
102
103
|
}
|
|
103
|
-
|
|
104
|
-
async function rpc(rpcUrl: string, doFetch: typeof fetch, method: string): Promise<string> {
|
|
105
|
-
const res = await doFetch(rpcUrl, {
|
|
106
|
-
method: "POST",
|
|
107
|
-
headers: { "content-type": "application/json" },
|
|
108
|
-
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params: [] }),
|
|
109
|
-
});
|
|
110
|
-
const json = (await res.json()) as { result?: string; error?: { message?: string } };
|
|
111
|
-
if (json.error || json.result === undefined) throw new Error(json.error?.message ?? `${method} failed`);
|
|
112
|
-
return json.result;
|
|
113
|
-
}
|
package/src/write/index.ts
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
import { decodeAbiParameters, encodeAbiParameters, encodeFunctionData, keccak256, parseAbi } from "viem";
|
|
14
14
|
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
|
|
15
15
|
import type { Address, Hash, Hex, MarketId } from "../types/public.js";
|
|
16
|
+
import { rpc } from "../transport/jsonrpc.js";
|
|
16
17
|
|
|
17
18
|
/** The CLOB precompile (chain id 1293 / 0x50d). */
|
|
18
19
|
export const CLOB_ADDRESS: Address = "0x50d0000000000000000000000000000000000002";
|
|
@@ -26,6 +27,7 @@ const CLOB_ABI = parseAbi([
|
|
|
26
27
|
"function cancelTrigger(bytes32 orderbookId, bytes32 triggerOrder, uint128 deadline)",
|
|
27
28
|
"function updateTrigger(bytes32 orderbookId, bytes32 triggerOrder, int256 newSize, uint256 newLimitPrice, uint256 newTriggerPrice, uint128 deadline)",
|
|
28
29
|
"function submitBatch(bytes[] inner)",
|
|
30
|
+
"function withdraw(address token, address recipient, uint256 amount, uint128 deadline)",
|
|
29
31
|
]);
|
|
30
32
|
|
|
31
33
|
/** ~10 years in microseconds — default far-future expiry for resting orders. */
|
|
@@ -587,18 +589,9 @@ export interface MintParams {
|
|
|
587
589
|
* on rejection (e.g. "Minting is disabled") and Error on on-chain revert. */
|
|
588
590
|
export async function mint(p: MintParams): Promise<TxReceipt> {
|
|
589
591
|
const doFetch = p.fetch ?? fetch;
|
|
590
|
-
const
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
headers: { "content-type": "application/json" },
|
|
594
|
-
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params: [] }),
|
|
595
|
-
});
|
|
596
|
-
const json = (await res.json()) as { result?: string; error?: { message?: string } };
|
|
597
|
-
if (json.error || json.result === undefined) throw new Error(json.error?.message ?? `${method} failed`);
|
|
598
|
-
return json.result;
|
|
599
|
-
};
|
|
600
|
-
const chainId = p.chainId ?? Number(await rpc("eth_chainId"));
|
|
601
|
-
const maxFeePerGas = BigInt(await rpc("eth_gasPrice"));
|
|
592
|
+
const read = (method: string) => rpc<string>(p.rpcUrl, method, [], { fetch: doFetch });
|
|
593
|
+
const chainId = p.chainId ?? Number(await read("eth_chainId"));
|
|
594
|
+
const maxFeePerGas = BigInt(await read("eth_gasPrice"));
|
|
602
595
|
const signer = privateKeyToAccount(generatePrivateKey());
|
|
603
596
|
const data = encodeFunctionData({
|
|
604
597
|
abi: MINT_ABI,
|
|
@@ -684,12 +677,65 @@ export function buildWaitlistDeposit(p: WaitlistDepositParams): SourceChainCall
|
|
|
684
677
|
};
|
|
685
678
|
}
|
|
686
679
|
|
|
680
|
+
// --- withdrawals (ADR 0033) --------------------------------------------------
|
|
681
|
+
//
|
|
682
|
+
// `Clob.withdraw` settles on L1 in ONE transaction: the CLOB balance is debited
|
|
683
|
+
// straight to the bridge's custody and N−F validators sign the L1 claim at
|
|
684
|
+
// solution execution. Nothing is credited to a pod account on the way, so the
|
|
685
|
+
// legacy `Bridge.withdraw` rules — `tx.value == amount`, a gas reserve, a second
|
|
686
|
+
// signature — do not apply, and MAX is the whole withdrawable balance.
|
|
687
|
+
//
|
|
688
|
+
// Two fields mean something different from the same-named ones elsewhere:
|
|
689
|
+
// - `recipient` is an address on the CLAIM chain, not on pod. Delegated calls
|
|
690
|
+
// stay pinned to the master (ADR 0018), so a session key cannot redirect it.
|
|
691
|
+
// - `amount` stays in pod's 18 decimals, but must be a whole number of
|
|
692
|
+
// claim-chain units or admission rejects it. Size it with `maxWithdrawable`
|
|
693
|
+
// / `quantizeWithdrawAmount` from `codec/bridge.ts` — this builder encodes
|
|
694
|
+
// what it is given and does no rounding of its own, because silently
|
|
695
|
+
// changing an amount a user signed for is worse than a clear rejection.
|
|
696
|
+
|
|
697
|
+
export interface ClobWithdrawParams {
|
|
698
|
+
/** Pod-side token address (native USD for cash). */
|
|
699
|
+
token: Address;
|
|
700
|
+
/** Recipient on the **claim chain**. */
|
|
701
|
+
recipient: Address;
|
|
702
|
+
/** 1e18-scaled, and a whole number of claim-chain units. */
|
|
703
|
+
amount: bigint;
|
|
704
|
+
/**
|
|
705
|
+
* Auction interval in microseconds (`market.auctionIntervalMs * 1000`).
|
|
706
|
+
*
|
|
707
|
+
* **Required, unlike the order builders'.** Admission refuses a deadline that
|
|
708
|
+
* is not a multiple of it, and the default here is a far-future timestamp with
|
|
709
|
+
* no reason to land on one — so an optional interval would silently produce a
|
|
710
|
+
* transaction that is rejected unless the clock happened to be aligned, and
|
|
711
|
+
* the rare one that slipped through would be stranded a decade out.
|
|
712
|
+
*/
|
|
713
|
+
auctionIntervalUs: number;
|
|
714
|
+
deadline?: number; // ms; default far future
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
export function buildClobWithdraw(p: ClobWithdrawParams): PodTxRequest {
|
|
718
|
+
const nowUs = BigInt(Date.now()) * 1000n;
|
|
719
|
+
const deadline = alignDeadline(us(p.deadline, nowUs + FAR_US), BigInt(p.auctionIntervalUs));
|
|
720
|
+
const data = encodeFunctionData({
|
|
721
|
+
abi: CLOB_ABI,
|
|
722
|
+
functionName: "withdraw",
|
|
723
|
+
args: [p.token, p.recipient, p.amount, deadline],
|
|
724
|
+
}) as Hex;
|
|
725
|
+
return { to: CLOB_ADDRESS, data, value: 0n, type: "eip1559", maxPriorityFeePerGas: 0n, gas: 1_000_000n };
|
|
726
|
+
}
|
|
727
|
+
|
|
687
728
|
/**
|
|
688
729
|
* Deterministic order id: `keccak256(abi.encode(signer, nonce, sequence))`,
|
|
689
730
|
* matching the engine (`trading/src/lib.rs`). `sequence` is the 0-based index
|
|
690
731
|
* of the intent within a `submitBatch` (0 for a single-intent tx). Needs the
|
|
691
732
|
* nonce, so it's only known up-front in managed mode; in advisory mode reconcile
|
|
692
733
|
* by `tx` from the `pod_orders_v2` stream instead.
|
|
734
|
+
*
|
|
735
|
+
* **This is also the `withdrawal_id`** that `pod_withdrawals` reports and that
|
|
736
|
+
* `GET /v1/bridge/withdrawals/by-id/{id}` is keyed on — same derivation, same
|
|
737
|
+
* intent identity (ADR 0033 §3). `signer` is whoever signed the transaction,
|
|
738
|
+
* i.e. the delegate under a session key, not the debited master.
|
|
693
739
|
*/
|
|
694
740
|
export function deriveOrderId(signer: Address, nonce: number, sequence = 0): Hash {
|
|
695
741
|
return keccak256(
|