import { wrapFetchWithPayment, x402Client } from '@x402/fetch';
import { ExactEvmScheme } from '@x402/evm/exact/client';
import { createWalletClient, createPublicClient, http } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
import { base } from 'viem/chains';
import {
EscrowScheme,
createBalanceSelector,
preferTokenPolicy,
} from '@agentokratia/x402-escrow/client';
const WETH = '0x4200000000000000000000000000000000000006';
const USDC = '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913';
const account = privateKeyToAccount(process.env.PRIVATE_KEY);
const walletClient = createWalletClient({
account, chain: base, transport: http(),
});
const publicClient = createPublicClient({
chain: base, transport: http(),
});
const client = new x402Client(
createBalanceSelector(publicClient, account.address)
)
.register('eip155:8453', new ExactEvmScheme(account))
.register('eip155:8453', new EscrowScheme(walletClient))
.registerPolicy(preferTokenPolicy([WETH, USDC]));
const paidFetch = wrapFetchWithPayment(fetch, client);
const response = await paidFetch('https://api.example.com/premium');