TopBar.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { _decorator, Component, Node } from "cc";
  2. import BaseUI from "../base/BaseUI";
  3. import { UIWaiting } from "../../core_tgx/easy_ui_framework/waiting/UIWaiting";
  4. import { AbsWrapper, AbsM } from "../web3/AbsM";
  5. const { ccclass, property } = _decorator;
  6. @ccclass("TopBar")
  7. export class TopBar extends BaseUI {
  8. protected simpleOnBtnClick(name: string): void {
  9. switch (name) {
  10. case "btn_connect":
  11. this.connectWallet();
  12. break;
  13. }
  14. }
  15. protected onLoad(): void {
  16. super.onLoad();
  17. if (AbsM.ins.isConnected) {
  18. this.setActive("connect_node", false);
  19. this.setActive("gem_node", true);
  20. } else {
  21. this.setActive("connect_node", true);
  22. this.setActive("gem_node", false);
  23. }
  24. }
  25. private _w: AbsWrapper;
  26. async connectWallet() {
  27. UIWaiting.show("Connecting Wallet");
  28. try {
  29. let wrapper: AbsWrapper = await AbsM.ins.connectWallet();
  30. this._w = wrapper;
  31. } catch (e) {
  32. UIWaiting.hide();
  33. return;
  34. }
  35. let balance = await AbsM.ins.getBalance(this._w);
  36. // console.log("balance", balance);
  37. this.setActive("lbl_eth_balance", true);
  38. if (balance <= 0) {
  39. this.setText("lbl_eth_balance", "<0.00");
  40. } else if (balance < 0.01) {
  41. this.setText("lbl_eth_balance", "<0.01");
  42. } else {
  43. this.setText("lbl_eth_balance", "" + balance.toFixed(2) + "");
  44. }
  45. this.setActive("connect_node", false);
  46. this.setActive("gem_node", true);
  47. // this.FindAs("top_layout", Layout).updateLayout();
  48. UIWaiting.hide();
  49. }
  50. async testTrans() {
  51. const signature = await this._w.result.walletClient.sendTransaction({
  52. to: "0x273B3527BF5b607dE86F504fED49e1582dD2a1C6",
  53. data: "0x69",
  54. });
  55. console.log("signature", signature);
  56. }
  57. }