migrating-an-onchainkit-app
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseOnchainKit Migration Skill
OnchainKit 迁移技能
Migrate apps from to standalone / components with zero OnchainKit dependency.
@coinbase/onchainkitwagmiviem将应用从迁移到独立的/组件,完全移除OnchainKit依赖。
@coinbase/onchainkitwagmiviemBefore Starting
开始之前
Create a file in the project root:
mistakes.mdmarkdown
undefined在项目根目录创建文件:
mistakes.mdmarkdown
undefinedMigration Mistakes & Learnings
迁移错误与经验总结
Track errors, fixes, and lessons learned during OnchainKit migration.
记录OnchainKit迁移过程中遇到的错误、修复方案和经验教训。
Errors
错误记录
Lessons Learned
经验总结
Append every error, fix, and lesson to this file throughout the migration. This file improves the skill over time.
在整个迁移过程中,将所有错误、修复方案和经验教训追加到该文件中。此文件将持续优化该技能的能力。Migration Workflow
迁移流程
Migrations MUST happen in this order. Do not skip steps.
迁移必须按照以下顺序进行,请勿跳过步骤。
Step 1: Detection
步骤1:检测
Scan the project to understand current OnchainKit usage:
- Search all files for imports
@coinbase/onchainkit - Identify which components are used:
- Provider: (always present if using OnchainKit)
OnchainKitProvider - Wallet: ,
Wallet,ConnectWallet,WalletDropdown,WalletModalConnected - Transaction: ,
Transaction,TransactionButtonTransactionStatus - Other: ,
Identity,Avatar,Name,Address,Swap, etc.Checkout
- Provider:
- Check for existing
package.json,wagmi,viemdependencies@tanstack/react-query - Identify the project's styling approach (Tailwind, CSS Modules, styled-components, etc.)
- Report findings to the user before proceeding
扫描项目以了解当前OnchainKit的使用情况:
- 搜索所有文件中的导入语句
@coinbase/onchainkit - 识别使用的组件类型:
- 提供者:(如果使用OnchainKit则必定存在)
OnchainKitProvider - 钱包:、
Wallet、ConnectWallet、WalletDropdown、WalletModalConnected - 交易:、
Transaction、TransactionButtonTransactionStatus - 其他:、
Identity、Avatar、Name、Address、Swap等Checkout
- 提供者:
- 检查中是否已存在
package.json、wagmi、viem依赖@tanstack/react-query - 识别项目的样式方案(Tailwind、CSS Modules、styled-components等)
- 在继续之前向用户报告检测结果
Step 2: Provider Migration (always first)
步骤2:提供者迁移(必须首先执行)
Read references/provider-migration.md for detailed instructions and code.
Summary:
- Ensure ,
wagmi, andviemare installed@tanstack/react-query - Create with chain and connector configuration
wagmi-config.ts - Replace with
OnchainKitProvider+WagmiProviderQueryClientProvider - Remove import
@coinbase/onchainkit/styles.css - Remove any or MiniKit imports from layout files
SafeArea
Validation gate: Run (or the project's build command). Must pass before continuing. If it fails, fix errors and document them in .
npm run buildmistakes.md阅读references/provider-migration.md获取详细说明和代码示例。
总结:
- 确保已安装、
wagmi和viem@tanstack/react-query - 创建文件,配置链和连接器
wagmi-config.ts - 将替换为
OnchainKitProvider+WagmiProviderQueryClientProvider - 移除导入语句
@coinbase/onchainkit/styles.css - 从布局文件中移除所有或MiniKit导入
SafeArea
验证关卡:运行(或项目对应的构建命令)。必须通过验证才能继续。如果构建失败,修复错误并将其记录到中。
npm run buildmistakes.mdStep 3: Wallet Migration (after provider)
步骤3:钱包迁移(在提供者迁移之后执行)
Read references/wallet-migration.md for detailed instructions and code.
Summary:
- Create a component using wagmi hooks (
WalletConnect,useAccount,useConnect)useDisconnect - Component includes a modal with wallet options: Base Account, Coinbase Wallet, MetaMask
- Shows truncated address + disconnect button when connected
- Replace all OnchainKit wallet imports and component usage
Validation gate: Run . Must pass before continuing. Document any errors in .
npm run buildmistakes.md阅读references/wallet-migration.md获取详细说明和代码示例。
总结:
- 使用wagmi钩子(、
useAccount、useConnect)创建useDisconnect组件WalletConnect - 组件包含带有钱包选项的模态框:Base Account、Coinbase Wallet、MetaMask
- 连接成功后显示截断的地址和断开连接按钮
- 替换所有OnchainKit钱包导入语句和组件用法
验证关卡:运行。必须通过验证才能继续。将任何错误记录到中。
npm run buildmistakes.mdStep 4: Transaction Migration (after wallet)
步骤4:交易迁移(在钱包迁移之后执行)
Read references/transaction-migration.md for detailed instructions and code.
Summary:
- Check the prop on existing
chainIdcomponents -- add any missing chains to<Transaction />wagmi-config.ts - Create a component using wagmi hooks (
TransactionForm,useWriteContract,useWaitForTransactionReceipt)useSwitchChain - Component handles the full lifecycle: idle, pending wallet confirmation, confirming on-chain, success, error
- Replace all OnchainKit transaction imports (,
Transaction,TransactionButton,TransactionStatus, etc.)TransactionSponsor - Update the array format -- use
calls,address,abi,functionNamewith properargstypingas const - Map callback to the new lifecycle status names (init, pending, confirmed, success, error)
onStatus
Validation gate: Run . Must pass before continuing. Document any errors in .
npm run buildmistakes.md阅读references/transaction-migration.md获取详细说明和代码示例。
总结:
- 检查现有组件上的
<Transaction />属性——将所有缺失的链添加到chainId中wagmi-config.ts - 使用wagmi钩子(、
useWriteContract、useWaitForTransactionReceipt)创建useSwitchChain组件TransactionForm - 组件处理完整生命周期:空闲、等待钱包确认、链上确认、成功、错误
- 替换所有OnchainKit交易导入语句(、
Transaction、TransactionButton、TransactionStatus等)TransactionSponsor - 更新数组格式——使用
calls、address、abi、functionName并添加正确的args类型声明as const - 将回调映射到新的生命周期状态名称(init、pending、confirmed、success、error)
onStatus
验证关卡:运行。必须通过验证才能继续。将任何错误记录到中。
npm run buildmistakes.mdStep 5: Cleanup
步骤5:清理
- Search for any remaining imports -- there should be none
@coinbase/onchainkit - Optionally remove from
@coinbase/onchainkitdependenciespackage.json - Run final to verify everything works
npm run build - Update with final lessons learned
mistakes.md
- 搜索所有剩余的导入语句——确保没有遗漏
@coinbase/onchainkit - (可选)从依赖中移除
package.json@coinbase/onchainkit - 运行最终的以验证所有功能正常
npm run build - 将最终的经验教训更新到中
mistakes.md
Troubleshooting
故障排除
Build fails after provider migration
提供者迁移后构建失败
- Missing dependencies: Ensure ,
wagmi,viemare installed@tanstack/react-query - Type errors with wagmi config: Check that array has at least one chain and
chainshas a matching entrytransports - "use client" missing: Both the provider and wallet components must have directive
"use client"
- 缺失依赖:确保已安装、
wagmi、viem@tanstack/react-query - wagmi配置类型错误:检查数组至少包含一条链,且
chains有对应的条目transports - 缺少"use client"指令:提供者和钱包组件都必须添加指令
"use client"
MetaMask SDK react-native warning
MetaMask SDK react-native警告
- The warning is harmless in web builds. It comes from MetaMask SDK and does not affect functionality.
Can't resolve '@react-native-async-storage/async-storage'
- 警告在Web构建中是无害的。它来自MetaMask SDK,不影响功能。
Can't resolve '@react-native-async-storage/async-storage'
Wallet won't connect
钱包无法连接
- Verify the wagmi config has the correct connectors configured
- Check that wraps the component tree before any wallet hooks are used
WagmiProvider - Ensure is inside
QueryClientProviderWagmiProvider
- 验证wagmi配置是否配置了正确的连接器
- 检查是否在任何钱包钩子使用之前包裹了组件树
WagmiProvider - 确保位于
QueryClientProvider内部WagmiProvider
Transaction receipt stuck in pending
交易收据一直处于pending状态
- Symptom: Transaction hash appears, tx confirms on-chain, but UI stays stuck on "Transaction in progress..." forever
- Cause: has no RPC to poll because the transaction's chain is missing from the wagmi config's
useWaitForTransactionReceipt+chainstransports - Fix: (1) Add the target chain to chains array AND transports object. (2) Always pass
wagmi-config.tstochainIdso it polls the correct chainuseWaitForTransactionReceipt({ hash, chainId })
- 症状:交易哈希已显示,交易在链上已确认,但UI一直停留在“交易处理中...”
- 原因:没有可轮询的RPC,因为交易所在的链未添加到wagmi配置的
useWaitForTransactionReceipt+chains中transports - 修复方案:(1) 将目标链添加到的chains数组和transports对象中。(2) 始终将
wagmi-config.ts传递给chainId,以便它轮询正确的链useWaitForTransactionReceipt({ hash, chainId })
Transaction targets wrong chain
交易指向错误的链
- The auto-switches chains, but the target chain must exist in the wagmi config's
TransactionFormarray andchainsobjecttransports - Common: add for testnet transactions (chainId 84532)
baseSepolia
- 会自动切换链,但目标链必须存在于wagmi配置的
TransactionForm数组和chains对象中transports - 常见情况:为测试网交易添加(chainId 84532)
baseSepolia
Next.js page export restrictions
Next.js页面导出限制
- Next.js only allows specific named exports from page files. Exporting contract call arrays or ABI constants from a page file will cause a build error
- Fix: make them non-exported declarations or move them to a separate module
const
- Next.js仅允许页面文件导出特定的命名导出。从页面文件导出合约调用数组或ABI常量会导致构建错误
- 修复方案:将它们改为非导出的声明,或者移至单独的模块中
const
ABI type errors after transaction migration
交易迁移后ABI类型错误
- When defining ABIs inline, use on the array for proper type inference
as const - Mark individual fields like and
type: 'function' as conststateMutability: 'nonpayable' as const
- 内联定义ABI时,对数组使用以获得正确的类型推断
as const - 标记单个字段,如和
type: 'function' as conststateMutability: 'nonpayable' as const
Existing wagmi setup detected
检测到现有wagmi配置
- If the project already wraps with , do NOT add another one
WagmiProvider - Instead, just update the existing wagmi config to include the needed connectors
- OnchainKit's provider detects and defers to existing wagmi providers -- the standalone setup should too
- 如果项目已使用包裹,请勿添加另一个
WagmiProvider - 相反,只需更新现有的wagmi配置以包含所需的连接器
- OnchainKit的提供者会检测并适配现有的wagmi提供者——独立设置也应如此
Important Notes
重要说明
- Always use and
wagmidirectly. Never import fromviem.@coinbase/onchainkit - The connector comes from
baseAccount, not from a separate package.wagmi/connectors - must include every chain the app transacts on. If the original OnchainKit
wagmi-config.tsused a specific chain, that chain must be in both<Transaction chainId={X} />andchains. Without it,transportswill hang forever.useWaitForTransactionReceipt - If the project uses Tailwind, use Tailwind classes for the components. If not, adapt to inline styles or the project's existing styling approach (e.g., CSS Modules).
- Do not export contract call arrays, ABI constants, or other non-page values from Next.js page files. Use non-exported constants or a separate module.
- Inspect the OnchainKit source code in if you need to understand how a specific component works internally.
node_modules/@coinbase/onchainkit/src/
- 始终直接使用和
wagmi。切勿从viem导入。@coinbase/onchainkit - 连接器来自
baseAccount,而非单独的包。wagmi/connectors - 必须包含应用进行交易的所有链。如果原始OnchainKit的
wagmi-config.ts使用了特定链,该链必须同时存在于<Transaction chainId={X} />和chains中。否则,transports会一直挂起。useWaitForTransactionReceipt - 如果项目使用Tailwind,为组件使用Tailwind类。如果不使用,则适配内联样式或项目现有的样式方案(如CSS Modules)。
- 不要从Next.js页面文件导出合约调用数组、ABI常量或其他非页面值。使用非导出常量或单独的模块。
- 如果需要了解特定组件的内部工作原理,可以查看中的OnchainKit源代码。
node_modules/@coinbase/onchainkit/src/