import { createPublicClient, createWalletClient, http } from 'viem';
import { baseSepolia } from 'viem/chains';
import { privateKeyToAccount } from 'viem/accounts';
import { X402rClient } from '@x402r/client';
import { getNetworkConfig, RequestStatus } from '@x402r/core';
async function main() {
const publicClient = createPublicClient({
chain: baseSepolia,
transport: http(),
});
const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`);
const walletClient = createWalletClient({
account,
chain: baseSepolia,
transport: http(),
});
const config = getNetworkConfig('eip155:84532')!;
const client = new X402rClient({
publicClient,
walletClient,
operatorAddress: '0x...',
refundRequestAddress: config.refundRequest,
});
// Check if a refund request already exists
const hasRequest = await client.hasRefundRequest(paymentInfo, 0n);
if (!hasRequest) {
// Request a refund
const { txHash } = await client.requestRefund(
paymentInfo,
paymentInfo.maxAmount, // full refund
0n
);
console.log(`Refund requested: ${txHash}`);
}
// Check status
const status = await client.getRefundStatus(paymentInfo, 0n);
if (status === RequestStatus.Approved) {
console.log('Refund was approved!');
}
// List my refund requests (paginated)
const { keys, total } = await client.getMyRefundRequests(0n, 10n);
console.log(`${total} total refund requests`);
for (const key of keys) {
const request = await client.getRefundRequestByKey(key);
console.log(`Amount: ${request.amount}, Status: ${request.status}`);
}
}
main().catch(console.error);