wative 1.0.8 → 1.0.10

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/assets.ts CHANGED
@@ -2,10 +2,11 @@ const chalk = require('chalk');
2
2
  import * as path from "path";
3
3
  import * as fs from 'fs';
4
4
  import { BigNumber } from 'bignumber.js';
5
- import { isEvmAddress, isSolanaAddress } from "./chain";
5
+ import { getChainType, isEvmAddress, isSolanaAddress } from "./chain";
6
6
  import { getNetworkInfoByName, getNetworkTypeByName } from "./network";
7
- import { chainAddressValidator, checkFileExistence, inputSomething, selectSomething } from "./utils";
8
- import { getTokenBalanceInEvm, getTokenBalanceInSolana, getTokenInfoInEvm, getTokenInfoInSolana } from './web3';
7
+ import { chainAddressValidator, checkFileExistence, inputSomething, numberValidator, selectSomething } from "./utils";
8
+ import { deleteTokenBalance, getTokenBalance, getTokenBalanceInEvm, getTokenBalanceInSolana, getTokenInfoInEvm, getTokenInfoInSolana, updateBalance } from './web3';
9
+ import inquirer from "inquirer";
9
10
 
10
11
 
11
12
  // symbol; name; address; decimals
@@ -47,8 +48,8 @@ export const showAssetsDetail = async (
47
48
  default_network: string,
48
49
  account_address: string
49
50
  ) => {
50
- const network_type = getNetworkTypeByName(keystore_path, default_network);
51
51
  const network_info = getNetworkInfoByName(keystore_path, default_network);
52
+ const network_type = getChainType(network_info.chainId);
52
53
  const asset_list = await getAssetList(keystore_path, network_info.chainId.toString());
53
54
  if (!asset_list) {
54
55
  console.log(chalk.red("No asset found"));
@@ -59,7 +60,8 @@ export const showAssetsDetail = async (
59
60
  account_address,
60
61
  asset_list,
61
62
  network_info.rpcUrl,
62
- network_type
63
+ network_type,
64
+ network_info.chainId
63
65
  );
64
66
 
65
67
  let asset_id = 1;
@@ -86,7 +88,8 @@ export const saveAsset = async (
86
88
  asset_address: string,
87
89
  symbol: string,
88
90
  name: string,
89
- decimals: number
91
+ decimals: number,
92
+ display_decimals: number
90
93
  ) => {
91
94
  let json_data_path = path.join(keystore_path, `assets.json`);
92
95
  let assets_mapping: any = {};
@@ -97,23 +100,103 @@ export const saveAsset = async (
97
100
  if (assets_mapping[chain_id] === undefined) {
98
101
  assets_mapping[chain_id] = [];
99
102
  }
103
+
104
+ for (let i = 0; i < assets_mapping[chain_id].length; i++) {
105
+ if (assets_mapping[chain_id][i].address === asset_address) {
106
+ assets_mapping[chain_id][i] = {
107
+ address: asset_address,
108
+ symbol,
109
+ name,
110
+ decimals,
111
+ display_decimals
112
+ };
113
+
114
+ fs.writeFileSync(json_data_path, JSON.stringify(assets_mapping, null, 4));
115
+ return;
116
+ }
117
+ }
118
+
100
119
  assets_mapping[chain_id].push({
101
120
  address: asset_address,
102
121
  symbol,
103
122
  name,
104
- decimals
123
+ decimals,
124
+ display_decimals
105
125
  });
106
126
  fs.writeFileSync(json_data_path, JSON.stringify(assets_mapping, null, 4));
107
127
  }
108
128
 
129
+ export const addToken = async (
130
+ keystore_path: string,
131
+ ) => {
132
+ const network_path = path.resolve(keystore_path, 'network.json');
133
+ const networks = JSON.parse(fs.readFileSync(network_path, 'utf8'));
134
+
135
+ let options: any = [];
136
+ let network_list = networks.networks;
137
+ for (let i = 0; i < network_list.length; i++) {
138
+ options.push(`${i + 1}. ${network_list[i].name}`);
139
+ }
140
+
141
+ let selected_option = await selectSomething(options);
142
+ let network_name = selected_option.split(".")[1].trim();
143
+ const network_type = getNetworkTypeByName(keystore_path, network_name);
144
+ const network_info = getNetworkInfoByName(keystore_path, network_name);
145
+
146
+ const token_address = await inputSomething("Enter contract address", chainAddressValidator);
147
+
148
+ let token_info_result: any;
149
+ if (network_type === "evm") {
150
+ if (!isEvmAddress(token_address)) {
151
+ console.log(chalk.red("evm address format error"));
152
+ return;
153
+ }
154
+ token_info_result = await getTokenInfoInEvm(token_address, network_info.rpcUrl);
155
+ } else {
156
+ if (!isSolanaAddress(token_address)) {
157
+ console.log(chalk.red("solana address format error"));
158
+ return;
159
+ }
160
+ token_info_result = await getTokenInfoInSolana(token_address, network_info.rpcUrl);
161
+ }
162
+ if (!token_info_result.status) {
163
+ console.log(chalk.red(token_info_result.output));
164
+ return;
165
+ }
166
+
167
+ if (!token_info_result.output.name) {
168
+ token_info_result.output.name = await inputSomething("Please input token name");
169
+ }
170
+ if (!token_info_result.output.symbol) {
171
+ token_info_result.output.symbol = await inputSomething("Please input token symbol");
172
+ }
173
+
174
+ console.log(
175
+ chalk.green(`Token ${token_info_result.output.name} loaded successfully.`)
176
+ );
177
+
178
+ let display_decimals = await inputSomething("Enter decimals for display", numberValidator);
179
+ display_decimals = parseInt(display_decimals);
180
+ await saveAsset(
181
+ keystore_path,
182
+ network_info.chainId.toString(),
183
+ token_address,
184
+ token_info_result.output.symbol,
185
+ token_info_result.output.name,
186
+ token_info_result.output.decimals,
187
+ display_decimals
188
+ );
189
+ }
190
+
109
191
  export const addAsset = async (
110
192
  keystore_path: string,
111
193
  default_network: string
112
194
  ) => {
113
- const token_address = await inputSomething("Please input Token address", chainAddressValidator);
114
195
  const network_type = getNetworkTypeByName(keystore_path, default_network);
115
196
  const network_info = getNetworkInfoByName(keystore_path, default_network);
116
197
 
198
+ const token_address = await inputSomething("Enter contract address", chainAddressValidator);
199
+
117
200
  let token_info_result: any;
118
201
  if (network_type === "evm") {
119
202
  if (!isEvmAddress(token_address)) {
@@ -132,22 +215,28 @@ export const addAsset = async (
132
215
  console.log(chalk.red(token_info_result.output));
133
216
  return;
134
217
  }
218
+
135
219
  if (!token_info_result.output.name) {
136
220
  token_info_result.output.name = await inputSomething("Please input token name");
137
221
  }
138
222
  if (!token_info_result.output.symbol) {
139
223
  token_info_result.output.symbol = await inputSomething("Please input token symbol");
140
224
  }
225
+
226
+ console.log(
227
+ chalk.green(`Token ${token_info_result.output.name} loaded successfully.`)
228
+ );
229
+
230
+ let display_decimals = await inputSomething("Enter decimals for display", numberValidator);
231
+ display_decimals = parseInt(display_decimals);
141
232
  await saveAsset(
142
233
  keystore_path,
143
234
  network_info.chainId.toString(),
144
235
  token_address,
145
236
  token_info_result.output.symbol,
146
237
  token_info_result.output.name,
147
- token_info_result.output.decimals
148
- );
149
- console.log(
150
- chalk.green("Asset added successfully")
238
+ token_info_result.output.decimals,
239
+ display_decimals
151
240
  );
152
241
  }
153
242
 
@@ -157,22 +246,23 @@ export const showAssets = async (
157
246
  default_network: string
158
247
  ) => {
159
248
  const options_assets_list = [
160
- "> Show Assets",
161
- "> Add Asset",
162
- "> Back"
249
+ '> Show Assets',
250
+ '> Add Asset',
251
+ new inquirer.Separator('----------------------------------'),
252
+ '> Back'
163
253
  ];
164
254
 
165
255
  const selected_option = await selectSomething(options_assets_list);
166
256
  switch (selected_option) {
167
- case "> Show Assets": {
257
+ case '> Show Assets': {
168
258
  await showAssetsDetail(keystore_path, default_network, account_address);
169
259
  break;
170
260
  }
171
- case "> Add Asset": {
261
+ case '> Add Asset': {
172
262
  await addAsset(keystore_path, default_network);
173
263
  break;
174
264
  }
175
- case "> Back": {
265
+ case '> Back': {
176
266
  return;
177
267
  }
178
268
  }
@@ -184,7 +274,8 @@ export const batchGetTokenBalance = async (
184
274
  account_address: string,
185
275
  asset_info_list: any,
186
276
  rpc_url: string,
187
- chain_type: string
277
+ chain_type: string,
278
+ chain_id: string
188
279
  ) => {
189
280
  let asset_list_len = asset_info_list.length;
190
281
  let promise_list: any = [];
@@ -216,6 +307,64 @@ export const batchGetTokenBalance = async (
216
307
  symbol: asset_info.symbol,
217
308
  balance: asset_balance
218
309
  });
310
+ deleteTokenBalance(
311
+ chain_id,
312
+ account_address,
313
+ asset_info.address
314
+ );
219
315
  }
316
+
317
+ deleteTokenBalance(
318
+ chain_id,
319
+ account_address,
320
+ "0"
321
+ )
220
322
  return asset_balance_info_list;
323
+ }
324
+
325
+ export const getBatchAccountBalance = async (
326
+ keystore_path: string,
327
+ account_address_list: string[],
328
+ asset_info_list: any,
329
+ rpc_url: string,
330
+ network_name: string,
331
+ show_gas_token: boolean,
332
+ show_extended_token: boolean,
333
+ ) => {
334
+ let asset_info_list_len = asset_info_list.length;
335
+
336
+ let network_info = getNetworkInfoByName(keystore_path, network_name);
337
+ let chain_type = getChainType(network_info.chainId.toString());
338
+ let is_evm = chain_type === "evm";
339
+ if (show_gas_token) {
340
+ await updateBalance(
341
+ account_address_list,
342
+ "0",
343
+ network_info.chainId.toString(),
344
+ rpc_url,
345
+ is_evm
346
+ );
347
+ }
348
+
349
+ if (show_extended_token) {
350
+ for (let i = 0; i < asset_info_list_len; i++) {
351
+ let asset_info = asset_info_list[i];
352
+ let token_address = asset_info.address;
353
+ await updateBalance(
354
+ account_address_list,
355
+ token_address,
356
+ network_info.chainId.toString(),
357
+ rpc_url,
358
+ is_evm
359
+ );
360
+ }
361
+ }
362
+ let batchAccountBalanceOf: any = getTokenBalance(
363
+ network_info.chainId.toString(),
364
+ account_address_list,
365
+ asset_info_list,
366
+ network_info
367
+ );
368
+
369
+ return batchAccountBalanceOf;
221
370
  }
package/src/home_page.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { inputSomething, selectSomething } from "./utils";
2
- import { showAvailableNetworks, showNetwork } from "./network";
1
+ import { selectSomething } from "./utils";
2
+ import { showNetwork } from "./network";
3
3
  import { accountSetting } from "./account";
4
4
  import { showTools } from "./tools";
5
5
  const { WativeCore } = require("wative-core");
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as fs from "fs";
2
2
  import * as path from "path";
3
- import { showIntroduction, getKeystorePath } from "./utils";
3
+ import { showIntroduction, getKeystorePath, updateStorage } from "./utils";
4
4
  import { createNetwork } from "./network";
5
5
  import { showHomePage } from "./home_page";
6
6
 
@@ -16,6 +16,7 @@ const main = async () => {
16
16
 
17
17
  const keystore_path = await getKeystorePath();
18
18
  await createNetwork(keystore_path);
19
+ updateStorage(keystore_path);
19
20
 
20
21
  const network_path = path.resolve(keystore_path, 'network.json');
21
22
  const networks = JSON.parse(fs.readFileSync(network_path, 'utf8'));