123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- /**
- * bitkeep钱包的tron
- * @type {{}}
- */
- import tools from "@/common/js/tools";
- import {ethers} from "ethers";
- import web3 from "web3";
- let bitkeepLucky = {}
- let contractArr=[
- '0x10186d85ac0579cb141ff37261f23cf4f1d254b5'
- ];
- const chainId='0x3e6'
- let provider=null
- /**
- * 钱包初始校验
- * @returns {*}
- */
- bitkeepLucky.getProvider= function (){
- console.log(window.bitkeep.ethereum)
- if(!provider){
- provider = window.bitkeep && window.bitkeep.ethereum;
- if (!provider) {
- return window.open('https://bitkeep.com/en/download?type=2');
- }
- }
- if(provider.chainId==='0x73'){
- // window.bitkeep.ethereum.rpc.rpcUrl='http://luckychain.win/rpc/'
- provider.rpc.rpcUrl='http://luckychain.win/rpc/'
- }else {
- uni.$emit('noLucky',false)
- }
- // console.log(provider)
- return provider;
- }
- bitkeepLucky.test=function (){
- provider=bitkeepLucky.getProvider()
- console.log(provider.isConnected())
- }
- /**
- * 获取我登陆的地址
- * @returns {any}
- */
- bitkeepLucky.getMyAddress=function () {
- return uni.getStorageSync('luckyAddress')
- }
- /**
- * 获取基本账户地址
- * @returns {Promise<unknown>}
- */
- bitkeepLucky.getAccounts=async function (){
- return new Promise( (resolve, reject) => {
- try {
- bitkeepLucky.getProvider().request({ method: 'eth_requestAccounts'})
- .then((address)=>{
- if(address.length>0){
- uni.setStorageSync('luckyAddress',address[0])
- resolve(address[0])
- }else {
- resolve(false)
- }
- })
- .catch((e)=>{
- console.log(e)
- })
- }catch (e) {
- reject(e)
- }
- })
- }
- /**
- * 获取我的余额
- * @param selectedAddress
- * @returns {Promise<unknown>}
- */
- bitkeepLucky.getBalance=async function (selectedAddress) {
- if(!selectedAddress){
- selectedAddress=bitkeepLucky.getMyAddress();
- }
- console.log('selectedAddress:'+selectedAddress)
- // alert('钱包地址:'+selectedAddress)
- return new Promise( (resolve, reject) => {
- try {
- if(!selectedAddress){
- resolve(0)
- }else {
- bitkeepLucky.getProvider().request({ method: 'eth_getBalance',params:[selectedAddress, "latest"]})
- .then((balance)=>{
- balance= ethers.utils.formatEther(balance.toString())
- // alert(JSON.stringify(balance))
- // console.log(balance)
- console.log('getBalance:'+balance)
- resolve(balance)
- })
- .catch((e)=>{
- resolve(0)
- })
- }
- }catch (e) {
- // alert('error:'+JSON.stringify(e))
- reject(e)
- }
- })
- }
- /**
- * 获取合约数量
- * @param selectedAddress
- * @param contractType
- * @returns {Promise<number>}
- */
- bitkeepLucky.getTokenBalance=async function (selectedAddress,contractType) {
- if(!contractType){
- contractType=0
- }
- if(!selectedAddress){
- selectedAddress=bitkeepLucky.getMyAddress();
- }
- let data='0x70a08231000000000000000000000000'+(selectedAddress.substring(2))
- console.log(web3.utils.isHex(`0x70a08231`))
- console.log(data)
- return new Promise( (resolve, reject) => {
- try {
- let data='0x70a08231000000000000000000000000'+(selectedAddress.substring(2))
- bitkeepLucky.getProvider().request({method: 'eth_call',params:[{'to':contractArr[contractType],'data':data}, "latest"]}).then((balance)=>{
- console.log('hexToNumber:-----------------'+balance)
- console.log(web3.utils.hexToNumber(balance))
- balance= (web3.utils.hexToNumber(balance)/1000000).toString()
- if(balance<0.000001){
- balance=0
- }
- resolve(balance)
- }).catch((e)=>{
- resolve(0)
- })
- }catch (e) {
- // alert('error:'+JSON.stringify(e))
- reject(e)
- }
- })
- }
- /**
- * 获取合约信息
- * @param contractType
- * @returns {Promise<boolean|*>}
- */
- bitkeepLucky.getContract=async function () {
- }
- /**
- * 合约转账
- * @param to
- * @param money
- * @param contractType
- */
- bitkeepLucky.getContractData=async function (to,money,contractType){
- // console.log(ethers.utils.getAddress(to))
- if(!contractType){
- contractType=0
- }
- let data={};
- console.log('web3----------------------------------------')
- money*=1000000
- money=web3.utils.numberToHex(money)
- money=tools.replenishZero(money)
- console.log(money)
- console.log(`transfer(${to},${money})`)
- console.log( web3.utils.sha3(`transfer(${to.substring(2)},${money})`))
- // console.log(ethers.utils.parseEther(money).toString())
- data.data='0xa9059cbb'+tools.replenishZero(to)+tools.replenishZero(money);
- // console.log(data.data)
- // console.log('0xa9059cbb'+(to.substring(2)))
- data.to=contractArr[contractType];
- data.from=bitkeepLucky.getMyAddress();
- data.gasPrice=await bitkeepLucky.getGasPrice();
- data.gasLimit=await bitkeepLucky.getEstimateGas();
- data.chainId=chainId;
- data.value= '0x0'
- return data;
- }
- /**
- * 获取gasPrice
- * @returns {Promise<unknown>}
- */
- bitkeepLucky.getGasPrice= async function (){
- let gasPrice =await bitkeepLucky.getProvider().request({ method: 'eth_gasPrice',params:[]})
- if(!gasPrice){
- gasPrice='0x0'
- }
- return gasPrice;
- }
- /**
- * 获取燃料数量
- * @param data
- * @returns {Promise<unknown>}
- */
- bitkeepLucky.getEstimateGas= async function (data){
- let estimateGas =await bitkeepLucky.getProvider().request({ method: 'eth_estimateGas',params:[data]})
- console.log('estimateGas:'+estimateGas)
- if(!estimateGas){
- estimateGas=100000
- }
- return estimateGas;
- }
- /**
- * 转账接口
- * @param to
- * @param money
- * @returns {{}}
- */
- bitkeepLucky.getTransactionData=async function (to,money){
- let data={};
- data.data='0x0';
- data.to=to;
- data.from=bitkeepLucky.getMyAddress();
- data.gasPrice=await bitkeepLucky.getGasPrice();
- data.gasLimit=await bitkeepLucky.getEstimateGas();
- data.chainId=chainId;
- if(tools.isDevelopment()){
- money=0.001;
- }
- data.value= web3.utils.numberToHex(ethers.utils.parseEther(money.toString()).toString());
- console.log('value:'+data.value)
- return data;
- }
- /**
- * 转账
- * @param data
- * @returns {Promise<unknown>}
- */
- bitkeepLucky.sendTransaction=function (data){
- return new Promise(async (resolve, reject) => {
- try {
- let txHash = await bitkeepLucky.getProvider().request({
- method: 'eth_sendTransaction',
- params: [data],
- });
- console.log('txHash:'+txHash)
- if(txHash){
- resolve(txHash)
- }else {
- reject(false)
- }
- }catch (e) {
- reject(false)
- }
- })
- }
- export default bitkeepLucky
|