@agent-fuel/sdk 0.1.0 → 0.2.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/index.d.cts CHANGED
@@ -15,6 +15,16 @@ type CreditVaultAccount = {
15
15
  frozen: boolean;
16
16
  created_slot: number;
17
17
  last_active_slot: number;
18
+ pending_count: number;
19
+ };
20
+ type PendingSpendAccount = {
21
+ pubkey: PublicKey;
22
+ vault: PublicKey;
23
+ agent: PublicKey;
24
+ service: PublicKey;
25
+ amount_usdc: number;
26
+ nonce: number;
27
+ requested_slot: number;
18
28
  };
19
29
  type SpendPolicyAccount = {
20
30
  pubkey: PublicKey;
@@ -66,6 +76,70 @@ type ReputationLookup = {
66
76
  updated_at: string;
67
77
  };
68
78
 
79
+ type PayArgs = {
80
+ /** Agent keypair — spends from the vault and pays tx fees. */
81
+ agent: Keypair;
82
+ /** Service keypair — co-signs the reputation half. Its pubkey must
83
+ * match the service registered on chain. */
84
+ service: Keypair;
85
+ /** Vault owner. */
86
+ owner: Pubkeyish;
87
+ /** Amount in micro-USDC. */
88
+ amountUsdc: number;
89
+ /** 32-byte receipt hash. Common: `sha256(agent|service|tick|...)`. */
90
+ receiptHash: Uint8Array;
91
+ connection: Connection;
92
+ };
93
+ type PayResult = {
94
+ signature: string;
95
+ };
96
+ /**
97
+ * Atomic spend + record_payment. The vault burn and reputation accrual
98
+ * happen in one transaction — without this, a service could mirror only
99
+ * payments it actually received, but the agent's score wouldn't reflect
100
+ * its own spending. Both sides of the SDK need this primitive to keep
101
+ * reputation honest.
102
+ */
103
+ declare function pay(args: PayArgs): Promise<PayResult>;
104
+
105
+ type RequestSpendArgs = {
106
+ agent: Keypair;
107
+ owner: Pubkeyish;
108
+ service: Pubkeyish;
109
+ amountUsdc: number;
110
+ connection: Connection;
111
+ };
112
+ type RequestSpendResult = {
113
+ signature: string;
114
+ /** The PendingSpend account the owner approves / cancels and the bot
115
+ * polls for resolution. */
116
+ pendingSpend: PublicKey;
117
+ /** Vault nonce burned by this request. */
118
+ nonce: number;
119
+ };
120
+ declare function requestSpend(args: RequestSpendArgs): Promise<RequestSpendResult>;
121
+
122
+ type RegisterServiceArgs = {
123
+ /** Pays rent for the registry PDA and submits the tx. Typically the
124
+ * same wallet that owns the service brand. */
125
+ sponsor: Keypair;
126
+ /** Long-lived service identity. Co-signs registration to prevent
127
+ * someone else from squatting on the keypair. Must hold ~0.05 SOL
128
+ * for downstream `record_payment` receipt PDAs to be fundable from
129
+ * the service side. */
130
+ service: Keypair;
131
+ name: string;
132
+ category: ServiceCategory;
133
+ /** Off-chain metadata URI (pricing, docs, endpoint). Empty string is
134
+ * allowed; chain just stores zero-padded bytes. */
135
+ serviceUri?: string;
136
+ connection: Connection;
137
+ };
138
+ type RegisterServiceResult = {
139
+ signature: string;
140
+ };
141
+ declare function registerService(args: RegisterServiceArgs): Promise<RegisterServiceResult>;
142
+
69
143
  type Cluster = "mainnet-beta" | "devnet" | "testnet" | "localnet";
70
144
  type AgentFuelOptions = {
71
145
  agent: Keypair;
@@ -108,6 +182,16 @@ declare class AgentFuel {
108
182
  getPolicy(ref?: VaultRef): Promise<SpendPolicyAccount>;
109
183
  checkService(serviceAuthority: Pubkeyish): Promise<ServiceRegistryAccount>;
110
184
  spend(args: SpendArgs): Promise<SpendResult>;
185
+ pay(args: Omit<PayArgs, "connection" | "agent" | "owner"> & {
186
+ owner?: Pubkeyish;
187
+ }): Promise<PayResult>;
188
+ requestSpend(args: Omit<RequestSpendArgs, "connection" | "agent" | "owner"> & {
189
+ owner?: Pubkeyish;
190
+ }): Promise<RequestSpendResult>;
191
+ registerService(args: Omit<RegisterServiceArgs, "connection">): Promise<RegisterServiceResult>;
192
+ computeScore(): Promise<{
193
+ signature: string;
194
+ }>;
111
195
  onEvent(callback: (frame: LiveEventFrame) => void, options?: OnEventOptions): Subscription;
112
196
  }
113
197
 
@@ -125,6 +209,21 @@ declare function createAssociatedTokenAccountIdempotentInstruction(payer: Public
125
209
  declare function vaultPda(owner: Pubkeyish, agent: Pubkeyish): PublicKey;
126
210
  declare function policyPda(vault: Pubkeyish): PublicKey;
127
211
  declare function serviceRegistryPda(serviceAuthority: Pubkeyish): PublicKey;
212
+ declare function agentProfilePda(agent: Pubkeyish): PublicKey;
213
+ /** PDA used by `record_payment` to track the agent ↔ service pair. The seeds
214
+ * are the *PDAs* of the agent profile and service registry, not the raw
215
+ * authority pubkeys — match the on-chain Anchor account constraint. */
216
+ declare function agentServiceLinkPda(agentProfile: Pubkeyish, serviceRegistry: Pubkeyish): PublicKey;
217
+ /** PendingSpend PDA — created by `request_spend`, consumed (and closed) by
218
+ * either `approve_spend` (CPIs into `spend`, transfers USDC) or
219
+ * `cancel_spend` (closes without transfer). Nonce is the vault's
220
+ * `pending_count` at request time, so every request gets a fresh PDA. */
221
+ declare function pendingSpendPda(vault: Pubkeyish, nonce: number | bigint): PublicKey;
222
+ /** Existence-as-signal: a `ReceiptUsed` PDA at `[b"receipt", hash]` proves
223
+ * the receipt has been recorded. `record_payment`'s `init` constraint (not
224
+ * `init_if_needed`) means a duplicate hash fails with `AccountAlreadyInUse`,
225
+ * which is the chain-side replay defence. */
226
+ declare function receiptUsedPda(receiptHash: Uint8Array): PublicKey;
128
227
 
129
228
  declare class AgentFuelError extends Error {
130
229
  constructor(message: string);
@@ -172,6 +271,44 @@ declare class NotWhitelistedError extends SpendPolicyError {
172
271
  readonly service: string;
173
272
  constructor(service: string);
174
273
  }
274
+ declare class RecordPaymentError extends AgentFuelError {
275
+ constructor(message: string);
276
+ }
277
+ declare class ReceiptAlreadyRecordedError extends RecordPaymentError {
278
+ readonly receiptHash: Uint8Array;
279
+ constructor(receiptHash: Uint8Array);
280
+ }
281
+ declare class ServiceInactiveError extends RecordPaymentError {
282
+ readonly service: string;
283
+ constructor(service: string);
284
+ }
285
+
286
+ type RecordPaymentArgs = {
287
+ /** Service authority keypair. Signs the tx and pays the fee. Must match
288
+ * the same keypair that was registered on chain as the service authority. */
289
+ service: Keypair;
290
+ /** Agent identity pubkey (NOT the AgentProfile PDA — we derive that). */
291
+ agent: Pubkeyish;
292
+ /** Amount in micro-USDC (1_000_000 = 1 USDC). */
293
+ amountUsdc: number;
294
+ /** 32-byte hash that uniquely identifies this payment. Common choice:
295
+ * `sha256(spendTxSignature)` — the spend signature is unique on chain,
296
+ * so its hash is too. The chain enforces single-use via `init` on the
297
+ * ReceiptUsed PDA; resubmitting the same hash fails with AccountAlreadyInUse. */
298
+ receiptHash: Uint8Array;
299
+ connection: Connection;
300
+ };
301
+ type RecordPaymentResult = {
302
+ signature: string;
303
+ };
304
+ declare function recordPayment(args: RecordPaymentArgs): Promise<RecordPaymentResult>;
305
+
306
+ type SubscribeOptions = {
307
+ onFrame: (frame: LiveEventFrame) => void;
308
+ onStatus?: (status: LiveStatus) => void;
309
+ };
310
+ declare function subscribeService(apiBase: string, servicePubkey: string, opts: SubscribeOptions): Subscription;
311
+ declare function subscribeVault(apiBase: string, vaultPubkey: string, opts: SubscribeOptions): Subscription;
175
312
 
176
313
  type PaymentRequirement = {
177
314
  recipient: string;
@@ -190,4 +327,4 @@ declare class PaymentParseError extends AgentFuelError {
190
327
  }
191
328
  declare function paymentRequired(fuel: AgentFuel, opts?: PaymentRequiredOptions): FetchLike;
192
329
 
193
- export { ASSOCIATED_TOKEN_PROGRAM_ID, AccountNotFoundError, AgentFuel, AgentFuelError, type AgentFuelOptions, type Cluster, type CreditVaultAccount, type FetchLike, HourlyLimitExceededError, HttpError, LifetimeLimitExceededError, type LiveEventFrame, type LiveStatus, NotWhitelistedError, type OnEventOptions, OwnerNotConfiguredError, PROGRAM_IDS, PaymentParseError, type PaymentRequiredOptions, type PaymentRequirement, PerTxLimitExceededError, type Pubkeyish, type ReputationLookup, SLOTS_PER_HOUR, type ServiceCategory, type ServiceRegistryAccount, type SpendArgs, type SpendPolicyAccount, SpendPolicyError, type SpendResult, type Subscription, TOKEN_PROGRAM_ID, VaultFrozenError, type VaultRef, ZeroAmountError, createAssociatedTokenAccountIdempotentInstruction, getAssociatedTokenAddress, paymentRequired, policyPda, serviceRegistryPda, vaultPda };
330
+ export { ASSOCIATED_TOKEN_PROGRAM_ID, AccountNotFoundError, AgentFuel, AgentFuelError, type AgentFuelOptions, type Cluster, type CreditVaultAccount, type FetchLike, HourlyLimitExceededError, HttpError, LifetimeLimitExceededError, type LiveEventFrame, type LiveStatus, NotWhitelistedError, type OnEventOptions, OwnerNotConfiguredError, PROGRAM_IDS, type PayArgs, type PayResult, PaymentParseError, type PaymentRequiredOptions, type PaymentRequirement, type PendingSpendAccount, PerTxLimitExceededError, type Pubkeyish, ReceiptAlreadyRecordedError, type RecordPaymentArgs, RecordPaymentError, type RecordPaymentResult, type RegisterServiceArgs, type RegisterServiceResult, type ReputationLookup, type RequestSpendArgs, type RequestSpendResult, SLOTS_PER_HOUR, type ServiceCategory, ServiceInactiveError, type ServiceRegistryAccount, type SpendArgs, type SpendPolicyAccount, SpendPolicyError, type SpendResult, type SubscribeOptions, type Subscription, TOKEN_PROGRAM_ID, VaultFrozenError, type VaultRef, ZeroAmountError, agentProfilePda, agentServiceLinkPda, createAssociatedTokenAccountIdempotentInstruction, getAssociatedTokenAddress, pay, paymentRequired, pendingSpendPda, policyPda, receiptUsedPda, recordPayment, registerService, requestSpend, serviceRegistryPda, subscribeService, subscribeVault, vaultPda };
package/dist/index.d.ts CHANGED
@@ -15,6 +15,16 @@ type CreditVaultAccount = {
15
15
  frozen: boolean;
16
16
  created_slot: number;
17
17
  last_active_slot: number;
18
+ pending_count: number;
19
+ };
20
+ type PendingSpendAccount = {
21
+ pubkey: PublicKey;
22
+ vault: PublicKey;
23
+ agent: PublicKey;
24
+ service: PublicKey;
25
+ amount_usdc: number;
26
+ nonce: number;
27
+ requested_slot: number;
18
28
  };
19
29
  type SpendPolicyAccount = {
20
30
  pubkey: PublicKey;
@@ -66,6 +76,70 @@ type ReputationLookup = {
66
76
  updated_at: string;
67
77
  };
68
78
 
79
+ type PayArgs = {
80
+ /** Agent keypair — spends from the vault and pays tx fees. */
81
+ agent: Keypair;
82
+ /** Service keypair — co-signs the reputation half. Its pubkey must
83
+ * match the service registered on chain. */
84
+ service: Keypair;
85
+ /** Vault owner. */
86
+ owner: Pubkeyish;
87
+ /** Amount in micro-USDC. */
88
+ amountUsdc: number;
89
+ /** 32-byte receipt hash. Common: `sha256(agent|service|tick|...)`. */
90
+ receiptHash: Uint8Array;
91
+ connection: Connection;
92
+ };
93
+ type PayResult = {
94
+ signature: string;
95
+ };
96
+ /**
97
+ * Atomic spend + record_payment. The vault burn and reputation accrual
98
+ * happen in one transaction — without this, a service could mirror only
99
+ * payments it actually received, but the agent's score wouldn't reflect
100
+ * its own spending. Both sides of the SDK need this primitive to keep
101
+ * reputation honest.
102
+ */
103
+ declare function pay(args: PayArgs): Promise<PayResult>;
104
+
105
+ type RequestSpendArgs = {
106
+ agent: Keypair;
107
+ owner: Pubkeyish;
108
+ service: Pubkeyish;
109
+ amountUsdc: number;
110
+ connection: Connection;
111
+ };
112
+ type RequestSpendResult = {
113
+ signature: string;
114
+ /** The PendingSpend account the owner approves / cancels and the bot
115
+ * polls for resolution. */
116
+ pendingSpend: PublicKey;
117
+ /** Vault nonce burned by this request. */
118
+ nonce: number;
119
+ };
120
+ declare function requestSpend(args: RequestSpendArgs): Promise<RequestSpendResult>;
121
+
122
+ type RegisterServiceArgs = {
123
+ /** Pays rent for the registry PDA and submits the tx. Typically the
124
+ * same wallet that owns the service brand. */
125
+ sponsor: Keypair;
126
+ /** Long-lived service identity. Co-signs registration to prevent
127
+ * someone else from squatting on the keypair. Must hold ~0.05 SOL
128
+ * for downstream `record_payment` receipt PDAs to be fundable from
129
+ * the service side. */
130
+ service: Keypair;
131
+ name: string;
132
+ category: ServiceCategory;
133
+ /** Off-chain metadata URI (pricing, docs, endpoint). Empty string is
134
+ * allowed; chain just stores zero-padded bytes. */
135
+ serviceUri?: string;
136
+ connection: Connection;
137
+ };
138
+ type RegisterServiceResult = {
139
+ signature: string;
140
+ };
141
+ declare function registerService(args: RegisterServiceArgs): Promise<RegisterServiceResult>;
142
+
69
143
  type Cluster = "mainnet-beta" | "devnet" | "testnet" | "localnet";
70
144
  type AgentFuelOptions = {
71
145
  agent: Keypair;
@@ -108,6 +182,16 @@ declare class AgentFuel {
108
182
  getPolicy(ref?: VaultRef): Promise<SpendPolicyAccount>;
109
183
  checkService(serviceAuthority: Pubkeyish): Promise<ServiceRegistryAccount>;
110
184
  spend(args: SpendArgs): Promise<SpendResult>;
185
+ pay(args: Omit<PayArgs, "connection" | "agent" | "owner"> & {
186
+ owner?: Pubkeyish;
187
+ }): Promise<PayResult>;
188
+ requestSpend(args: Omit<RequestSpendArgs, "connection" | "agent" | "owner"> & {
189
+ owner?: Pubkeyish;
190
+ }): Promise<RequestSpendResult>;
191
+ registerService(args: Omit<RegisterServiceArgs, "connection">): Promise<RegisterServiceResult>;
192
+ computeScore(): Promise<{
193
+ signature: string;
194
+ }>;
111
195
  onEvent(callback: (frame: LiveEventFrame) => void, options?: OnEventOptions): Subscription;
112
196
  }
113
197
 
@@ -125,6 +209,21 @@ declare function createAssociatedTokenAccountIdempotentInstruction(payer: Public
125
209
  declare function vaultPda(owner: Pubkeyish, agent: Pubkeyish): PublicKey;
126
210
  declare function policyPda(vault: Pubkeyish): PublicKey;
127
211
  declare function serviceRegistryPda(serviceAuthority: Pubkeyish): PublicKey;
212
+ declare function agentProfilePda(agent: Pubkeyish): PublicKey;
213
+ /** PDA used by `record_payment` to track the agent ↔ service pair. The seeds
214
+ * are the *PDAs* of the agent profile and service registry, not the raw
215
+ * authority pubkeys — match the on-chain Anchor account constraint. */
216
+ declare function agentServiceLinkPda(agentProfile: Pubkeyish, serviceRegistry: Pubkeyish): PublicKey;
217
+ /** PendingSpend PDA — created by `request_spend`, consumed (and closed) by
218
+ * either `approve_spend` (CPIs into `spend`, transfers USDC) or
219
+ * `cancel_spend` (closes without transfer). Nonce is the vault's
220
+ * `pending_count` at request time, so every request gets a fresh PDA. */
221
+ declare function pendingSpendPda(vault: Pubkeyish, nonce: number | bigint): PublicKey;
222
+ /** Existence-as-signal: a `ReceiptUsed` PDA at `[b"receipt", hash]` proves
223
+ * the receipt has been recorded. `record_payment`'s `init` constraint (not
224
+ * `init_if_needed`) means a duplicate hash fails with `AccountAlreadyInUse`,
225
+ * which is the chain-side replay defence. */
226
+ declare function receiptUsedPda(receiptHash: Uint8Array): PublicKey;
128
227
 
129
228
  declare class AgentFuelError extends Error {
130
229
  constructor(message: string);
@@ -172,6 +271,44 @@ declare class NotWhitelistedError extends SpendPolicyError {
172
271
  readonly service: string;
173
272
  constructor(service: string);
174
273
  }
274
+ declare class RecordPaymentError extends AgentFuelError {
275
+ constructor(message: string);
276
+ }
277
+ declare class ReceiptAlreadyRecordedError extends RecordPaymentError {
278
+ readonly receiptHash: Uint8Array;
279
+ constructor(receiptHash: Uint8Array);
280
+ }
281
+ declare class ServiceInactiveError extends RecordPaymentError {
282
+ readonly service: string;
283
+ constructor(service: string);
284
+ }
285
+
286
+ type RecordPaymentArgs = {
287
+ /** Service authority keypair. Signs the tx and pays the fee. Must match
288
+ * the same keypair that was registered on chain as the service authority. */
289
+ service: Keypair;
290
+ /** Agent identity pubkey (NOT the AgentProfile PDA — we derive that). */
291
+ agent: Pubkeyish;
292
+ /** Amount in micro-USDC (1_000_000 = 1 USDC). */
293
+ amountUsdc: number;
294
+ /** 32-byte hash that uniquely identifies this payment. Common choice:
295
+ * `sha256(spendTxSignature)` — the spend signature is unique on chain,
296
+ * so its hash is too. The chain enforces single-use via `init` on the
297
+ * ReceiptUsed PDA; resubmitting the same hash fails with AccountAlreadyInUse. */
298
+ receiptHash: Uint8Array;
299
+ connection: Connection;
300
+ };
301
+ type RecordPaymentResult = {
302
+ signature: string;
303
+ };
304
+ declare function recordPayment(args: RecordPaymentArgs): Promise<RecordPaymentResult>;
305
+
306
+ type SubscribeOptions = {
307
+ onFrame: (frame: LiveEventFrame) => void;
308
+ onStatus?: (status: LiveStatus) => void;
309
+ };
310
+ declare function subscribeService(apiBase: string, servicePubkey: string, opts: SubscribeOptions): Subscription;
311
+ declare function subscribeVault(apiBase: string, vaultPubkey: string, opts: SubscribeOptions): Subscription;
175
312
 
176
313
  type PaymentRequirement = {
177
314
  recipient: string;
@@ -190,4 +327,4 @@ declare class PaymentParseError extends AgentFuelError {
190
327
  }
191
328
  declare function paymentRequired(fuel: AgentFuel, opts?: PaymentRequiredOptions): FetchLike;
192
329
 
193
- export { ASSOCIATED_TOKEN_PROGRAM_ID, AccountNotFoundError, AgentFuel, AgentFuelError, type AgentFuelOptions, type Cluster, type CreditVaultAccount, type FetchLike, HourlyLimitExceededError, HttpError, LifetimeLimitExceededError, type LiveEventFrame, type LiveStatus, NotWhitelistedError, type OnEventOptions, OwnerNotConfiguredError, PROGRAM_IDS, PaymentParseError, type PaymentRequiredOptions, type PaymentRequirement, PerTxLimitExceededError, type Pubkeyish, type ReputationLookup, SLOTS_PER_HOUR, type ServiceCategory, type ServiceRegistryAccount, type SpendArgs, type SpendPolicyAccount, SpendPolicyError, type SpendResult, type Subscription, TOKEN_PROGRAM_ID, VaultFrozenError, type VaultRef, ZeroAmountError, createAssociatedTokenAccountIdempotentInstruction, getAssociatedTokenAddress, paymentRequired, policyPda, serviceRegistryPda, vaultPda };
330
+ export { ASSOCIATED_TOKEN_PROGRAM_ID, AccountNotFoundError, AgentFuel, AgentFuelError, type AgentFuelOptions, type Cluster, type CreditVaultAccount, type FetchLike, HourlyLimitExceededError, HttpError, LifetimeLimitExceededError, type LiveEventFrame, type LiveStatus, NotWhitelistedError, type OnEventOptions, OwnerNotConfiguredError, PROGRAM_IDS, type PayArgs, type PayResult, PaymentParseError, type PaymentRequiredOptions, type PaymentRequirement, type PendingSpendAccount, PerTxLimitExceededError, type Pubkeyish, ReceiptAlreadyRecordedError, type RecordPaymentArgs, RecordPaymentError, type RecordPaymentResult, type RegisterServiceArgs, type RegisterServiceResult, type ReputationLookup, type RequestSpendArgs, type RequestSpendResult, SLOTS_PER_HOUR, type ServiceCategory, ServiceInactiveError, type ServiceRegistryAccount, type SpendArgs, type SpendPolicyAccount, SpendPolicyError, type SpendResult, type SubscribeOptions, type Subscription, TOKEN_PROGRAM_ID, VaultFrozenError, type VaultRef, ZeroAmountError, agentProfilePda, agentServiceLinkPda, createAssociatedTokenAccountIdempotentInstruction, getAssociatedTokenAddress, pay, paymentRequired, pendingSpendPda, policyPda, receiptUsedPda, recordPayment, registerService, requestSpend, serviceRegistryPda, subscribeService, subscribeVault, vaultPda };