tokenpocket-bnb.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. if(ethChainId!=='0x38'){
  35. // uni.$emit('noBan',false)
  36. }else {
  37. chainId=ethChainId
  38. }
  39. })
  40. }
  41. return provider;
  42. }
  43. /**
  44. * 获取我登陆的地址
  45. * @returns {any}
  46. */
  47. tokenpocketBnb.getMyAddress=function () {
  48. // return uni.getStorageSync('babAddress')
  49. }
  50. /**
  51. * 获取基本账户地址
  52. * @returns {Promise<unknown>}
  53. */
  54. tokenpocketBnb.getAccounts=async function (){
  55. return new Promise( (resolve, reject) => {
  56. try {
  57. tokenpocketBnb.getProvider().request({ method: 'eth_requestAccounts'})
  58. .then((address)=>{
  59. console.log(address)
  60. if(address.length>0){
  61. // uni.setStorageSync('babAddress',address[0])
  62. // resolve(address[0])
  63. }else {
  64. resolve(false)
  65. }
  66. })
  67. .catch((e)=>{
  68. console.log(e)
  69. })
  70. }catch (e) {
  71. reject(e)
  72. }
  73. })
  74. }
  75. /**
  76. * 获取我的余额
  77. * @param selectedAddress
  78. * @returns {Promise<unknown>}
  79. */
  80. tokenpocketBnb.getBalance=async function (selectedAddress) {
  81. if(!selectedAddress){
  82. selectedAddress=tokenpocketBnb.getMyAddress();
  83. }
  84. return new Promise( (resolve, reject) => {
  85. try {
  86. tokenpocketBnb.getProvider().request({ method: 'eth_getBalance',params:[selectedAddress,'latest']})
  87. .then((balance)=>{
  88. balance= ethers.utils.formatEther(balance.toString())
  89. resolve(balance)
  90. })
  91. .catch((e)=>{
  92. console.log(e)
  93. resolve(0)
  94. })
  95. }catch (e) {
  96. reject(e)
  97. }
  98. })
  99. }
  100. /**
  101. * 获取gasPrice
  102. * @returns {Promise<unknown>}
  103. */
  104. tokenpocketBnb.getGasPrice= async function (){
  105. let gasPrice =await tokenpocketBnb.getProvider().request({ method: 'eth_gasPrice',params:[]})
  106. // gasPrice=web3.utils.hexToNumber(gasPrice)
  107. console.log('gasPrice:'+gasPrice)
  108. if(!gasPrice){
  109. gasPrice='0x12a05f200'
  110. }
  111. return gasPrice;
  112. }
  113. /**
  114. * 获取燃料数量
  115. * @param data
  116. * @returns {Promise<unknown>}
  117. */
  118. tokenpocketBnb.getEstimateGas= async function (data){
  119. let estimateGas =await tokenpocketBnb.getProvider().request({ method: 'eth_estimateGas',params:[data]})
  120. // estimateGas=web3.utils.hexToNumber(estimateGas)
  121. if(!estimateGas){
  122. estimateGas=100000
  123. }
  124. console.log('estimateGas:',estimateGas)
  125. return estimateGas;
  126. }
  127. /**
  128. * 转账接口
  129. * @param to
  130. * @param money
  131. * @returns {{}}
  132. */
  133. tokenpocketBnb.getTransactionData=async function (to,money){
  134. let data={};
  135. data.data='0x0';
  136. data.to=to;
  137. data.from=tokenpocketBnb.getMyAddress();
  138. data.gasPrice=await tokenpocketBnb.getGasPrice();
  139. data.gas=await tokenpocketBnb.getEstimateGas();
  140. data.chainId=chainId;
  141. if(tools.isDevelopment()){
  142. money=0.00001;
  143. }
  144. console.log(money.toString())
  145. data.value=web3.utils.numberToHex(ethers.utils.parseEther(money.toString()).toString());
  146. console.log('value:'+data.value)
  147. return data;
  148. }
  149. /**
  150. * 转账
  151. * @param data
  152. * @returns {Promise<unknown>}
  153. */
  154. tokenpocketBnb.sendTransaction=function (data){
  155. // eslint-disable-next-line no-async-promise-executor
  156. return new Promise(async (resolve, reject) => {
  157. console.log(data)
  158. try {
  159. let txHash = await tokenpocketBnb.getProvider().request({
  160. method: 'eth_sendTransaction',
  161. params: [data],
  162. });
  163. console.log('txHash:'+txHash)
  164. if(txHash){
  165. resolve(txHash)
  166. }else {
  167. reject(false)
  168. }
  169. }catch (e) {
  170. reject(false)
  171. }
  172. })
  173. }
  174. export default tokenpocketBnb