tokenpocket-bnb.js 5.6 KB

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