// bsc-transfer.js const { Web3 } = require('web3'); const Tx = require('ethereumjs-tx').Transaction; // 对于旧版需要签名,web3 1.8+ 推荐用内置 require('dotenv').config(); // ==================== 配置区 ==================== // BSC 主网 RPC(可换成 Ankr、QuickNode 等更快的节点) const BSC_RPC_URL = 'https://bsc-dataseed.binance.org/'; // 或者更快的: https://bsc.getblock.io/your-api-key/mainnet/ const web3 = new Web3(BSC_RPC_URL); // 你的私钥(强烈建议使用 .env 文件存放) const PRIVATE_KEY = process.env.PRIVATE_KEY; const fromAddress = process.env.FROM_ADDRESS; // 你的钱包地址 // 接收地址 const toAddress = '0x接收地址xxxxxxxxxxxxxxxxxxxxxxxxxx'; // 转账金额(单位:BNB 或代币数量) const amountBNB = '0.01'; // 如果转 BNB,直接填多少 BNB // 如果是转 BEP-20 代币,取消下面注释并填写合约地址和精度 // const tokenAddress = '0x55d398326f99059fF775485246999027B3197955'; // 例子:USDT // const decimals = 18; // const amountToken = '10'; // 转 10 USDT // =============================================== async function transferBNB() { try { // 获取当前 nonce const nonce = await web3.eth.getTransactionCount(fromAddress, 'pending'); // 构造交易 const txObject = { nonce: web3.utils.toHex(nonce), to: toAddress, value: web3.utils.toHex(web3.utils.toWei(amountBNB, 'ether')), gasLimit: web3.utils.toHex(21000), gasPrice: web3.utils.toHex(web3.utils.toWei('5', 'gwei')), // 可根据实际情况调整 // chainId: 56 为 BSC 主网 chainId: 56 }; // 估算更准确的 gas(可选) const estimatedGas = await web3.eth.estimateGas(txObject); txObject.gasLimit = web3.utils.toHex(estimatedGas + 5000); // 稍微多一点防止 out of gas // 签名交易(web3 1.x 自带账户签名方式) const signedTx = await web3.eth.accounts.signTransaction(txObject, PRIVATE_KEY); console.log('正在广播交易...'); const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction); console.log('转账成功!'); console.log(`交易哈希: https://bscscan.com/tx/${receipt.transactionHash}`); console.log(`区块号: ${receipt.blockNumber}`); } catch (error) { console.error('转账失败:', error.message); } } // 如果要转 BEP-20 代币,使用下面这个函数 async function transferToken() { const tokenContract = new web3.eth.Contract([ { "constant": false, "inputs": [ { "name": "_to", "type": "address" }, { "name": "_value", "type": "uint256" } ], "name": "transfer", "outputs": [{ "name": "", "type": "bool" }], "type": "function" } ], tokenAddress); const amount = web3.utils.toBN(amountToken * Math.pow(10, decimals)); const data = tokenContract.methods.transfer(toAddress, amount).encodeABI(); const nonce = await web3.eth.getTransactionCount(fromAddress, 'pending'); const txObject = { nonce: web3.utils.toHex(nonce), to: tokenAddress, value: '0x00', gasLimit: web3.utils.toHex(100000), gasPrice: web3.utils.toHex(web3.utils.toWei('5', 'gwei')), data: data, chainId: 56 }; const estimatedGas = await web3.eth.estimateGas(txObject); txObject.gasLimit = web3.utils.toHex(estimatedGas + 10000); const signedTx = await web3.eth.accounts.signTransaction(txObject, PRIVATE_KEY); const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction); console.log('代币转账成功!'); console.log(`交易哈希: https://bscscan.com/tx/${receipt.transactionHash}`); } // 运行(根据需要选择下面其中一行取消注释) // transferBNB(); // 转原生 BNB // transferToken(); // 转 BEP-20 代币 // 默认示例:转 0.01 BNB transferBNB();