tokenpocket-bnb.js 4.3 KB

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