bitkeep-lucky.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 bitkeepLucky = {}
  9. let contractArr=[
  10. '0x10186d85ac0579cb141ff37261f23cf4f1d254b5'
  11. ];
  12. const chainId='0x3e6'
  13. let provider=null
  14. /**
  15. * 钱包初始校验
  16. * @returns {*}
  17. */
  18. bitkeepLucky.getProvider= function (){
  19. console.log(window.bitkeep.ethereum)
  20. if(!provider){
  21. provider = window.bitkeep && window.bitkeep.ethereum;
  22. if (!provider) {
  23. return window.open('https://bitkeep.com/en/download?type=2');
  24. }
  25. }
  26. if(provider.chainId==='0x73'){
  27. // window.bitkeep.ethereum.rpc.rpcUrl='http://luckychain.win/rpc/'
  28. provider.rpc.rpcUrl='http://luckychain.win/rpc/'
  29. }else {
  30. uni.$emit('noLucky',false)
  31. }
  32. // console.log(provider)
  33. return provider;
  34. }
  35. bitkeepLucky.test=function (){
  36. provider=bitkeepLucky.getProvider()
  37. console.log(provider.isConnected())
  38. }
  39. /**
  40. * 获取我登陆的地址
  41. * @returns {any}
  42. */
  43. bitkeepLucky.getMyAddress=function () {
  44. return uni.getStorageSync('luckyAddress')
  45. }
  46. /**
  47. * 获取基本账户地址
  48. * @returns {Promise<unknown>}
  49. */
  50. bitkeepLucky.getAccounts=async function (){
  51. return new Promise( (resolve, reject) => {
  52. try {
  53. bitkeepLucky.getProvider().request({ method: 'eth_requestAccounts'})
  54. .then((address)=>{
  55. if(address.length>0){
  56. uni.setStorageSync('luckyAddress',address[0])
  57. resolve(address[0])
  58. }else {
  59. resolve(false)
  60. }
  61. })
  62. .catch((e)=>{
  63. console.log(e)
  64. })
  65. }catch (e) {
  66. reject(e)
  67. }
  68. })
  69. }
  70. /**
  71. * 获取我的余额
  72. * @param selectedAddress
  73. * @returns {Promise<unknown>}
  74. */
  75. bitkeepLucky.getBalance=async function (selectedAddress) {
  76. if(!selectedAddress){
  77. selectedAddress=bitkeepLucky.getMyAddress();
  78. }
  79. console.log('selectedAddress:'+selectedAddress)
  80. // alert('钱包地址:'+selectedAddress)
  81. return new Promise( (resolve, reject) => {
  82. try {
  83. if(!selectedAddress){
  84. resolve(0)
  85. }else {
  86. bitkeepLucky.getProvider().request({ method: 'eth_getBalance',params:[selectedAddress, "latest"]})
  87. .then((balance)=>{
  88. balance= ethers.utils.formatEther(balance.toString())
  89. // alert(JSON.stringify(balance))
  90. // console.log(balance)
  91. console.log('getBalance:'+balance)
  92. resolve(balance)
  93. })
  94. .catch((e)=>{
  95. resolve(0)
  96. })
  97. }
  98. }catch (e) {
  99. // alert('error:'+JSON.stringify(e))
  100. reject(e)
  101. }
  102. })
  103. }
  104. /**
  105. * 获取合约数量
  106. * @param selectedAddress
  107. * @param contractType
  108. * @returns {Promise<number>}
  109. */
  110. bitkeepLucky.getTokenBalance=async function (selectedAddress,contractType) {
  111. if(!contractType){
  112. contractType=0
  113. }
  114. if(!selectedAddress){
  115. selectedAddress=bitkeepLucky.getMyAddress();
  116. }
  117. let data='0x70a08231000000000000000000000000'+(selectedAddress.substring(2))
  118. console.log(web3.utils.isHex(`0x70a08231`))
  119. console.log(data)
  120. return new Promise( (resolve, reject) => {
  121. try {
  122. let data='0x70a08231000000000000000000000000'+(selectedAddress.substring(2))
  123. bitkeepLucky.getProvider().request({method: 'eth_call',params:[{'to':contractArr[contractType],'data':data}, "latest"]}).then((balance)=>{
  124. console.log('hexToNumber:-----------------'+balance)
  125. console.log(web3.utils.hexToNumber(balance))
  126. balance= (web3.utils.hexToNumber(balance)/1000000).toString()
  127. if(balance<0.000001){
  128. balance=0
  129. }
  130. resolve(balance)
  131. }).catch((e)=>{
  132. resolve(0)
  133. })
  134. }catch (e) {
  135. // alert('error:'+JSON.stringify(e))
  136. reject(e)
  137. }
  138. })
  139. }
  140. /**
  141. * 获取合约信息
  142. * @param contractType
  143. * @returns {Promise<boolean|*>}
  144. */
  145. bitkeepLucky.getContract=async function () {
  146. }
  147. /**
  148. * 合约转账
  149. * @param to
  150. * @param money
  151. * @param contractType
  152. */
  153. bitkeepLucky.getContractData=async function (to,money,contractType){
  154. // console.log(ethers.utils.getAddress(to))
  155. if(!contractType){
  156. contractType=0
  157. }
  158. let data={};
  159. console.log('web3----------------------------------------')
  160. money*=1000000
  161. money=web3.utils.numberToHex(money)
  162. money=tools.replenishZero(money)
  163. console.log(money)
  164. console.log(`transfer(${to},${money})`)
  165. console.log( web3.utils.sha3(`transfer(${to.substring(2)},${money})`))
  166. // console.log(ethers.utils.parseEther(money).toString())
  167. data.data='0xa9059cbb'+tools.replenishZero(to)+tools.replenishZero(money);
  168. // console.log(data.data)
  169. // console.log('0xa9059cbb'+(to.substring(2)))
  170. data.to=contractArr[contractType];
  171. data.from=bitkeepLucky.getMyAddress();
  172. data.gasPrice=await bitkeepLucky.getGasPrice();
  173. data.gasLimit=await bitkeepLucky.getEstimateGas();
  174. data.chainId=chainId;
  175. data.value= '0x0'
  176. return data;
  177. }
  178. /**
  179. * 获取gasPrice
  180. * @returns {Promise<unknown>}
  181. */
  182. bitkeepLucky.getGasPrice= async function (){
  183. let gasPrice =await bitkeepLucky.getProvider().request({ method: 'eth_gasPrice',params:[]})
  184. if(!gasPrice){
  185. gasPrice='0x0'
  186. }
  187. return gasPrice;
  188. }
  189. /**
  190. * 获取燃料数量
  191. * @param data
  192. * @returns {Promise<unknown>}
  193. */
  194. bitkeepLucky.getEstimateGas= async function (data){
  195. let estimateGas =await bitkeepLucky.getProvider().request({ method: 'eth_estimateGas',params:[data]})
  196. console.log('estimateGas:'+estimateGas)
  197. if(!estimateGas){
  198. estimateGas=100000
  199. }
  200. return estimateGas;
  201. }
  202. /**
  203. * 转账接口
  204. * @param to
  205. * @param money
  206. * @returns {{}}
  207. */
  208. bitkeepLucky.getTransactionData=async function (to,money){
  209. let data={};
  210. data.data='0x0';
  211. data.to=to;
  212. data.from=bitkeepLucky.getMyAddress();
  213. data.gasPrice=await bitkeepLucky.getGasPrice();
  214. data.gasLimit=await bitkeepLucky.getEstimateGas();
  215. data.chainId=chainId;
  216. if(tools.isDevelopment()){
  217. money=0.001;
  218. }
  219. data.value= web3.utils.numberToHex(ethers.utils.parseEther(money.toString()).toString());
  220. console.log('value:'+data.value)
  221. return data;
  222. }
  223. /**
  224. * 转账
  225. * @param data
  226. * @returns {Promise<unknown>}
  227. */
  228. bitkeepLucky.sendTransaction=function (data){
  229. return new Promise(async (resolve, reject) => {
  230. try {
  231. let txHash = await bitkeepLucky.getProvider().request({
  232. method: 'eth_sendTransaction',
  233. params: [data],
  234. });
  235. console.log('txHash:'+txHash)
  236. if(txHash){
  237. resolve(txHash)
  238. }else {
  239. reject(false)
  240. }
  241. }catch (e) {
  242. reject(false)
  243. }
  244. })
  245. }
  246. export default bitkeepLucky