wative 1.0.20 → 1.0.21

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/account.ts CHANGED
@@ -23,6 +23,7 @@ import { selectToolsOptions } from './tools';
23
23
  import inquirer from "inquirer";
24
24
  import { getChainType } from "./chain";
25
25
 
26
+ require("dotenv").config();
26
27
  const cliProgress = require('cli-progress');
27
28
  const { WativeCore } = require("wative-core");
28
29
 
@@ -240,7 +241,12 @@ export const loginIn = async (account_label: string, wative_core: typeof WativeC
240
241
  }
241
242
 
242
243
  for (let i = 0; i < 3; i++) {
243
- const password = await inputPassword(`Please input the password for Account [${account_label}]`);
244
+ let password: any;
245
+ if (process.env.WativePassword && process.env.WativePassword !== '') {
246
+ password = process.env.WativePassword;
247
+ } else {
248
+ password = await inputPassword(`Please input the password for Account [${account_label}]`);
249
+ }
244
250
  if (!password) {
245
251
  console.log(
246
252
  chalk.red("Password can't be empty")
@@ -552,6 +558,7 @@ const showAccounts = async (keystore_path: string, account_label: string, wative
552
558
  let network_info = getNetworkInfoByName(keystore_path, default_network);
553
559
  let network_type = getChainType(network_info.chainId.toString());
554
560
 
561
+ let pad_index = account_info.data.length.toString().length + 1;
555
562
  for (let i = 0; i < account_info.data.length; i++) {
556
563
  let tag = account_info.data[i].tag ? `(${account_info.data[i].tag})` : "";
557
564
  let is_disable_address = wative_core.account.checkIsDisableAddress(disabled_slots, i);
@@ -569,10 +576,10 @@ const showAccounts = async (keystore_path: string, account_label: string, wative
569
576
 
570
577
  if (is_disable_address) {
571
578
  option_accounts.push(
572
- new inquirer.Separator(chalk.yellow(`#${i} ${formatAddr(account_address, status.fullAddr)} ${tag}`))
579
+ new inquirer.Separator(chalk.yellow(`#${i.toString().padEnd(pad_index)} ${formatAddr(account_address, status.fullAddr)} ${tag}`))
573
580
  )
574
581
  } else {
575
- option_accounts.push(`#${i} ${formatAddr(account_address, status.fullAddr)} ${tag}`);
582
+ option_accounts.push(`#${i.toString().padEnd(pad_index)} ${formatAddr(account_address, status.fullAddr)} ${tag}`);
576
583
  }
577
584
  }
578
585
 
@@ -646,11 +653,11 @@ const showAccountDetail = async (keystore_path: string, account_label: string, s
646
653
 
647
654
  switch (selected_account_detail) {
648
655
  case tag: {
649
- let cureent_tag = 'Current tag';
656
+ let current_tag = 'Current tag';
650
657
  if ("tag" in account_info.data[selected_account_id] && account_info.data[selected_account_id].tag !== "") {
651
- cureent_tag = `Current tag (${account_info.data[selected_account_id].tag})`;
658
+ current_tag = `Current tag (${account_info.data[selected_account_id].tag})`;
652
659
  }
653
- let tag = await inputSomething(cureent_tag, tagValidator);
660
+ let tag = await inputSomething(current_tag, tagValidator);
654
661
  tag = tag.replace(/\s+/g, " ").trim();
655
662
  account_info.data[selected_account_id].tag = tag.toLocaleLowerCase();
656
663
  saveAccountInfo(keystore_path, account_label, account_info);
package/src/assets.ts CHANGED
@@ -3,7 +3,7 @@ import * as path from "path";
3
3
  import * as fs from 'fs';
4
4
  import { BigNumber } from 'bignumber.js';
5
5
  import { getChainType, isEvmAddress, isSolanaAddress } from "./chain";
6
- import { getNetworkInfoByName, getNetworkTypeByName } from "./network";
6
+ import { getNetworkInfoByName, getNetworkTypeByName, getShowTokenList } from "./network";
7
7
  import { chainAddressValidator, checkFileExistence, inputSomething, numberValidator, selectSomething } from "./utils";
8
8
  import { deleteTokenBalance, getTokenBalance, getTokenBalanceInEvm, getTokenBalanceInSolana, getTokenInfoInEvm, getTokenInfoInSolana, updateBalance } from './web3';
9
9
  import inquirer from "inquirer";
@@ -36,7 +36,7 @@ export const getAssetListByTokenName = async (
36
36
  const asset_list = await getAssetList(keystore_path, chain_id);
37
37
  let token_address_list: any = [];
38
38
  for (let i = 0; i < asset_list.length; i++) {
39
- if (asset_list[i].symbol === token_name) {
39
+ if (asset_list[i].symbol.toLocaleLowerCase() === token_name.toLocaleLowerCase()) {
40
40
  token_address_list.push(asset_list[i]);
41
41
  }
42
42
  }
@@ -102,7 +102,10 @@ export const saveAsset = async (
102
102
  }
103
103
 
104
104
  for (let i = 0; i < assets_mapping[chain_id].length; i++) {
105
- if (assets_mapping[chain_id][i].address === asset_address) {
105
+ if (
106
+ assets_mapping[chain_id][i].address.toLocaleLowerCase() === asset_address.toLocaleLowerCase() ||
107
+ assets_mapping[chain_id][i].symbol.toLocaleLowerCase() === symbol.toLocaleLowerCase()
108
+ ) {
106
109
  assets_mapping[chain_id][i] = {
107
110
  address: asset_address,
108
111
  symbol,
@@ -172,7 +175,7 @@ export const addToken = async (
172
175
  }
173
176
 
174
177
  console.log(
175
- chalk.green(`Token ${token_info_result.output.name} loaded successfully.`)
178
+ chalk.green(`Token ${token_info_result.output.symbol}(${token_info_result.output.name}) loaded successfully.`)
176
179
  );
177
180
 
178
181
  let display_decimals = await inputSomething("Enter decimals for display", numberValidator);
@@ -224,7 +227,7 @@ export const addAsset = async (
224
227
  }
225
228
 
226
229
  console.log(
227
- chalk.green(`Token ${token_info_result.output.name} loaded successfully.`)
230
+ chalk.green(`Token ${token_info_result.output.symbol}(${token_info_result.output.name}) loaded successfully.`)
228
231
  );
229
232
 
230
233
  let display_decimals = await inputSomething("Enter decimals for display", numberValidator);
@@ -355,9 +358,15 @@ export const getBatchAccountBalance = async (
355
358
  }
356
359
 
357
360
  if (show_extended_token) {
361
+ let showTokenList = getShowTokenList(keystore_path, network_name);
362
+
358
363
  for (let i = 0; i < asset_info_list_len; i++) {
359
364
  let asset_info = asset_info_list[i];
360
365
  let token_address = asset_info.address;
366
+ if (!showTokenList.includes(token_address)) {
367
+ continue;
368
+ }
369
+
361
370
  await updateBalance(
362
371
  account_address_list,
363
372
  token_address,
package/src/network.ts CHANGED
@@ -13,9 +13,9 @@ import {
13
13
  getSymbol,
14
14
  getColor,
15
15
  Result,
16
- getAccountChainType,
17
16
  formatAddr,
18
- checkFileExistence
17
+ checkFileExistence,
18
+ selectCheckboxSomething
19
19
  } from "./utils";
20
20
  import {
21
21
  getAccountInfo,
@@ -362,6 +362,18 @@ export const selectFlag = async () => {
362
362
  }
363
363
  }
364
364
 
365
+ export const getShowTokenList = (keystore_path: string, chain_name: string) => {
366
+ const network_path = path.resolve(keystore_path, 'network.json');
367
+ const networks = JSON.parse(fs.readFileSync(network_path, 'utf8'));
368
+
369
+ let showTokensMap = networks.showTokens;
370
+ if (chain_name in showTokensMap) {
371
+ return showTokensMap[chain_name];
372
+ }
373
+
374
+ return [];
375
+ }
376
+
365
377
  export const showTokenList = async (keystore_path: string) => {
366
378
  const network_path = path.resolve(keystore_path, 'network.json');
367
379
  const networks = JSON.parse(fs.readFileSync(network_path, 'utf8'));
@@ -381,16 +393,48 @@ export const showTokenList = async (keystore_path: string) => {
381
393
 
382
394
  const assets = JSON.parse(fs.readFileSync(assets_path, 'utf8'));
383
395
 
396
+ let options: any = [];
397
+ let default_options: any = [];
398
+ let option_info_map: any = {};
384
399
  for (let chain_id in assets) {
385
- console.log(`----- Networks (${network_settings_map[chain_id]}) -------`);
386
-
400
+ option_info_map[options.length] = {
401
+ name: network_settings_map[chain_id],
402
+ address: null
403
+ };
404
+ options.push(new inquirer.Separator(`----- Networks (${network_settings_map[chain_id]}) -------`));
405
+ let show_token_list: any = getShowTokenList(keystore_path, network_settings_map[chain_id]);
387
406
  let token_list_len = assets[chain_id].length;
388
407
  for (let i = 0; i < token_list_len; i++) {
389
408
  let token_info = assets[chain_id][i];
390
- console.log(`${i + 1}.${token_info.symbol} CA: ${chalk.green(`${formatAddr(token_info.address)}`)
409
+ if (show_token_list.includes(token_info.address)) {
410
+ default_options.push(`${i + 1}.${token_info.symbol.padEnd(6)} CA: ${chalk.green(`${formatAddr(token_info.address).padEnd(45)}`)
411
+ } Decimals: ${token_info.display_decimals ? token_info.display_decimals : "4"}`);
412
+ }
413
+
414
+ option_info_map[options.length] = {
415
+ name: network_settings_map[chain_id],
416
+ address: token_info.address
417
+ };
418
+ options.push(`${i + 1}.${token_info.symbol.padEnd(6)} CA: ${chalk.green(`${formatAddr(token_info.address).padEnd(45)}`)
391
419
  } Decimals: ${token_info.display_decimals ? token_info.display_decimals : "4"}`);
392
420
  }
393
421
  }
422
+
423
+ let selected_option = await selectCheckboxSomething(options, "Choose Tokens", default_options);
424
+ let show_token_list_map: any = {};
425
+ for (let i = 0; i < selected_option.length; i++) {
426
+ let _option = selected_option[i];
427
+ let option_index = options.indexOf(_option);
428
+ let option_info = option_info_map[option_index];
429
+ if (option_info.name in show_token_list_map) {
430
+ show_token_list_map[option_info.name].push(option_info.address);
431
+ } else {
432
+ show_token_list_map[option_info.name] = [option_info.address];
433
+ }
434
+ }
435
+
436
+ networks.showTokens = show_token_list_map;
437
+ fs.writeFileSync(network_path, JSON.stringify(networks, null, 2));
394
438
  }
395
439
 
396
440
  export const enhancedBalanceDisplay = async (keystore_path: string) => {
package/src/tools.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  const chalk = require('chalk');
2
2
  import { BigNumber } from 'bignumber.js';
3
3
  import inquirer from 'inquirer';
4
- import { addrValidator, chainAddressValidator, confirmSomething, editorSomething, getAccountChainType, hexValidator, inputSomething, numberValidator, selectSomething } from "./utils";
4
+ import { addrValidator, chainAddressValidator, confirmSomething, editorSomething, gasValidator, getAccountChainType, hexValidator, inputSomething, numberValidator, selectSomething } from "./utils";
5
5
  import { loginIn } from './account';
6
6
  import { getAccountBalance, getDefaultNetworkByAddress, getEvmNetworks, getNetworkInfoByName, getNetworkTypeByName } from './network';
7
7
  import { GasUtil } from './tx_gas_utils';
@@ -68,7 +68,7 @@ const sendEvmRawTransaction = async (chain_id: string, chain_rpc_url: string, ch
68
68
  const estimateGasPrice = await gasUtil.getGasPrice();
69
69
  const estimateGasPriceEthers = (new BigNumber(estimateGasPrice.toString())).dividedBy(1e9);
70
70
  const _estimateGasPrice = estimateGasPriceEthers.toFixed(4, BigNumber.ROUND_FLOOR);
71
- const inputGasPrice = await inputSomething(`Gas price (${_estimateGasPrice}), agree or enter`);
71
+ const inputGasPrice = await inputSomething(`Gas price (${_estimateGasPrice}), agree or enter`, gasValidator);
72
72
  let gasPrice = inputGasPrice ? inputGasPrice : _estimateGasPrice;
73
73
  gasPrice = (new BigNumber(gasPrice)).multipliedBy(1e9).toFixed(0);
74
74
 
@@ -104,24 +104,6 @@ const sendEvmRawTransaction = async (chain_id: string, chain_rpc_url: string, ch
104
104
  chain_symbols,
105
105
  18
106
106
  );
107
-
108
- // let option_select_value_list: any = [
109
- // '> Input value',
110
- // `> Total Balance (${total_balance} ${chain_symbols})`
111
- // ]
112
- // let selected_value = await selectSomething(option_select_value_list);
113
- // let value_id = option_select_value_list.indexOf(selected_value);
114
- // if (value_id === 0) {
115
- // let input_value = await inputSomething("value", numberValidator);
116
- // value = new BigNumber(input_value).multipliedBy(1e18).toFixed(0, BigNumber.ROUND_FLOOR);
117
- // } else if (value_id === 1) {
118
- // value = max_value;
119
- // }
120
-
121
- // if ((new BigNumber(value)).isGreaterThan(new BigNumber(max_value))) {
122
- // console.log(chalk.red("Not enough balance"));
123
- // return;
124
- // }
125
107
  } else {
126
108
  data = await editorSomething("data", hexValidator);
127
109
  value = await inputSomething("value", numberValidator);
@@ -236,7 +218,7 @@ export const sendSolanaRawTransaction = async (chain_id: string, chain_rpc_url:
236
218
  }
237
219
 
238
220
  const account_balance = new BigNumber(account_balance_result.output);
239
-
221
+
240
222
  const max_value = account_balance.minus(5000).minus(used_prioritization_fee);
241
223
  let total_balance = (new BigNumber(max_value)).dividedBy(1e9).toFixed(4, BigNumber.ROUND_FLOOR);
242
224
  if (new BigNumber(total_balance) < new BigNumber(0)) {
@@ -383,12 +365,30 @@ const sendEvmTokenTransaction = async (keystore_path: string, chain_rpc_url: str
383
365
  }
384
366
 
385
367
  const receipt = await inputSomething("Receipt", chainAddressValidator);
386
- const amount = await inputSomething("Amount", numberValidator);
368
+ const token_balance_result = await getTokenBalanceInEvm(account_address, token_address, chain_rpc_url);
369
+ if (!token_balance_result.status) {
370
+ console.log(chalk.red(token_balance_result.output));
371
+ return;
372
+ }
373
+ let amount: any;
374
+ for (let i = 0; i < 5; i++) {
375
+ let input_amount = await inputSomething("Amount", numberValidator);
376
+ if ((new BigNumber(input_amount).multipliedBy(Math.pow(10, Number(token_decimals)))).isLessThanOrEqualTo(new BigNumber(token_balance_result.output))) {
377
+ amount = input_amount;
378
+ break;
379
+ }
380
+ console.log(chalk.red("Not enough balance"));
381
+ }
382
+
383
+ if (!amount) {
384
+ return;
385
+ }
386
+
387
387
  const gasUtil = new GasUtil(chain_rpc_url);
388
388
  const estimateGasPrice = await gasUtil.getGasPrice();
389
389
  const estimateGasPriceEthers = (new BigNumber(estimateGasPrice.toString())).dividedBy(1e9);
390
390
  const _estimateGasPrice = estimateGasPriceEthers.toFixed(4, BigNumber.ROUND_FLOOR);
391
- const inputGasPrice = await inputSomething(`Gas price (${_estimateGasPrice}), agree or enter`);
391
+ const inputGasPrice = await inputSomething(`Gas price (${_estimateGasPrice}), agree or enter`, gasValidator);
392
392
  let gasPrice = inputGasPrice ? inputGasPrice : _estimateGasPrice;
393
393
  gasPrice = (new BigNumber(gasPrice)).multipliedBy(1e9).toFixed(0);
394
394
 
@@ -512,7 +512,24 @@ const sendSolanaTokenTransaction = async (keystore_path: string, chain_rpc_url:
512
512
  }
513
513
 
514
514
  const receipt = await inputSomething("Receipt", chainAddressValidator);
515
- const amount = await inputSomething("Amount", numberValidator);
515
+ const token_balance_result = await getTokenBalanceInEvm(account_address, token_address, chain_rpc_url);
516
+ if (!token_balance_result.status) {
517
+ console.log(chalk.red(token_balance_result.output));
518
+ return;
519
+ }
520
+ let amount: any;
521
+ for (let i = 0; i < 5; i++) {
522
+ let input_amount = await inputSomething("Amount", numberValidator);
523
+ if ((new BigNumber(input_amount).multipliedBy(Math.pow(10, Number(token_decimals)))).isLessThanOrEqualTo(new BigNumber(token_balance_result.output))) {
524
+ amount = input_amount;
525
+ break;
526
+ }
527
+ console.log(chalk.red("Not enough balance"));
528
+ }
529
+
530
+ if (!amount) {
531
+ return;
532
+ }
516
533
 
517
534
  let transfer_amount = new BigNumber(amount).multipliedBy(Math.pow(10, token_decimals)).toFixed(0);
518
535
  let from_token_account = getAssociatedTokenAddressSync(
package/src/utils.ts CHANGED
@@ -91,7 +91,7 @@ export const tagValidator = (tag: string) => {
91
91
 
92
92
  tag = tag.replace(/\s+/g, " ").trim();
93
93
 
94
- if (tag.length > 10) {
94
+ if (tag.length > 40) {
95
95
  return "Tag length should be less than 10";
96
96
  }
97
97
 
@@ -116,6 +116,13 @@ export const numberValidator = (value: string) => {
116
116
  return true;
117
117
  }
118
118
 
119
+ export const gasValidator = (value: string) => {
120
+ if (isNaN(Number(value)) || Number(value) > 1000) {
121
+ return "Value should be a number";
122
+ }
123
+ return true;
124
+ }
125
+
119
126
  export const passphraseValidator = (passphrase: string) => {
120
127
  passphrase = passphrase.replace(/\s+/g, " ");
121
128
  const mnemonic_word_list = passphrase.split(/\s+/g);
@@ -188,6 +195,25 @@ export const selectSomething = async (options: any[], message?: string, _default
188
195
  return inputText.toString()
189
196
  }
190
197
 
198
+ export const selectCheckboxSomething = async (options: any[], message?: string, _default?: any) => {
199
+ const questions = [
200
+ {
201
+ type: 'checkbox',
202
+ name: 'inputText',
203
+ message: message || 'Choose an option',
204
+ default: _default,
205
+ choices: options,
206
+ pageSize: 30,
207
+ filter: (val: string) => {
208
+ return val;
209
+ },
210
+ },
211
+ ]
212
+
213
+ const { inputText } = await inquirer.prompt(questions)
214
+ return inputText
215
+ }
216
+
191
217
  export const showIntroduction = () => {
192
218
  console.log(
193
219
  chalk.green(
@@ -598,6 +624,20 @@ export const listWithLazyBalanceLoading = async (
598
624
  show_extended_token
599
625
  );
600
626
 
627
+ let pad_index_map: any = {};
628
+
629
+ for (let account_address in batchBalanceOf) {
630
+ for (let token_name in batchBalanceOf[account_address]) {
631
+ let balance = batchBalanceOf[account_address][token_name];
632
+ let new_pad_index = balance.length + 1;
633
+ if (!pad_index_map[token_name]) {
634
+ pad_index_map[token_name] = new_pad_index;
635
+ } else if (pad_index_map[token_name] < new_pad_index) {
636
+ pad_index_map[token_name] = new_pad_index;
637
+ }
638
+ }
639
+ }
640
+
601
641
  let gas_token_name = network_info.nativeCurrency.symbol;
602
642
  for (let i = fromIndex; i < toIndex; i++) {
603
643
  let _index = i >= accounts.length / 2 ? i - accounts.length / 2 : i;
@@ -622,7 +662,7 @@ export const listWithLazyBalanceLoading = async (
622
662
  continue;
623
663
  }
624
664
 
625
- token_message += `${token_name}: ${batchBalanceOf[account_address][token_name]} `;
665
+ token_message += `${token_name}:${batchBalanceOf[account_address][token_name].padEnd(pad_index_map[token_name])} `;
626
666
  }
627
667
 
628
668
  let nameList = name.split('(');
@@ -653,6 +693,10 @@ export const updateStorage = (keystore_path: string) => {
653
693
  }
654
694
  }
655
695
 
696
+ if (!networks.showTokens) {
697
+ networks.showTokens = {};
698
+ }
699
+
656
700
  let account_list_len = networks.accounts.length;
657
701
  for (let i = 0; i < account_list_len; i++) {
658
702
  let account_label = networks.accounts[account_list_len - 1 - i];