@weblock-wallet/sdk 0.1.56 → 0.1.57

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/dist/index.js CHANGED
@@ -104474,14 +104474,9 @@ var WalletService = class {
104474
104474
  });
104475
104475
  const network = await this.networkService.getCurrentNetwork();
104476
104476
  const decimals = network?.decimals || DECIMALS.ETH;
104477
- const formatted = TokenAmount.format(response.result, decimals);
104478
104477
  return {
104479
104478
  raw: response.result,
104480
- // aliases (helpful for apps that expect numeric fields)
104481
- balanceWei: response.result,
104482
- result: response.result,
104483
- formatted,
104484
- balance: formatted,
104479
+ formatted: TokenAmount.format(response.result, decimals),
104485
104480
  decimals,
104486
104481
  symbol: network?.symbol || "ETH"
104487
104482
  };
@@ -104624,6 +104619,18 @@ var WalletService = class {
104624
104619
  if (!from) {
104625
104620
  throw new SDKError("Wallet not found", "WALLET_NOT_FOUND" /* WALLET_NOT_FOUND */);
104626
104621
  }
104622
+ const toHexQuantity = (v5) => {
104623
+ if (v5 === void 0 || v5 === null) return "0x0";
104624
+ const s5 = String(v5).trim();
104625
+ if (!s5) return "0x0";
104626
+ if (s5.startsWith("0x") || s5.startsWith("0X")) return s5;
104627
+ try {
104628
+ const bi = BigInt(s5);
104629
+ return "0x" + bi.toString(16);
104630
+ } catch {
104631
+ return s5;
104632
+ }
104633
+ };
104627
104634
  const [share1, share2] = await Promise.all([
104628
104635
  this.walletClient.getWallet().then((wallet2) => wallet2.share1),
104629
104636
  LocalForage.get(STORAGE_KEYS.share2(this.orgHost))
@@ -104638,6 +104645,20 @@ var WalletService = class {
104638
104645
  const wallet = new Wallet(privateKey);
104639
104646
  const nonce = params.nonce ?? await this.getTransactionCount(from, params.chainId);
104640
104647
  const gasPrice = params.gasPrice ?? await this.getGasPrice(params.chainId);
104648
+ let gasLimit = params.gasLimit;
104649
+ if (!gasLimit) {
104650
+ const est = await this.estimateGas(
104651
+ {
104652
+ from,
104653
+ to: params.to,
104654
+ value: toHexQuantity(params.value),
104655
+ data: params.data || "0x"
104656
+ },
104657
+ params.chainId
104658
+ );
104659
+ const buffered = Math.max(21e3, Math.ceil(est * 1.2));
104660
+ gasLimit = "0x" + buffered.toString(16);
104661
+ }
104641
104662
  const signedTx = await wallet.signTransaction({
104642
104663
  to: params.to,
104643
104664
  value: params.value,
@@ -104645,7 +104666,7 @@ var WalletService = class {
104645
104666
  chainId: params.chainId,
104646
104667
  nonce,
104647
104668
  gasPrice,
104648
- gasLimit: params.gasLimit
104669
+ gasLimit
104649
104670
  });
104650
104671
  return this.sendRawTransaction(signedTx, params.chainId);
104651
104672
  } catch (error) {
@@ -104704,11 +104725,34 @@ var NetworkService = class {
104704
104725
  async switchNetwork(networkId) {
104705
104726
  try {
104706
104727
  const networks = await this.getRegisteredNetworks();
104707
- const network = networks.find((n5) => n5.id === networkId);
104728
+ const key = String(networkId || "").trim();
104729
+ let network = networks.find((n5) => n5.id === key);
104730
+ if (!network) {
104731
+ const lower = key.toLowerCase();
104732
+ network = networks.find((n5) => (n5.name || "").toLowerCase() === lower);
104733
+ }
104734
+ if (!network && /^\d+$/.test(key)) {
104735
+ const chainId = Number(key);
104736
+ network = networks.find((n5) => n5.chainId === chainId);
104737
+ }
104738
+ if (!network) {
104739
+ const alias = key.toLowerCase();
104740
+ const aliasChainId = {
104741
+ fuji: 43113,
104742
+ avalanchefuji: 43113,
104743
+ "avalanche-fuji": 43113,
104744
+ avaxfuji: 43113,
104745
+ "avax-fuji": 43113
104746
+ };
104747
+ const chainId = aliasChainId[alias];
104748
+ if (chainId) {
104749
+ network = networks.find((n5) => n5.chainId === chainId);
104750
+ }
104751
+ }
104708
104752
  if (!network) {
104709
104753
  throw new SDKError("Network not found", "INVALID_NETWORK" /* INVALID_NETWORK */);
104710
104754
  }
104711
- await LocalForage.save(`${this.orgHost}:currentNetwork`, networkId);
104755
+ await LocalForage.save(`${this.orgHost}:currentNetwork`, network.id);
104712
104756
  } catch (error) {
104713
104757
  if (error instanceof SDKError) throw error;
104714
104758
  throw new SDKError("Failed to switch network", "NETWORK_ERROR" /* NETWORK_ERROR */);