To Base Chain
import { Controller } from "@hotwired/stimulus" import { wrapFetchWithPayment } from "x402-fetch" import { createWalletClient, custom } from "viem" import { baseSepolia, base } from "viem/chains" // 同时导入 mainnet,方便以后切换
export default class extends Controller { static targets = ["status", "post", "button"] static values = { url: String, // 可选:通过 data-attribute 指定目标链,默认 sepolia chain: String // "sepolia" 或 "mainnet" }
// 统一获取目标链配置 get targetChain() { const chain = this.chainValue || "sepolia" return chain === "mainnet" ? base : baseSepolia }
async pay() { this.buttonTarget.disabled = true this.buttonTarget.textContent = "连接中..."
if (!window.ethereum) {
this.statusTarget.textContent = "错误:请安装 MetaMask"
this.resetButton()
return
}
try {
/...
回复