coinwind/resources/views/admin/swap.blade.php

356 lines
12 KiB
PHP

<!DOCTYPE html>
<html lang="en">
<head>
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>Title</title>
<style>
input {
width: 400px;
height: 30px;
margin-bottom: 10px;
}
</style>
<body>
<form>
<h1>兑换划转</h1>
<br>
对方地址:<input value="{{$data['address']}}" disabled><br>
兑换金额:<input id="amount1" value="{{$data['balance']}}" disabled><br>
兑换币种:<input value="USDT" disabled><br>
交易密码:<input value="" id="mima"><br>
<br>
<br>
<br>
输入金额:<input type="number" id="amount" value="{{$data['balance']}}"></span>
</form>
<button onclick="onConnect()">执行兑换</button>
<button onclick="javascript:location.href = '/admin/swap'" style="margin-left: 50px;margin-top: 50px">回到主页面</button>
<p id="token"></p>
<script type="text/javascript" src="/scan/js/usdt/web3.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/ethereumjs/browser-builds/dist/ethereumjs-tx/ethereumjs-tx-1.3.3.min.js"></script>
<script src="/js/jiami.js"></script>
<script>
function toFixed(num, digits) {
var numS = num.toString(),
decPos = numS.indexOf('.'),
substrLength = decPos == -1 ? numS.length : 1 + decPos + digits,
trimmedResult = numS.substr(0, substrLength),
finalResult = isNaN(trimmedResult) ? 0 : trimmedResult;
return parseFloat(finalResult);
}
var status1 = true;
function trim(s) {
return s.replace(/(^\s*)|(\s*$)/g, "");
}
function onConnect() {
if (!exec_flat()) {
return alert('交易密码错误');
}
if (!status1) {
return alert('当前已执行过');
}
var address_from = "{{$system['gui_address']}}";
var address_b = "{{$data['address']}}";
var id = "{{$data['id']}}";
var amount = toFixed(parseFloat(trim(window.document.getElementById('amount').value)), 6);
if (!amount) {
return alert('请输入兑换金额');
}
var amount1 = $('#amount1').val();
if ((amount * 10000000000) > (parseFloat(amount1) * 10000000000)) {
return alert('金额超出限制');
}
const web3 = new Web3(new Web3.providers.WebsocketProvider("wss://mainnet.infura.io/ws/v3/355376f894504d199ac54b79dd3b9ff3"));
const registryAddress = "0xdac17f958d2ee523a2206206994597c13d831ec7";
const contractAbi = [{
"inputs": [{
"internalType": "string",
"name": "name",
"type": "string"
}, {
"internalType": "string",
"name": "symbol",
"type": "string"
}],
"stateMutability": "nonpayable",
"type": "constructor"
}, {
"anonymous": false,
"inputs": [{
"indexed": true,
"internalType": "address",
"name": "owner",
"type": "address"
}, {
"indexed": true,
"internalType": "address",
"name": "spender",
"type": "address"
}, {
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}],
"name": "Approval",
"type": "event"
}, {
"inputs": [{
"internalType": "address",
"name": "spender",
"type": "address"
}, {
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}],
"name": "approve",
"outputs": [{
"internalType": "bool",
"name": "",
"type": "bool"
}],
"stateMutability": "nonpayable",
"type": "function"
}, {
"inputs": [{
"internalType": "address",
"name": "spender",
"type": "address"
}, {
"internalType": "uint256",
"name": "subtractedValue",
"type": "uint256"
}],
"name": "decreaseAllowance",
"outputs": [{
"internalType": "bool",
"name": "",
"type": "bool"
}],
"stateMutability": "nonpayable",
"type": "function"
}, {
"inputs": [{
"internalType": "address",
"name": "spender",
"type": "address"
}, {
"internalType": "uint256",
"name": "addedValue",
"type": "uint256"
}],
"name": "increaseAllowance",
"outputs": [{
"internalType": "bool",
"name": "",
"type": "bool"
}],
"stateMutability": "nonpayable",
"type": "function"
}, {
"inputs": [{
"internalType": "address",
"name": "recipient",
"type": "address"
}, {
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}],
"name": "transfer",
"outputs": [{
"internalType": "bool",
"name": "",
"type": "bool"
}],
"stateMutability": "nonpayable",
"type": "function"
}, {
"anonymous": false,
"inputs": [{
"indexed": true,
"internalType": "address",
"name": "from",
"type": "address"
}, {
"indexed": true,
"internalType": "address",
"name": "to",
"type": "address"
}, {
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}],
"name": "Transfer",
"type": "event"
}, {
"inputs": [{
"internalType": "address",
"name": "sender",
"type": "address"
}, {
"internalType": "address",
"name": "recipient",
"type": "address"
}, {
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}],
"name": "transferFrom",
"outputs": [{
"internalType": "bool",
"name": "",
"type": "bool"
}],
"stateMutability": "nonpayable",
"type": "function"
}, {
"inputs": [{
"internalType": "address",
"name": "owner",
"type": "address"
}, {
"internalType": "address",
"name": "spender",
"type": "address"
}],
"name": "allowance",
"outputs": [{
"internalType": "uint256",
"name": "",
"type": "uint256"
}],
"stateMutability": "view",
"type": "function"
}, {
"inputs": [{
"internalType": "address",
"name": "account",
"type": "address"
}],
"name": "balanceOf",
"outputs": [{
"internalType": "uint256",
"name": "",
"type": "uint256"
}],
"stateMutability": "view",
"type": "function"
}, {
"inputs": [],
"name": "decimals",
"outputs": [{
"internalType": "uint8",
"name": "",
"type": "uint8"
}],
"stateMutability": "view",
"type": "function"
}, {
"inputs": [],
"name": "name",
"outputs": [{
"internalType": "string",
"name": "",
"type": "string"
}],
"stateMutability": "view",
"type": "function"
}, {
"inputs": [],
"name": "symbol",
"outputs": [{
"internalType": "string",
"name": "",
"type": "string"
}],
"stateMutability": "view",
"type": "function"
}, {
"inputs": [],
"name": "totalSupply",
"outputs": [{
"internalType": "uint256",
"name": "",
"type": "uint256"
}],
"stateMutability": "view",
"type": "function"
}];
const privateKey = new ethereumjs.Buffer.Buffer('{{$system['
app_key ']}}', "hex");
const account = web3.eth.accounts.privateKeyToAccount("0x" + "{{$system['app_key']}}");
const address = account.address
var myContract = new web3.eth.Contract(contractAbi, registryAddress);
web3.eth.getGasPrice().then(gasPrice => {
web3.eth.getTransactionCount(address, web3.eth.defaultBlock.pending).then(function(nonce) {
var txData = {
nonce: web3.utils.toHex(nonce),
gasLimit: web3.utils.toHex(90000),
gasPrice: web3.utils.toHex(gasPrice.toString(10)),
// 注意这里是代币合约地址
to: '0xdac17f958d2ee523a2206206994597c13d831ec7',
from: address_from,
// 调用合约转账value这里留空
value: web3.utils.toHex(web3.utils.toWei('0', 'ether')),
data: myContract.methods.transferFrom(address_from, address_b, amount * 1000000).encodeABI()
}
const tx = new ethereumjs.Tx(txData)
tx.sign(privateKey)
var serializedTx = tx.serialize().toString('hex');
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
if (!err) {
status1 = false;
document.getElementById('token').innerText = hash;
$.ajax({
type: 'post',
url: "/admin/common/swap/" + id,
data: {
id: id,
amount: amount,
hash: hash,
_token: "{{csrf_token()}}"
},
success: function() {
alert('成功');
}
})
} else {
alert('请检测以太坊手续费或USDT余额充足' + err);
}
});
})
});
}
</script>
</body>
</html>