run402 1.18.0 → 1.18.1
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/lib/functions.mjs +3 -22
- package/lib/paid-fetch.mjs +65 -1
- package/package.json +1 -1
package/lib/functions.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { readFileSync
|
|
2
|
-
import { findProject,
|
|
1
|
+
import { readFileSync } from "fs";
|
|
2
|
+
import { findProject, API } from "./config.mjs";
|
|
3
|
+
import { setupPaidFetch } from "./paid-fetch.mjs";
|
|
3
4
|
|
|
4
5
|
const HELP = `run402 functions — Manage serverless functions
|
|
5
6
|
|
|
@@ -27,26 +28,6 @@ Notes:
|
|
|
27
28
|
- Deploy may require payment if the project lease has expired
|
|
28
29
|
`;
|
|
29
30
|
|
|
30
|
-
async function setupPaidFetch() {
|
|
31
|
-
if (!existsSync(ALLOWANCE_FILE)) {
|
|
32
|
-
console.error(JSON.stringify({ status: "error", message: "No agent allowance found. Run: run402 allowance create && run402 allowance fund" }));
|
|
33
|
-
process.exit(1);
|
|
34
|
-
}
|
|
35
|
-
const allowance = readAllowance();
|
|
36
|
-
const { privateKeyToAccount } = await import("viem/accounts");
|
|
37
|
-
const { createPublicClient, http } = await import("viem");
|
|
38
|
-
const { baseSepolia } = await import("viem/chains");
|
|
39
|
-
const { x402Client, wrapFetchWithPayment } = await import("@x402/fetch");
|
|
40
|
-
const { ExactEvmScheme } = await import("@x402/evm/exact/client");
|
|
41
|
-
const { toClientEvmSigner } = await import("@x402/evm");
|
|
42
|
-
const account = privateKeyToAccount(allowance.privateKey);
|
|
43
|
-
const publicClient = createPublicClient({ chain: baseSepolia, transport: http() });
|
|
44
|
-
const signer = toClientEvmSigner(account, publicClient);
|
|
45
|
-
const client = new x402Client();
|
|
46
|
-
client.register("eip155:84532", new ExactEvmScheme(signer));
|
|
47
|
-
return wrapFetchWithPayment(fetch, client);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
31
|
async function deploy(projectId, name, args) {
|
|
51
32
|
const p = findProject(projectId);
|
|
52
33
|
const opts = { file: null, timeout: undefined, memory: undefined, deps: undefined };
|
package/lib/paid-fetch.mjs
CHANGED
|
@@ -3,11 +3,33 @@
|
|
|
3
3
|
* Branches on allowance rail:
|
|
4
4
|
* - "mpp": uses mppx.fetch (Tempo pathUSD)
|
|
5
5
|
* - "x402" (default): uses @x402/fetch (Base USDC)
|
|
6
|
+
*
|
|
7
|
+
* Checks on-chain balances at setup time and selects funded networks.
|
|
6
8
|
*/
|
|
7
9
|
|
|
8
10
|
import { readAllowance, ALLOWANCE_FILE } from "./config.mjs";
|
|
9
11
|
import { existsSync } from "fs";
|
|
10
12
|
|
|
13
|
+
const USDC_ABI = [{ name: "balanceOf", type: "function", stateMutability: "view", inputs: [{ name: "account", type: "address" }], outputs: [{ name: "", type: "uint256" }] }];
|
|
14
|
+
const USDC_MAINNET = "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913";
|
|
15
|
+
const USDC_SEPOLIA = "0x036CbD53842c5426634e7929541eC2318f3dCF7e";
|
|
16
|
+
const PATH_USD = "0x20c0000000000000000000000000000000000000";
|
|
17
|
+
const TEMPO_RPC = "https://rpc.moderato.tempo.xyz/";
|
|
18
|
+
|
|
19
|
+
async function checkBalance(publicClient, tokenAddress, walletAddress) {
|
|
20
|
+
try {
|
|
21
|
+
const raw = await publicClient.readContract({
|
|
22
|
+
address: tokenAddress,
|
|
23
|
+
abi: USDC_ABI,
|
|
24
|
+
functionName: "balanceOf",
|
|
25
|
+
args: [walletAddress],
|
|
26
|
+
});
|
|
27
|
+
return Number(raw);
|
|
28
|
+
} catch {
|
|
29
|
+
return 0;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
11
33
|
export async function setupPaidFetch() {
|
|
12
34
|
if (!existsSync(ALLOWANCE_FILE)) {
|
|
13
35
|
console.error(JSON.stringify({ status: "error", message: "No agent allowance found. Run: run402 allowance create && run402 allowance fund" }));
|
|
@@ -18,6 +40,23 @@ export async function setupPaidFetch() {
|
|
|
18
40
|
const account = privateKeyToAccount(allowance.privateKey);
|
|
19
41
|
|
|
20
42
|
if (allowance.rail === "mpp") {
|
|
43
|
+
const { createPublicClient, http, defineChain } = await import("viem");
|
|
44
|
+
const tempoModerato = defineChain({
|
|
45
|
+
id: 42431,
|
|
46
|
+
name: "Tempo Moderato",
|
|
47
|
+
nativeCurrency: { name: "pathUSD", symbol: "pathUSD", decimals: 6 },
|
|
48
|
+
rpcUrls: { default: { http: [TEMPO_RPC] } },
|
|
49
|
+
});
|
|
50
|
+
const tempoClient = createPublicClient({ chain: tempoModerato, transport: http() });
|
|
51
|
+
const balance = await checkBalance(tempoClient, PATH_USD, allowance.address);
|
|
52
|
+
if (balance === 0) {
|
|
53
|
+
console.error(JSON.stringify({
|
|
54
|
+
status: "error",
|
|
55
|
+
message: `No pathUSD balance on Tempo Moderato (0). Fund your wallet: run402 allowance fund`,
|
|
56
|
+
}));
|
|
57
|
+
process.exit(1);
|
|
58
|
+
}
|
|
59
|
+
|
|
21
60
|
const { Mppx, tempo } = await import("mppx/client");
|
|
22
61
|
const mppx = Mppx.create({
|
|
23
62
|
polyfill: false,
|
|
@@ -26,7 +65,7 @@ export async function setupPaidFetch() {
|
|
|
26
65
|
return mppx.fetch;
|
|
27
66
|
}
|
|
28
67
|
|
|
29
|
-
// Default: x402
|
|
68
|
+
// Default: x402
|
|
30
69
|
const { createPublicClient, http } = await import("viem");
|
|
31
70
|
const { base, baseSepolia } = await import("viem/chains");
|
|
32
71
|
const { x402Client, wrapFetchWithPayment } = await import("@x402/fetch");
|
|
@@ -36,8 +75,33 @@ export async function setupPaidFetch() {
|
|
|
36
75
|
const mainnetClient = createPublicClient({ chain: base, transport: http() });
|
|
37
76
|
const sepoliaClient = createPublicClient({ chain: baseSepolia, transport: http() });
|
|
38
77
|
|
|
78
|
+
// Check balances in parallel
|
|
79
|
+
const [mainnetBalance, sepoliaBalance] = await Promise.all([
|
|
80
|
+
checkBalance(mainnetClient, USDC_MAINNET, allowance.address),
|
|
81
|
+
checkBalance(sepoliaClient, USDC_SEPOLIA, allowance.address),
|
|
82
|
+
]);
|
|
83
|
+
|
|
84
|
+
if (mainnetBalance === 0 && sepoliaBalance === 0) {
|
|
85
|
+
console.error(JSON.stringify({
|
|
86
|
+
status: "error",
|
|
87
|
+
message: `No USDC balance on any supported network (Base: $${(mainnetBalance / 1e6).toFixed(2)}, Base Sepolia: $${(sepoliaBalance / 1e6).toFixed(2)}). Fund your wallet or run: run402 allowance fund`,
|
|
88
|
+
}));
|
|
89
|
+
process.exit(1);
|
|
90
|
+
}
|
|
91
|
+
|
|
39
92
|
const client = new x402Client();
|
|
40
93
|
client.register("eip155:8453", new ExactEvmScheme(toClientEvmSigner(account, mainnetClient)));
|
|
41
94
|
client.register("eip155:84532", new ExactEvmScheme(toClientEvmSigner(account, sepoliaClient)));
|
|
95
|
+
|
|
96
|
+
// Policy: only allow networks where the wallet has funds
|
|
97
|
+
client.registerPolicy((_version, reqs) => {
|
|
98
|
+
const funded = reqs.filter((r) => {
|
|
99
|
+
if (r.network === "eip155:8453") return mainnetBalance > 0;
|
|
100
|
+
if (r.network === "eip155:84532") return sepoliaBalance > 0;
|
|
101
|
+
return false;
|
|
102
|
+
});
|
|
103
|
+
return funded.length > 0 ? funded : reqs;
|
|
104
|
+
});
|
|
105
|
+
|
|
42
106
|
return wrapFetchWithPayment(fetch, client);
|
|
43
107
|
}
|
package/package.json
CHANGED