@faremeter/payment-solana 0.15.0 → 0.16.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.
@@ -1,7 +1,28 @@
1
+ import { x402PaymentRequirements, type x402PaymentPayload, type x402SettleResponse, type x402VerifyResponse } from "@faremeter/types/x402";
1
2
  import type { FacilitatorHandler } from "@faremeter/types/facilitator";
3
+ import { fetchMint } from "@solana-program/token";
2
4
  import { type Rpc, type SolanaRpcApi } from "@solana/kit";
3
5
  import type { TransactionError } from "@solana/rpc-types";
4
6
  import { Keypair, PublicKey } from "@solana/web3.js";
7
+ import { logger } from "./logger.js";
8
+ export interface HookBaseArgs {
9
+ network: string;
10
+ rpc: Rpc<SolanaRpcApi>;
11
+ feePayerKeypair: Keypair;
12
+ mint: PublicKey;
13
+ mintInfo: Awaited<ReturnType<typeof fetchMint>>;
14
+ requirements: x402PaymentRequirements;
15
+ payment: x402PaymentPayload;
16
+ logger: typeof logger;
17
+ }
18
+ export type HookResponseArgs<Response> = HookBaseArgs & {
19
+ response: Response;
20
+ };
21
+ export type HookResponseFuncs<Response> = (args: HookResponseArgs<Response>) => Promise<Response> | Promise<void>;
22
+ export interface FacilitatorHooks {
23
+ afterVerify?: HookResponseFuncs<x402VerifyResponse>;
24
+ afterSettle?: HookResponseFuncs<x402SettleResponse>;
25
+ }
5
26
  export declare const PaymentRequirementsExtraFeatures: import("arktype/internal/methods/object.ts").ObjectType<{
6
27
  xSettlementAccountSupported?: boolean;
7
28
  }, {}>;
@@ -21,6 +42,7 @@ interface FacilitatorOptions {
21
42
  features?: {
22
43
  enableSettlementAccounts?: boolean;
23
44
  };
45
+ hooks?: readonly FacilitatorHooks[];
24
46
  }
25
47
  export declare const PaymentPayloadTransaction: import("arktype/internal/methods/object.ts").ObjectType<{
26
48
  transaction: (In: string) => import("arktype").Out<Readonly<{
@@ -1 +1 @@
1
- {"version":3,"file":"facilitator.d.ts","sourceRoot":"","sources":["../../../src/exact/facil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gCAAgC,GAC1C,OAAO,gCAAgC,CAAC,KAAK,CAAC;AAEhD,eAAO,MAAM,wBAAwB;;;;;;;MAKnC,CAAC;AAEH,UAAU,kBAAkB;IAC1B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IAGtB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,EAAE;QACT,wBAAwB,CAAC,EAAE,OAAO,CAAC;KACpC,CAAC;IACF,KAAK,CAAC,EAAE,SAAS,gBAAgB,EAAE,CAAC;CACrC;AASD,eAAO,MAAM,yBAAyB;;;;;MAEpC,CAAC;AACH,MAAM,MAAM,yBAAyB,GAAG,OAAO,yBAAyB,CAAC,KAAK,CAAC;AAE/E,eAAO,MAAM,+BAA+B;;;MAK1C,CAAC;AACH,MAAM,MAAM,+BAA+B,GACzC,OAAO,+BAA+B,CAAC,KAAK,CAAC;AAE/C,wBAAgB,wBAAwB,CAAC,CAAC,EAAE,gBAAgB,UAY3D;AAiDD,eAAO,MAAM,wBAAwB,GACnC,SAAS,MAAM,EACf,KAAK,GAAG,CAAC,YAAY,CAAC,EACtB,iBAAiB,OAAO,EACxB,MAAM,SAAS,EACf,SAAS,kBAAkB,KAC1B,OAAO,CAAC,kBAAkB,CA+X5B,CAAC"}
@@ -50,7 +50,7 @@ const sendTransaction = async (rpc, signedTransaction, maxRetries, retryDelayMs)
50
50
  })
51
51
  .send();
52
52
  if (simResult.value.err) {
53
- logger.error("transaction simulation failed: {*}", simResult.value);
53
+ logger.error("transaction simulation failed", simResult.value);
54
54
  return { success: false, error: "Transaction simulation failed" };
55
55
  }
56
56
  const signature = await rpc
@@ -78,6 +78,14 @@ export const createFacilitatorHandler = async (network, rpc, feePayerKeypair, mi
78
78
  const { isMatchingRequirement } = generateMatcher(network, mint.toBase58());
79
79
  const { maxRetries = 30, retryDelayMs = 1000, maxPriorityFee = 100_000, } = config ?? {};
80
80
  const mintInfo = await fetchMint(rpc, address(mint.toBase58()));
81
+ const hookArgs = {
82
+ network,
83
+ rpc,
84
+ feePayerKeypair,
85
+ mint,
86
+ mintInfo,
87
+ logger,
88
+ };
81
89
  const features = {};
82
90
  if (config?.features?.enableSettlementAccounts) {
83
91
  features.xSettlementAccountSupported = true;
@@ -101,7 +109,7 @@ export const createFacilitatorHandler = async (network, rpc, feePayerKeypair, mi
101
109
  const { value: accountBalance } = await rpc
102
110
  .getTokenAccountBalance(settleATA, { commitment: "confirmed" })
103
111
  .send();
104
- logger.debug("settlement account info: {*}", {
112
+ logger.debug("settlement account info", {
105
113
  settleOwner,
106
114
  settleATA,
107
115
  accountBalance,
@@ -244,7 +252,29 @@ export const createFacilitatorHandler = async (network, rpc, feePayerKeypair, mi
244
252
  if ("error" in verifyResult) {
245
253
  return errorResponse(verifyResult.error);
246
254
  }
247
- return { isValid: true };
255
+ let response = { isValid: true };
256
+ const hooks = config?.hooks;
257
+ if (hooks !== undefined) {
258
+ const args = {
259
+ ...hookArgs,
260
+ requirements,
261
+ payment,
262
+ response,
263
+ };
264
+ for (const hook of hooks) {
265
+ if (hook.afterVerify === undefined) {
266
+ continue;
267
+ }
268
+ const res = await hook.afterVerify({
269
+ ...args,
270
+ response,
271
+ });
272
+ if (res !== undefined) {
273
+ response = res;
274
+ }
275
+ }
276
+ }
277
+ return response;
248
278
  };
249
279
  const handleSettle = async (requirements, payment) => {
250
280
  if (!isMatchingRequirement(requirements)) {
@@ -278,12 +308,34 @@ export const createFacilitatorHandler = async (network, rpc, feePayerKeypair, mi
278
308
  if (!result.success) {
279
309
  return errorResponse(result.error);
280
310
  }
281
- return {
311
+ let response = {
282
312
  success: true,
283
313
  error: null,
284
314
  txHash: result.signature,
285
315
  networkId: payment.network,
286
316
  };
317
+ const hooks = config?.hooks;
318
+ if (hooks !== undefined) {
319
+ const args = {
320
+ ...hookArgs,
321
+ requirements,
322
+ payment,
323
+ response,
324
+ };
325
+ for (const hook of hooks) {
326
+ if (hook.afterSettle === undefined) {
327
+ continue;
328
+ }
329
+ const res = await hook.afterSettle({
330
+ ...args,
331
+ response,
332
+ });
333
+ if (res !== undefined) {
334
+ response = res;
335
+ }
336
+ }
337
+ }
338
+ return response;
287
339
  };
288
340
  return {
289
341
  getSupported,
@@ -1,2 +1,2 @@
1
- export declare const logger: import("@logtape/logtape").Logger;
1
+ export declare const logger: import("@faremeter/logs").Logger;
2
2
  //# sourceMappingURL=logger.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../../src/exact/logger.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,MAAM,mCAAmD,CAAC"}
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../../src/exact/logger.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,MAAM,kCAAyD,CAAC"}
@@ -1,2 +1,2 @@
1
- import { getLogger } from "@logtape/logtape";
2
- export const logger = getLogger(["faremeter", "payment-solana-exact"]);
1
+ import { getLogger } from "@faremeter/logs";
2
+ export const logger = await getLogger(["faremeter", "payment-solana-exact"]);