tokenpocket-bnb.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. // '0x41D8081BE81e5940F23ADd6F68bFA0Bd0aF24Af4',//token
  11. // '0x337610d27c682e347c9cd60bd4b3b107c9d34ddd',//usdt
  12. // '0x7EC437314542acFAB189671b935ba56605483796',//token 正式
  13. // '0x4007dafabdad01f301529072d4f3eec5b6c424b3',//token 正式
  14. // '0x4007dafabdad01f301529072d4f3eec5b6c424b3',//token 正式
  15. '0xb0B913aE044560eC47d311bDDbD7D2196314759A',//token 正式
  16. '0x55d398326f99059fF775485246999027B3197955',//usdt 正式
  17. ];
  18. let chainId=''
  19. let provider;
  20. /**
  21. * 钱包初始校验
  22. * @returns {*}
  23. */
  24. tokenpocketBnb.getProvider= function (){
  25. console.log(window.ethereum)
  26. console.log(window.ethereum.isTokenPocket)
  27. if (!window.ethereum || typeof window.ethereum.isTokenPocket === 'undefined') {
  28. return false
  29. }
  30. if(!provider){
  31. provider = window.ethereum;
  32. }
  33. // alert(provider.isConnected())
  34. console.log(chainId)
  35. if(!chainId){
  36. provider.request({method:'eth_chainId'}).then((ethChainId)=>{
  37. console.log(ethChainId)
  38. if(ethChainId!=='0x38'){
  39. uni.$emit('noBan',false)
  40. }else {
  41. chainId=ethChainId
  42. }
  43. })
  44. }
  45. return provider;
  46. }
  47. tokenpocketBnb.getPrice= async function () {
  48. // const provider = new ethers.providers.JsonRpcProvider('https://bsc-prebsc-dataseed.bnbchain.org/');
  49. const provider = new ethers.providers.JsonRpcProvider('https://bsc-dataseed3.defibit.io'); //zs
  50. const contract = {
  51. // factory: '0x6725F303b657a9451d8BA641348b6761A6CC7a17', // PancakeSwap V2 factory
  52. // router: '0xD99D1c33F9fC3444f8101754aBC46c52416550D1', // PancakeSwap V2 router
  53. factory: '0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73', // PancakeSwap V2 factory zs
  54. router: '0x10ED43C718714eb63d5aA57B78B54704E256024E', // PancakeSwap V2 router zs
  55. };
  56. const router = new ethers.Contract(
  57. contract.router,
  58. [{
  59. "inputs": [{"internalType": "uint256", "name": "amountIn", "type": "uint256"}, {
  60. "internalType": "address[]",
  61. "name": "path",
  62. "type": "address[]"
  63. }],
  64. "name": "getAmountsOut",
  65. "outputs": [{"internalType": "uint256[]", "name": "amounts", "type": "uint256[]"}],
  66. "stateMutability": "view",
  67. "type": "function"
  68. }],
  69. provider
  70. );
  71. const amounts = await router.getAmountsOut(ethers.utils.parseUnits('1', 18), [contractArr[0], contractArr[1]]);
  72. return amounts[1].toString() / 1e18;
  73. }
  74. /**
  75. * 获取我登陆的地址
  76. * @returns {any}
  77. */
  78. tokenpocketBnb.getMyAddress=function () {
  79. return uni.getStorageSync('babAddress')
  80. }
  81. /**
  82. * 获取基本账户地址
  83. * @returns {Promise<unknown>}
  84. */
  85. tokenpocketBnb.getAccounts=async function (){
  86. return new Promise( (resolve, reject) => {
  87. try {
  88. tokenpocketBnb.getProvider().request({ method: 'eth_requestAccounts'})
  89. .then((address)=>{
  90. if(address.length>0){
  91. uni.setStorageSync('babAddress',address[0])
  92. resolve(address[0])
  93. }else {
  94. resolve(false)
  95. }
  96. })
  97. .catch((e)=>{
  98. console.log(e)
  99. })
  100. }catch (e) {
  101. reject(e)
  102. }
  103. })
  104. }
  105. /**
  106. * 获取我的余额
  107. * @param selectedAddress
  108. * @returns {Promise<unknown>}
  109. */
  110. tokenpocketBnb.getBalance=async function (selectedAddress) {
  111. if(!selectedAddress){
  112. selectedAddress=tokenpocketBnb.getMyAddress();
  113. }
  114. return new Promise( (resolve, reject) => {
  115. try {
  116. tokenpocketBnb.getProvider().request({ method: 'eth_getBalance',params:[selectedAddress,'latest']})
  117. .then((balance)=>{
  118. balance= ethers.utils.formatEther(balance.toString())
  119. resolve(balance)
  120. })
  121. .catch((e)=>{
  122. resolve(0)
  123. })
  124. }catch (e) {
  125. reject(e)
  126. }
  127. })
  128. }
  129. /**
  130. * 获取合约数量
  131. * @param selectedAddress
  132. * @param contractType
  133. * @returns {Promise<number>}
  134. */
  135. tokenpocketBnb.getTokenBalance=async function (selectedAddress,contractType) {
  136. if(!contractType){
  137. contractType=0
  138. }
  139. if(!selectedAddress){
  140. selectedAddress=tokenpocketBnb.getMyAddress();
  141. }
  142. return new Promise( (resolve, reject) => {
  143. let data='0x70a08231000000000000000000000000'+(selectedAddress.substring(2))
  144. tokenpocketBnb.getProvider().request({method: 'eth_call',params:[{'to':contractArr[contractType],'data':data}, "latest"]}).then((balance)=>{
  145. balance= (web3.utils.hexToNumberString(balance)/1000000000000000000).toString()
  146. if(balance<0.000001){
  147. balance=0
  148. }
  149. resolve(balance)
  150. }).catch((e)=>{
  151. console.log(e)
  152. resolve(0)
  153. })
  154. })
  155. }
  156. /**
  157. * 获取gasPrice
  158. * @returns {Promise<unknown>}
  159. */
  160. tokenpocketBnb.getGasPrice= async function (){
  161. let gasPrice =await tokenpocketBnb.getProvider().request({ method: 'eth_gasPrice',params:[]})
  162. // gasPrice=web3.utils.hexToNumber(gasPrice)
  163. console.log('gasPrice:'+gasPrice)
  164. if(!gasPrice){
  165. gasPrice='0x12a05f200'
  166. }
  167. return gasPrice;
  168. }
  169. /**
  170. * 获取燃料数量
  171. * @param data
  172. * @returns {Promise<unknown>}
  173. */
  174. tokenpocketBnb.getEstimateGas= async function (data){
  175. console.log('estimateGas---------------start',data)
  176. let estimateGas =await tokenpocketBnb.getProvider().request({ method: 'eth_estimateGas',params:[data, "latest"]})
  177. // estimateGas=web3.utils.hexToNumber(estimateGas)
  178. if(!estimateGas || web3.utils.hexToNumberString(estimateGas)<100000){
  179. estimateGas=1000000
  180. estimateGas=web3.utils.numberToHex('1500000');
  181. }
  182. console.log('estimateGas:',estimateGas)
  183. return estimateGas;
  184. }
  185. /**
  186. * 转账接口
  187. * @param to
  188. * @param money
  189. * @returns {{}}
  190. */
  191. tokenpocketBnb.getTransactionData=async function (to,money){
  192. let data={};
  193. data.to=to;
  194. data.from=tokenpocketBnb.getMyAddress();
  195. data.gasPrice=await tokenpocketBnb.getGasPrice();
  196. data.gas=await tokenpocketBnb.getEstimateGas(data);
  197. data.chainId=chainId;
  198. if(tools.isDevelopment()){
  199. money=0.00001;
  200. }
  201. data.data='0x0';
  202. money=money+''
  203. console.log(money)
  204. data.value=web3.utils.numberToHex(ethers.utils.parseEther(money).toString());
  205. console.log(data)
  206. return data;
  207. }
  208. tokenpocketBnb.getContractTransaction=async function (sendData,contractType){
  209. console.log('getContractTransaction-----------------start')
  210. let data={};
  211. data.to=contractArr[contractType];
  212. data.from=tokenpocketBnb.getMyAddress();
  213. console.log('getContractTransaction-----------------1')
  214. data.gasPrice=await tokenpocketBnb.getGasPrice();
  215. console.log('getContractTransaction-----------------2')
  216. data.gas=await tokenpocketBnb.getEstimateGas();
  217. data.value='0x0';
  218. data.data=sendData;
  219. console.log('getContractTransaction-----------------3')
  220. data.chainId=chainId;
  221. console.log('value:'+data.value)
  222. console.log('getContractTransaction-----------------end')
  223. return data;
  224. }
  225. /**
  226. * 转账
  227. * @param data
  228. * @returns {Promise<unknown>}
  229. */
  230. tokenpocketBnb.sendTransaction=function (data){
  231. return new Promise(async (resolve, reject) => {
  232. console.log(data)
  233. try {
  234. let txHash = await tokenpocketBnb.getProvider().request({
  235. method: 'eth_sendTransaction',
  236. params: [data],
  237. });
  238. console.log('txHash:'+txHash)
  239. if(txHash){
  240. resolve(txHash)
  241. }else {
  242. reject(false)
  243. }
  244. }catch (e) {
  245. reject(false)
  246. }
  247. })
  248. }
  249. export default tokenpocketBnb