const TronWeb = require('tronweb') const bip39 = require('bip39'); const bip32 = require('bip32'); const util = require('ethereumjs-util') const ethers = require('ethers') const bitcoin = require('bitcoinjs-lib') let Tx = require('ethereumjs-tx'); const HttpProvider = TronWeb.providers.HttpProvider; const fullNode = new HttpProvider("https://api.trongrid.io"); const solidityNode = new HttpProvider("https://api.trongrid.io"); const eventServer = new HttpProvider("https://api.trongrid.io"); const privateKey = "2d39fb63e81f0cb999b9f0271f5e200d1187c31ac040939218ca054fe5d37b41"; const tronWeb = new TronWeb(fullNode, solidityNode, eventServer, privateKey); let tron = { //查询TRX余额 getTronBalance: async function(address) { console.log(fullNode) console.log(solidityNode) console.log(eventServer) console.log(address) let balance = await tronWeb.trx.getBalance(address); console.log('查询trx余额',balance); console.log(Number(tronWeb.fromSun(balance).toString())); return Number(tronWeb.fromSun(balance).toString()); }, //查询TRC/USDT余额 getTronTokenBalance: async function(address, contract) { const contract_address = await tronWeb.address.fromHex(contract); console.log('查询trc20余额'); contract = await tronWeb.contract().at(contract_address); let symbol = await contract.name().call(); let decimals = await contract.decimals.call(); let totalSupply = contract.totalSupply().call(); let balance = await contract.balanceOf(address).call(); console.log('代币trc余额' + tronWeb.fromSun(balance)) return balance / Math.pow(10, 6); }, // 发起TRC/USDT交易 sendRawTransaction: async function(transaction, privateKey){ let signedTx = await tronWeb.trx.sign(transaction.transaction, privateKey) await tronWeb.trx.sendRawTransaction(signedTx); console.log(signedTx.txID); return signedTx.txID; }, // 发起TRX交易 sendTransaction: async function(transaction, privateKey) { const signedtxn = await tronWeb.trx.sign(transaction, privateKey); const receipt = await tronWeb.trx.sendRawTransaction(signedtxn); // console.log(tradeobj) // console.log(signedtxn) // console.log(receipt) // console.log(receipt.txid) if (receipt.txid !== undefined) { console.log(receipt.txid) return receipt.txid } return null; }, transaction:async function(transaction, privateKey, contract){ if(contract){ return await this.sendRawTransaction(transaction, privateKey); }else{ return await this.sendTransaction(transaction, privateKey); } }, getTransaction:async function(fromAddress, toAddress, amount, remark, contract){ if(contract){ return await this.prepareRawTransaction(fromAddress, toAddress, amount, remark, contract); }else{ return await this.prepareTransaction(fromAddress, toAddress, amount); } }, prepareRawTransaction: async function(fromAddress, toAddress, amount, remark, contract) { console.log(fromAddress, toAddress, amount, remark, contract) const parameter = [{ type: 'address', value: toAddress }, { type: 'uint256', value: amount * Math.pow(10, 6) }] const transaction = await tronWeb.transactionBuilder.triggerSmartContract(contract, "transfer(address,uint256)", {}, parameter, tronWeb.address.toHex(fromAddress)) console.log(remark,'备注') return transaction; }, // 发起TRX交易 prepareTransaction: async function(fromAddress, toAddress, amount) { console.log(amount) const transaction = await tronWeb.transactionBuilder.sendTrx(toAddress, amount * Math.pow(10, 6), fromAddress); return transaction; }, getContract:function(address,list,success){ uni.request({ url: 'https://apiasia.tronscan.io:5566/api/account/tokens?limit=100&address=' + address, //请求接口 header: { 'content-type': 'application/x-www-form-urlencoded', //自定义请求头信息 }, success: (res) => { var newList=[]; newList.push(list[0]) newList.push(list[1]) for(let i=1;i