tokenpocket-bnb.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * bitkeep钱包的tron
  3. * @type {{}}
  4. */
  5. import tools from "@/common/js/tools";
  6. import {ethers} from "ethers";
  7. let tokenpocketBnb = {}
  8. let chainId=''
  9. let provider;
  10. /**
  11. * 钱包初始校验
  12. * @returns {*}
  13. */
  14. tokenpocketBnb.getProvider= function (){
  15. if (typeof window.ethereum.isTokenPocket === 'undefined') {
  16. return false
  17. }
  18. if(!provider){
  19. provider = window.ethereum;
  20. }
  21. if(!chainId){
  22. provider.request({method:'eth_chainId'}).then((ethChainId)=>{
  23. console.log('ethChainId:'+ethChainId)
  24. if(ethChainId!=='0x38'){
  25. uni.$emit('noBan',false)
  26. }else {
  27. chainId=ethChainId
  28. }
  29. })
  30. }
  31. return provider;
  32. }
  33. /**
  34. * 获取我登陆的地址
  35. * @returns {any}
  36. */
  37. tokenpocketBnb.getMyAddress=function () {
  38. return uni.getStorageSync('babAddress')
  39. }
  40. /**
  41. * 获取基本账户地址
  42. * @returns {Promise<unknown>}
  43. */
  44. tokenpocketBnb.getAccounts=async function (){
  45. return new Promise( (resolve, reject) => {
  46. try {
  47. tokenpocketBnb.getProvider().request({ method: 'eth_requestAccounts'})
  48. .then((address)=>{
  49. console.log('address:',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. if(!gasPrice){
  96. gasPrice='0x0'
  97. }
  98. return gasPrice;
  99. }
  100. /**
  101. * 获取燃料数量
  102. * @param data
  103. * @returns {Promise<unknown>}
  104. */
  105. tokenpocketBnb.getEstimateGas= async function (data){
  106. let estimateGas =await tokenpocketBnb.getProvider().request({ method: 'eth_estimateGas',params:[data]})
  107. console.log('estimateGas:'+estimateGas)
  108. if(!estimateGas){
  109. estimateGas=100000
  110. }
  111. return estimateGas;
  112. }
  113. /**
  114. * 转账接口
  115. * @param to
  116. * @param money
  117. * @returns {{}}
  118. */
  119. tokenpocketBnb.getTransactionData=async function (to,money){
  120. let data={};
  121. data.data='0x0';
  122. data.to=to;
  123. data.from=tokenpocketBnb.getMyAddress();
  124. data.gasPrice=await tokenpocketBnb.getGasPrice();
  125. data.gasLimit=await tokenpocketBnb.getEstimateGas();
  126. data.chainId=chainId;
  127. if(tools.isDevelopment()){
  128. money=0.0001;
  129. }
  130. data.value=ethers.utils.parseEther(money.toString()).toString();
  131. console.log('value:'+data.value)
  132. return data;
  133. }
  134. /**
  135. * 转账
  136. * @param data
  137. * @returns {Promise<unknown>}
  138. */
  139. tokenpocketBnb.sendTransaction=function (data){
  140. return new Promise(async (resolve, reject) => {
  141. try {
  142. let txHash = await tokenpocketBnb.getProvider().request({
  143. method: 'eth_sendTransaction',
  144. params: [data],
  145. });
  146. console.log('txHash:'+txHash)
  147. if(txHash){
  148. resolve(txHash)
  149. }else {
  150. reject(false)
  151. }
  152. }catch (e) {
  153. reject(false)
  154. }
  155. })
  156. }
  157. export default tokenpocketBnb