152 lines
4.1 KiB
JavaScript
152 lines
4.1 KiB
JavaScript
import { ethers } from 'ethers';
|
|
|
|
async function testConnection() {
|
|
try {
|
|
// 尝试不同的BSC RPC端点
|
|
const rpcUrls = [
|
|
'https://bsc-dataseed1.binance.org:443',
|
|
'https://bsc-dataseed.binance.org/',
|
|
'https://bsc-dataseed1.binance.org/',
|
|
'https://bsc-dataseed2.binance.org/',
|
|
'https://bsc-dataseed3.binance.org/',
|
|
'https://bsc-dataseed4.binance.org/',
|
|
'https://bsc.nodereal.io/',
|
|
'https://binance.nodereal.io/'
|
|
];
|
|
|
|
for (const url of rpcUrls) {
|
|
try {
|
|
this.networkStatus = `正在尝试连接: ${url}`;
|
|
const provider = new ethers.providers.JsonRpcProvider(url);
|
|
// 尝试获取区块号来验证连接
|
|
const blockNumber = await provider.getBlockNumber();
|
|
this.networkStatus = `已连接到BSC网络: ${url}, 当前区块: ${blockNumber}`;
|
|
return provider; // 返回成功连接的provider
|
|
} catch (e) {
|
|
console.log(`连接到 ${url} 失败:`, e.message);
|
|
}
|
|
}
|
|
|
|
throw new Error("无法连接到任何BSC节点");
|
|
} catch (error) {
|
|
console.error('网络连接测试失败:', error);
|
|
this.networkStatus = `连接失败: ${error.message}`;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// USDT转账函数
|
|
export const usdtTransfer = async (to_addr, coin, privateKey) => {
|
|
console.log('开始USDT转账流程...');
|
|
|
|
try {
|
|
console.log('获取eth实例...');
|
|
const eth = await getEth();
|
|
const utils = new Utils();
|
|
const accounts = new Accounts(eth.currentProvider);
|
|
|
|
const USDT_CONTRACT_ADDRESS = '0x55d398326f99059fF775485246999027B3197955';
|
|
const usdtContractABI = [{
|
|
"constant": false,
|
|
"inputs": [{
|
|
"name": "to",
|
|
"type": "address"
|
|
},
|
|
{
|
|
"name": "value",
|
|
"type": "uint256"
|
|
}
|
|
],
|
|
"name": "transfer",
|
|
"outputs": [{
|
|
"name": "",
|
|
"type": "bool"
|
|
}],
|
|
"payable": false,
|
|
"stateMutability": "nonpayable",
|
|
"type": "function"
|
|
}];
|
|
|
|
console.log('创建账户实例...');
|
|
const account = accounts.privateKeyToAccount(privateKey);
|
|
|
|
console.log('创建合约实例...');
|
|
const contract = new Contract(usdtContractABI, USDT_CONTRACT_ADDRESS);
|
|
contract.setProvider(eth.currentProvider);
|
|
|
|
console.log('转换转账金额...');
|
|
const amount = utils.toWei(coin.toString(), 'ether');
|
|
|
|
console.log('获取gas价格...');
|
|
const gasPrice = await eth.getGasPrice();
|
|
const gasPriceWithBuffer = utils.toBN(gasPrice).mul(utils.toBN(12)).div(utils.toBN(10)).toString();
|
|
|
|
console.log('准备发送交易...');
|
|
const data = contract.methods.transfer(to_addr, amount).encodeABI();
|
|
|
|
const txParams = {
|
|
from: account.address,
|
|
to: USDT_CONTRACT_ADDRESS,
|
|
data: data,
|
|
gas: 210000,
|
|
gasPrice: gasPriceWithBuffer
|
|
};
|
|
|
|
console.log('签名交易...');
|
|
const signedTx = await account.signTransaction(txParams);
|
|
|
|
console.log('发送交易...');
|
|
const sendPromise = eth.sendSignedTransaction(signedTx.rawTransaction);
|
|
|
|
// 设置60秒超时
|
|
const receipt = await Promise.race([
|
|
sendPromise,
|
|
timeoutPromise(60000)
|
|
]);
|
|
|
|
console.log('交易已确认');
|
|
return receipt;
|
|
} catch (error) {
|
|
console.error("USDT转账错误:", error);
|
|
throw new Error(error.message || '转账失败');
|
|
}
|
|
};
|
|
|
|
// BNB转账函数
|
|
export const BNBTransfer = async (to_addr, coin, userSiyao) => {
|
|
try {
|
|
const provider = await testConnection();
|
|
if (!provider) {
|
|
uni.showToast({
|
|
title: '网络连接失败',
|
|
icon: 'none'
|
|
});
|
|
return;
|
|
}
|
|
const wallet = new ethers.Wallet(userSiyao, provider);
|
|
const gasPrice = await provider.getGasPrice();
|
|
const amountWei = ethers.utils.parseEther(coin);
|
|
const tx = {
|
|
to: to_addr,
|
|
value: amountWei,
|
|
gasLimit: 21000, // 基本转账的gas限制
|
|
gasPrice: gasPrice,
|
|
nonce: await provider.getTransactionCount(wallet.address, 'latest')
|
|
};
|
|
const transaction = await wallet.sendTransaction(tx);
|
|
this.txHash = transaction.hash;
|
|
|
|
// 等待交易确认
|
|
const receipt = await transaction.wait();
|
|
// let zhifuResult = await BNBTransfer(_that.userMoneyAdress,_that.initAdress,_that.outMoney,userSiyao);
|
|
console.log(receipt, 'sasdasdasdasd');
|
|
uni.hideLoading()
|
|
uni.showToast({
|
|
title:"BNB打款成功",
|
|
icon:"success",
|
|
duration:1000
|
|
})
|
|
} catch (err) {
|
|
console.log(err, '转账失败');
|
|
}
|
|
}; |