@agether/sdk 2.12.2 → 2.14.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/cli.d.ts +29 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +533 -161
- package/dist/clients/AgentIdentityClient.d.ts +200 -0
- package/dist/clients/AgentIdentityClient.d.ts.map +1 -0
- package/dist/clients/AgentIdentityClient.js +351 -0
- package/dist/clients/AgetherClient.d.ts +242 -0
- package/dist/clients/AgetherClient.d.ts.map +1 -0
- package/dist/clients/AgetherClient.js +736 -0
- package/dist/clients/MorphoClient.d.ts +572 -0
- package/dist/clients/MorphoClient.d.ts.map +1 -0
- package/dist/clients/MorphoClient.js +1974 -0
- package/dist/clients/ScoringClient.d.ts +103 -0
- package/dist/clients/ScoringClient.d.ts.map +1 -0
- package/dist/clients/ScoringClient.js +112 -0
- package/dist/clients/X402Client.d.ts +198 -0
- package/dist/clients/X402Client.d.ts.map +1 -0
- package/dist/clients/X402Client.js +438 -0
- package/dist/index.d.mts +175 -27
- package/dist/index.d.ts +175 -27
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +533 -161
- package/dist/index.mjs +533 -161
- package/dist/types/index.d.ts +132 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +46 -0
- package/dist/utils/abis.d.ts +29 -0
- package/dist/utils/abis.d.ts.map +1 -0
- package/dist/utils/abis.js +138 -0
- package/dist/utils/config.d.ts +36 -0
- package/dist/utils/config.d.ts.map +1 -0
- package/dist/utils/config.js +168 -0
- package/dist/utils/format.d.ts +44 -0
- package/dist/utils/format.d.ts.map +1 -0
- package/dist/utils/format.js +75 -0
- package/package.json +1 -1
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ScoringClient — Client for the Agether backend scoring API
|
|
3
|
+
*
|
|
4
|
+
* Supports multi-chain: pass `chainId` to target a specific chain
|
|
5
|
+
* (Base 8453, Ethereum 1). When omitted, the backend uses its default chain.
|
|
6
|
+
*
|
|
7
|
+
* Endpoints:
|
|
8
|
+
* GET /score/:agentId?chain=<chainId> — x402-gated, compute + submit score onchain
|
|
9
|
+
* GET /score/:agentId/current?chain=<chainId> — free, read current onchain score
|
|
10
|
+
* GET /health — service health
|
|
11
|
+
* GET /status — detailed status
|
|
12
|
+
* GET /agents/:agentId/details?chain=<chainId> — agent details
|
|
13
|
+
*/
|
|
14
|
+
import { ScoreResult, ChainId } from '../types';
|
|
15
|
+
import { X402Config } from './X402Client';
|
|
16
|
+
export interface ScoringClientConfig {
|
|
17
|
+
/** Backend base URL (e.g. https://api.agether.ai) */
|
|
18
|
+
endpoint: string;
|
|
19
|
+
/** Default chain to use for all calls (optional — backend falls back to its own default) */
|
|
20
|
+
chainId?: ChainId;
|
|
21
|
+
/** x402 config for paid scoring calls (optional — if not set, paid calls will fail) */
|
|
22
|
+
x402?: X402Config;
|
|
23
|
+
}
|
|
24
|
+
export declare class ScoringClient {
|
|
25
|
+
private client;
|
|
26
|
+
private x402Client?;
|
|
27
|
+
private endpoint;
|
|
28
|
+
private defaultChainId?;
|
|
29
|
+
constructor(config: ScoringClientConfig);
|
|
30
|
+
/** Build query string with ?chain= if chainId is set */
|
|
31
|
+
private chainQuery;
|
|
32
|
+
/**
|
|
33
|
+
* Request a fresh score computation.
|
|
34
|
+
*
|
|
35
|
+
* This is x402-gated: the backend returns 402, the X402Client
|
|
36
|
+
* signs an EIP-3009 payment, and the backend computes + submits
|
|
37
|
+
* the score onchain via AgentReputation.submitScore().
|
|
38
|
+
*
|
|
39
|
+
* Returns the ScoreResult with breakdown and txHash.
|
|
40
|
+
*
|
|
41
|
+
* @param agentId Agent ID
|
|
42
|
+
* @param chainId Target chain (optional — overrides default)
|
|
43
|
+
*/
|
|
44
|
+
requestScore(agentId: string | bigint, chainId?: ChainId): Promise<ScoreResult>;
|
|
45
|
+
/**
|
|
46
|
+
* Get the current onchain score (free, no payment required).
|
|
47
|
+
* @param chainId Target chain (optional — overrides default)
|
|
48
|
+
*/
|
|
49
|
+
getCurrentScore(agentId: string | bigint, chainId?: ChainId): Promise<{
|
|
50
|
+
agentId: string;
|
|
51
|
+
score: number;
|
|
52
|
+
timestamp: number;
|
|
53
|
+
signer: string;
|
|
54
|
+
fresh: boolean;
|
|
55
|
+
age: number;
|
|
56
|
+
}>;
|
|
57
|
+
/**
|
|
58
|
+
* Get detailed agent info from backend.
|
|
59
|
+
* @param chainId Target chain (optional — overrides default)
|
|
60
|
+
*/
|
|
61
|
+
getAgentDetails(agentId: string | bigint, chainId?: ChainId): Promise<{
|
|
62
|
+
agentId: string;
|
|
63
|
+
owner: string;
|
|
64
|
+
account: string;
|
|
65
|
+
accountExists: boolean;
|
|
66
|
+
kyaApproved: boolean;
|
|
67
|
+
score: number;
|
|
68
|
+
scoreFresh: boolean;
|
|
69
|
+
eligible: boolean;
|
|
70
|
+
}>;
|
|
71
|
+
/**
|
|
72
|
+
* Health check.
|
|
73
|
+
*/
|
|
74
|
+
getHealth(): Promise<{
|
|
75
|
+
status: string;
|
|
76
|
+
timestamp: string;
|
|
77
|
+
}>;
|
|
78
|
+
/**
|
|
79
|
+
* Detailed status (contracts, signer, chain).
|
|
80
|
+
*/
|
|
81
|
+
getStatus(): Promise<{
|
|
82
|
+
status: string;
|
|
83
|
+
chain: {
|
|
84
|
+
id: number;
|
|
85
|
+
name: string;
|
|
86
|
+
};
|
|
87
|
+
contracts: Record<string, string>;
|
|
88
|
+
scoring: {
|
|
89
|
+
priceUsdc: string;
|
|
90
|
+
x402PayToAddress: string;
|
|
91
|
+
};
|
|
92
|
+
signer: string;
|
|
93
|
+
}>;
|
|
94
|
+
/**
|
|
95
|
+
* Agent count and list.
|
|
96
|
+
* @param chainId Target chain (optional — overrides default)
|
|
97
|
+
*/
|
|
98
|
+
getAgentCount(chainId?: ChainId): Promise<{
|
|
99
|
+
totalAgents: number;
|
|
100
|
+
totalAccounts: number;
|
|
101
|
+
}>;
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=ScoringClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ScoringClient.d.ts","sourceRoot":"","sources":["../../src/clients/ScoringClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAGH,OAAO,EAAE,WAAW,EAAgB,OAAO,EAAE,MAAM,UAAU,CAAC;AAC9D,OAAO,EAAc,UAAU,EAAE,MAAM,cAAc,CAAC;AAEtD,MAAM,WAAW,mBAAmB;IAClC,qDAAqD;IACrD,QAAQ,EAAE,MAAM,CAAC;IACjB,4FAA4F;IAC5F,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,uFAAuF;IACvF,IAAI,CAAC,EAAE,UAAU,CAAC;CACnB;AAED,qBAAa,aAAa;IACxB,OAAO,CAAC,MAAM,CAAgB;IAC9B,OAAO,CAAC,UAAU,CAAC,CAAa;IAChC,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,cAAc,CAAC,CAAU;gBAErB,MAAM,EAAE,mBAAmB;IAavC,wDAAwD;IACxD,OAAO,CAAC,UAAU;IASlB;;;;;;;;;;;OAWG;IACG,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,WAAW,CAAC;IA6BrF;;;OAGG;IACG,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC;QAC1E,OAAO,EAAE,MAAM,CAAC;QAChB,KAAK,EAAE,MAAM,CAAC;QACd,SAAS,EAAE,MAAM,CAAC;QAClB,MAAM,EAAE,MAAM,CAAC;QACf,KAAK,EAAE,OAAO,CAAC;QACf,GAAG,EAAE,MAAM,CAAC;KACb,CAAC;IAUF;;;OAGG;IACG,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC;QAC1E,OAAO,EAAE,MAAM,CAAC;QAChB,KAAK,EAAE,MAAM,CAAC;QACd,OAAO,EAAE,MAAM,CAAC;QAChB,aAAa,EAAE,OAAO,CAAC;QACvB,WAAW,EAAE,OAAO,CAAC;QACrB,KAAK,EAAE,MAAM,CAAC;QACd,UAAU,EAAE,OAAO,CAAC;QACpB,QAAQ,EAAE,OAAO,CAAC;KACnB,CAAC;IAUF;;OAEG;IACG,SAAS,IAAI,OAAO,CAAC;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,CAAC;IAKjE;;OAEG;IACG,SAAS,IAAI,OAAO,CAAC;QACzB,MAAM,EAAE,MAAM,CAAC;QACf,KAAK,EAAE;YAAE,EAAE,EAAE,MAAM,CAAC;YAAC,IAAI,EAAE,MAAM,CAAA;SAAE,CAAC;QACpC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAClC,OAAO,EAAE;YAAE,SAAS,EAAE,MAAM,CAAC;YAAC,gBAAgB,EAAE,MAAM,CAAA;SAAE,CAAC;QACzD,MAAM,EAAE,MAAM,CAAC;KAChB,CAAC;IAKF;;;OAGG;IACG,aAAa,CAAC,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC;QAAE,WAAW,EAAE,MAAM,CAAC;QAAC,aAAa,EAAE,MAAM,CAAA;KAAE,CAAC;CAKhG"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ScoringClient — Client for the Agether backend scoring API
|
|
3
|
+
*
|
|
4
|
+
* Supports multi-chain: pass `chainId` to target a specific chain
|
|
5
|
+
* (Base 8453, Ethereum 1). When omitted, the backend uses its default chain.
|
|
6
|
+
*
|
|
7
|
+
* Endpoints:
|
|
8
|
+
* GET /score/:agentId?chain=<chainId> — x402-gated, compute + submit score onchain
|
|
9
|
+
* GET /score/:agentId/current?chain=<chainId> — free, read current onchain score
|
|
10
|
+
* GET /health — service health
|
|
11
|
+
* GET /status — detailed status
|
|
12
|
+
* GET /agents/:agentId/details?chain=<chainId> — agent details
|
|
13
|
+
*/
|
|
14
|
+
import axios from 'axios';
|
|
15
|
+
import { AgetherError } from '../types';
|
|
16
|
+
import { X402Client } from './X402Client';
|
|
17
|
+
export class ScoringClient {
|
|
18
|
+
constructor(config) {
|
|
19
|
+
this.endpoint = config.endpoint;
|
|
20
|
+
this.defaultChainId = config.chainId;
|
|
21
|
+
this.client = axios.create({
|
|
22
|
+
baseURL: config.endpoint,
|
|
23
|
+
headers: { 'Content-Type': 'application/json' },
|
|
24
|
+
timeout: 30000,
|
|
25
|
+
});
|
|
26
|
+
if (config.x402) {
|
|
27
|
+
this.x402Client = new X402Client(config.x402);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/** Build query string with ?chain= if chainId is set */
|
|
31
|
+
chainQuery(chainId) {
|
|
32
|
+
const id = chainId ?? this.defaultChainId;
|
|
33
|
+
return id != null ? `?chain=${id}` : '';
|
|
34
|
+
}
|
|
35
|
+
// ════════════════════════════════════════════════════════
|
|
36
|
+
// Score (x402-gated — computes & submits onchain)
|
|
37
|
+
// ════════════════════════════════════════════════════════
|
|
38
|
+
/**
|
|
39
|
+
* Request a fresh score computation.
|
|
40
|
+
*
|
|
41
|
+
* This is x402-gated: the backend returns 402, the X402Client
|
|
42
|
+
* signs an EIP-3009 payment, and the backend computes + submits
|
|
43
|
+
* the score onchain via AgentReputation.submitScore().
|
|
44
|
+
*
|
|
45
|
+
* Returns the ScoreResult with breakdown and txHash.
|
|
46
|
+
*
|
|
47
|
+
* @param agentId Agent ID
|
|
48
|
+
* @param chainId Target chain (optional — overrides default)
|
|
49
|
+
*/
|
|
50
|
+
async requestScore(agentId, chainId) {
|
|
51
|
+
const id = agentId.toString();
|
|
52
|
+
const qs = this.chainQuery(chainId);
|
|
53
|
+
if (!this.x402Client) {
|
|
54
|
+
throw new AgetherError('x402 config required for paid scoring. Provide x402 in ScoringClientConfig.', 'X402_NOT_CONFIGURED');
|
|
55
|
+
}
|
|
56
|
+
const result = await this.x402Client.get(`${this.endpoint}/score/${id}${qs}`);
|
|
57
|
+
if (!result.success || !result.data) {
|
|
58
|
+
throw new AgetherError(`Scoring request failed: ${result.error || 'unknown error'}`, 'SCORING_FAILED');
|
|
59
|
+
}
|
|
60
|
+
return result.data;
|
|
61
|
+
}
|
|
62
|
+
// ════════════════════════════════════════════════════════
|
|
63
|
+
// Current Score (free — reads onchain)
|
|
64
|
+
// ════════════════════════════════════════════════════════
|
|
65
|
+
/**
|
|
66
|
+
* Get the current onchain score (free, no payment required).
|
|
67
|
+
* @param chainId Target chain (optional — overrides default)
|
|
68
|
+
*/
|
|
69
|
+
async getCurrentScore(agentId, chainId) {
|
|
70
|
+
const qs = this.chainQuery(chainId);
|
|
71
|
+
const response = await this.client.get(`/score/${agentId.toString()}/current${qs}`);
|
|
72
|
+
return response.data;
|
|
73
|
+
}
|
|
74
|
+
// ════════════════════════════════════════════════════════
|
|
75
|
+
// Agent Details
|
|
76
|
+
// ════════════════════════════════════════════════════════
|
|
77
|
+
/**
|
|
78
|
+
* Get detailed agent info from backend.
|
|
79
|
+
* @param chainId Target chain (optional — overrides default)
|
|
80
|
+
*/
|
|
81
|
+
async getAgentDetails(agentId, chainId) {
|
|
82
|
+
const qs = this.chainQuery(chainId);
|
|
83
|
+
const response = await this.client.get(`/agents/${agentId.toString()}/details${qs}`);
|
|
84
|
+
return response.data;
|
|
85
|
+
}
|
|
86
|
+
// ════════════════════════════════════════════════════════
|
|
87
|
+
// Service Status
|
|
88
|
+
// ════════════════════════════════════════════════════════
|
|
89
|
+
/**
|
|
90
|
+
* Health check.
|
|
91
|
+
*/
|
|
92
|
+
async getHealth() {
|
|
93
|
+
const response = await this.client.get('/health');
|
|
94
|
+
return response.data;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Detailed status (contracts, signer, chain).
|
|
98
|
+
*/
|
|
99
|
+
async getStatus() {
|
|
100
|
+
const response = await this.client.get('/status');
|
|
101
|
+
return response.data;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Agent count and list.
|
|
105
|
+
* @param chainId Target chain (optional — overrides default)
|
|
106
|
+
*/
|
|
107
|
+
async getAgentCount(chainId) {
|
|
108
|
+
const qs = this.chainQuery(chainId);
|
|
109
|
+
const response = await this.client.get(`/agents/count${qs}`);
|
|
110
|
+
return response.data;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* x402 HTTP Client — Make paid API calls via the x402 protocol (v2)
|
|
3
|
+
*
|
|
4
|
+
* Built on top of the official @x402/fetch + @x402/evm SDK.
|
|
5
|
+
* https://docs.x402.org/getting-started/quickstart-for-buyers
|
|
6
|
+
*
|
|
7
|
+
* Flow:
|
|
8
|
+
* 1. Client → Resource Server (normal request)
|
|
9
|
+
* 2. Resource Server → 402 with PAYMENT-REQUIRED header (base64 JSON)
|
|
10
|
+
* 3. @x402/fetch auto-picks PaymentRequirements, signs EIP-3009 via EVM scheme
|
|
11
|
+
* 4. Client → Resource Server (retries with PAYMENT-SIGNATURE header)
|
|
12
|
+
* 5. Resource Server → verifies via facilitator → settles → 200 + data
|
|
13
|
+
*
|
|
14
|
+
* Auto-Draw: When autoDraw is enabled and USDC balance is insufficient,
|
|
15
|
+
* the client automatically borrows from Morpho Blue before paying.
|
|
16
|
+
*
|
|
17
|
+
* Auto-Yield: When autoYield is enabled, the client first tries to cover
|
|
18
|
+
* the deficit from earned supply yield (principal untouched) before borrowing.
|
|
19
|
+
*
|
|
20
|
+
* Waterfall when both enabled: balance → yield → borrow
|
|
21
|
+
*
|
|
22
|
+
* Spending Limits: Optional daily spending cap (dailySpendLimitUsdc) with
|
|
23
|
+
* persistent state via onSpendingUpdate callback. The caller (e.g. plugin)
|
|
24
|
+
* is responsible for persisting and restoring state via initialSpendingState.
|
|
25
|
+
*
|
|
26
|
+
* Chain support: Base (8453), Base Sepolia (84532), Ethereum (1).
|
|
27
|
+
*/
|
|
28
|
+
import type { WalletClient } from 'viem';
|
|
29
|
+
/**
|
|
30
|
+
* Convenience type alias for any viem-compatible WalletClient.
|
|
31
|
+
* Use this when declaring variables or parameters that accept
|
|
32
|
+
* viem WalletClient instances from Privy, Turnkey, MetaMask, etc.
|
|
33
|
+
*/
|
|
34
|
+
export type AgetherViemWallet = WalletClient;
|
|
35
|
+
/** Base configuration fields shared by both signing modes. */
|
|
36
|
+
interface X402BaseConfig {
|
|
37
|
+
rpcUrl: string;
|
|
38
|
+
agentId?: string;
|
|
39
|
+
accountAddress?: string;
|
|
40
|
+
/**
|
|
41
|
+
* Auto-draw: automatically borrow from Morpho Blue when USDC balance
|
|
42
|
+
* is insufficient for x402 payment. Requires agentId to be set.
|
|
43
|
+
* Default: false
|
|
44
|
+
*/
|
|
45
|
+
autoDraw?: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Auto-yield: when USDC is insufficient, try to cover the deficit from
|
|
48
|
+
* earned supply yield BEFORE borrowing. Withdraws only the yield portion
|
|
49
|
+
* (principal stays intact). Works independently or combined with autoDraw.
|
|
50
|
+
*
|
|
51
|
+
* Waterfall when both enabled: balance → yield → borrow
|
|
52
|
+
* Default: false
|
|
53
|
+
*/
|
|
54
|
+
autoYield?: boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Daily spending limit in USDC (e.g. '100' for $100/day).
|
|
57
|
+
* Tracks cumulative daily borrows and rejects auto-draw if exceeded.
|
|
58
|
+
*/
|
|
59
|
+
dailySpendLimitUsdc?: string;
|
|
60
|
+
/**
|
|
61
|
+
* Pre-loaded spending state from a previous session.
|
|
62
|
+
* Pass this to resume the daily spending tracker after a restart.
|
|
63
|
+
* If the date doesn't match today, it's ignored (fresh day).
|
|
64
|
+
*/
|
|
65
|
+
initialSpendingState?: {
|
|
66
|
+
date: string;
|
|
67
|
+
totalBorrowed: string;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Called after every auto-draw borrow so the caller can persist the
|
|
71
|
+
* updated spending state (e.g. to a cache file). Receives the full
|
|
72
|
+
* SpendingTracker with the current date and cumulative amount.
|
|
73
|
+
*/
|
|
74
|
+
onSpendingUpdate?: (state: {
|
|
75
|
+
date: string;
|
|
76
|
+
totalBorrowed: string;
|
|
77
|
+
}) => void;
|
|
78
|
+
/**
|
|
79
|
+
* ERC-7579 validator module address (e.g. Agether8004ValidationModule).
|
|
80
|
+
* Required for Safe7579 smart wallets so that `isValidSignature` calls
|
|
81
|
+
* are routed through the correct validator. The signature is prefixed
|
|
82
|
+
* with this 20-byte address before being sent.
|
|
83
|
+
*/
|
|
84
|
+
validatorModule?: string;
|
|
85
|
+
}
|
|
86
|
+
/** Config with SDK-managed private key (existing behavior). */
|
|
87
|
+
interface X402PrivateKeyConfig extends X402BaseConfig {
|
|
88
|
+
/** Raw hex private key (with or without '0x' prefix). SDK creates the wallet internally. */
|
|
89
|
+
privateKey: string;
|
|
90
|
+
walletClient?: never;
|
|
91
|
+
}
|
|
92
|
+
/** Config with caller-provided viem WalletClient (custody-agnostic). */
|
|
93
|
+
interface X402WalletClientConfig extends X402BaseConfig {
|
|
94
|
+
/**
|
|
95
|
+
* A viem WalletClient with an attached account (e.g. from Privy, Turnkey, MetaMask).
|
|
96
|
+
* The client **must** have an `account` property set. The SDK will use it for
|
|
97
|
+
* EIP-712 typed-data signing and will never recreate or reconnect the client.
|
|
98
|
+
*/
|
|
99
|
+
walletClient: WalletClient;
|
|
100
|
+
privateKey?: never;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* X402Client configuration.
|
|
104
|
+
*
|
|
105
|
+
* Provide **either** `privateKey` (SDK manages wallet) **or** `walletClient`
|
|
106
|
+
* (external custody provider). The two fields are mutually exclusive.
|
|
107
|
+
*/
|
|
108
|
+
export type X402Config = X402PrivateKeyConfig | X402WalletClientConfig;
|
|
109
|
+
export interface X402Response<T = unknown> {
|
|
110
|
+
success: boolean;
|
|
111
|
+
data?: T;
|
|
112
|
+
error?: string;
|
|
113
|
+
paymentInfo?: {
|
|
114
|
+
amount: string;
|
|
115
|
+
asset: string;
|
|
116
|
+
network: string;
|
|
117
|
+
txHash?: string;
|
|
118
|
+
};
|
|
119
|
+
autoDrawInfo?: {
|
|
120
|
+
borrowed?: string;
|
|
121
|
+
borrowTx?: string;
|
|
122
|
+
yieldWithdrawn?: string;
|
|
123
|
+
yieldTx?: string;
|
|
124
|
+
reason: string;
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/** One item inside the `accepts` array returned by the resource server */
|
|
128
|
+
export interface PaymentRequirements {
|
|
129
|
+
scheme: string;
|
|
130
|
+
network: string;
|
|
131
|
+
amount: string;
|
|
132
|
+
asset: string;
|
|
133
|
+
payTo: string;
|
|
134
|
+
maxTimeoutSeconds: number;
|
|
135
|
+
extra?: Record<string, unknown>;
|
|
136
|
+
}
|
|
137
|
+
/** Spending tracker — tracks cumulative daily borrows */
|
|
138
|
+
export interface SpendingTracker {
|
|
139
|
+
/** Date string (YYYY-MM-DD UTC) */
|
|
140
|
+
date: string;
|
|
141
|
+
/** Cumulative USDC borrowed today (6 decimal raw units) */
|
|
142
|
+
totalBorrowed: bigint;
|
|
143
|
+
/** Daily limit in raw units (6 decimals), 0 = unlimited */
|
|
144
|
+
dailyLimit: bigint;
|
|
145
|
+
}
|
|
146
|
+
export declare class X402Client {
|
|
147
|
+
private config;
|
|
148
|
+
private paidFetch;
|
|
149
|
+
private address;
|
|
150
|
+
private _spendingTracker;
|
|
151
|
+
constructor(config: X402Config);
|
|
152
|
+
get<T = unknown>(url: string, opts?: RequestInit): Promise<X402Response<T>>;
|
|
153
|
+
post<T = unknown>(url: string, body?: unknown, opts?: RequestInit): Promise<X402Response<T>>;
|
|
154
|
+
getAddress(): string;
|
|
155
|
+
/** Get the current spending tracker state */
|
|
156
|
+
getSpendingTracker(): SpendingTracker;
|
|
157
|
+
/** Get remaining daily spending allowance in USDC (human-readable) */
|
|
158
|
+
getRemainingDailyAllowance(): string;
|
|
159
|
+
/**
|
|
160
|
+
* Pay with auto-funding: Make an x402 request with automatic USDC sourcing.
|
|
161
|
+
*
|
|
162
|
+
* Uses a **plan-then-execute** approach: all reads happen first, and if the
|
|
163
|
+
* full deficit can't be covered the method fails with NO side-effects
|
|
164
|
+
* (no yield withdrawn, no USDC borrowed).
|
|
165
|
+
*
|
|
166
|
+
* Waterfall (when both autoYield + autoDraw enabled):
|
|
167
|
+
* 1. Check USDC balance on AgentAccount
|
|
168
|
+
* 2. Probe the URL to discover payment amount (if 402)
|
|
169
|
+
* 3. If insufficient USDC — PLANNING PHASE (read-only):
|
|
170
|
+
* a. Calculate total available yield across supply positions
|
|
171
|
+
* b. Calculate max borrowable from Morpho markets
|
|
172
|
+
* c. Verify yield + borrow can cover full deficit — if not, fail immediately
|
|
173
|
+
* 4. EXECUTION PHASE (only if plan is feasible):
|
|
174
|
+
* a. Withdraw needed yield from supply positions
|
|
175
|
+
* b. Borrow remaining deficit from Morpho
|
|
176
|
+
* 5. Proceed with x402 payment
|
|
177
|
+
*/
|
|
178
|
+
payWithAutoDraw<T = unknown>(url: string, opts?: RequestInit & {
|
|
179
|
+
morphoClient?: any;
|
|
180
|
+
}): Promise<X402Response<T>>;
|
|
181
|
+
private request;
|
|
182
|
+
/**
|
|
183
|
+
* Probe a URL to discover payment requirements without paying.
|
|
184
|
+
* Makes a request and parses the 402 PAYMENT-REQUIRED header.
|
|
185
|
+
* @returns Payment amount in raw USDC units (6 decimals), or null if not a 402.
|
|
186
|
+
*/
|
|
187
|
+
private _probePaymentAmount;
|
|
188
|
+
/** Reset spending tracker if it's a new day */
|
|
189
|
+
private _resetTrackerIfNewDay;
|
|
190
|
+
/** Track a new spending amount and notify the caller for persistence */
|
|
191
|
+
private _trackSpending;
|
|
192
|
+
/**
|
|
193
|
+
* Check if a borrow amount is within the fixed daily spending limit.
|
|
194
|
+
*/
|
|
195
|
+
private _checkSpendingLimit;
|
|
196
|
+
}
|
|
197
|
+
export {};
|
|
198
|
+
//# sourceMappingURL=X402Client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"X402Client.d.ts","sourceRoot":"","sources":["../../src/clients/X402Client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAMH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AAIzC;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,GAAG,YAAY,CAAC;AAE7C,8DAA8D;AAC9D,UAAU,cAAc;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB;;;;;;;OAOG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB;;;OAGG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B;;;;OAIG;IACH,oBAAoB,CAAC,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,aAAa,EAAE,MAAM,CAAA;KAAE,CAAC;IAC/D;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,CAAC,KAAK,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,aAAa,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,CAAC;IAC5E;;;;;OAKG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED,+DAA+D;AAC/D,UAAU,oBAAqB,SAAQ,cAAc;IACnD,4FAA4F;IAC5F,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,CAAC,EAAE,KAAK,CAAC;CACtB;AAED,wEAAwE;AACxE,UAAU,sBAAuB,SAAQ,cAAc;IACrD;;;;OAIG;IACH,YAAY,EAAE,YAAY,CAAC;IAC3B,UAAU,CAAC,EAAE,KAAK,CAAC;CACpB;AAED;;;;;GAKG;AACH,MAAM,MAAM,UAAU,GA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