tokenpocket-bnb.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * bitkeep钱包的tron
  3. * @type {{}}
  4. */
  5. import tools from "@/common/js/tools";
  6. import {ethers} from "ethers";
  7. import web3 from "web3";
  8. let tokenpocketBnb = {}
  9. let chainId=''
  10. let provider;
  11. /**
  12. * 钱包初始校验
  13. * @returns {*}
  14. */
  15. tokenpocketBnb.getProvider= function (){
  16. console.log(window.ethereum)
  17. console.log(window.ethereum.isTokenPocket)
  18. if (!window.ethereum || typeof window.ethereum.isTokenPocket === 'undefined') {
  19. return false
  20. }
  21. if(!provider){
  22. provider = window.ethereum;
  23. }
  24. // alert(provider.isConnected())
  25. console.log(chainId)
  26. if(!chainId){
  27. provider.request({method:'eth_chainId'}).then((ethChainId)=>{
  28. console.log(ethChainId)
  29. if(ethChainId!=='0x38'){
  30. uni.$emit('noBan',false)
  31. }else {
  32. chainId=ethChainId
  33. }
  34. })
  35. }
  36. return provider;
  37. }
  38. /**
  39. * 获取我登陆的地址
  40. * @returns {any}
  41. */
  42. tokenpocketBnb.getMyAddress=function () {
  43. return uni.getStorageSync('babAddress')
  44. }
  45. /**
  46. * 获取基本账户地址
  47. * @returns {Promise<unknown>}
  48. */
  49. tokenpocketBnb.getAccounts=async function (){
  50. return new Promise( (resolve, reject) => {
  51. try {
  52. tokenpocketBnb.getProvider().request({ method: 'eth_requestAccounts'})
  53. .then((address)=>{
  54. if(address.length>0){
  55. uni.setStorageSync('babAddress',address[0])
  56. resolve(address[0])
  57. }else {
  58. resolve(false)
  59. }
  60. })
  61. .catch((e)=>{
  62. console.log(e)
  63. })
  64. }catch (e) {
  65. reject(e)
  66. }
  67. })
  68. }
  69. /**
  70. * 获取我的余额
  71. * @param selectedAddress
  72. * @returns {Promise<unknown>}
  73. */
  74. tokenpocketBnb.getBalance=async function (selectedAddress) {
  75. if(!selectedAddress){
  76. selectedAddress=tokenpocketBnb.getMyAddress();
  77. }
  78. return new Promise( (resolve, reject) => {
  79. try {
  80. tokenpocketBnb.getProvider().request({ method: 'eth_getBalance',params:[selectedAddress,'latest']})
  81. .then((balance)=>{
  82. balance= ethers.utils.formatEther(balance.toString())
  83. resolve(balance)
  84. })
  85. .catch((e)=>{
  86. resolve(0)
  87. })
  88. }catch (e) {
  89. reject(e)
  90. }
  91. })
  92. }
  93. /**
  94. * 获取gasPrice
  95. * @returns {Promise<unknown>}
  96. */
  97. tokenpocketBnb.getGasPrice= async function (){
  98. let gasPrice =await tokenpocketBnb.getProvider().request({ method: 'eth_gasPrice',params:[]})
  99. // gasPrice=web3.utils.hexToNumber(gasPrice)
  100. console.log('gasPrice:'+gasPrice)
  101. if(!gasPrice){
  102. gasPrice='0x12a05f200'
  103. }
  104. return gasPrice;
  105. }
  106. /**
  107. * 获取燃料数量
  108. * @param data
  109. * @returns {Promise<unknown>}
  110. */
  111. tokenpocketBnb.getEstimateGas= async function (data){
  112. let estimateGas =await tokenpocketBnb.getProvider().request({ method: 'eth_estimateGas',params:[data]})
  113. // estimateGas=web3.utils.hexToNumber(estimateGas)
  114. if(!estimateGas){
  115. estimateGas=100000
  116. }
  117. console.log('estimateGas:',estimateGas)
  118. return estimateGas;
  119. }
  120. /**
  121. * 转账接口
  122. * @param to
  123. * @param money
  124. * @returns {{}}
  125. */
  126. tokenpocketBnb.getTransactionData=async function (to,money){
  127. let data={};
  128. data.data='0x0';
  129. data.to=to;
  130. data.from=tokenpocketBnb.getMyAddress();
  131. data.gasPrice=await tokenpocketBnb.getGasPrice();
  132. data.gas=await tokenpocketBnb.getEstimateGas();
  133. data.chainId=chainId;
  134. if(tools.isDevelopment()){
  135. money=0.00001;
  136. }
  137. console.log(money.toString())
  138. data.value=web3.utils.numberToHex(ethers.utils.parseEther(money.toString()).toString());
  139. console.log('value:'+data.value)
  140. return data;
  141. }
  142. /**
  143. * 转账
  144. * @param data
  145. * @returns {Promise<unknown>}
  146. */
  147. tokenpocketBnb.sendTransaction=function (data){
  148. return new Promise(async (resolve, reject) => {
  149. console.log(data)
  150. try {
  151. let txHash = await tokenpocketBnb.getProvider().request({
  152. method: 'eth_sendTransaction',
  153. params: [data],
  154. });
  155. console.log('txHash:'+txHash)
  156. if(txHash){
  157. resolve(txHash)
  158. }else {
  159. reject(false)
  160. }
  161. }catch (e) {
  162. reject(false)
  163. }
  164. })
  165. }
  166. export default tokenpocketBnb