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.
77 lines
2.7 KiB
77 lines
2.7 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(address)
|
|
console.log('查询trx余额');
|
|
let balance = await tronWeb.trx.getBalance(address);
|
|
console.log(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) {
|
|
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;
|
|
}
|
|
|
|
}
|
|
export default tron
|
|
|