tokenpocket-bnb.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * bitkeep钱包的tron
  3. * @type {{}}
  4. */
  5. import tools from "@/common/js/tools";
  6. import bitkeepTron from "@/common/wallet/bitkeep-wallet/bitkeep-tron";
  7. import {ethers} from "ethers";
  8. let tokenpocketBnb = {}
  9. let contractArr=[
  10. 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'
  11. ];
  12. const chainId='0x38'
  13. /**
  14. * 钱包初始校验
  15. * @returns {*}
  16. */
  17. tokenpocketBnb.isInstall=function (){
  18. console.log('---------------------------')
  19. console.log(window.ethereum)
  20. if(!window.ethereum){
  21. return false
  22. }
  23. console.log(window.ethereum.isTokenPocket)
  24. return typeof window.ethereum.isTokenPocket !== 'undefined';
  25. }
  26. /**
  27. * 获取我登陆的地址
  28. * @returns {any}
  29. */
  30. tokenpocketBnb.getMyAddress=function () {
  31. return uni.getStorageSync('myAddress')
  32. }
  33. /**
  34. * 获取基本账户地址
  35. * @returns {Promise<unknown>}
  36. */
  37. tokenpocketBnb.getAccounts=async function (){
  38. return new Promise( (resolve, reject) => {
  39. try {
  40. window.ethereum.request({ method: 'eth_requestAccounts'})
  41. .then((address)=>{
  42. console.log('address:',address)
  43. if(address.length>0){
  44. uni.setStorageSync('myAddress',address[0])
  45. resolve(address[0])
  46. }else {
  47. resolve(false)
  48. }
  49. })
  50. .catch((e)=>{
  51. console.log(e)
  52. })
  53. }catch (e) {
  54. reject(e)
  55. }
  56. })
  57. }
  58. /**
  59. * 获取我的余额
  60. * @param selectedAddress
  61. * @returns {Promise<unknown>}
  62. */
  63. tokenpocketBnb.getBalance=async function (selectedAddress) {
  64. if(!selectedAddress){
  65. selectedAddress=bitkeepTron.getMyAddress();
  66. }
  67. return new Promise( (resolve, reject) => {
  68. try {
  69. window.ethereum.request({ method: 'eth_getBalance',params:[selectedAddress]})
  70. .then((balance)=>{
  71. balance= ethers.utils.formatEther(balance.toString())
  72. resolve(balance)
  73. })
  74. .catch((e)=>{
  75. resolve(0)
  76. })
  77. }catch (e) {
  78. reject(e)
  79. }
  80. })
  81. }
  82. /**
  83. * 获取合约数量
  84. * @param selectedAddress
  85. * @param contractType
  86. * @returns {Promise<number>}
  87. */
  88. tokenpocketBnb.getTokenBalance=async function (selectedAddress,contractType) {
  89. if(!selectedAddress){
  90. selectedAddress=tokenpocketBnb.getMyAddress();
  91. }
  92. let tokenBalance = await tronWeb.transactionBuilder.triggerConstantContract(
  93. contractArr[contractType],
  94. "balanceOf(address)",
  95. {},
  96. [{ type: 'address', value: selectedAddress }],
  97. tronWeb.defaultAddress.base58,
  98. );
  99. let balance = tokenBalance.constant_result[0];
  100. balance= parseInt(balance, 16);
  101. balance=balance/1000000;
  102. return balance;
  103. }
  104. /**
  105. * 获取合约信息
  106. * @param contractType
  107. * @returns {Promise<boolean|*>}
  108. */
  109. tokenpocketBnb.getContract=async function (contractType) {
  110. let contractData = await tronWeb.trx.getContract(contractArr[contractType])
  111. if(contractData.abi){
  112. return contractData.abi
  113. }else {
  114. return false
  115. }
  116. }
  117. /**
  118. * 合约转账
  119. * @param to
  120. * @param money
  121. * @param contractType
  122. */
  123. tokenpocketBnb.getContractData=function (to,money,contractType){
  124. let data={};
  125. data.contract=contractArr[contractType];
  126. data.to=to;
  127. data.from=tokenpocketBnb.getMyAddress();
  128. if(tools.isDevelopment()){
  129. money=0.001;
  130. }
  131. data.value=money;
  132. return data;
  133. }
  134. /**
  135. * 获取gasPrice
  136. * @returns {Promise<unknown>}
  137. */
  138. tokenpocketBnb.getGasPrice= async function (){
  139. let gasPrice =await window.ethereum.request({ method: 'eth_gasPrice',params:[]})
  140. if(!gasPrice){
  141. gasPrice='0x0'
  142. }
  143. return gasPrice;
  144. }
  145. /**
  146. * 获取燃料数量
  147. * @param data
  148. * @returns {Promise<unknown>}
  149. */
  150. tokenpocketBnb.getEstimateGas= async function (data){
  151. let estimateGas =await window.ethereum.request({ method: 'eth_estimateGas',params:[data]})
  152. console.log('estimateGas:'+estimateGas)
  153. if(!estimateGas){
  154. estimateGas=100000
  155. }
  156. return estimateGas;
  157. }
  158. /**
  159. * 转账接口
  160. * @param to
  161. * @param money
  162. * @returns {{}}
  163. */
  164. tokenpocketBnb.getTransactionData=async function (to,money){
  165. let data={};
  166. data.data='0x0';
  167. data.to=to;
  168. data.from=tokenpocketBnb.getMyAddress();
  169. data.gasPrice=await tokenpocketBnb.getGasPrice();
  170. data.gasLimit=await tokenpocketBnb.getEstimateGas();
  171. data.chainId=chainId;
  172. if(tools.isDevelopment()){
  173. money=0.001;
  174. }
  175. data.value=ethers.utils.parseEther(money.toString()).toString();
  176. console.log('value:'+data.value)
  177. return data;
  178. }
  179. /**
  180. * 转账
  181. * @param data
  182. * @returns {Promise<unknown>}
  183. */
  184. tokenpocketBnb.sendTransaction=function (data){
  185. return new Promise(async (resolve, reject) => {
  186. try {
  187. let txHash = await window.ethereum.request({
  188. method: 'eth_sendTransaction',
  189. params: [data],
  190. });
  191. console.log('txHash:'+txHash)
  192. if(txHash){
  193. resolve(txHash)
  194. }else {
  195. reject(false)
  196. }
  197. }catch (e) {
  198. reject(false)
  199. }
  200. })
  201. }
  202. export default tokenpocketBnb