@nickthelegend69/fund402 0.1.0 → 0.1.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 CHANGED
@@ -128,6 +128,12 @@ const data = await res.json(); // paid + served — the agent's own balance ca
128
128
  Prefer Axios? `withPaymentInterceptor(config)` returns an `AxiosInstance` with the
129
129
  same behaviour (Axios is an optional peer dependency).
130
130
 
131
+ **Tiers & collateral.** Trusted (Tier-3) agents borrow with **zero collateral**.
132
+ New/established (Tier-1/2) agents must post 150% collateral — `fund402Fetch`
133
+ **auto-approves** the vault to escrow it before borrowing (`collateralRatio`, default
134
+ `1.5`; set `autoApprove: false` to manage the allowance yourself, or `collateralRatio: 0`
135
+ for Tier-3).
136
+
131
137
  ---
132
138
 
133
139
  ## How settlement works (and why you can trust it)
@@ -165,10 +171,17 @@ hits the endpoint → gets a 402 → borrows from the pool (the vault fronts the
165
171
  the merchant) → retries → the server verifies the settlement on-chain via CSPR.cloud
166
172
  → serves the resource.
167
173
 
168
- Last live run agent `01baa8d8…` (Tier-3, **zero collateral**) borrowed `1000000`
169
- F402; the pool fronted it to the merchant and the server served the resource after
170
- verifying it on-chain:
171
- **[settlement deploy `96f30ddf…` ↗](https://testnet.cspr.live/deploy/96f30ddfac9b3b8bc04a9fe274b1c006aff398ac624e7360669a2c1f3dc28264)** (`status: processed`).
174
+ Both credit paths are proven live through the SDK:
175
+
176
+ - **Tier-3 (zero collateral)** — agent `01baa8d8…` borrowed `1000000` F402; the pool
177
+ fronted it to the merchant.
178
+ [settlement deploy `96f30ddf…` ↗](https://testnet.cspr.live/deploy/96f30ddfac9b3b8bc04a9fe274b1c006aff398ac624e7360669a2c1f3dc28264) (`status: processed`).
179
+ - **Tier-1 (150% collateral)** — agent `017e90a1…` had `fund402Fetch` auto-approve and
180
+ **escrow `1500000` collateral** ([approve `f088362a…`](https://testnet.cspr.live/deploy/f088362a89720107dfe475ba8bc0660dbdb278cf25c3d633cf4e846414d8aa47)),
181
+ then borrow; verified on-chain (`collateral=1500000`).
182
+ [settlement deploy `a9dd1581…` ↗](https://testnet.cspr.live/deploy/a9dd158199ca65fda900863cd43db577983c1dcf3d6661409bf8d4c56bab649f) (`status: processed`).
183
+
184
+ Run them yourself: `npm run test:e2e` (Tier-3) and `npm run test:e2e:collateral` (Tier-1).
172
185
 
173
186
  ## API
174
187
 
package/dist/casper.js CHANGED
@@ -118,7 +118,9 @@ async function ensureCollateralAllowance(cfg, spender, amount) {
118
118
  spender: pkgKey(spender.vaultContractHash),
119
119
  amount: casper_js_sdk_1.CLValue.newCLUInt256(amount.toString()),
120
120
  }));
121
- const payment = casper_js_sdk_1.ExecutableDeployItem.standardPayment("2000000000");
121
+ // 5 CSPR — the gas the proven live approves use; lower (e.g. 2 CSPR) is rejected
122
+ // by Condor at submission as Invalid Deploy.
123
+ const payment = casper_js_sdk_1.ExecutableDeployItem.standardPayment("5000000000");
122
124
  const deploy = casper_js_sdk_1.Deploy.makeDeploy(header, payment, session);
123
125
  deploy.sign(signer);
124
126
  const result = await client.putDeploy(deploy);
package/dist/client.d.ts CHANGED
@@ -16,6 +16,8 @@ export interface Fund402ClientConfig {
16
16
  keyAlgorithm?: KeyAlgorithm;
17
17
  /** Over-collateralisation ratio the agent posts (vault re-checks on-chain). Default 1.5. */
18
18
  collateralRatio?: number;
19
+ /** Auto-approve the vault to escrow collateral before a Tier-1/2 borrow. Default true. */
20
+ autoApprove?: boolean;
19
21
  /** Gas (motes) for the borrow_and_pay deploy. Default 5 CSPR. */
20
22
  borrowGasMotes?: string;
21
23
  /** Underlying fetch implementation. Default global fetch. */
@@ -23,7 +25,7 @@ export interface Fund402ClientConfig {
23
25
  onEvent?: (event: Fund402Event) => void;
24
26
  }
25
27
  export interface Fund402Event {
26
- type: "intercepted_402" | "borrowing" | "borrow_submitted" | "payment_settled" | "payment_sent" | "request_retried" | "payment_confirmed";
28
+ type: "intercepted_402" | "approving" | "approve_submitted" | "borrowing" | "borrow_submitted" | "payment_settled" | "payment_sent" | "request_retried" | "payment_confirmed";
27
29
  data: Record<string, unknown>;
28
30
  timestamp: number;
29
31
  }
package/dist/client.js CHANGED
@@ -79,6 +79,20 @@ async function payViaPool(config, option, resource) {
79
79
  const amount = BigInt(option.amount);
80
80
  const ratio = config.collateralRatio ?? 1.5;
81
81
  const collateral = (amount * BigInt(Math.round(ratio * 100))) / 100n;
82
+ // Tier-1/2 collateralized borrow: the vault escrows `collateral` via transfer_from,
83
+ // so the agent must approve it on the CEP-18 asset first. Tier-3 borrows post zero
84
+ // collateral and skip this entirely.
85
+ if (collateral > 0n && config.autoApprove !== false) {
86
+ const asset = (option.asset ?? "").replace(/^0x/, "");
87
+ if (!asset)
88
+ throw new Error("402 challenge has no `asset` — cannot approve collateral");
89
+ emit(config, "approving", { asset, collateral: collateral.toString() });
90
+ const { deployHash: approveHash } = await (0, casper_1.ensureCollateralAllowance)({ ...wiring, assetPackageHash: asset }, { vaultContractHash: wiring.vaultContractHash }, collateral);
91
+ emit(config, "approve_submitted", { deployHash: approveHash });
92
+ const okApprove = await (0, casper_1.waitForDeploy)(wiring, approveHash);
93
+ if (!okApprove)
94
+ throw new Error(`collateral approve deploy failed: ${approveHash}`);
95
+ }
82
96
  emit(config, "borrowing", { amount: amount.toString(), collateral: collateral.toString() });
83
97
  const { deployHash } = await (0, casper_1.borrowAndPayOnChain)(wiring, {
84
98
  merchant: option.payTo,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nickthelegend69/fund402",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Create x402-gated HTTP endpoints settled by the Fund402 lending pool on Casper — and the agent client that pays them with just-in-time credit. Drop-in middleware for Express, Hono and Next.js.",
5
5
  "keywords": [
6
6
  "x402",
@@ -36,7 +36,8 @@
36
36
  "build": "tsc -p tsconfig.json",
37
37
  "prepublishOnly": "npm run build",
38
38
  "test": "npm run build && node test/server.test.mjs && node test/eip712.test.mjs",
39
- "test:e2e": "npm run build && node test/e2e-live.mjs"
39
+ "test:e2e": "npm run build && node test/e2e-live.mjs",
40
+ "test:e2e:collateral": "npm run build && node test/e2e-collateral.mjs"
40
41
  },
41
42
  "dependencies": {
42
43
  "@noble/hashes": "^1.4.0",