bitkeep-tron.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * bitkeep钱包的tron
  3. * @type {{}}
  4. */
  5. import tools from "@/common/js/tools";
  6. let bitkeepTron = {}
  7. let contractArr=[
  8. 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'
  9. ];
  10. /**
  11. * 钱包初始校验
  12. * @returns {*}
  13. */
  14. bitkeepTron.isInstall=function (){
  15. return window.tronLink && window.tronWeb;
  16. }
  17. /**
  18. * 获取我登陆的地址
  19. * @returns {any}
  20. */
  21. bitkeepTron.getMyAddress=function () {
  22. return uni.getStorageSync('myAddress')
  23. }
  24. /**
  25. * 获取基本账户地址
  26. * @returns {Promise<unknown>}
  27. */
  28. bitkeepTron.getAccounts=async function (){
  29. return new Promise(async (resolve, reject) => {
  30. try {
  31. await window.tronLink.request({method: "tron_requestAccounts"});
  32. let selectedAddress = tronWeb.defaultAddress.base58;
  33. uni.setStorageSync('myAddress',selectedAddress)
  34. resolve(selectedAddress)
  35. }catch (e) {
  36. reject(e)
  37. }
  38. })
  39. }
  40. /**
  41. * 获取我的余额
  42. * @param selectedAddress
  43. * @returns {Promise<unknown>}
  44. */
  45. bitkeepTron.getBalance=async function (selectedAddress) {
  46. if(!selectedAddress){
  47. selectedAddress=bitkeepTron.getMyAddress();
  48. }
  49. let balance = await tronWeb.trx.getBalance(selectedAddress);
  50. balance=balance/1000000;
  51. return balance
  52. }
  53. /**
  54. * 获取合约数量
  55. * @param selectedAddress
  56. * @param contractType
  57. * @returns {Promise<number>}
  58. */
  59. bitkeepTron.getTokenBalance=async function (selectedAddress,contractType) {
  60. if(!selectedAddress){
  61. selectedAddress=bitkeepTron.getMyAddress();
  62. }
  63. let tokenBalance = await tronWeb.transactionBuilder.triggerConstantContract(
  64. contractArr[contractType],
  65. "balanceOf(address)",
  66. {},
  67. [{ type: 'address', value: selectedAddress }],
  68. tronWeb.defaultAddress.base58,
  69. );
  70. let balance = tokenBalance.constant_result[0];
  71. balance= parseInt(balance, 16);
  72. balance=balance/1000000;
  73. return balance;
  74. }
  75. bitkeepTron.getContract=async function (contractType) {
  76. let contractData = await tronWeb.trx.getContract(contractArr[contractType])
  77. if(contractData.abi){
  78. return contractData.abi
  79. }else {
  80. return false
  81. }
  82. }
  83. /**
  84. * 合约转账
  85. * @param to
  86. * @param money
  87. * @param contractType
  88. */
  89. bitkeepTron.getContractData=function (to,money,contractType){
  90. let data={};
  91. data.contract=contractArr[contractType];
  92. data.to=to;
  93. data.from=bitkeepTron.getMyAddress();
  94. if(tools.isDevelopment()){
  95. money=0.001;
  96. }
  97. data.value=money;
  98. return data;
  99. }
  100. bitkeepTron.getTransactionData=function (to,money){
  101. let data={};
  102. data.to=to;
  103. data.from=bitkeepTron.getMyAddress();
  104. if(tools.isDevelopment()){
  105. // money=0.001;
  106. }
  107. data.value=money;
  108. return data;
  109. }
  110. /**
  111. * trc转账
  112. * @param data
  113. * @returns {Promise<unknown>}
  114. */
  115. bitkeepTron.sendTransaction=function (data){
  116. return new Promise(async (resolve, reject) => {
  117. try {
  118. let broastTx;
  119. if (data.contract) {
  120. const parameter = [{ type: 'address', value: data.to * Math.pow(10, 18) }];
  121. let tx = await tronWeb.transactionBuilder.triggerSmartContract(
  122. data.contract,
  123. "registrationExt(address)",
  124. {},
  125. parameter,
  126. data.from
  127. );
  128. let signedTx = await tronWeb.trx.sign(tx.transaction);
  129. broastTx = await tronWeb.trx.sendRawTransaction(signedTx);
  130. } else {
  131. console.log('transactionBuilder:')
  132. console.log(data.to, data.value * Math.pow(10, 6), data.from)
  133. let tx = await tronWeb.transactionBuilder.sendTrx(data.to, data.value * Math.pow(10, 6), data.from);
  134. // sign 签名
  135. let signedTx = await tronWeb.trx.sign(tx);
  136. // broadcast 广播
  137. broastTx = await tronWeb.trx.sendRawTransaction(signedTx);
  138. }
  139. if(broastTx.txid){
  140. resolve(broastTx.txid)
  141. }else {
  142. reject(false)
  143. }
  144. }catch (e) {
  145. reject(false)
  146. }
  147. })
  148. }
  149. export default bitkeepTron