@coinbase/agentkit 0.0.0-nightly-20250915210424 → 0.0.0-nightly-20250917210422
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 +9 -1
- package/dist/action-providers/pyth/pythActionProvider.d.ts +2 -2
- package/dist/action-providers/pyth/pythActionProvider.js +83 -31
- package/dist/action-providers/pyth/pythActionProvider.test.js +178 -26
- package/dist/action-providers/pyth/schemas.d.ts +6 -0
- package/dist/action-providers/pyth/schemas.js +9 -1
- package/dist/action-providers/x402/schemas.d.ts +7 -0
- package/dist/action-providers/x402/schemas.js +11 -1
- package/dist/action-providers/x402/utils.d.ts +55 -0
- package/dist/action-providers/x402/utils.js +160 -0
- package/dist/action-providers/x402/x402ActionProvider.d.ts +9 -9
- package/dist/action-providers/x402/x402ActionProvider.js +158 -39
- package/dist/action-providers/x402/x402ActionProvider.test.js +116 -10
- package/dist/wallet-providers/cdpEvmWalletProvider.d.ts +7 -0
- package/dist/wallet-providers/cdpEvmWalletProvider.js +9 -0
- package/dist/wallet-providers/cdpEvmWalletProvider.test.js +7 -0
- package/dist/wallet-providers/cdpSmartWalletProvider.d.ts +7 -0
- package/dist/wallet-providers/cdpSmartWalletProvider.js +13 -1
- package/dist/wallet-providers/cdpSmartWalletProvider.test.js +6 -2
- package/dist/wallet-providers/evmWalletProvider.d.ts +9 -2
- package/dist/wallet-providers/evmWalletProvider.js +4 -0
- package/dist/wallet-providers/legacyCdpSmartWalletProvider.d.ts +9 -0
- package/dist/wallet-providers/legacyCdpSmartWalletProvider.js +11 -0
- package/dist/wallet-providers/legacyCdpWalletProvider.d.ts +7 -0
- package/dist/wallet-providers/legacyCdpWalletProvider.js +16 -0
- package/dist/wallet-providers/legacyCdpWalletProvider.test.js +6 -0
- package/dist/wallet-providers/privyEvmDelegatedEmbeddedWalletProvider.d.ts +7 -0
- package/dist/wallet-providers/privyEvmDelegatedEmbeddedWalletProvider.js +27 -0
- package/dist/wallet-providers/privyEvmWalletProvider.test.js +11 -0
- package/dist/wallet-providers/viemWalletProvider.d.ts +7 -0
- package/dist/wallet-providers/viemWalletProvider.js +16 -0
- package/dist/wallet-providers/viemWalletProvider.test.js +9 -0
- package/dist/wallet-providers/zeroDevWalletProvider.d.ts +7 -0
- package/dist/wallet-providers/zeroDevWalletProvider.js +12 -0
- package/dist/wallet-providers/zeroDevWalletProvider.test.js +10 -0
- package/package.json +5 -4
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getX402Network = getX402Network;
|
|
4
|
+
exports.handleHttpError = handleHttpError;
|
|
5
|
+
exports.formatPaymentOption = formatPaymentOption;
|
|
6
|
+
exports.isUsdcAsset = isUsdcAsset;
|
|
7
|
+
exports.convertWholeUnitsToAtomic = convertWholeUnitsToAtomic;
|
|
8
|
+
const utils_1 = require("../erc20/utils");
|
|
9
|
+
const constants_1 = require("../erc20/constants");
|
|
10
|
+
const viem_1 = require("viem");
|
|
11
|
+
/**
|
|
12
|
+
* Converts the internal network ID to the format expected by the x402 protocol.
|
|
13
|
+
*
|
|
14
|
+
* @param network - The network to convert
|
|
15
|
+
* @returns The network ID in x402 format
|
|
16
|
+
* @throws Error if the network is not supported
|
|
17
|
+
*/
|
|
18
|
+
function getX402Network(network) {
|
|
19
|
+
switch (network.networkId) {
|
|
20
|
+
case "base-mainnet":
|
|
21
|
+
return "base";
|
|
22
|
+
case "base-sepolia":
|
|
23
|
+
return "base-sepolia";
|
|
24
|
+
case "solana-mainnet":
|
|
25
|
+
return "solana";
|
|
26
|
+
case "solana-devnet":
|
|
27
|
+
return "solana-devnet";
|
|
28
|
+
default:
|
|
29
|
+
return network.networkId;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Helper method to handle HTTP errors consistently.
|
|
34
|
+
*
|
|
35
|
+
* @param error - The axios error to handle
|
|
36
|
+
* @param url - The URL that was being accessed when the error occurred
|
|
37
|
+
* @returns A JSON string containing formatted error details
|
|
38
|
+
*/
|
|
39
|
+
function handleHttpError(error, url) {
|
|
40
|
+
if (error.response) {
|
|
41
|
+
return JSON.stringify({
|
|
42
|
+
error: true,
|
|
43
|
+
message: `HTTP ${error.response.status} error when accessing ${url}`,
|
|
44
|
+
details: error.response.data?.error || error.response.statusText,
|
|
45
|
+
suggestion: "Check if the URL is correct and the API is available.",
|
|
46
|
+
}, null, 2);
|
|
47
|
+
}
|
|
48
|
+
if (error.request) {
|
|
49
|
+
return JSON.stringify({
|
|
50
|
+
error: true,
|
|
51
|
+
message: `Network error when accessing ${url}`,
|
|
52
|
+
details: error.message,
|
|
53
|
+
suggestion: "Check your internet connection and verify the API endpoint is accessible.",
|
|
54
|
+
}, null, 2);
|
|
55
|
+
}
|
|
56
|
+
return JSON.stringify({
|
|
57
|
+
error: true,
|
|
58
|
+
message: `Error making request to ${url}`,
|
|
59
|
+
details: error.message,
|
|
60
|
+
suggestion: "Please check the request parameters and try again.",
|
|
61
|
+
}, null, 2);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Formats a payment option into a human-readable string.
|
|
65
|
+
*
|
|
66
|
+
* @param option - The payment option to format
|
|
67
|
+
* @param option.asset - The asset address or identifier
|
|
68
|
+
* @param option.maxAmountRequired - The maximum amount required for the payment
|
|
69
|
+
* @param option.network - The network identifier
|
|
70
|
+
* @param walletProvider - The wallet provider for token details lookup
|
|
71
|
+
* @returns A formatted string like "0.1 USDC on base"
|
|
72
|
+
*/
|
|
73
|
+
async function formatPaymentOption(option, walletProvider) {
|
|
74
|
+
const { asset, maxAmountRequired, network } = option;
|
|
75
|
+
// Check if this is an EVM network and we can use ERC20 helpers
|
|
76
|
+
const walletNetwork = walletProvider.getNetwork();
|
|
77
|
+
const isEvmNetwork = walletNetwork.protocolFamily === "evm";
|
|
78
|
+
if (isEvmNetwork) {
|
|
79
|
+
const networkId = walletNetwork.networkId;
|
|
80
|
+
const tokenSymbols = constants_1.TOKEN_ADDRESSES_BY_SYMBOLS[networkId];
|
|
81
|
+
if (tokenSymbols) {
|
|
82
|
+
for (const [symbol, address] of Object.entries(tokenSymbols)) {
|
|
83
|
+
if (asset.toLowerCase() === address.toLowerCase()) {
|
|
84
|
+
const decimals = symbol === "USDC" || symbol === "EURC" ? 6 : 18;
|
|
85
|
+
const formattedAmount = (0, viem_1.formatUnits)(BigInt(maxAmountRequired), decimals);
|
|
86
|
+
return `${formattedAmount} ${symbol} on ${network} network`;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// Fall back to getTokenDetails for unknown tokens
|
|
91
|
+
try {
|
|
92
|
+
const tokenDetails = await (0, utils_1.getTokenDetails)(walletProvider, asset);
|
|
93
|
+
if (tokenDetails) {
|
|
94
|
+
const formattedAmount = (0, viem_1.formatUnits)(BigInt(maxAmountRequired), tokenDetails.decimals);
|
|
95
|
+
return `${formattedAmount} ${tokenDetails.name} on ${network} network`;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
// If we can't get token details, fall back to raw format
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// Fallback to original format for non-EVM networks or when token details can't be fetched
|
|
103
|
+
return `${asset} ${maxAmountRequired} on ${network} network`;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Checks if an asset is USDC on any supported network.
|
|
107
|
+
*
|
|
108
|
+
* @param asset - The asset address or identifier
|
|
109
|
+
* @param walletProvider - The wallet provider for network context
|
|
110
|
+
* @returns True if the asset is USDC, false otherwise
|
|
111
|
+
*/
|
|
112
|
+
function isUsdcAsset(asset, walletProvider) {
|
|
113
|
+
const walletNetwork = walletProvider.getNetwork();
|
|
114
|
+
const isEvmNetwork = walletNetwork.protocolFamily === "evm";
|
|
115
|
+
if (isEvmNetwork) {
|
|
116
|
+
const networkId = walletNetwork.networkId;
|
|
117
|
+
const tokenSymbols = constants_1.TOKEN_ADDRESSES_BY_SYMBOLS[networkId];
|
|
118
|
+
if (tokenSymbols && tokenSymbols.USDC) {
|
|
119
|
+
return asset.toLowerCase() === tokenSymbols.USDC.toLowerCase();
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Converts whole units to atomic units for a given asset.
|
|
126
|
+
*
|
|
127
|
+
* @param wholeUnits - The amount in whole units (e.g., 0.1 for 0.1 USDC)
|
|
128
|
+
* @param asset - The asset address or identifier
|
|
129
|
+
* @param walletProvider - The wallet provider for token details lookup
|
|
130
|
+
* @returns The amount in atomic units as a string, or null if conversion fails
|
|
131
|
+
*/
|
|
132
|
+
async function convertWholeUnitsToAtomic(wholeUnits, asset, walletProvider) {
|
|
133
|
+
// Check if this is an EVM network and we can use ERC20 helpers
|
|
134
|
+
const walletNetwork = walletProvider.getNetwork();
|
|
135
|
+
const isEvmNetwork = walletNetwork.protocolFamily === "evm";
|
|
136
|
+
if (isEvmNetwork) {
|
|
137
|
+
const networkId = walletNetwork.networkId;
|
|
138
|
+
const tokenSymbols = constants_1.TOKEN_ADDRESSES_BY_SYMBOLS[networkId];
|
|
139
|
+
if (tokenSymbols) {
|
|
140
|
+
for (const [symbol, address] of Object.entries(tokenSymbols)) {
|
|
141
|
+
if (asset.toLowerCase() === address.toLowerCase()) {
|
|
142
|
+
const decimals = symbol === "USDC" || symbol === "EURC" ? 6 : 18;
|
|
143
|
+
return (0, viem_1.parseUnits)(wholeUnits.toString(), decimals).toString();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
// Fall back to getTokenDetails for unknown tokens
|
|
148
|
+
try {
|
|
149
|
+
const tokenDetails = await (0, utils_1.getTokenDetails)(walletProvider, asset);
|
|
150
|
+
if (tokenDetails) {
|
|
151
|
+
return (0, viem_1.parseUnits)(wholeUnits.toString(), tokenDetails.decimals).toString();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
// If we can't get token details, fall back to assuming 18 decimals
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
// Fallback to 18 decimals for unknown tokens or non-EVM networks
|
|
159
|
+
return (0, viem_1.parseUnits)(wholeUnits.toString(), 18).toString();
|
|
160
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { ActionProvider } from "../actionProvider";
|
|
3
3
|
import { Network } from "../../network";
|
|
4
|
-
import { HttpRequestSchema, RetryWithX402Schema, DirectX402RequestSchema } from "./schemas";
|
|
4
|
+
import { HttpRequestSchema, RetryWithX402Schema, DirectX402RequestSchema, ListX402ServicesSchema } from "./schemas";
|
|
5
5
|
import { EvmWalletProvider } from "../../wallet-providers";
|
|
6
6
|
/**
|
|
7
7
|
* X402ActionProvider provides actions for making HTTP requests, with optional x402 payment handling.
|
|
@@ -12,6 +12,14 @@ export declare class X402ActionProvider extends ActionProvider<EvmWalletProvider
|
|
|
12
12
|
* Initializes the provider with x402 capabilities.
|
|
13
13
|
*/
|
|
14
14
|
constructor();
|
|
15
|
+
/**
|
|
16
|
+
* Discovers available x402 services with optional filtering.
|
|
17
|
+
*
|
|
18
|
+
* @param walletProvider - The wallet provider to use for network filtering
|
|
19
|
+
* @param args - Optional filters: maxUsdcPrice
|
|
20
|
+
* @returns JSON string with the list of services (filtered by network and description)
|
|
21
|
+
*/
|
|
22
|
+
discoverX402Services(walletProvider: EvmWalletProvider, args: z.infer<typeof ListX402ServicesSchema>): Promise<string>;
|
|
15
23
|
/**
|
|
16
24
|
* Makes a basic HTTP request to an API endpoint.
|
|
17
25
|
*
|
|
@@ -43,13 +51,5 @@ export declare class X402ActionProvider extends ActionProvider<EvmWalletProvider
|
|
|
43
51
|
* @returns True if the network is supported, false otherwise
|
|
44
52
|
*/
|
|
45
53
|
supportsNetwork: (network: Network) => boolean;
|
|
46
|
-
/**
|
|
47
|
-
* Helper method to handle HTTP errors consistently.
|
|
48
|
-
*
|
|
49
|
-
* @param error - The axios error to handle
|
|
50
|
-
* @param url - The URL that was being accessed when the error occurred
|
|
51
|
-
* @returns A JSON string containing formatted error details
|
|
52
|
-
*/
|
|
53
|
-
private handleHttpError;
|
|
54
54
|
}
|
|
55
55
|
export declare const x402ActionProvider: () => X402ActionProvider;
|
|
@@ -20,7 +20,10 @@ const schemas_1 = require("./schemas");
|
|
|
20
20
|
const wallet_providers_1 = require("../../wallet-providers");
|
|
21
21
|
const axios_1 = __importDefault(require("axios"));
|
|
22
22
|
const x402_axios_1 = require("x402-axios");
|
|
23
|
-
const
|
|
23
|
+
const verify_1 = require("x402/verify");
|
|
24
|
+
const x402_1 = require("@coinbase/x402");
|
|
25
|
+
const utils_1 = require("./utils");
|
|
26
|
+
const SUPPORTED_NETWORKS = ["base-mainnet", "base-sepolia", "solana-mainnet", "solana-devnet"];
|
|
24
27
|
/**
|
|
25
28
|
* X402ActionProvider provides actions for making HTTP requests, with optional x402 payment handling.
|
|
26
29
|
*/
|
|
@@ -39,6 +42,116 @@ class X402ActionProvider extends actionProvider_1.ActionProvider {
|
|
|
39
42
|
*/
|
|
40
43
|
this.supportsNetwork = (network) => network.protocolFamily === "evm" && SUPPORTED_NETWORKS.includes(network.networkId);
|
|
41
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Discovers available x402 services with optional filtering.
|
|
47
|
+
*
|
|
48
|
+
* @param walletProvider - The wallet provider to use for network filtering
|
|
49
|
+
* @param args - Optional filters: maxUsdcPrice
|
|
50
|
+
* @returns JSON string with the list of services (filtered by network and description)
|
|
51
|
+
*/
|
|
52
|
+
async discoverX402Services(walletProvider, args) {
|
|
53
|
+
try {
|
|
54
|
+
const { list } = (0, verify_1.useFacilitator)(x402_1.facilitator);
|
|
55
|
+
const services = await list();
|
|
56
|
+
if (!services || !services.items) {
|
|
57
|
+
return JSON.stringify({
|
|
58
|
+
error: true,
|
|
59
|
+
message: "No services found",
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
// Get the current wallet network
|
|
63
|
+
const walletNetwork = (0, utils_1.getX402Network)(walletProvider.getNetwork());
|
|
64
|
+
// Filter services by network, description, and optional USDC price
|
|
65
|
+
const hasValidMaxUsdcPrice = typeof args.maxUsdcPrice === "number" &&
|
|
66
|
+
Number.isFinite(args.maxUsdcPrice) &&
|
|
67
|
+
args.maxUsdcPrice > 0;
|
|
68
|
+
const filteredServices = services.items.filter(item => {
|
|
69
|
+
// Filter by network - only include services that accept the current wallet network
|
|
70
|
+
const accepts = Array.isArray(item.accepts) ? item.accepts : [];
|
|
71
|
+
const hasMatchingNetwork = accepts.some(req => req.network === walletNetwork);
|
|
72
|
+
// Filter out services with empty or default descriptions
|
|
73
|
+
const hasDescription = accepts.some(req => req.description &&
|
|
74
|
+
req.description.trim() !== "" &&
|
|
75
|
+
req.description.trim() !== "Access to protected content");
|
|
76
|
+
return hasMatchingNetwork && hasDescription;
|
|
77
|
+
});
|
|
78
|
+
// Apply USDC price filtering if maxUsdcPrice is provided (only consider USDC assets)
|
|
79
|
+
let priceFilteredServices = filteredServices;
|
|
80
|
+
if (hasValidMaxUsdcPrice) {
|
|
81
|
+
priceFilteredServices = [];
|
|
82
|
+
for (const item of filteredServices) {
|
|
83
|
+
const accepts = Array.isArray(item.accepts) ? item.accepts : [];
|
|
84
|
+
let shouldInclude = false;
|
|
85
|
+
for (const req of accepts) {
|
|
86
|
+
if (req.network === walletNetwork && req.asset && req.maxAmountRequired) {
|
|
87
|
+
// Only consider USDC assets when maxUsdcPrice filter is applied
|
|
88
|
+
if ((0, utils_1.isUsdcAsset)(req.asset, walletProvider)) {
|
|
89
|
+
try {
|
|
90
|
+
const maxUsdcPriceAtomic = await (0, utils_1.convertWholeUnitsToAtomic)(args.maxUsdcPrice, req.asset, walletProvider);
|
|
91
|
+
if (maxUsdcPriceAtomic) {
|
|
92
|
+
const requirement = BigInt(req.maxAmountRequired);
|
|
93
|
+
const maxUsdcPriceAtomicBigInt = BigInt(maxUsdcPriceAtomic);
|
|
94
|
+
if (requirement <= maxUsdcPriceAtomicBigInt) {
|
|
95
|
+
shouldInclude = true;
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
// If conversion fails, skip this requirement
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (shouldInclude) {
|
|
108
|
+
priceFilteredServices.push(item);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
// Format the filtered services
|
|
113
|
+
const filtered = await Promise.all(priceFilteredServices.map(async (item) => {
|
|
114
|
+
const accepts = Array.isArray(item.accepts) ? item.accepts : [];
|
|
115
|
+
const matchingAccept = accepts.find(req => req.network === walletNetwork);
|
|
116
|
+
// Format amount if available
|
|
117
|
+
let formattedMaxAmount = matchingAccept?.maxAmountRequired;
|
|
118
|
+
if (matchingAccept?.maxAmountRequired && matchingAccept?.asset) {
|
|
119
|
+
formattedMaxAmount = await (0, utils_1.formatPaymentOption)({
|
|
120
|
+
asset: matchingAccept.asset,
|
|
121
|
+
maxAmountRequired: matchingAccept.maxAmountRequired,
|
|
122
|
+
network: matchingAccept.network,
|
|
123
|
+
}, walletProvider);
|
|
124
|
+
}
|
|
125
|
+
return {
|
|
126
|
+
resource: item.resource,
|
|
127
|
+
description: matchingAccept?.description || "",
|
|
128
|
+
cost: formattedMaxAmount,
|
|
129
|
+
...(matchingAccept?.outputSchema?.input && {
|
|
130
|
+
input: matchingAccept.outputSchema.input,
|
|
131
|
+
}),
|
|
132
|
+
...(matchingAccept?.outputSchema?.output && {
|
|
133
|
+
output: matchingAccept.outputSchema.output,
|
|
134
|
+
}),
|
|
135
|
+
...(item.metadata && item.metadata.length > 0 && { metadata: item.metadata }),
|
|
136
|
+
};
|
|
137
|
+
}));
|
|
138
|
+
return JSON.stringify({
|
|
139
|
+
success: true,
|
|
140
|
+
walletNetwork,
|
|
141
|
+
total: services.items.length,
|
|
142
|
+
returned: filtered.length,
|
|
143
|
+
items: filtered,
|
|
144
|
+
}, null, 2);
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
148
|
+
return JSON.stringify({
|
|
149
|
+
error: true,
|
|
150
|
+
message: "Failed to list x402 services",
|
|
151
|
+
details: message,
|
|
152
|
+
}, null, 2);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
42
155
|
/**
|
|
43
156
|
* Makes a basic HTTP request to an API endpoint.
|
|
44
157
|
*
|
|
@@ -64,19 +177,32 @@ class X402ActionProvider extends actionProvider_1.ActionProvider {
|
|
|
64
177
|
data: response.data,
|
|
65
178
|
}, null, 2);
|
|
66
179
|
}
|
|
180
|
+
// Check if wallet network matches any available payment options
|
|
181
|
+
const walletNetwork = (0, utils_1.getX402Network)(walletProvider.getNetwork());
|
|
182
|
+
const availableNetworks = response.data.accepts.map(option => option.network);
|
|
183
|
+
const hasMatchingNetwork = availableNetworks.includes(walletNetwork);
|
|
184
|
+
let paymentOptionsText = `The wallet network ${walletNetwork} does not match any available payment options (${availableNetworks.join(", ")}).`;
|
|
185
|
+
// Format payment options for matching networks
|
|
186
|
+
if (hasMatchingNetwork) {
|
|
187
|
+
const matchingOptions = response.data.accepts.filter(option => option.network === walletNetwork);
|
|
188
|
+
const formattedOptions = await Promise.all(matchingOptions.map(option => (0, utils_1.formatPaymentOption)(option, walletProvider)));
|
|
189
|
+
paymentOptionsText = `The payment options are: ${formattedOptions.join(", ")}`;
|
|
190
|
+
}
|
|
67
191
|
return JSON.stringify({
|
|
68
192
|
status: "error_402_payment_required",
|
|
69
193
|
acceptablePaymentOptions: response.data.accepts,
|
|
70
194
|
nextSteps: [
|
|
71
195
|
"Inform the user that the requested server replied with a 402 Payment Required response.",
|
|
72
|
-
|
|
73
|
-
"Ask the user if they want to retry the request with payment.",
|
|
74
|
-
|
|
196
|
+
paymentOptionsText,
|
|
197
|
+
hasMatchingNetwork ? "Ask the user if they want to retry the request with payment." : "",
|
|
198
|
+
hasMatchingNetwork
|
|
199
|
+
? `Use retry_http_request_with_x402 to retry the request with payment.`
|
|
200
|
+
: "",
|
|
75
201
|
],
|
|
76
202
|
});
|
|
77
203
|
}
|
|
78
204
|
catch (error) {
|
|
79
|
-
return
|
|
205
|
+
return (0, utils_1.handleHttpError)(error, args.url);
|
|
80
206
|
}
|
|
81
207
|
}
|
|
82
208
|
/**
|
|
@@ -88,6 +214,16 @@ class X402ActionProvider extends actionProvider_1.ActionProvider {
|
|
|
88
214
|
*/
|
|
89
215
|
async retryWithX402(walletProvider, args) {
|
|
90
216
|
try {
|
|
217
|
+
// Check network compatibility before attempting payment
|
|
218
|
+
const walletNetwork = (0, utils_1.getX402Network)(walletProvider.getNetwork());
|
|
219
|
+
const selectedNetwork = args.selectedPaymentOption.network;
|
|
220
|
+
if (walletNetwork !== selectedNetwork) {
|
|
221
|
+
return JSON.stringify({
|
|
222
|
+
error: true,
|
|
223
|
+
message: "Network mismatch",
|
|
224
|
+
details: `Wallet is on ${walletNetwork} but payment requires ${selectedNetwork}`,
|
|
225
|
+
}, null, 2);
|
|
226
|
+
}
|
|
91
227
|
// Make the request with payment handling
|
|
92
228
|
const account = walletProvider.toSigner();
|
|
93
229
|
const paymentSelector = (accepts) => {
|
|
@@ -108,7 +244,9 @@ class X402ActionProvider extends actionProvider_1.ActionProvider {
|
|
|
108
244
|
}
|
|
109
245
|
return accepts[0];
|
|
110
246
|
};
|
|
111
|
-
const api = (0, x402_axios_1.withPaymentInterceptor)(axios_1.default.create({}),
|
|
247
|
+
const api = (0, x402_axios_1.withPaymentInterceptor)(axios_1.default.create({}),
|
|
248
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
249
|
+
account, paymentSelector);
|
|
112
250
|
const response = await api.request({
|
|
113
251
|
url: args.url,
|
|
114
252
|
method: args.method ?? "GET",
|
|
@@ -142,7 +280,7 @@ class X402ActionProvider extends actionProvider_1.ActionProvider {
|
|
|
142
280
|
});
|
|
143
281
|
}
|
|
144
282
|
catch (error) {
|
|
145
|
-
return
|
|
283
|
+
return (0, utils_1.handleHttpError)(error, args.url);
|
|
146
284
|
}
|
|
147
285
|
}
|
|
148
286
|
/**
|
|
@@ -155,6 +293,7 @@ class X402ActionProvider extends actionProvider_1.ActionProvider {
|
|
|
155
293
|
async makeHttpRequestWithX402(walletProvider, args) {
|
|
156
294
|
try {
|
|
157
295
|
const account = walletProvider.toSigner();
|
|
296
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
158
297
|
const api = (0, x402_axios_1.withPaymentInterceptor)(axios_1.default.create({}), account);
|
|
159
298
|
const response = await api.request({
|
|
160
299
|
url: args.url,
|
|
@@ -183,42 +322,21 @@ class X402ActionProvider extends actionProvider_1.ActionProvider {
|
|
|
183
322
|
}, null, 2);
|
|
184
323
|
}
|
|
185
324
|
catch (error) {
|
|
186
|
-
return
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* Helper method to handle HTTP errors consistently.
|
|
191
|
-
*
|
|
192
|
-
* @param error - The axios error to handle
|
|
193
|
-
* @param url - The URL that was being accessed when the error occurred
|
|
194
|
-
* @returns A JSON string containing formatted error details
|
|
195
|
-
*/
|
|
196
|
-
handleHttpError(error, url) {
|
|
197
|
-
if (error.response) {
|
|
198
|
-
return JSON.stringify({
|
|
199
|
-
error: true,
|
|
200
|
-
message: `HTTP ${error.response.status} error when accessing ${url}`,
|
|
201
|
-
details: error.response.data?.error || error.response.statusText,
|
|
202
|
-
suggestion: "Check if the URL is correct and the API is available.",
|
|
203
|
-
}, null, 2);
|
|
204
|
-
}
|
|
205
|
-
if (error.request) {
|
|
206
|
-
return JSON.stringify({
|
|
207
|
-
error: true,
|
|
208
|
-
message: `Network error when accessing ${url}`,
|
|
209
|
-
details: error.message,
|
|
210
|
-
suggestion: "Check your internet connection and verify the API endpoint is accessible.",
|
|
211
|
-
}, null, 2);
|
|
325
|
+
return (0, utils_1.handleHttpError)(error, args.url);
|
|
212
326
|
}
|
|
213
|
-
return JSON.stringify({
|
|
214
|
-
error: true,
|
|
215
|
-
message: `Error making request to ${url}`,
|
|
216
|
-
details: error.message,
|
|
217
|
-
suggestion: "Please check the request parameters and try again.",
|
|
218
|
-
}, null, 2);
|
|
219
327
|
}
|
|
220
328
|
}
|
|
221
329
|
exports.X402ActionProvider = X402ActionProvider;
|
|
330
|
+
__decorate([
|
|
331
|
+
(0, actionDecorator_1.CreateAction)({
|
|
332
|
+
name: "discover_x402_services",
|
|
333
|
+
description: "Discover available x402 services. Optionally filter by a maximum price in whole units of USDC (only USDC payment options will be considered when filter is applied).",
|
|
334
|
+
schema: schemas_1.ListX402ServicesSchema,
|
|
335
|
+
}),
|
|
336
|
+
__metadata("design:type", Function),
|
|
337
|
+
__metadata("design:paramtypes", [wallet_providers_1.EvmWalletProvider, void 0]),
|
|
338
|
+
__metadata("design:returntype", Promise)
|
|
339
|
+
], X402ActionProvider.prototype, "discoverX402Services", null);
|
|
222
340
|
__decorate([
|
|
223
341
|
(0, actionDecorator_1.CreateAction)({
|
|
224
342
|
name: "make_http_request",
|
|
@@ -272,6 +390,7 @@ This action combines both steps into one, which means:
|
|
|
272
390
|
- No chance to review payment details before paying
|
|
273
391
|
- No confirmation step
|
|
274
392
|
- Automatic payment processing
|
|
393
|
+
- Assumes payment option is compatible with wallet network
|
|
275
394
|
|
|
276
395
|
EXAMPLES:
|
|
277
396
|
- Production: make_http_request_with_x402("https://api.example.com/data")
|