tokenpocket-bnb.js 4.2 KB

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