You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
4.2 KiB
127 lines
4.2 KiB
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(fromAddress, privateKey, toAddress, amount, remark, contract) {
|
|
console.log(fromAddress, privateKey, 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))
|
|
transaction.transaction.data = remark
|
|
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(fromAddress, privateKey, toAddress, amount) {
|
|
console.log(amount)
|
|
const tradeobj = await tronWeb.transactionBuilder.sendTrx(toAddress, amount * Math.pow(10, 6),
|
|
fromAddress);
|
|
const signedtxn = await tronWeb.trx.sign(tradeobj, 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;
|
|
},
|
|
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<res.data.data.length;i++){
|
|
if(res.data.data[i].tokenAbbr==='USDT'){
|
|
list[1].balance=(Number(res.data.data[i].balance)/ Math.pow(10, res.data.data[i].tokenDecimal));
|
|
}else{
|
|
console.log(Number(res.data.data[i].balance)/Math.pow(10, res.data.data[i].tokenDecimal))
|
|
|
|
var coin={
|
|
name: res.data.data[i].tokenAbbr,
|
|
xname: res.data.data[i].tokenName,
|
|
balance: (Number(res.data.data[i].balance)/ Math.pow(10, res.data.data[i].tokenDecimal)),
|
|
icon: res.data.data[i].tokenLogo,
|
|
|
|
}
|
|
if(res.data.data[i].tokenType==='trc20'){
|
|
coin.contractAddress=res.data.data[i].tokenId
|
|
}else{
|
|
coin.contractAddress=res.data.data[i].owner_address
|
|
}
|
|
newList.push(coin)
|
|
|
|
}
|
|
|
|
|
|
}
|
|
console.log(list,12313)
|
|
success(newList);
|
|
// coinList: [{
|
|
// name: "ETH",
|
|
// xname: 'Ethereum',
|
|
// balance: 0,
|
|
// icon: require('@/static/tongyonh/Frame3299.png')
|
|
// },
|
|
|
|
}
|
|
});
|
|
}
|
|
|
|
}
|
|
export default tron
|
|
|