tokenpocket-bnb.js 5.5 KB

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