import { createLocalWallet } from "@faremeter/wallet-evm";
import { createPaymentHandler } from "@faremeter/payment-evm/exact";
import { wrap as wrapFetch } from "@faremeter/fetch";
import { createPublicClient, http, formatEther, defineChain } from "viem";
import "dotenv/config";
export const skaleChain = defineChain({
id: 324705682,
name: "SKALE Base Sepolia",
nativeCurrency: { decimals: 18, name: "Credits", symbol: "CREDIT" },
rpcUrls: {
default: { http: ["https://base-sepolia-testnet.skalenodes.com/v1/base-testnet"] },
},
});
async function main() {
const privateKey = process.env.PRIVATE_KEY;
if (!privateKey) {
throw new Error("PRIVATE_KEY environment variable is required");
}
// Setup wallet
const wallet = await createLocalWallet(skaleChain, privateKey);
console.log(`Wallet address: ${wallet.account.address}`);
// Setup payment-enabled fetch
const assetAddress = process.env.PAYMENT_TOKEN_ADDRESS as `0x${string}`;
const assetName = process.env.PAYMENT_TOKEN_NAME || "Bridged USDC (SKALE Bridge)";
const fetchWithPayment = wrapFetch(fetch, {
handlers: [
createPaymentHandler(wallet, {
asset: {
address: assetAddress,
contractName: assetName,
forwarderName: assetName,
forwarderVersion: "1",
},
}),
],
});
// Access paywalled resource - payment handled automatically!
const url = "http://localhost:3000/premium/data";
try {
console.log(`Accessing ${url}...`);
const response = await fetchWithPayment(url, {
method: "GET",
headers: { "Content-Type": "application/json" },
});
if (!response.ok) {
throw new Error(`Request failed: ${response.status}`);
}
const data = await response.json();
console.log("Premium data:", data);
} catch (error) {
console.error("Error:", error);
}
}
main();