tokenpocket-bnb.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. if (!window.ethereum || typeof window.ethereum.isTokenPocket === 'undefined') {
  17. return false
  18. }
  19. if(!provider){
  20. provider = window.ethereum;
  21. }
  22. console.log(provider)
  23. if(!chainId){
  24. provider.request({method:'eth_chainId'}).then((ethChainId)=>{
  25. console.log('ethChainId:'+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. console.log('address:',address)
  52. if(address.length>0){
  53. uni.setStorageSync('babAddress',address[0])
  54. resolve(address[0])
  55. }else {
  56. resolve(false)
  57. }
  58. })
  59. .catch((e)=>{
  60. console.log(e)
  61. })
  62. }catch (e) {
  63. reject(e)
  64. }
  65. })
  66. }
  67. /**
  68. * 获取我的余额
  69. * @param selectedAddress
  70. * @returns {Promise<unknown>}
  71. */
  72. tokenpocketBnb.getBalance=async function (selectedAddress) {
  73. if(!selectedAddress){
  74. selectedAddress=tokenpocketBnb.getMyAddress();
  75. }
  76. return new Promise( (resolve, reject) => {
  77. try {
  78. tokenpocketBnb.getProvider().request({ method: 'eth_getBalance',params:[selectedAddress]})
  79. .then((balance)=>{
  80. balance= ethers.utils.formatEther(balance.toString())
  81. resolve(balance)
  82. })
  83. .catch((e)=>{
  84. resolve(0)
  85. })
  86. }catch (e) {
  87. reject(e)
  88. }
  89. })
  90. }
  91. /**
  92. * 获取gasPrice
  93. * @returns {Promise<unknown>}
  94. */
  95. tokenpocketBnb.getGasPrice= async function (){
  96. let gasPrice =await tokenpocketBnb.getProvider().request({ method: 'eth_gasPrice',params:[]})
  97. // gasPrice=web3.utils.hexToNumber(gasPrice)
  98. console.log('gasPrice:'+gasPrice)
  99. if(!gasPrice){
  100. gasPrice='0x12a05f200'
  101. }
  102. return gasPrice;
  103. }
  104. /**
  105. * 获取燃料数量
  106. * @param data
  107. * @returns {Promise<unknown>}
  108. */
  109. tokenpocketBnb.getEstimateGas= async function (data){
  110. let estimateGas =await tokenpocketBnb.getProvider().request({ method: 'eth_estimateGas',params:[data]})
  111. // estimateGas=web3.utils.hexToNumber(estimateGas)
  112. if(!estimateGas){
  113. estimateGas=100000
  114. }
  115. console.log('estimateGas:',estimateGas)
  116. return estimateGas;
  117. }
  118. /**
  119. * 转账接口
  120. * @param to
  121. * @param money
  122. * @returns {{}}
  123. */
  124. tokenpocketBnb.getTransactionData=async function (to,money){
  125. let data={};
  126. data.data='0x0';
  127. data.to=to;
  128. data.from=tokenpocketBnb.getMyAddress();
  129. data.gasPrice=await tokenpocketBnb.getGasPrice();
  130. data.gas=await tokenpocketBnb.getEstimateGas();
  131. data.chainId=chainId;
  132. if(tools.isDevelopment()){
  133. money=0.00001;
  134. }
  135. console.log(money.toString())
  136. data.value=web3.utils.numberToHex(ethers.utils.parseEther(money.toString()).toString());
  137. console.log('value:'+data.value)
  138. return data;
  139. }
  140. /**
  141. * 转账
  142. * @param data
  143. * @returns {Promise<unknown>}
  144. */
  145. tokenpocketBnb.sendTransaction=function (data){
  146. return new Promise(async (resolve, reject) => {
  147. console.log(data)
  148. try {
  149. let txHash = await tokenpocketBnb.getProvider().request({
  150. method: 'eth_sendTransaction',
  151. params: [data],
  152. });
  153. console.log('txHash:'+txHash)
  154. if(txHash){
  155. resolve(txHash)
  156. }else {
  157. reject(false)
  158. }
  159. }catch (e) {
  160. reject(false)
  161. }
  162. })
  163. }
  164. export default tokenpocketBnb