grid402-mcp 1.2.0 → 1.2.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/package.json +2 -1
- package/x402_payer.mjs +90 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "grid402-mcp",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "Official Model Context Protocol (MCP) Server for Grid402 - Public Open Data e Network Utilities Hub (Forex, Crypto, DNS, SSL, ASN, CEP, CNPJ) with Base x402 micropayments e Sandbox.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"files": [
|
|
11
11
|
"index.mjs",
|
|
12
12
|
"tools.mjs",
|
|
13
|
+
"x402_payer.mjs",
|
|
13
14
|
"README.md",
|
|
14
15
|
"package.json"
|
|
15
16
|
],
|
package/x402_payer.mjs
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Grid402 MCP Server - Módulo de Liquidação x402 On-Chain (Base Mainnet)
|
|
3
|
+
*
|
|
4
|
+
* Permite pagamentos automáticos de US$ 0.0005 USDC via transferência ERC-20
|
|
5
|
+
* quando uma chave privada é fornecida no ambiente do agente.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { ethers } from 'ethers';
|
|
9
|
+
|
|
10
|
+
export const BASE_RPC_URLS = [
|
|
11
|
+
'https://mainnet.base.org',
|
|
12
|
+
'https://1rpc.io/base',
|
|
13
|
+
'https://base.llamarpc.com',
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
export const USDC_CONTRACT_BASE = '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913'.toLowerCase();
|
|
17
|
+
export const GRID402_RECIPIENT_WALLET = '0xa31c31069c43ac1c0075a93c325b0dcd7744356c'.toLowerCase();
|
|
18
|
+
export const REQUIRED_RAW_UNITS = 500n; // 0.000500 USDC (6 casas decimais)
|
|
19
|
+
|
|
20
|
+
const ERC20_ABI = [
|
|
21
|
+
'function transfer(address to, uint256 amount) returns (bool)',
|
|
22
|
+
'function balanceOf(address account) view returns (uint256)',
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Cria um provider com fallback resiliente para a Base Mainnet
|
|
27
|
+
*/
|
|
28
|
+
export async function getBaseProvider(preferredRpc = null) {
|
|
29
|
+
const rpcs = preferredRpc ? [preferredRpc, ...BASE_RPC_URLS] : BASE_RPC_URLS;
|
|
30
|
+
for (const rpc of rpcs) {
|
|
31
|
+
try {
|
|
32
|
+
const provider = new ethers.JsonRpcProvider(rpc, 8453, { staticNetwork: true });
|
|
33
|
+
await provider.getBlockNumber();
|
|
34
|
+
return provider;
|
|
35
|
+
} catch {
|
|
36
|
+
// Tenta próximo RPC
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
throw new Error('Não foi possível conectar a nenhum nó RPC da Base Mainnet.');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Executa a transferência de 0.0005 USDC na Base para liquidar o desafio x402
|
|
44
|
+
*
|
|
45
|
+
* @param {object} challenge Desafio retornado pelo Grid402 (HTTP 402)
|
|
46
|
+
* @param {string} privateKey Chave privada hexadecimal da carteira pagadora
|
|
47
|
+
* @param {string} [preferredRpc] URL opcional de RPC da Base
|
|
48
|
+
* @returns {Promise<{ txHash: string, blockNumber: number }>}
|
|
49
|
+
*/
|
|
50
|
+
export async function settleX402Payment(challenge, privateKey, preferredRpc = null) {
|
|
51
|
+
if (!privateKey || typeof privateKey !== 'string' || !privateKey.startsWith('0x')) {
|
|
52
|
+
throw new Error('Chave privada inválida. Deve iniciar com 0x e conter 64 caracteres hexadecimais.');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const recipient = (challenge?.recipient_wallet || GRID402_RECIPIENT_WALLET).toLowerCase();
|
|
56
|
+
const tokenContract = (challenge?.token_contract || USDC_CONTRACT_BASE).toLowerCase();
|
|
57
|
+
const rawUnits = BigInt(challenge?.price_raw_units || REQUIRED_RAW_UNITS);
|
|
58
|
+
|
|
59
|
+
const provider = await getBaseProvider(preferredRpc);
|
|
60
|
+
const wallet = new ethers.Wallet(privateKey, provider);
|
|
61
|
+
|
|
62
|
+
// Instancia contrato ERC-20 do USDC na Base
|
|
63
|
+
const usdc = new ethers.Contract(tokenContract, ERC20_ABI, wallet);
|
|
64
|
+
|
|
65
|
+
// 1. Verifica saldo de USDC
|
|
66
|
+
try {
|
|
67
|
+
const balance = await usdc.balanceOf(wallet.address);
|
|
68
|
+
if (balance < rawUnits) {
|
|
69
|
+
throw new Error(`Saldo insuficiente de USDC na carteira ${wallet.address}. Requerido: 0.0005 USDC, Atual: ${ethers.formatUnits(balance, 6)} USDC.`);
|
|
70
|
+
}
|
|
71
|
+
} catch (err) {
|
|
72
|
+
if (err.message.includes('Saldo insuficiente')) {
|
|
73
|
+
throw err;
|
|
74
|
+
}
|
|
75
|
+
// Caso leitura falhe por RPC restrito, tenta envio direto
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// 2. Envia transferência ERC-20
|
|
79
|
+
const tx = await usdc.transfer(recipient, rawUnits);
|
|
80
|
+
const receipt = await tx.wait(1);
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
txHash: receipt.hash,
|
|
84
|
+
blockNumber: receipt.blockNumber,
|
|
85
|
+
from: wallet.address,
|
|
86
|
+
to: recipient,
|
|
87
|
+
amountUsdc: '0.0005',
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|