使用xrpl.js发送XRP至钱包时报Faucet URL未定义错误如何解决
问题原因
你将节点切换到XRP主网后,代码仍保留了仅测试网可用的client.fundWallet()调用:
"Uncaught (in promise) Error: Faucet URL is not defined or inferrable."
这个方法的作用是从测试网水龙头领取测试XRP、生成测试钱包,主网不存在公共水龙头,xrpl库无法自动匹配主网水龙头地址,因此触发报错。同时你的代码存在变量覆盖问题:顶部已经用私钥导入了正式钱包,fundWallet()返回的结果会把你导入的正式钱包覆盖,导致后续交易走测试钱包逻辑。
修复步骤
- 删掉以下两行测试网相关代码:
console.log("Getting a wallet from the Testnet faucet...") const {wallet, balance} = await client.fundWallet()
- 确认后续交易构造时,
Account字段使用的是你顶部导入的正式钱包地址,不需要额外获取测试钱包。 - 检查钱包余额:主网XRP账户需保留至少10XRP的激活最低余额,转账前需确认你的钱包余额≥【转账金额35XRP + 交易手续费≈0.00001XRP + 10XRP保留金】,否则交易会失败。
- 若目标地址未激活(余额不足10XRP),到账金额会自动扣除10XRP作为目标地址的激活保留金,实际到账25XRP。
修正后的核心代码示例
// Dependencies for Node.js. // In browsers, use a <script> tag instead. if (typeof module !== "undefined") { var xrpl = require('xrpl') } // 用你自己的私钥导入正式钱包 const wallet = xrpl.Wallet.fromSeed("替换为你的钱包私钥") console.log(wallet.address) async function main() { console.log("Connecting to XRP Mainnet...") const client = new xrpl.Client('wss://xrplcluster.com') await client.connect() // 直接构造交易,不需要调用fundWallet const prepared = await client.autofill({ "TransactionType": "Payment", "Account": wallet.address, // 用你导入的钱包地址 "Amount": xrpl.xrpToDrops("35"), "Destination": "替换为目标XRP地址" }) const max_ledger = prepared.LastLedgerSequence console.log("Prepared transaction instructions:", prepared) console.log("Transaction cost:", xrpl.dropsToXrp(prepared.Fee), "XRP") console.log("Transaction expires after ledger:", max_ledger) const signed = wallet.sign(prepared) console.log("Identifying hash:", signed.hash) console.log("Signed blob:", signed.tx_blob) const tx = await client.submitAndWait(signed.tx_blob) console.log("Transaction result:", tx.result.meta.TransactionResult) console.log("Balance changes:", JSON.stringify(xrpl.getBalanceChanges(tx.result.meta), null, 2)) client.disconnect() } main()
内容的提问来源于stack exchange,提问作者StillLearning
相关产品推荐
相关产品推荐

