bitkeep-tron.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /**
  2. * bitkeep钱包的tron
  3. * @type {{}}
  4. */
  5. import tools from "@/common/js/tools";
  6. let bitkeepTron = {}
  7. let contractArr=[
  8. 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t',
  9. 'TTUzsY3UMVTwT8a7wmJ3P6ar7aj5ddjVsR',
  10. ];
  11. const tronCrossAddress = "TTUzsY3UMVTwT8a7wmJ3P6ar7aj5ddjVsR";
  12. const tronCrossAbi = [
  13. {
  14. "anonymous": false,
  15. "inputs": [
  16. {
  17. "indexed": true,
  18. "internalType": "address",
  19. "name": "_l1Token",
  20. "type": "address"
  21. },
  22. {
  23. "indexed": true,
  24. "internalType": "address",
  25. "name": "_l2Token",
  26. "type": "address"
  27. },
  28. {
  29. "indexed": true,
  30. "internalType": "address",
  31. "name": "_from",
  32. "type": "address"
  33. },
  34. {
  35. "indexed": false,
  36. "internalType": "address",
  37. "name": "_to",
  38. "type": "address"
  39. },
  40. {
  41. "indexed": false,
  42. "internalType": "uint256",
  43. "name": "_amount",
  44. "type": "uint256"
  45. },
  46. {
  47. "indexed": false,
  48. "internalType": "bytes",
  49. "name": "_data",
  50. "type": "bytes"
  51. }
  52. ],
  53. "name": "ERC20DepositInitiated",
  54. "type": "event"
  55. },
  56. {
  57. "anonymous": false,
  58. "inputs": [
  59. {
  60. "indexed": true,
  61. "internalType": "address",
  62. "name": "_l1Token",
  63. "type": "address"
  64. },
  65. {
  66. "indexed": true,
  67. "internalType": "address",
  68. "name": "_l2Token",
  69. "type": "address"
  70. },
  71. {
  72. "indexed": true,
  73. "internalType": "address",
  74. "name": "_from",
  75. "type": "address"
  76. },
  77. {
  78. "indexed": false,
  79. "internalType": "address",
  80. "name": "_to",
  81. "type": "address"
  82. },
  83. {
  84. "indexed": false,
  85. "internalType": "uint256",
  86. "name": "_amount",
  87. "type": "uint256"
  88. },
  89. {
  90. "indexed": false,
  91. "internalType": "bytes",
  92. "name": "_data",
  93. "type": "bytes"
  94. }
  95. ],
  96. "name": "ERC20WithdrawalFinalized",
  97. "type": "event"
  98. },
  99. {
  100. "anonymous": false,
  101. "inputs": [
  102. {
  103. "indexed": true,
  104. "internalType": "address",
  105. "name": "_from",
  106. "type": "address"
  107. },
  108. {
  109. "indexed": true,
  110. "internalType": "address",
  111. "name": "_to",
  112. "type": "address"
  113. },
  114. {
  115. "indexed": false,
  116. "internalType": "uint256",
  117. "name": "_amount",
  118. "type": "uint256"
  119. },
  120. {
  121. "indexed": false,
  122. "internalType": "bytes",
  123. "name": "_data",
  124. "type": "bytes"
  125. }
  126. ],
  127. "name": "ETHDepositInitiated",
  128. "type": "event"
  129. },
  130. {
  131. "anonymous": false,
  132. "inputs": [
  133. {
  134. "indexed": true,
  135. "internalType": "address",
  136. "name": "_from",
  137. "type": "address"
  138. },
  139. {
  140. "indexed": true,
  141. "internalType": "address",
  142. "name": "_to",
  143. "type": "address"
  144. },
  145. {
  146. "indexed": false,
  147. "internalType": "uint256",
  148. "name": "_amount",
  149. "type": "uint256"
  150. },
  151. {
  152. "indexed": false,
  153. "internalType": "bytes",
  154. "name": "_data",
  155. "type": "bytes"
  156. }
  157. ],
  158. "name": "ETHWithdrawalFinalized",
  159. "type": "event"
  160. },
  161. {
  162. "anonymous": false,
  163. "inputs": [
  164. {
  165. "indexed": true,
  166. "internalType": "address",
  167. "name": "previousOwner",
  168. "type": "address"
  169. },
  170. {
  171. "indexed": true,
  172. "internalType": "address",
  173. "name": "newOwner",
  174. "type": "address"
  175. }
  176. ],
  177. "name": "OwnershipTransferred",
  178. "type": "event"
  179. },
  180. {
  181. "inputs": [
  182. {
  183. "internalType": "address",
  184. "name": "_l1Token",
  185. "type": "address"
  186. },
  187. {
  188. "internalType": "address",
  189. "name": "_l2Token",
  190. "type": "address"
  191. },
  192. {
  193. "internalType": "uint256",
  194. "name": "_amount",
  195. "type": "uint256"
  196. },
  197. {
  198. "internalType": "uint32",
  199. "name": "_l2Gas",
  200. "type": "uint32"
  201. },
  202. {
  203. "internalType": "bytes",
  204. "name": "_data",
  205. "type": "bytes"
  206. }
  207. ],
  208. "name": "depositERC20",
  209. "outputs": [],
  210. "stateMutability": "nonpayable",
  211. "type": "function"
  212. },
  213. {
  214. "inputs": [
  215. {
  216. "internalType": "address",
  217. "name": "_l1Token",
  218. "type": "address"
  219. },
  220. {
  221. "internalType": "address",
  222. "name": "_l2Token",
  223. "type": "address"
  224. },
  225. {
  226. "internalType": "address",
  227. "name": "_to",
  228. "type": "address"
  229. },
  230. {
  231. "internalType": "uint256",
  232. "name": "_amount",
  233. "type": "uint256"
  234. },
  235. {
  236. "internalType": "uint32",
  237. "name": "_l2Gas",
  238. "type": "uint32"
  239. },
  240. {
  241. "internalType": "bytes",
  242. "name": "_data",
  243. "type": "bytes"
  244. }
  245. ],
  246. "name": "depositERC20To",
  247. "outputs": [],
  248. "stateMutability": "nonpayable",
  249. "type": "function"
  250. },
  251. {
  252. "inputs": [
  253. {
  254. "internalType": "uint32",
  255. "name": "_l2Gas",
  256. "type": "uint32"
  257. },
  258. {
  259. "internalType": "bytes",
  260. "name": "_data",
  261. "type": "bytes"
  262. }
  263. ],
  264. "name": "depositETH",
  265. "outputs": [],
  266. "stateMutability": "payable",
  267. "type": "function"
  268. },
  269. {
  270. "inputs": [
  271. {
  272. "internalType": "address",
  273. "name": "_to",
  274. "type": "address"
  275. },
  276. {
  277. "internalType": "uint32",
  278. "name": "_l2Gas",
  279. "type": "uint32"
  280. },
  281. {
  282. "internalType": "bytes",
  283. "name": "_data",
  284. "type": "bytes"
  285. }
  286. ],
  287. "name": "depositETHTo",
  288. "outputs": [],
  289. "stateMutability": "payable",
  290. "type": "function"
  291. },
  292. {
  293. "inputs": [
  294. {
  295. "internalType": "address",
  296. "name": "",
  297. "type": "address"
  298. },
  299. {
  300. "internalType": "address",
  301. "name": "",
  302. "type": "address"
  303. }
  304. ],
  305. "name": "deposits",
  306. "outputs": [
  307. {
  308. "internalType": "uint256",
  309. "name": "",
  310. "type": "uint256"
  311. }
  312. ],
  313. "stateMutability": "view",
  314. "type": "function"
  315. },
  316. {
  317. "inputs": [],
  318. "name": "donateETH",
  319. "outputs": [],
  320. "stateMutability": "payable",
  321. "type": "function"
  322. },
  323. {
  324. "inputs": [
  325. {
  326. "internalType": "address",
  327. "name": "_l1Token",
  328. "type": "address"
  329. },
  330. {
  331. "internalType": "address",
  332. "name": "_l2Token",
  333. "type": "address"
  334. },
  335. {
  336. "internalType": "address",
  337. "name": "_from",
  338. "type": "address"
  339. },
  340. {
  341. "internalType": "address",
  342. "name": "_to",
  343. "type": "address"
  344. },
  345. {
  346. "internalType": "uint256",
  347. "name": "_amount",
  348. "type": "uint256"
  349. },
  350. {
  351. "internalType": "bytes",
  352. "name": "_data",
  353. "type": "bytes"
  354. }
  355. ],
  356. "name": "finalizeERC20Withdrawal",
  357. "outputs": [],
  358. "stateMutability": "nonpayable",
  359. "type": "function"
  360. },
  361. {
  362. "inputs": [
  363. {
  364. "internalType": "address",
  365. "name": "_from",
  366. "type": "address"
  367. },
  368. {
  369. "internalType": "address",
  370. "name": "_to",
  371. "type": "address"
  372. },
  373. {
  374. "internalType": "uint256",
  375. "name": "_amount",
  376. "type": "uint256"
  377. },
  378. {
  379. "internalType": "bytes",
  380. "name": "_data",
  381. "type": "bytes"
  382. }
  383. ],
  384. "name": "finalizeETHWithdrawal",
  385. "outputs": [],
  386. "stateMutability": "nonpayable",
  387. "type": "function"
  388. },
  389. {
  390. "inputs": [],
  391. "name": "owner",
  392. "outputs": [
  393. {
  394. "internalType": "address",
  395. "name": "",
  396. "type": "address"
  397. }
  398. ],
  399. "stateMutability": "view",
  400. "type": "function"
  401. },
  402. {
  403. "inputs": [],
  404. "name": "renounceOwnership",
  405. "outputs": [],
  406. "stateMutability": "nonpayable",
  407. "type": "function"
  408. },
  409. {
  410. "inputs": [
  411. {
  412. "internalType": "address",
  413. "name": "newOwner",
  414. "type": "address"
  415. }
  416. ],
  417. "name": "transferOwnership",
  418. "outputs": [],
  419. "stateMutability": "nonpayable",
  420. "type": "function"
  421. },
  422. {
  423. "stateMutability": "payable",
  424. "type": "receive"
  425. }
  426. ];
  427. /**
  428. * 钱包初始校验
  429. * @returns {*}
  430. */
  431. bitkeepTron.isInstall=function (){
  432. return window.tronLink && window.tronWeb;
  433. }
  434. /**
  435. * 获取我登陆的地址
  436. * @returns {any}
  437. */
  438. bitkeepTron.getMyAddress=function () {
  439. return uni.getStorageSync('myAddress')
  440. }
  441. /**
  442. * 获取基本账户地址
  443. * @returns {Promise<unknown>}
  444. */
  445. bitkeepTron.getAccounts=async function (){
  446. return new Promise(async (resolve, reject) => {
  447. try {
  448. await window.tronLink.request({method: "tron_requestAccounts"});
  449. let selectedAddress = tronWeb.defaultAddress.base58;
  450. uni.setStorageSync('myAddress',selectedAddress)
  451. resolve(selectedAddress)
  452. }catch (e) {
  453. reject(e)
  454. }
  455. })
  456. }
  457. /**
  458. * 获取我的余额
  459. * @param selectedAddress
  460. * @returns {Promise<unknown>}
  461. */
  462. bitkeepTron.getBalance=async function (selectedAddress) {
  463. if(!selectedAddress){
  464. selectedAddress=bitkeepTron.getMyAddress();
  465. }
  466. let balance = await tronWeb.trx.getBalance(selectedAddress);
  467. balance=balance/1000000;
  468. return balance
  469. }
  470. /**
  471. * 获取合约数量
  472. * @param selectedAddress
  473. * @param contractType
  474. * @returns {Promise<number>}
  475. */
  476. bitkeepTron.getTokenBalance=async function (selectedAddress,contractType) {
  477. if(!selectedAddress){
  478. selectedAddress=bitkeepTron.getMyAddress();
  479. }
  480. let tokenBalance = await tronWeb.transactionBuilder.triggerConstantContract(
  481. contractArr[contractType],
  482. "balanceOf(address)",
  483. {},
  484. [{ type: 'address', value: selectedAddress }],
  485. tronWeb.defaultAddress.base58,
  486. );
  487. let balance = tokenBalance.constant_result[0];
  488. balance= parseInt(balance, 16);
  489. balance=balance/1000000;
  490. return balance;
  491. }
  492. /**
  493. * 获取合约信息
  494. * @param contractType
  495. * @returns {Promise<boolean|*>}
  496. */
  497. bitkeepTron.getContract=async function (contractType) {
  498. let contractData = await tronWeb.trx.getContract(contractArr[contractType])
  499. console.log(contractData)
  500. if(contractData.abi){
  501. return contractData
  502. }else {
  503. return false
  504. }
  505. }
  506. /**
  507. * 合约转账
  508. * @param to
  509. * @param money
  510. * @param contractType
  511. */
  512. bitkeepTron.getContractData=function (to,money,contractType){
  513. let data={};
  514. data.contract=contractArr[contractType];
  515. data.to=to;
  516. data.from=bitkeepTron.getMyAddress();
  517. if(tools.isDevelopment()){
  518. money=0.001;
  519. }
  520. data.value=money;
  521. return data;
  522. }
  523. /**
  524. * 转账接口
  525. * @param to
  526. * @param money
  527. * @returns {{}}
  528. */
  529. bitkeepTron.getTransactionData=function (to,money){
  530. let data={};
  531. data.to=to;
  532. data.from=bitkeepTron.getMyAddress();
  533. if(tools.isDevelopment()){
  534. money=0.001;
  535. }
  536. data.value=money;
  537. return data;
  538. }
  539. bitkeepTron.depositUsdtTo=async function (address,amount){
  540. return new Promise(async (resolve, reject) => {
  541. try {
  542. let _amount = tronWeb.toSun(amount)
  543. let trxUsdtContract = await tronWeb.contract().at(contractArr[0])
  544. let res = await trxUsdtContract.methods.approve(tronCrossAddress, _amount).send()
  545. if (res) {
  546. let timeNum=0;
  547. let timeServe= setInterval(async () => {
  548. let hashRes = await tronWeb.trx.getTransaction(res)
  549. if(hashRes.ret.length>0){
  550. if(hashRes.ret[0].contractRet==='SUCCESS'){
  551. const cInstance = tronWeb.contract(tronCrossAbi, tronCrossAddress);
  552. let _l1Token = "0xa614f803b6fd780986a42c78ec9c7f77e6ded13c".toLowerCase()
  553. let _l2Token = "0x10186D85Ac0579Cb141Ff37261f23CF4F1D254b5".toLowerCase()
  554. let sendRes = await cInstance.methods.depositERC20To(_l1Token, _l2Token, address, _amount, 0, []).send()
  555. console.log(sendRes)
  556. resolve(sendRes)
  557. clearInterval(timeServe)
  558. }else if(hashRes.ret[0].contractRet==='REVERT'){
  559. resolve(false)
  560. clearInterval(timeServe)
  561. }else if(timeNum>5){
  562. clearInterval(timeServe)
  563. resolve(false)
  564. }
  565. ++timeNum
  566. }
  567. }, 500)
  568. }else {
  569. resolve(false)
  570. }
  571. }catch (e) {
  572. reject(false)
  573. }
  574. })
  575. }
  576. /**
  577. * trc转账
  578. * @param data
  579. * @returns {Promise<unknown>}
  580. */
  581. bitkeepTron.sendTransaction=function (data){
  582. return new Promise(async (resolve, reject) => {
  583. try {
  584. let broastTx={};
  585. if (data.contract) {
  586. let decimal = 18
  587. let Contract = await tronWeb.contract().at(data.contract)
  588. const decimalCall = Contract.decimals || Contract.DECIMALS;
  589. if (decimalCall) {
  590. decimal = await decimalCall().call()
  591. }
  592. let txid = await Contract.transfer(
  593. data.to,
  594. tronWeb.toHex(data.value * Math.pow(10, decimal))
  595. ).send()
  596. if(txid){
  597. broastTx.txid=txid
  598. }
  599. } else {
  600. let tx = await tronWeb.transactionBuilder.sendTrx(data.to, data.value * Math.pow(10, 6), data.from);
  601. // sign 签名
  602. let signedTx = await tronWeb.trx.sign(tx);
  603. // broadcast 广播
  604. broastTx = await tronWeb.trx.sendRawTransaction(signedTx);
  605. }
  606. if(broastTx.txid){
  607. resolve(broastTx.txid)
  608. }else {
  609. reject(false)
  610. }
  611. }catch (e) {
  612. reject(false)
  613. }
  614. })
  615. }
  616. export default bitkeepTron