| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- /**
- * bitkeep钱包的tron
- * @type {{}}
- */
- import tools from "@/common/js/tools";
- import {ethers} from "ethers";
- import web3 from "web3";
- let tokenpocketBnb = {}
- let contractArr=[
- // '0x41D8081BE81e5940F23ADd6F68bFA0Bd0aF24Af4',//token
- // '0x337610d27c682e347c9cd60bd4b3b107c9d34ddd',//usdt
- // '0x7EC437314542acFAB189671b935ba56605483796',//token 正式
- // '0x4007dafabdad01f301529072d4f3eec5b6c424b3',//token 正式
- // '0x4007dafabdad01f301529072d4f3eec5b6c424b3',//token 正式
- // '0xb0B913aE044560eC47d311bDDbD7D2196314759A',//token 正式
- '0x6E0F8A5Aa1BF451c61a47732DcCCE9fF381a270b',//token 正式
- '0x55d398326f99059fF775485246999027B3197955',//usdt 正式
- ];
- let chainId=''
- let provider;
- /**
- * 钱包初始校验
- * @returns {*}
- */
- tokenpocketBnb.getProvider= function (){
- console.log(window.ethereum)
- console.log(window.ethereum.isTokenPocket)
- if (!window.ethereum || typeof window.ethereum.isTokenPocket === 'undefined') {
- return false
- }
- if(!provider){
- provider = window.ethereum;
- }
- // alert(provider.isConnected())
- console.log(chainId)
- if(!chainId){
- provider.request({method:'eth_chainId'}).then((ethChainId)=>{
- console.log(ethChainId)
- if(ethChainId!=='0x38'){
- uni.$emit('noBan',false)
- }else {
- chainId=ethChainId
- }
- })
- }
- return provider;
- }
- tokenpocketBnb.getPrice= async function () {
- // const provider = new ethers.providers.JsonRpcProvider('https://bsc-prebsc-dataseed.bnbchain.org/');
- const provider = new ethers.providers.JsonRpcProvider('https://bsc-dataseed3.defibit.io'); //zs
- const contract = {
- // factory: '0x6725F303b657a9451d8BA641348b6761A6CC7a17', // PancakeSwap V2 factory
- // router: '0xD99D1c33F9fC3444f8101754aBC46c52416550D1', // PancakeSwap V2 router
- factory: '0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73', // PancakeSwap V2 factory zs
- router: '0x10ED43C718714eb63d5aA57B78B54704E256024E', // PancakeSwap V2 router zs
- };
- const router = new ethers.Contract(
- contract.router,
- [{
- "inputs": [{"internalType": "uint256", "name": "amountIn", "type": "uint256"}, {
- "internalType": "address[]",
- "name": "path",
- "type": "address[]"
- }],
- "name": "getAmountsOut",
- "outputs": [{"internalType": "uint256[]", "name": "amounts", "type": "uint256[]"}],
- "stateMutability": "view",
- "type": "function"
- }],
- provider
- );
- const amounts = await router.getAmountsOut(ethers.utils.parseUnits('1', 18), [contractArr[0], contractArr[1]]);
- return amounts[1].toString() / 1e18;
- }
- /**
- * 获取我登陆的地址
- * @returns {any}
- */
- tokenpocketBnb.getMyAddress=function () {
- return uni.getStorageSync('babAddress')
- }
- /**
- * 获取基本账户地址
- * @returns {Promise<unknown>}
- */
- tokenpocketBnb.getAccounts=async function (){
- return new Promise( (resolve, reject) => {
- try {
- tokenpocketBnb.getProvider().request({ method: 'eth_requestAccounts'})
- .then((address)=>{
- if(address.length>0){
- uni.setStorageSync('babAddress',address[0])
- resolve(address[0])
- }else {
- resolve(false)
- }
- })
- .catch((e)=>{
- console.log(e)
- })
- }catch (e) {
- reject(e)
- }
- })
- }
- /**
- * 获取我的余额
- * @param selectedAddress
- * @returns {Promise<unknown>}
- */
- tokenpocketBnb.getBalance=async function (selectedAddress) {
- if(!selectedAddress){
- selectedAddress=tokenpocketBnb.getMyAddress();
- }
- return new Promise( (resolve, reject) => {
- try {
- tokenpocketBnb.getProvider().request({ method: 'eth_getBalance',params:[selectedAddress,'latest']})
- .then((balance)=>{
- balance= ethers.utils.formatEther(balance.toString())
- resolve(balance)
- })
- .catch((e)=>{
- resolve(0)
- })
- }catch (e) {
- reject(e)
- }
- })
- }
- /**
- * 获取合约数量
- * @param selectedAddress
- * @param contractType
- * @returns {Promise<number>}
- */
- tokenpocketBnb.getTokenBalance=async function (selectedAddress,contractType) {
- if(!contractType){
- contractType=0
- }
- if(!selectedAddress){
- selectedAddress=tokenpocketBnb.getMyAddress();
- }
- return new Promise( (resolve, reject) => {
- let data='0x70a08231000000000000000000000000'+(selectedAddress.substring(2))
- tokenpocketBnb.getProvider().request({method: 'eth_call',params:[{'to':contractArr[contractType],'data':data}, "latest"]}).then((balance)=>{
- balance= (web3.utils.hexToNumberString(balance)/1000000000000000000).toString()
- if(balance<0.000001){
- balance=0
- }
- resolve(balance)
- }).catch((e)=>{
- console.log(e)
- resolve(0)
- })
- })
- }
- /**
- * 获取gasPrice
- * @returns {Promise<unknown>}
- */
- tokenpocketBnb.getGasPrice= async function (){
- let gasPrice =await tokenpocketBnb.getProvider().request({ method: 'eth_gasPrice',params:[]})
- // gasPrice=web3.utils.hexToNumber(gasPrice)
- console.log('gasPrice:'+gasPrice)
- if(!gasPrice){
- gasPrice='0x12a05f200'
- }
- return gasPrice;
- }
- /**
- * 获取燃料数量
- * @param data
- * @returns {Promise<unknown>}
- */
- tokenpocketBnb.getEstimateGas= async function (data){
- console.log('estimateGas---------------start',data)
- let estimateGas =await tokenpocketBnb.getProvider().request({ method: 'eth_estimateGas',params:[data, "latest"]})
- // estimateGas=web3.utils.hexToNumber(estimateGas)
- if(!estimateGas || web3.utils.hexToNumberString(estimateGas)<100000){
- estimateGas=1000000
- estimateGas=web3.utils.numberToHex('1500000');
- }
- console.log('estimateGas:',estimateGas)
- return estimateGas;
- }
- /**
- * 转账接口
- * @param to
- * @param money
- * @returns {{}}
- */
- tokenpocketBnb.getTransactionData=async function (to,money){
- let data={};
- data.to=to;
- data.from=tokenpocketBnb.getMyAddress();
- data.gasPrice=await tokenpocketBnb.getGasPrice();
- data.gas=await tokenpocketBnb.getEstimateGas(data);
- data.chainId=chainId;
- if(tools.isDevelopment()){
- money=0.00001;
- }
- data.data='0x0';
- money=money+''
- console.log(money)
- data.value=web3.utils.numberToHex(ethers.utils.parseEther(money).toString());
- console.log(data)
- return data;
- }
- tokenpocketBnb.getContractTransaction=async function (sendData,contractType){
- console.log('getContractTransaction-----------------start')
- let data={};
- data.to=contractArr[contractType];
- data.from=tokenpocketBnb.getMyAddress();
- console.log('getContractTransaction-----------------1')
- data.gasPrice=await tokenpocketBnb.getGasPrice();
- console.log('getContractTransaction-----------------2')
- data.gas=await tokenpocketBnb.getEstimateGas();
- data.value='0x0';
- data.data=sendData;
- console.log('getContractTransaction-----------------3')
- data.chainId=chainId;
- console.log('value:'+data.value)
- console.log('getContractTransaction-----------------end')
- return data;
- }
- /**
- * 转账
- * @param data
- * @returns {Promise<unknown>}
- */
- tokenpocketBnb.sendTransaction=function (data){
- return new Promise(async (resolve, reject) => {
- console.log(data)
- try {
- let txHash = await tokenpocketBnb.getProvider().request({
- method: 'eth_sendTransaction',
- params: [data],
- });
- console.log('txHash:'+txHash)
- if(txHash){
- resolve(txHash)
- }else {
- reject(false)
- }
- }catch (e) {
- reject(false)
- }
- })
- }
- export default tokenpocketBnb
|