tokenpocket-bnb.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. '0x15ef4d92595aad506885861668cf463c09d1063e',//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. console.log(selectedAddress)
  111. let data='0x70a08231000000000000000000000000'+(selectedAddress.substring(2))
  112. console.log(web3.utils.isHex(`0x70a08231`))
  113. console.log(data)
  114. return new Promise( (resolve, reject) => {
  115. try {
  116. let data='0x70a08231000000000000000000000000'+(selectedAddress.substring(2))
  117. console.log([{'to':contractArr[contractType],'data':data}, "latest"],data)
  118. tokenpocketBnb.getProvider().request({method: 'eth_call',params:[{'to':contractArr[contractType],'data':data}, "latest"]}).then((balance)=>{
  119. console.log("balanceNum:",web3.utils.hexToNumber(balance))
  120. balance= (web3.utils.hexToNumber(balance)/(contractType===1?1000000:1000000000000000000)).toString()
  121. if(balance<0.000001){
  122. balance=0
  123. }
  124. resolve(balance)
  125. }).catch((e)=>{
  126. console.log(e)
  127. resolve(0)
  128. })
  129. }catch (e) {
  130. // alert('error:'+JSON.stringify(e))
  131. reject(e)
  132. }
  133. })
  134. }
  135. /**
  136. * 获取gasPrice
  137. * @returns {Promise<unknown>}
  138. */
  139. tokenpocketBnb.getGasPrice= async function (){
  140. let gasPrice =await tokenpocketBnb.getProvider().request({ method: 'eth_gasPrice',params:[]})
  141. // gasPrice=web3.utils.hexToNumber(gasPrice)
  142. console.log('gasPrice:'+gasPrice)
  143. if(!gasPrice){
  144. gasPrice='0x12a05f200'
  145. }
  146. return gasPrice;
  147. }
  148. /**
  149. * 获取燃料数量
  150. * @param data
  151. * @returns {Promise<unknown>}
  152. */
  153. tokenpocketBnb.getEstimateGas= async function (data){
  154. let estimateGas =await tokenpocketBnb.getProvider().request({ method: 'eth_estimateGas',params:[data]})
  155. // estimateGas=web3.utils.hexToNumber(estimateGas)
  156. if(!estimateGas){
  157. estimateGas=100000
  158. }
  159. console.log('estimateGas:',estimateGas)
  160. return estimateGas;
  161. }
  162. /**
  163. * 转账接口
  164. * @param to
  165. * @param money
  166. * @returns {{}}
  167. */
  168. tokenpocketBnb.getTransactionData=async function (to,money){
  169. let data={};
  170. data.data='0x0';
  171. data.to=to;
  172. data.from=tokenpocketBnb.getMyAddress();
  173. data.gasPrice=await tokenpocketBnb.getGasPrice();
  174. data.gas=await tokenpocketBnb.getEstimateGas();
  175. data.chainId=chainId;
  176. if(tools.isDevelopment()){
  177. money=0.00001;
  178. }
  179. console.log(money.toString())
  180. data.value=web3.utils.numberToHex(ethers.utils.parseEther(money.toString()).toString());
  181. console.log('value:'+data.value)
  182. return data;
  183. }
  184. /**
  185. * 转账
  186. * @param data
  187. * @returns {Promise<unknown>}
  188. */
  189. tokenpocketBnb.sendTransaction=function (data){
  190. return new Promise(async (resolve, reject) => {
  191. console.log(data)
  192. try {
  193. let txHash = await tokenpocketBnb.getProvider().request({
  194. method: 'eth_sendTransaction',
  195. params: [data],
  196. });
  197. console.log('txHash:'+txHash)
  198. if(txHash){
  199. resolve(txHash)
  200. }else {
  201. reject(false)
  202. }
  203. }catch (e) {
  204. reject(false)
  205. }
  206. })
  207. }
  208. export default tokenpocketBnb