@agether/sdk 1.0.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/README.md +480 -0
- package/dist/cli.d.mts +2 -0
- package/dist/cli.d.ts +19 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +2149 -0
- package/dist/cli.mjs +0 -0
- package/dist/clients/AgentIdentityClient.d.ts +163 -0
- package/dist/clients/AgentIdentityClient.d.ts.map +1 -0
- package/dist/clients/AgentIdentityClient.js +293 -0
- package/dist/clients/AgetherClient.d.ts +101 -0
- package/dist/clients/AgetherClient.d.ts.map +1 -0
- package/dist/clients/AgetherClient.js +272 -0
- package/dist/clients/ScoringClient.d.ts +138 -0
- package/dist/clients/ScoringClient.d.ts.map +1 -0
- package/dist/clients/ScoringClient.js +135 -0
- package/dist/clients/VaultClient.d.ts +62 -0
- package/dist/clients/VaultClient.d.ts.map +1 -0
- package/dist/clients/VaultClient.js +157 -0
- package/dist/clients/WalletClient.d.ts +73 -0
- package/dist/clients/WalletClient.d.ts.map +1 -0
- package/dist/clients/WalletClient.js +174 -0
- package/dist/clients/X402Client.d.ts +61 -0
- package/dist/clients/X402Client.d.ts.map +1 -0
- package/dist/clients/X402Client.js +303 -0
- package/dist/index.d.mts +932 -0
- package/dist/index.d.ts +932 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1680 -0
- package/dist/index.mjs +1610 -0
- package/dist/types/index.d.ts +220 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +52 -0
- package/dist/utils/abis.d.ts +21 -0
- package/dist/utils/abis.d.ts.map +1 -0
- package/dist/utils/abis.js +134 -0
- package/dist/utils/config.d.ts +31 -0
- package/dist/utils/config.d.ts.map +1 -0
- package/dist/utils/config.js +117 -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 +57 -0
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AgetherClient - Main SDK client for AI agents (v2)
|
|
3
|
+
*
|
|
4
|
+
* v2 flow:
|
|
5
|
+
* 1. Agent registers via ERC-8004 → gets agentId
|
|
6
|
+
* 2. AccountFactory.createAccount(agentId) → AgentAccount (smart wallet)
|
|
7
|
+
* 3. Agent calls applyForCredit(account, requestedLimit) on ReputationCredit
|
|
8
|
+
* 4. Backend scoring evaluates → calls approveCreditLine / rejectCreditLine
|
|
9
|
+
* 5. Agent draws/repays via AgentAccount.drawCredit / repayCredit
|
|
10
|
+
*/
|
|
11
|
+
import { ethers, Contract } from 'ethers';
|
|
12
|
+
import { AgetherError, InsufficientCreditError, } from '../types';
|
|
13
|
+
import { ACCOUNT_FACTORY_ABI, AGENT_ACCOUNT_ABI, REPUTATION_CREDIT_ABI, ERC20_ABI, } from '../utils/abis';
|
|
14
|
+
import { getDefaultConfig } from '../utils/config';
|
|
15
|
+
export class AgetherClient {
|
|
16
|
+
constructor(options) {
|
|
17
|
+
this.config = options.config;
|
|
18
|
+
this.signer = options.signer;
|
|
19
|
+
this.agentId = options.agentId;
|
|
20
|
+
const provider = options.signer.provider;
|
|
21
|
+
if (!provider) {
|
|
22
|
+
throw new AgetherError('Signer must have a provider', 'NO_PROVIDER');
|
|
23
|
+
}
|
|
24
|
+
// Initialize contracts
|
|
25
|
+
this.accountFactory = new Contract(options.config.contracts.accountFactory, ACCOUNT_FACTORY_ABI, options.signer);
|
|
26
|
+
this.reputationCredit = new Contract(options.config.contracts.reputationCredit, REPUTATION_CREDIT_ABI, options.signer);
|
|
27
|
+
}
|
|
28
|
+
// ============ Static Factory ============
|
|
29
|
+
/**
|
|
30
|
+
* Create client from private key.
|
|
31
|
+
*
|
|
32
|
+
* Simplest usage — only needs key, agentId, and chainId:
|
|
33
|
+
* AgetherClient.fromPrivateKey(key, 42n, ChainId.Sepolia)
|
|
34
|
+
*
|
|
35
|
+
* All contract addresses and RPC URLs are resolved automatically.
|
|
36
|
+
*/
|
|
37
|
+
static fromPrivateKey(privateKey, agentId, chainIdOrConfig) {
|
|
38
|
+
const config = typeof chainIdOrConfig === 'number'
|
|
39
|
+
? getDefaultConfig(chainIdOrConfig)
|
|
40
|
+
: chainIdOrConfig;
|
|
41
|
+
const provider = new ethers.JsonRpcProvider(config.rpcUrl);
|
|
42
|
+
const signer = new ethers.Wallet(privateKey, provider);
|
|
43
|
+
return new AgetherClient({ config, signer, agentId });
|
|
44
|
+
}
|
|
45
|
+
// ============ Account Management ============
|
|
46
|
+
/**
|
|
47
|
+
* Create an AgentAccount (smart wallet) for this agent.
|
|
48
|
+
* Returns the account address.
|
|
49
|
+
*/
|
|
50
|
+
async createAccount() {
|
|
51
|
+
const tx = await this.accountFactory.createAccount(this.agentId);
|
|
52
|
+
const receipt = await tx.wait();
|
|
53
|
+
// Parse AccountCreated event
|
|
54
|
+
const event = receipt.logs
|
|
55
|
+
.map((log) => {
|
|
56
|
+
try {
|
|
57
|
+
return this.accountFactory.interface.parseLog(log);
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
.find((e) => e?.name === 'AccountCreated');
|
|
64
|
+
if (event) {
|
|
65
|
+
this.accountAddress = event.args.account;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
// Fallback: query from factory
|
|
69
|
+
this.accountAddress = await this.accountFactory.getAccount(this.agentId);
|
|
70
|
+
}
|
|
71
|
+
return this.accountAddress;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Get the AgentAccount address for this agent.
|
|
75
|
+
*/
|
|
76
|
+
async getAccountAddress() {
|
|
77
|
+
if (this.accountAddress)
|
|
78
|
+
return this.accountAddress;
|
|
79
|
+
const addr = await this.accountFactory.getAccount(this.agentId);
|
|
80
|
+
if (addr === ethers.ZeroAddress) {
|
|
81
|
+
throw new AgetherError('No account found. Create one first with createAccount().', 'NO_ACCOUNT');
|
|
82
|
+
}
|
|
83
|
+
this.accountAddress = addr;
|
|
84
|
+
return addr;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Check if an account already exists.
|
|
88
|
+
*/
|
|
89
|
+
async accountExists() {
|
|
90
|
+
return this.accountFactory.accountExists(this.agentId);
|
|
91
|
+
}
|
|
92
|
+
// ============ Credit Application ============
|
|
93
|
+
/**
|
|
94
|
+
* Apply for a credit line.
|
|
95
|
+
* Submits on-chain application → backend evaluates → approves/rejects.
|
|
96
|
+
*/
|
|
97
|
+
async apply(limitOrApplication) {
|
|
98
|
+
const limit = typeof limitOrApplication === 'bigint'
|
|
99
|
+
? limitOrApplication
|
|
100
|
+
: limitOrApplication.requestedLimit;
|
|
101
|
+
const account = await this.getAccountAddress();
|
|
102
|
+
const tx = await this.reputationCredit.applyForCredit(account, limit);
|
|
103
|
+
const receipt = await tx.wait();
|
|
104
|
+
// Parse CreditApplied event
|
|
105
|
+
const event = receipt.logs
|
|
106
|
+
.map((log) => {
|
|
107
|
+
try {
|
|
108
|
+
return this.reputationCredit.interface.parseLog(log);
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
})
|
|
114
|
+
.find((e) => e?.name === 'CreditApplied');
|
|
115
|
+
if (!event) {
|
|
116
|
+
throw new AgetherError('Failed to parse CreditApplied event', 'EVENT_PARSE_ERROR');
|
|
117
|
+
}
|
|
118
|
+
return receipt.hash;
|
|
119
|
+
}
|
|
120
|
+
// ============ Credit Line Info ============
|
|
121
|
+
/**
|
|
122
|
+
* Get full credit line details from ReputationCredit.
|
|
123
|
+
*/
|
|
124
|
+
async getCreditLine() {
|
|
125
|
+
const account = await this.getAccountAddress();
|
|
126
|
+
const data = await this.reputationCredit.getCreditLineDetails(account);
|
|
127
|
+
return {
|
|
128
|
+
agentId: data[0],
|
|
129
|
+
limit: data[1],
|
|
130
|
+
used: data[2],
|
|
131
|
+
aprBps: data[3],
|
|
132
|
+
accruedInterest: data[4],
|
|
133
|
+
requestedLimit: data[5],
|
|
134
|
+
createdAt: data[6],
|
|
135
|
+
lastActivityAt: data[7],
|
|
136
|
+
status: Number(data[8]),
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Get ICreditProvider.CreditInfo view.
|
|
141
|
+
*/
|
|
142
|
+
async getCreditInfo() {
|
|
143
|
+
const account = await this.getAccountAddress();
|
|
144
|
+
const info = await this.reputationCredit.getCreditInfo(account);
|
|
145
|
+
return {
|
|
146
|
+
limit: info.limit,
|
|
147
|
+
used: info.used,
|
|
148
|
+
available: info.available,
|
|
149
|
+
accruedInterest: info.accruedInterest,
|
|
150
|
+
aprBps: info.aprBps,
|
|
151
|
+
isActive: info.isActive,
|
|
152
|
+
requiresCollateral: info.requiresCollateral,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Get credit line status.
|
|
157
|
+
*/
|
|
158
|
+
async getStatus() {
|
|
159
|
+
const account = await this.getAccountAddress();
|
|
160
|
+
const status = await this.reputationCredit.getCreditLineStatus(account);
|
|
161
|
+
return Number(status);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Get available credit.
|
|
165
|
+
*/
|
|
166
|
+
async getAvailableCredit() {
|
|
167
|
+
const account = await this.getAccountAddress();
|
|
168
|
+
return this.reputationCredit.maxDrawable(account);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Get total debt (principal + interest).
|
|
172
|
+
*/
|
|
173
|
+
async getTotalDebt() {
|
|
174
|
+
const account = await this.getAccountAddress();
|
|
175
|
+
return this.reputationCredit.getTotalDebt(account);
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Check if agent is eligible for credit.
|
|
179
|
+
*/
|
|
180
|
+
async isEligible() {
|
|
181
|
+
const account = await this.getAccountAddress();
|
|
182
|
+
return this.reputationCredit.isEligible(account);
|
|
183
|
+
}
|
|
184
|
+
// ============ Draw & Repay ============
|
|
185
|
+
/**
|
|
186
|
+
* Draw funds from credit line via AgentAccount.
|
|
187
|
+
*/
|
|
188
|
+
async draw(amountOrRequest) {
|
|
189
|
+
const account = await this.getAccountAddress();
|
|
190
|
+
const amount = typeof amountOrRequest === 'bigint'
|
|
191
|
+
? amountOrRequest
|
|
192
|
+
: amountOrRequest.amount;
|
|
193
|
+
// Check available credit
|
|
194
|
+
const available = await this.getAvailableCredit();
|
|
195
|
+
if (amount > available) {
|
|
196
|
+
throw new InsufficientCreditError(available, amount);
|
|
197
|
+
}
|
|
198
|
+
// Draw via AgentAccount
|
|
199
|
+
const agentAccount = new Contract(account, AGENT_ACCOUNT_ABI, this.signer);
|
|
200
|
+
const tx = await agentAccount.drawCredit(this.config.contracts.reputationCredit, amount);
|
|
201
|
+
const receipt = await tx.wait();
|
|
202
|
+
return {
|
|
203
|
+
txHash: receipt.hash,
|
|
204
|
+
blockNumber: receipt.blockNumber,
|
|
205
|
+
status: receipt.status === 1 ? 'success' : 'failed',
|
|
206
|
+
gasUsed: receipt.gasUsed,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Repay credit line debt via AgentAccount.
|
|
211
|
+
*/
|
|
212
|
+
async repay(amountOrRequest) {
|
|
213
|
+
const account = await this.getAccountAddress();
|
|
214
|
+
const amount = typeof amountOrRequest === 'bigint'
|
|
215
|
+
? amountOrRequest
|
|
216
|
+
: amountOrRequest.amount;
|
|
217
|
+
// Approve stablecoin spending on the AgentAccount
|
|
218
|
+
const stablecoin = new Contract(this.config.contracts.usdc, ERC20_ABI, this.signer);
|
|
219
|
+
const allowance = await stablecoin.allowance(await this.signer.getAddress(), account);
|
|
220
|
+
if (allowance < amount) {
|
|
221
|
+
const approveTx = await stablecoin.approve(account, amount);
|
|
222
|
+
await approveTx.wait();
|
|
223
|
+
}
|
|
224
|
+
// Fund account then repay
|
|
225
|
+
const agentAccount = new Contract(account, AGENT_ACCOUNT_ABI, this.signer);
|
|
226
|
+
await (await agentAccount.fund(this.config.contracts.usdc, amount)).wait();
|
|
227
|
+
const tx = await agentAccount.repayCredit(this.config.contracts.reputationCredit, amount);
|
|
228
|
+
const receipt = await tx.wait();
|
|
229
|
+
return {
|
|
230
|
+
txHash: receipt.hash,
|
|
231
|
+
blockNumber: receipt.blockNumber,
|
|
232
|
+
status: receipt.status === 1 ? 'success' : 'failed',
|
|
233
|
+
gasUsed: receipt.gasUsed,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Repay full debt.
|
|
238
|
+
*/
|
|
239
|
+
async repayFull() {
|
|
240
|
+
const debt = await this.getTotalDebt();
|
|
241
|
+
// Add 0.1% buffer for accruing interest
|
|
242
|
+
const amount = debt + debt / 1000n;
|
|
243
|
+
return this.repay(amount);
|
|
244
|
+
}
|
|
245
|
+
// ============ Limit Increase ============
|
|
246
|
+
/**
|
|
247
|
+
* Request a limit increase on-chain.
|
|
248
|
+
*/
|
|
249
|
+
async requestLimitIncrease(newLimit) {
|
|
250
|
+
const account = await this.getAccountAddress();
|
|
251
|
+
const tx = await this.reputationCredit.requestLimitIncrease(account, newLimit);
|
|
252
|
+
const receipt = await tx.wait();
|
|
253
|
+
return receipt.hash;
|
|
254
|
+
}
|
|
255
|
+
// ============ x402 Payments ============
|
|
256
|
+
/**
|
|
257
|
+
* Pay for a service using credit line.
|
|
258
|
+
*/
|
|
259
|
+
async pay(_service, amount, _asset = 'USDC') {
|
|
260
|
+
return this.draw(amount);
|
|
261
|
+
}
|
|
262
|
+
// ============ Getters ============
|
|
263
|
+
get chainId() {
|
|
264
|
+
return this.config.chainId;
|
|
265
|
+
}
|
|
266
|
+
get contracts() {
|
|
267
|
+
return this.config.contracts;
|
|
268
|
+
}
|
|
269
|
+
get currentAccountAddress() {
|
|
270
|
+
return this.accountAddress;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ScoringClient - Client for the Agether backend scoring API (v2)
|
|
3
|
+
*
|
|
4
|
+
* Connects to backend service at /credit/* and /admin/* endpoints.
|
|
5
|
+
* The backend evaluates agents based on:
|
|
6
|
+
* - On-chain reputation (AgentReputation contract, Bayesian scoring)
|
|
7
|
+
* - Code audit status (ValidationRegistry)
|
|
8
|
+
* - Default history
|
|
9
|
+
* - Risk model
|
|
10
|
+
*
|
|
11
|
+
* v2: account-based (not creditLineId-based)
|
|
12
|
+
*/
|
|
13
|
+
import { ScoringRequest, ScoringResult } from '../types';
|
|
14
|
+
export interface ScoringContext {
|
|
15
|
+
purpose?: string;
|
|
16
|
+
reasoningTrace?: string[];
|
|
17
|
+
runtimeEnv?: string;
|
|
18
|
+
}
|
|
19
|
+
export interface RiskCheckResponse {
|
|
20
|
+
riskScore: number;
|
|
21
|
+
level: 'low' | 'medium' | 'high';
|
|
22
|
+
wouldApprove: boolean;
|
|
23
|
+
factors: RiskFactor[];
|
|
24
|
+
}
|
|
25
|
+
export interface RiskFactor {
|
|
26
|
+
name: string;
|
|
27
|
+
score: number;
|
|
28
|
+
weight: number;
|
|
29
|
+
description: string;
|
|
30
|
+
}
|
|
31
|
+
export interface GraduationStatus {
|
|
32
|
+
eligible: boolean;
|
|
33
|
+
reason: string;
|
|
34
|
+
stats: {
|
|
35
|
+
onTimeRepayments: number;
|
|
36
|
+
totalVolume: string;
|
|
37
|
+
creditScore: number;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export declare class ScoringClient {
|
|
41
|
+
private client;
|
|
42
|
+
constructor(endpoint: string, apiKey?: string);
|
|
43
|
+
/**
|
|
44
|
+
* Evaluate a credit application (does not submit on-chain)
|
|
45
|
+
* Backend endpoint: POST /credit/evaluate
|
|
46
|
+
*/
|
|
47
|
+
evaluateCredit(request: ScoringRequest): Promise<ScoringResult>;
|
|
48
|
+
/**
|
|
49
|
+
* Process a credit application (evaluate + on-chain approve/reject)
|
|
50
|
+
* Backend endpoint: POST /admin/credit/process
|
|
51
|
+
*
|
|
52
|
+
* Requires the credit application to be in Pending status on-chain
|
|
53
|
+
* (agent must have called applyForCredit first).
|
|
54
|
+
*/
|
|
55
|
+
processApplication(agentId: bigint, requestedLimit: bigint, codeHash?: string): Promise<{
|
|
56
|
+
success: boolean;
|
|
57
|
+
txHash?: string;
|
|
58
|
+
error?: string;
|
|
59
|
+
}>;
|
|
60
|
+
/**
|
|
61
|
+
* Get credit score for an agent
|
|
62
|
+
* Backend endpoint: GET /credit/score/:agentId
|
|
63
|
+
*/
|
|
64
|
+
getCreditScore(agentId: bigint): Promise<{
|
|
65
|
+
agentId: string;
|
|
66
|
+
creditScore: number;
|
|
67
|
+
bayesianScore: number;
|
|
68
|
+
confidence: number;
|
|
69
|
+
}>;
|
|
70
|
+
/**
|
|
71
|
+
* Preview scored limit for an agent
|
|
72
|
+
* Backend endpoint: GET /credit/preview/:agentId
|
|
73
|
+
*/
|
|
74
|
+
previewScoredLimit(agentId: bigint): Promise<{
|
|
75
|
+
limit: string;
|
|
76
|
+
score: string;
|
|
77
|
+
eligible: boolean;
|
|
78
|
+
}>;
|
|
79
|
+
/**
|
|
80
|
+
* Get credit line info for an agent
|
|
81
|
+
* Backend endpoint: GET /credit/agent/:agentId
|
|
82
|
+
*/
|
|
83
|
+
getAgentCredit(agentId: bigint): Promise<{
|
|
84
|
+
agentId: string;
|
|
85
|
+
account: string;
|
|
86
|
+
creditInfo: Record<string, unknown>;
|
|
87
|
+
creditLine: Record<string, unknown>;
|
|
88
|
+
}>;
|
|
89
|
+
/**
|
|
90
|
+
* Get protocol info
|
|
91
|
+
* Backend endpoint: GET /credit/protocol-info
|
|
92
|
+
*/
|
|
93
|
+
getProtocolInfo(): Promise<{
|
|
94
|
+
dailyDrawLimit: string;
|
|
95
|
+
totalBorrowed: string;
|
|
96
|
+
asset: string;
|
|
97
|
+
}>;
|
|
98
|
+
/**
|
|
99
|
+
* Get backend service status
|
|
100
|
+
* Backend endpoint: GET /status
|
|
101
|
+
*/
|
|
102
|
+
getStatus(): Promise<{
|
|
103
|
+
status: string;
|
|
104
|
+
version: string;
|
|
105
|
+
signer: string;
|
|
106
|
+
contracts: {
|
|
107
|
+
healthy: boolean;
|
|
108
|
+
};
|
|
109
|
+
}>;
|
|
110
|
+
/**
|
|
111
|
+
* Get full OCCR score explanation with subscores, decay, and loan positions
|
|
112
|
+
* Backend endpoint: GET /agents/:agentId/score-explanation
|
|
113
|
+
*/
|
|
114
|
+
getScoreExplanation(agentId: bigint): Promise<{
|
|
115
|
+
agentId: string;
|
|
116
|
+
subscores: {
|
|
117
|
+
historical: number;
|
|
118
|
+
currentRisk: number;
|
|
119
|
+
utilization: number;
|
|
120
|
+
onChainTx: number;
|
|
121
|
+
newCredit: number;
|
|
122
|
+
};
|
|
123
|
+
rawScore: number;
|
|
124
|
+
decayedScore: number;
|
|
125
|
+
stressMultiplier: number;
|
|
126
|
+
decayPeriod: number;
|
|
127
|
+
loanPositions: Array<{
|
|
128
|
+
amount: string;
|
|
129
|
+
collateral: string;
|
|
130
|
+
ltvBps: number;
|
|
131
|
+
timestamp: number;
|
|
132
|
+
liquidated: boolean;
|
|
133
|
+
repaid: boolean;
|
|
134
|
+
active: boolean;
|
|
135
|
+
}>;
|
|
136
|
+
}>;
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=ScoringClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ScoringClient.d.ts","sourceRoot":"","sources":["../../src/clients/ScoringClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,EACL,cAAc,EACd,aAAa,EAGd,MAAM,UAAU,CAAC;AAElB,MAAM,WAAW,cAAc;IAC7B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,iBAAiB;IAChC,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,KAAK,G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,EAAE,OAAO,CAAC;YAChB,MAAM,EAAE,OAAO,CAAC;SACjB,CAAC,CAAC;KACJ,CAAC;CAMH"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ScoringClient - Client for the Agether backend scoring API (v2)
|
|
3
|
+
*
|
|
4
|
+
* Connects to backend service at /credit/* and /admin/* endpoints.
|
|
5
|
+
* The backend evaluates agents based on:
|
|
6
|
+
* - On-chain reputation (AgentReputation contract, Bayesian scoring)
|
|
7
|
+
* - Code audit status (ValidationRegistry)
|
|
8
|
+
* - Default history
|
|
9
|
+
* - Risk model
|
|
10
|
+
*
|
|
11
|
+
* v2: account-based (not creditLineId-based)
|
|
12
|
+
*/
|
|
13
|
+
import axios from 'axios';
|
|
14
|
+
import { ScoringRejectedError, AgetherError, } from '../types';
|
|
15
|
+
export class ScoringClient {
|
|
16
|
+
constructor(endpoint, apiKey) {
|
|
17
|
+
this.client = axios.create({
|
|
18
|
+
baseURL: endpoint,
|
|
19
|
+
headers: {
|
|
20
|
+
'Content-Type': 'application/json',
|
|
21
|
+
...(apiKey && { Authorization: `Bearer ${apiKey}` }),
|
|
22
|
+
},
|
|
23
|
+
timeout: 30000,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Evaluate a credit application (does not submit on-chain)
|
|
28
|
+
* Backend endpoint: POST /credit/evaluate
|
|
29
|
+
*/
|
|
30
|
+
async evaluateCredit(request) {
|
|
31
|
+
try {
|
|
32
|
+
const response = await this.client.post('/credit/evaluate', {
|
|
33
|
+
agentId: request.agentId.toString(),
|
|
34
|
+
requestedLimit: request.requestedLimit.toString(),
|
|
35
|
+
codeHash: request.codeHash,
|
|
36
|
+
});
|
|
37
|
+
const data = response.data;
|
|
38
|
+
if (!data.approved) {
|
|
39
|
+
throw new ScoringRejectedError(data.riskScore, data.reason);
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
approved: data.approved,
|
|
43
|
+
limit: BigInt(data.limit),
|
|
44
|
+
aprBps: data.aprBps,
|
|
45
|
+
riskScore: data.riskScore,
|
|
46
|
+
bayesianScore: data.bayesianScore ?? 0,
|
|
47
|
+
confidence: data.confidence ?? 0,
|
|
48
|
+
reason: data.reason,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
if (error instanceof ScoringRejectedError) {
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
|
+
if (axios.isAxiosError(error)) {
|
|
56
|
+
throw new AgetherError(`Scoring request failed: ${error.message}`, 'SCORING_REQUEST_FAILED', { status: error.response?.status });
|
|
57
|
+
}
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Process a credit application (evaluate + on-chain approve/reject)
|
|
63
|
+
* Backend endpoint: POST /admin/credit/process
|
|
64
|
+
*
|
|
65
|
+
* Requires the credit application to be in Pending status on-chain
|
|
66
|
+
* (agent must have called applyForCredit first).
|
|
67
|
+
*/
|
|
68
|
+
async processApplication(agentId, requestedLimit, codeHash) {
|
|
69
|
+
try {
|
|
70
|
+
const response = await this.client.post('/admin/credit/process', {
|
|
71
|
+
agentId: agentId.toString(),
|
|
72
|
+
requestedLimit: requestedLimit.toString(),
|
|
73
|
+
codeHash,
|
|
74
|
+
});
|
|
75
|
+
return response.data;
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
if (axios.isAxiosError(error)) {
|
|
79
|
+
if (error.response?.status === 400) {
|
|
80
|
+
return error.response.data;
|
|
81
|
+
}
|
|
82
|
+
throw new AgetherError(`Process application failed: ${error.message}`, 'PROCESS_APPLICATION_FAILED', { status: error.response?.status });
|
|
83
|
+
}
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Get credit score for an agent
|
|
89
|
+
* Backend endpoint: GET /credit/score/:agentId
|
|
90
|
+
*/
|
|
91
|
+
async getCreditScore(agentId) {
|
|
92
|
+
const response = await this.client.get(`/credit/score/${agentId.toString()}`);
|
|
93
|
+
return response.data;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Preview scored limit for an agent
|
|
97
|
+
* Backend endpoint: GET /credit/preview/:agentId
|
|
98
|
+
*/
|
|
99
|
+
async previewScoredLimit(agentId) {
|
|
100
|
+
const response = await this.client.get(`/credit/preview/${agentId.toString()}`);
|
|
101
|
+
return response.data;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Get credit line info for an agent
|
|
105
|
+
* Backend endpoint: GET /credit/agent/:agentId
|
|
106
|
+
*/
|
|
107
|
+
async getAgentCredit(agentId) {
|
|
108
|
+
const response = await this.client.get(`/credit/agent/${agentId.toString()}`);
|
|
109
|
+
return response.data;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Get protocol info
|
|
113
|
+
* Backend endpoint: GET /credit/protocol-info
|
|
114
|
+
*/
|
|
115
|
+
async getProtocolInfo() {
|
|
116
|
+
const response = await this.client.get('/credit/protocol-info');
|
|
117
|
+
return response.data;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Get backend service status
|
|
121
|
+
* Backend endpoint: GET /status
|
|
122
|
+
*/
|
|
123
|
+
async getStatus() {
|
|
124
|
+
const response = await this.client.get('/status');
|
|
125
|
+
return response.data;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Get full OCCR score explanation with subscores, decay, and loan positions
|
|
129
|
+
* Backend endpoint: GET /agents/:agentId/score-explanation
|
|
130
|
+
*/
|
|
131
|
+
async getScoreExplanation(agentId) {
|
|
132
|
+
const response = await this.client.get(`/agents/${agentId.toString()}/score-explanation`);
|
|
133
|
+
return response.data;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* VaultClient - Client for liquidity providers
|
|
3
|
+
*/
|
|
4
|
+
import { Signer } from 'ethers';
|
|
5
|
+
import { AgetherConfig, VaultStats, LPPosition, TransactionResult } from '../types';
|
|
6
|
+
export interface VaultClientOptions {
|
|
7
|
+
config: AgetherConfig;
|
|
8
|
+
signer: Signer;
|
|
9
|
+
}
|
|
10
|
+
export declare class VaultClient {
|
|
11
|
+
private config;
|
|
12
|
+
private signer;
|
|
13
|
+
private vault;
|
|
14
|
+
private asset?;
|
|
15
|
+
constructor(options: VaultClientOptions);
|
|
16
|
+
/**
|
|
17
|
+
* Deposit assets to vault
|
|
18
|
+
*/
|
|
19
|
+
deposit(amount: bigint): Promise<TransactionResult>;
|
|
20
|
+
/**
|
|
21
|
+
* Withdraw assets from vault
|
|
22
|
+
*/
|
|
23
|
+
withdraw(amount: bigint): Promise<TransactionResult>;
|
|
24
|
+
/**
|
|
25
|
+
* Redeem shares for assets
|
|
26
|
+
*/
|
|
27
|
+
redeem(shares: bigint): Promise<TransactionResult>;
|
|
28
|
+
/**
|
|
29
|
+
* Get vault statistics
|
|
30
|
+
*/
|
|
31
|
+
getStats(): Promise<VaultStats>;
|
|
32
|
+
/**
|
|
33
|
+
* Get LP position
|
|
34
|
+
*/
|
|
35
|
+
getPosition(address?: string): Promise<LPPosition>;
|
|
36
|
+
/**
|
|
37
|
+
* Preview deposit (how many shares for assets)
|
|
38
|
+
*/
|
|
39
|
+
previewDeposit(assets: bigint): Promise<bigint>;
|
|
40
|
+
/**
|
|
41
|
+
* Preview withdraw (how many assets for shares)
|
|
42
|
+
*/
|
|
43
|
+
previewRedeem(shares: bigint): Promise<bigint>;
|
|
44
|
+
/**
|
|
45
|
+
* Check if withdrawal is allowed (sufficient liquidity)
|
|
46
|
+
*/
|
|
47
|
+
canWithdraw(assets: bigint): Promise<boolean>;
|
|
48
|
+
/**
|
|
49
|
+
* Get current APY estimate
|
|
50
|
+
*/
|
|
51
|
+
estimateAPY(): Promise<number>;
|
|
52
|
+
private getAsset;
|
|
53
|
+
/**
|
|
54
|
+
* Get underlying asset address
|
|
55
|
+
*/
|
|
56
|
+
getAssetAddress(): Promise<string>;
|
|
57
|
+
/**
|
|
58
|
+
* Get vault share token address
|
|
59
|
+
*/
|
|
60
|
+
getVaultAddress(): string;
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=VaultClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VaultClient.d.ts","sourceRoot":"","sources":["../../src/clients/VaultClient.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,MAAM,EAAY,MAAM,QAAQ,CAAC;AAC1C,OAAO,EACL,aAAa,EACb,UAAU,EACV,UAAU,EACV,iBAAiB,EAClB,MAAM,UAAU,CAAC;AAGlB,MAAM,WAAW,kBAAkB;IACjC,MAAM,EAAE,aAAa,CAAC;IACtB,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,qBAAa,WAAW;IACtB,OAAO,CAAC,MAAM,CAAgB;IAC9B,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,KAAK,CAAW;IACxB,OAAO,CAAC,KAAK,CAAC,CAAW;gBAEb,OAAO,EAAE,kBAAkB;IAavC;;OAEG;IACG,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC;IA4BzD;;OAEG;IACG,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAe1D;;OAEG;IACG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAiBxD;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,UAAU,CAAC;IAyBrC;;OAEG;IACG,WAAW,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;IAiBxD;;OAEG;IACG,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAIrD;;OAEG;IACG,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MA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