Onlife/static/web3/transfer-web.html
2025-04-19 15:38:48 +08:00

235 lines
6.3 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Transfer Web</title>
<script src="./web3.min.js"></script>
</head>
<body>
<script type="text/javascript" src="./uni.webview.js"></script>
<script type="text/javascript" src="./axios.min.js"></script>
<script>
// 初始化Web3
let web3 = new Web3(new Web3.providers.HttpProvider('https://bsc-dataseed1.binance.org:443'));
//公共转账abi
const otherContractABI = [{
"constant": false,
"inputs": [{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [{
"name": "",
"type": "bool"
}],
"type": "function"
}];
//查询当前GAS费
async function searchGasFee(){
let nowGas = await web3.eth.getGasPrice();
let gasInHex = web3.utils.toHex(nowGas);
let nowGwei = web3.utils.fromWei(nowGas, 'gwei');
let gasInBNB = web3.utils.fromWei(gasInHex, 'ether');
console.log(nowGwei,'当前GAS费(GWEI)');
console.log(gasInBNB, '当前GAS费(bnb)');
sendMessage({
status: "success",
type: "gasFee",
nowGwei,
gasInBNB
})
}
//查询USDT交易历史记录
async function getTransactions(address) {
const USDT_CONTRACT_ADDRESS = "0x55d398326f99059fF775485246999027B3197955";
const BSCSCAN_API_KEY = "1UQ3PHID4UJUDTVD4EJK35PZB8XDKS7K9T";
try {
// 使用 BSCScan API 获取交易记录
const response = await axios.get('https://api.bscscan.com/api', {
params: {
module: 'account',
action: 'tokentx',
contractaddress: USDT_CONTRACT_ADDRESS,
address: address,
apikey: BSCSCAN_API_KEY,
sort: 'desc' // 按时间降序排序
}
});
// 处理 API 响应
if (response.data.status === '1') {
const transactions = response.data.result;
sendMessage({
status: "success",
type: "transactions",
transactions
})
} else {
sendMessage({
status: "error",
type: "transactions"
})
}
} catch (error) {
sendMessage({
status: "error",
type: "transactions"
})
}
}
//BNB转账
async function bnbTransfer(params) {
const {
type,
fromAdress,
toAdress,
coin,
privateKey
} = params;
try {
const amount = web3.utils.toWei(coin + '', 'ether');
const gasPrice = await web3.eth.getGasPrice();
const transactionObject = {
from: fromAdress,
to: toAdress,
value: amount,
gas: 210000,
gasPrice: gasPrice
};
const signedTx = await web3.eth.accounts.signTransaction(transactionObject, privateKey);
const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction)
.on('transactionHash', hash => {
console.log(hash,'hash');
sendMessage({
status: "success",
hash: hash,
type,
time:Date.now()
})
})
} catch (error) {
console.log(error,'错误');
sendMessage({
status: "error",
type
})
}
}
//USDT转账
async function usdtTransfer(params) {
const {
type,
fromAdress,
toAdress,
coin,
privateKey
} = params;
try {
const USDT_CONTRACT_ADDRESS = '0x55d398326f99059fF775485246999027B3197955';
const usdtContractABI = [{
"constant": false,
"inputs": [{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [{
"name": "",
"type": "bool"
}],
"type": "function"
}];
const usdtContract = new web3.eth.Contract(usdtContractABI, USDT_CONTRACT_ADDRESS);
const amountToSend = Number(coin) * 10 ** 18;
const txCount = await web3.eth.getTransactionCount(fromAdress);
const txObject = {
from: fromAdress,
to: USDT_CONTRACT_ADDRESS,
gasPrice: web3.utils.toHex(await web3.eth.getGasPrice()),
gasLimit: web3.utils.toHex(210000),
value: '0',
data: usdtContract.methods.transfer(toAdress, amountToSend).encodeABI(),
nonce: web3.utils.toHex(txCount)
};
const signedTx = await web3.eth.accounts.signTransaction(txObject, privateKey);
let receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction)
.on('transactionHash', hash => {
console.error('交易成功:', hash);
sendMessage({
status: "success",
hash: hash,
type
})
})
} catch (error) {
console.log(error, '转账失败');
sendMessage({
status: "error",
type
})
}
}
//其他转账ETH、BTBC等
async function otherTransfer(params){
const {
type,//代码类型
fromAdress,//发送地址
toAdress,//接收地址
coin,//发送数量
privateKey,//私钥
otherContractAddress//合约地址
} = params;
try {
const publicContract = new web3.eth.Contract(otherContractABI,otherContractAddress);
const amountToSend = web3.utils.toWei(coin + '', 'ether');
const txCount = await web3.eth.getTransactionCount(fromAdress);
const txObject = {
from: fromAdress,
to: otherContractAddress,
gasPrice: web3.utils.toHex(await web3.eth.getGasPrice()),
gasLimit: web3.utils.toHex(210000),
value: '0',
data: publicContract.methods.transfer(toAdress, amountToSend).encodeABI(),
nonce: web3.utils.toHex(txCount)
};
const signedTx = await web3.eth.accounts.signTransaction(txObject, privateKey);
let receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
if (receipt.transactionHash) {
console.log('交易成功:', receipt.transactionHash);
sendMessage({
status: "success",
hash: receipt.transactionHash,
type,
time:Date.now()
})
}
} catch (error) {
console.log(error, '转账失败');
sendMessage({
status: "error",
type
})
}
}
function sendMessage(message) {
uni.postMessage({
data: message
});
}
</script>
</body>
</html>