tokenpocket-bnb.js 7.9 KB

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