wative 1.1.13 → 1.1.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils.ts CHANGED
@@ -5,7 +5,7 @@ import inquirer from 'inquirer'
5
5
  import * as fs from 'fs'
6
6
  import * as path from "path";
7
7
 
8
- import { getChainIdByEvm, hasTokenBalance } from "./web3";
8
+ import { getChainIdByEvm, batchHasTokenBalance } from "./web3";
9
9
  import { MNEMONIC_WORDS } from './config';
10
10
  import { getChainType } from './chain';
11
11
  import { getBatchAccountBalance } from './assets';
@@ -632,6 +632,117 @@ export const formatAddr = (addr: string, force_full_addr: boolean = true) => {
632
632
  return addr.slice(0, 6) + "..." + addr.slice(-6);
633
633
  }
634
634
 
635
+ // 提取账户地址的辅助函数
636
+ const extractAccountFromChoice = (choice: any, is_evm: boolean): string | null => {
637
+ if (!choice.name) {
638
+ return null;
639
+ }
640
+
641
+ let account: any = null;
642
+
643
+ if (is_evm) {
644
+ account = /0x[0-9a-fA-f]{40}/.exec(choice.name);
645
+ account = account ? account[0] : null;
646
+ } else {
647
+ let re_result = /\s+([0-9a-zA-Z]*)/.exec(choice.name);
648
+ account = re_result ? re_result[1] : null;
649
+
650
+ if (account === "Back") {
651
+ account = null;
652
+ }
653
+ }
654
+
655
+ return account;
656
+ };
657
+
658
+ // 计算分页索引的辅助函数
659
+ const calculatePageIndices = (selected: number, pageSize: number, accountsLength: number): { fromIndex: number, toIndex: number } => {
660
+ let fromIndex: number, toIndex: number;
661
+
662
+ if (selected < pageSize / 2) {
663
+ if (accountsLength / 2 <= pageSize) {
664
+ fromIndex = 0;
665
+ toIndex = accountsLength / 2;
666
+ } else {
667
+ fromIndex = 0;
668
+ toIndex = pageSize;
669
+ }
670
+ } else {
671
+ if (accountsLength / 2 <= pageSize) {
672
+ fromIndex = 0;
673
+ toIndex = accountsLength / 2;
674
+ } else {
675
+ fromIndex = selected - pageSize / 2 + 1;
676
+ toIndex = selected + pageSize / 2 + 1;
677
+ }
678
+ }
679
+
680
+ return { fromIndex, toIndex };
681
+ };
682
+
683
+ // 提取账户列表的辅助函数
684
+ const extractAccountsList = (choices: any[], is_evm: boolean): (string | null)[] => {
685
+ const accounts: (string | null)[] = [];
686
+
687
+ for (let i = 0; i < choices.length; i++) {
688
+ const account = extractAccountFromChoice(choices[i], is_evm);
689
+ accounts.push(account);
690
+ }
691
+
692
+ return accounts.concat(accounts);
693
+ };
694
+
695
+ // 处理账户余额显示的辅助函数
696
+ const processAccountBalanceDisplay = (
697
+ choices: any[],
698
+ accounts: (string | null)[],
699
+ batchBalanceOf: any,
700
+ pad_index_map: any,
701
+ gas_token_name: string,
702
+ show_gas_token: boolean,
703
+ show_extended_token: boolean,
704
+ fromIndex: number,
705
+ toIndex: number
706
+ ) => {
707
+ for (let i = fromIndex; i < toIndex; i++) {
708
+ let _index = i >= accounts.length / 2 ? i - accounts.length / 2 : i;
709
+ let _choice = choices[_index];
710
+ if (!_choice.name) {
711
+ continue;
712
+ }
713
+
714
+ let name = _choice.name;
715
+ let account_address = accounts[i];
716
+ let token_message = '';
717
+ if (!account_address) {
718
+ continue;
719
+ }
720
+
721
+ for (let token_name in batchBalanceOf[account_address]) {
722
+ if (!show_gas_token && token_name === gas_token_name) {
723
+ continue;
724
+ }
725
+ if (!show_extended_token && token_name !== gas_token_name) {
726
+ continue;
727
+ }
728
+
729
+ if (name.includes(token_name)) {
730
+ continue;
731
+ }
732
+
733
+ token_message += `${token_name}:${batchBalanceOf[account_address][token_name].padEnd(pad_index_map[token_name])} `;
734
+ }
735
+
736
+ let nameList = name.split('(');
737
+ if (nameList.length > 1) {
738
+ let tag = nameList[1];
739
+ choices[_index].name = nameList[0] + ` ${token_message}` + `(${tag}`;
740
+ } else {
741
+ choices[_index].name = nameList[0] + ` ${token_message}`;
742
+ }
743
+ }
744
+ };
745
+
635
746
  export const listWithLazyBalanceLoading = async (
636
747
  options: string[],
637
748
  is_evm: boolean,
@@ -642,8 +753,7 @@ export const listWithLazyBalanceLoading = async (
642
753
  show_extended_token: boolean,
643
754
  direct_display: boolean,
644
755
  message?: string,
645
- default_account?: any,
646
- default_account_addr?: any,
756
+ default_account?: any
647
757
  ) => {
648
758
  const questions = [
649
759
  {
@@ -665,6 +775,7 @@ export const listWithLazyBalanceLoading = async (
665
775
  let selected = 0;
666
776
  let round = 0;
667
777
  let pre_selected: any;
778
+
668
779
  while (!promise.ui.activePrompt.answers[promise.ui.activePrompt.opt.name]) {
669
780
  if (!direct_display) {
670
781
  await sleep(1000);
@@ -684,7 +795,14 @@ export const listWithLazyBalanceLoading = async (
684
795
  if (direct_display) {
685
796
  round = 6;
686
797
  direct_display = false;
687
- let has_balance = hasTokenBalance(network_info.chainId.toString(), default_account_addr as string, asset_info_list[0].address);
798
+
799
+ const pageSize = promise.ui.activePrompt.opt.pageSize;
800
+ const accounts = extractAccountsList(promise.ui.activePrompt.opt.choices.choices, is_evm);
801
+ const { fromIndex, toIndex } = calculatePageIndices(selected, pageSize, accounts.length);
802
+ const active_account: any = accounts.slice(fromIndex, toIndex);
803
+
804
+ let has_balance = batchHasTokenBalance(network_info.chainId.toString(), active_account);
805
+ console.log('has_balance', has_balance);
688
806
  if (!has_balance) {
689
807
  round = 0;
690
808
  continue;
@@ -694,57 +812,13 @@ export const listWithLazyBalanceLoading = async (
694
812
  if (round < 5) {
695
813
  continue;
696
814
  }
815
+
697
816
  round = 0;
698
817
  const pageSize = promise.ui.activePrompt.opt.pageSize;
818
+ const accounts = extractAccountsList(promise.ui.activePrompt.opt.choices.choices, is_evm);
819
+ const { fromIndex, toIndex } = calculatePageIndices(selected, pageSize, accounts.length);
820
+ const active_account: any = accounts.slice(fromIndex, toIndex);
699
821
 
700
- let accounts: any = [];
701
- for (let i = 0; i < promise.ui.activePrompt.opt.choices.choices.length; i++) {
702
- if (!promise.ui.activePrompt.opt.choices.choices[i].name) {
703
- accounts.push(null);
704
- continue;
705
- }
706
-
707
- let account: any = null;
708
-
709
- if (is_evm) {
710
- account = /0x[0-9a-fA-f]{40}/.exec(promise.ui.activePrompt.opt.choices.choices[i].name);
711
- account = account ? account[0] : null;
712
- } else {
713
- let re_result = account = /\s+([0-9a-zA-Z]*)/.exec(promise.ui.activePrompt.opt.choices.choices[i].name);
714
- account = re_result ? re_result[1] : null;
715
-
716
- if (account === "Back") {
717
- account = null;
718
- }
719
- }
720
-
721
- if (account) {
722
- accounts.push(account)
723
- } else {
724
- accounts.push(null);
725
- }
726
- }
727
- accounts = accounts.concat(accounts);
728
- let fromIndex: number, toIndex: number;
729
- if (selected < pageSize / 2) {
730
- if (accounts.length / 2 <= pageSize) {
731
- fromIndex = 0;
732
- toIndex = accounts.length / 2;
733
- } else {
734
- fromIndex = 0;
735
- toIndex = pageSize;
736
- }
737
- } else {
738
- if (accounts.length / 2 <= pageSize) {
739
- fromIndex = 0;
740
- toIndex = accounts.length / 2;
741
- } else {
742
- fromIndex = selected - pageSize / 2 + 1;
743
- toIndex = selected + pageSize / 2 + 1;
744
- }
745
- }
746
-
747
- let active_account: any = accounts.slice(fromIndex, toIndex);
748
822
  let batchBalanceOf = await getBatchAccountBalance(
749
823
  keystore_path,
750
824
  active_account,
@@ -769,48 +843,26 @@ export const listWithLazyBalanceLoading = async (
769
843
  }
770
844
  }
771
845
 
772
- let gas_token_name = network_info.nativeCurrency.symbol;
773
- for (let i = fromIndex; i < toIndex; i++) {
774
- let _index = i >= accounts.length / 2 ? i - accounts.length / 2 : i;
775
- let _choice = promise.ui.activePrompt.opt.choices.choices[_index];
776
- if (!_choice.name) {
777
- continue;
778
- }
779
-
780
- let name = _choice.name;
781
- let account_address = accounts[i];
782
- let token_message = '';
783
-
784
- for (let token_name in batchBalanceOf[account_address]) {
785
- if (!show_gas_token && token_name === gas_token_name) {
786
- continue;
787
- }
788
- if (!show_extended_token && token_name !== gas_token_name) {
789
- continue;
790
- }
791
-
792
- if (name.includes(token_name)) {
793
- continue;
794
- }
795
-
796
- token_message += `${token_name}:${batchBalanceOf[account_address][token_name].padEnd(pad_index_map[token_name])} `;
797
- }
798
-
799
- let nameList = name.split('(');
800
- if (nameList.length > 1) {
801
- let tag = nameList[1];
802
- promise.ui.activePrompt.opt.choices.choices[_index].name = nameList[0] + ` ${token_message}` + `(${tag}`;
803
- } else {
804
- promise.ui.activePrompt.opt.choices.choices[_index].name = nameList[0] + ` ${token_message}`;
805
- }
806
- }
846
+ const gas_token_name = network_info.nativeCurrency.symbol;
847
+ processAccountBalanceDisplay(
848
+ promise.ui.activePrompt.opt.choices.choices,
849
+ accounts,
850
+ batchBalanceOf,
851
+ pad_index_map,
852
+ gas_token_name,
853
+ show_gas_token,
854
+ show_extended_token,
855
+ fromIndex,
856
+ toIndex
857
+ );
858
+
807
859
  promise.ui.activePrompt.screen.render('');
808
860
  promise.ui.activePrompt.render();
809
861
  pre_selected = selected;
810
862
  }
811
863
 
812
864
  return promise.ui.activePrompt.answers[promise.ui.activePrompt.opt.name];
813
- }
865
+ };
814
866
 
815
867
  export const updateStorage = (keystore_path: string) => {
816
868
  const network_path = path.resolve(keystore_path, 'network.json');
package/src/web3.ts CHANGED
@@ -397,13 +397,19 @@ export const batchUpdateUserTokenBalance = async (chain_id: string, account_addr
397
397
  }
398
398
  }
399
399
 
400
- export const hasTokenBalance = (chain_id: string, account_address: string, token_address: string) => {
400
+ export const batchHasTokenBalance = (chain_id: string, account_address_list: string[]) => {
401
+ let hasAccount = 0;
401
402
  if (chain_id in chainIdAccountBalanceMap) {
402
- if (token_address in chainIdAccountBalanceMap[chain_id]) {
403
- if (account_address in chainIdAccountBalanceMap[chain_id][token_address]) {
404
- return true;
403
+ if ("0" in chainIdAccountBalanceMap[chain_id]) {
404
+ for (let i = 0; i < account_address_list.length; i++) {
405
+ if (account_address_list[i] in chainIdAccountBalanceMap[chain_id]["0"]) {
406
+ hasAccount += 1;
407
+ }
405
408
  }
406
409
  }
407
410
  }
408
- return false;
411
+ if (hasAccount === 0) {
412
+ return false
413
+ }
414
+ return hasAccount / account_address_list.length > 0.8;
409
415
  }