如何使用thirdweb接入Polygon Amoy测试网(TypeScript环境)
解决thirdweb接入Polygon Amoy测试网的问题
问题原因
thirdweb内置的supportedChains列表中暂时未将"amoy"或"polygonamoy"作为预设链名,直接使用字符串标识会触发无效链错误。
解决方案
有两种可行方式:
1. 更新thirdweb依赖包(优先尝试)
thirdweb会持续更新内置链列表,先将相关依赖更新到最新版本,之后可能直接使用字符串标识:
npm update @thirdweb-dev/chains @thirdweb-dev/react @thirdweb-dev/sdk
更新完成后,尝试重新使用:
const activeChain = "amoy";
2. 手动自定义Amoy链配置
如果更新后仍无法识别,直接定义Amoy测试网的链参数对象:
import { Chain } from "@thirdweb-dev/chains"; // 定义Polygon Amoy测试网的链配置 const amoyChain: Chain = { chainId: 80002, name: "Polygon Amoy", nativeCurrency: { decimals: 18, name: "MATIC", symbol: "MATIC", }, rpc: ["https://rpc-amoy.polygon.technology/"], blockExplorers: [ { name: "Amoy Polygonscan", url: "https://amoy.polygonscan.com/", }, ], testnet: true, };
之后在初始化thirdweb Provider或SDK时传入该配置:
- 用React
ThirdwebProvider:
import { ThirdwebProvider } from "@thirdweb-dev/react"; function App() { return ( <ThirdwebProvider activeChain={amoyChain}> {/* 你的应用内容 */} </ThirdwebProvider> ); }
- 直接初始化SDK:
import { ThirdwebSDK } from "@thirdweb-dev/sdk"; const sdk = new ThirdwebSDK(amoyChain);
内容的提问来源于stack exchange,提问作者Potato
相关产品推荐
相关产品推荐

