You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BSC链Aptos Bridge跨链交易Node.js脚本报错:LzApp原生代币不足

解决BSC通过Aptos Bridge跨链交易时"LzApp: Not enough native token"错误

问题根源分析

  • 核心错误:手续费参数传递错误
    你代码中手动计算了totalFee,但交易的value字段却误用了不存在的fees.totalFee(estimateFees返回的结构仅包含nativeFee和zroFee),导致实际支付的手续费为0,触发手续费不足的回滚报错。
  • 次要问题:Nonce获取逻辑不当
    批准交易完成后,直接调用getTransactionCount获取nonce可能拿到旧值(链上交易未确认时),应该用批准交易收据中的nonce + 1确保nonce正确递增,避免重复nonce导致交易失败。

修复后的代码

web3.eth.getTransactionCount(fromAddress).then((nonce) => {
    const approveTxData = tokenContract.methods.approve(tokenBridgeAddress, amountLD).encodeABI();
    const approveTransaction = {
        to: usddTokenAddress,
        data: approveTxData,
        gas: 100000,
        nonce: nonce,
    };

    web3.eth.accounts.signTransaction(approveTransaction, privateKey).then((signedApproveTx) => {
        web3.eth.sendSignedTransaction(signedApproveTx.rawTransaction).on('receipt', (approveReceipt) => {
            console.log('Approve receipt:', approveReceipt);

            const callParams = {
                refundAddress: fromAddress,
                zroPaymentAddress: zroPaymentAddress
            };

            const adapterParams = ethers.utils.solidityPack(
                ['uint16', 'uint256'],
                [1, 300000] // 可根据实际情况调整目标链gas limit
            );

            console.log('AdapterParams:', adapterParams);

            const dstChainId = 108;
            const userApplication = fromAddress;
            const payload = '0x'
            const payInZRO = false;

            endpointContract.methods.estimateFees(dstChainId, userApplication, payload, payInZRO, adapterParams).call()
                .then(fees => {
                    console.log('Fees (Native):', fees.nativeFee, 'wei');
                    console.log('Fees (ZRO):', fees.zroFee, 'wei');
                    // payInZRO为false时仅需支付nativeFee,此处计算总和兼容zroFee非0场景
                    const totalFee = BigInt(fees.nativeFee) + BigInt(fees.zroFee);
                    console.log('Total fees:', totalFee.toString(), 'wei');

                    // 使用批准交易的nonce+1,避免链上未确认时获取旧nonce
                    const nextNonce = approveReceipt.nonce + 1;
                    const txData = tokenBridgeContract.methods.sendToAptos(usddTokenAddress, toAddress, amountLD, callParams, adapterParams).encodeABI();
                    const transaction = {
                        to: tokenBridgeAddress,
                        data: txData,
                        gas: 300000,
                        nonce: nextNonce,
                        value: totalFee.toString() // 传递计算后的有效手续费
                    };

                    web3.eth.accounts.signTransaction(transaction, privateKey).then((signedTx) => {
                        web3.eth.sendSignedTransaction(signedTx.rawTransaction).on('receipt', console.log).on('error', console.error);
                    }).catch(console.error);
                })
                .catch(console.error);
        }).on('error', console.error);
    });
});

额外注意事项

  • 确保发起交易的地址持有足够的BSC原生代币(BNB),不仅要覆盖计算出的totalFee,还要留足交易本身的gas费用(可适当调高gas参数值,比如设置为400000)。
  • 可根据实际跨链情况调整adapterParams中的目标链gas limit,若后续仍有报错,尝试将300000调高至500000。
  • 确认dstChainId配置正确,Aptos的LayerZero链ID确实为108。

内容的提问来源于stack exchange,提问作者Tim Reznik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 11:47:43