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.
107 lines
3.8 KiB
107 lines
3.8 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(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&&contract!='_') {
|
|
return await this.sendRawTransaction(transaction, privateKey);
|
|
} else {
|
|
return await this.sendTransaction(transaction, privateKey);
|
|
}
|
|
},
|
|
getTransaction: async function(fromAddress, toAddress, amount, remark, contract) {
|
|
if (contract&&contract!='_') {
|
|
console.log(contract,'进来')
|
|
return await this.prepareRawTransaction(fromAddress, toAddress, amount, remark, contract);
|
|
} else {
|
|
console.log(contract,'进来12')
|
|
return await this.prepareTransaction(fromAddress, toAddress, amount,remark);
|
|
}
|
|
},
|
|
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(transaction.transaction, '备注')
|
|
await tronWeb.transactionBuilder.addUpdateData(transaction.transaction, remark, 'utf8');
|
|
console.log(transaction.transaction, '备注')
|
|
return transaction;
|
|
},
|
|
// 发起TRX交易
|
|
prepareTransaction: async function(fromAddress, toAddress, amount, remark) {
|
|
console.log(amount)
|
|
const transaction = await tronWeb.transactionBuilder.sendTrx(toAddress, amount * Math.pow(10, 6),
|
|
fromAddress);
|
|
return transaction;
|
|
}
|
|
|
|
|
|
}
|
|
export default tron
|
|
|