@enclave-hq/wallet-sdk 1.2.0 → 1.2.2

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.
@@ -661,25 +661,69 @@ var MetaMaskAdapter = class extends BrowserWalletAdapter {
661
661
  throw new Error("Wallet client not initialized");
662
662
  }
663
663
  try {
664
- console.log("\u{1F50D} [MetaMask writeContract] params.gasPrice:", params.gasPrice, "type:", typeof params.gasPrice);
665
- let processedGasPrice;
666
- if (!params.gasPrice) {
667
- processedGasPrice = void 0;
668
- } else if (params.gasPrice === "auto") {
669
- processedGasPrice = void 0;
670
- } else {
671
- processedGasPrice = BigInt(params.gasPrice);
672
- }
673
- console.log("\u{1F50D} [MetaMask writeContract] processedGasPrice:", processedGasPrice);
674
- const txHash = await this.walletClient.writeContract({
664
+ console.log("\u{1F50D} [MetaMask writeContract] Gas params:", {
665
+ gasPrice: params.gasPrice,
666
+ maxFeePerGas: params.maxFeePerGas,
667
+ maxPriorityFeePerGas: params.maxPriorityFeePerGas
668
+ });
669
+ const txOptions = {
675
670
  address: params.address,
676
671
  abi: params.abi,
677
672
  functionName: params.functionName,
678
673
  ...params.args ? { args: params.args } : {},
679
674
  value: params.value ? BigInt(params.value) : void 0,
680
- gas: params.gas ? BigInt(params.gas) : void 0,
681
- gasPrice: processedGasPrice
682
- });
675
+ gas: params.gas ? BigInt(params.gas) : void 0
676
+ };
677
+ if (params.maxFeePerGas || params.maxPriorityFeePerGas) {
678
+ if (params.maxFeePerGas) {
679
+ txOptions.maxFeePerGas = BigInt(params.maxFeePerGas);
680
+ }
681
+ if (params.maxPriorityFeePerGas) {
682
+ txOptions.maxPriorityFeePerGas = BigInt(params.maxPriorityFeePerGas);
683
+ }
684
+ console.log("\u{1F50D} [MetaMask writeContract] Using EIP-1559 gas params");
685
+ } else if (params.gasPrice) {
686
+ if (params.gasPrice === "auto") {
687
+ console.log("\u{1F50D} [MetaMask writeContract] Auto gas price - letting viem decide");
688
+ } else {
689
+ txOptions.gasPrice = BigInt(params.gasPrice);
690
+ console.log("\u{1F50D} [MetaMask writeContract] Using legacy gasPrice");
691
+ }
692
+ } else {
693
+ console.log("\u{1F50D} [MetaMask writeContract] No gas params - fetching and setting reasonable gas fees");
694
+ if (this.publicClient) {
695
+ try {
696
+ const feesPerGas = await this.publicClient.estimateFeesPerGas().catch(() => null);
697
+ if (feesPerGas) {
698
+ const minPriorityFeeWei = BigInt(1e8);
699
+ const maxPriorityFeePerGas = feesPerGas.maxPriorityFeePerGas > minPriorityFeeWei ? feesPerGas.maxPriorityFeePerGas : minPriorityFeeWei;
700
+ const adjustedMaxFeePerGas = feesPerGas.maxFeePerGas > maxPriorityFeePerGas ? feesPerGas.maxFeePerGas : maxPriorityFeePerGas + BigInt(1e9);
701
+ txOptions.maxFeePerGas = adjustedMaxFeePerGas;
702
+ txOptions.maxPriorityFeePerGas = maxPriorityFeePerGas;
703
+ const maxFeePerGasGwei = Number(adjustedMaxFeePerGas) / 1e9;
704
+ const maxPriorityFeePerGasGwei = Number(maxPriorityFeePerGas) / 1e9;
705
+ console.log("\u{1F4B0} [MetaMask writeContract] Set gas fees (EIP-1559):", {
706
+ maxFeePerGas: `${maxFeePerGasGwei.toFixed(6)} Gwei`,
707
+ maxPriorityFeePerGas: `${maxPriorityFeePerGasGwei.toFixed(6)} Gwei`,
708
+ maxFeePerGasWei: adjustedMaxFeePerGas.toString(),
709
+ maxPriorityFeePerGasWei: maxPriorityFeePerGas.toString(),
710
+ note: "\u5DF2\u8BBE\u7F6E\u5408\u7406\u7684 gas \u8D39\u7528\uFF0C\u907F\u514D MetaMask \u4F7F\u7528\u9ED8\u8BA4\u503C"
711
+ });
712
+ } else {
713
+ const gasPrice = await this.publicClient.getGasPrice();
714
+ txOptions.gasPrice = gasPrice;
715
+ const gasPriceGwei = Number(gasPrice) / 1e9;
716
+ console.log("\u{1F4B0} [MetaMask writeContract] Set gas price (Legacy):", {
717
+ gasPrice: `${gasPriceGwei.toFixed(6)} Gwei`,
718
+ gasPriceWei: gasPrice.toString()
719
+ });
720
+ }
721
+ } catch (err) {
722
+ console.warn("\u26A0\uFE0F [MetaMask writeContract] Failed to estimate gas fees, letting viem auto-estimate:", err);
723
+ }
724
+ }
725
+ }
726
+ const txHash = await this.walletClient.writeContract(txOptions);
683
727
  return txHash;
684
728
  } catch (error) {
685
729
  if (error.code === 4001) {
@@ -866,16 +910,57 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
866
910
  await this.ensureAvailable();
867
911
  try {
868
912
  this.setState("connecting" /* CONNECTING */);
913
+ const w = window;
869
914
  const tronWeb = this.getTronWeb();
870
- const result = await tronWeb.request({
871
- method: "tron_requestAccounts"
872
- });
873
- if (!result || !result.code || result.code !== 200) {
874
- throw new ConnectionRejectedError(this.type);
915
+ if (tronWeb.ready) {
916
+ await tronWeb.ready;
917
+ }
918
+ let address = tronWeb.defaultAddress?.base58;
919
+ if (!address && tronWeb.defaultAddress?.hex && tronWeb.address && typeof tronWeb.address.fromHex === "function") {
920
+ try {
921
+ address = tronWeb.address.fromHex(tronWeb.defaultAddress.hex);
922
+ } catch (e) {
923
+ }
875
924
  }
876
- const address = tronWeb.defaultAddress?.base58;
877
925
  if (!address) {
878
- throw new Error("Failed to get Tron address");
926
+ for (let i = 0; i < 20; i++) {
927
+ await new Promise((resolve) => setTimeout(resolve, 100));
928
+ address = tronWeb.defaultAddress?.base58;
929
+ if (address) break;
930
+ }
931
+ }
932
+ if (!address) {
933
+ if (w.tronLink && typeof w.tronLink.request === "function") {
934
+ try {
935
+ const result = await w.tronLink.request({
936
+ method: "tron_requestAccounts"
937
+ });
938
+ if (!result || result.code !== 200) {
939
+ throw new ConnectionRejectedError(this.type);
940
+ }
941
+ } catch (error) {
942
+ if (error.code === 4001 || error.message?.includes("User rejected") || error.message?.includes("rejected")) {
943
+ throw new ConnectionRejectedError(this.type);
944
+ }
945
+ }
946
+ }
947
+ address = tronWeb.defaultAddress?.base58;
948
+ if (!address && tronWeb.defaultAddress?.hex && tronWeb.address && typeof tronWeb.address.fromHex === "function") {
949
+ try {
950
+ address = tronWeb.address.fromHex(tronWeb.defaultAddress.hex);
951
+ } catch (e) {
952
+ }
953
+ }
954
+ if (!address) {
955
+ for (let i = 0; i < 20; i++) {
956
+ await new Promise((resolve) => setTimeout(resolve, 100));
957
+ address = tronWeb.defaultAddress?.base58;
958
+ if (address) break;
959
+ }
960
+ }
961
+ }
962
+ if (!address) {
963
+ throw new Error("Failed to get Tron address. Please make sure your wallet is unlocked and try again.");
879
964
  }
880
965
  const tronChainId = chainId || _TronLinkAdapter.TRON_MAINNET_CHAIN_ID;
881
966
  const account = {
@@ -951,17 +1036,41 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
951
1036
  }
952
1037
  /**
953
1038
  * 读取合约
1039
+ * 参考 webserver 的实现,使用 TronWeb 合约实例的标准 call() 方法
954
1040
  */
955
1041
  async readContract(params) {
956
1042
  this.ensureConnected();
957
1043
  try {
958
1044
  const tronWeb = this.getTronWeb();
959
- const contract = await tronWeb.contract(params.abi, params.address);
960
- const result = await contract[params.functionName](...params.args || []).call();
961
- return result;
1045
+ if (!this.currentAccount) {
1046
+ throw new Error("No account connected");
1047
+ }
1048
+ try {
1049
+ const contract = await tronWeb.contract(params.abi, params.address);
1050
+ const method = contract[params.functionName];
1051
+ if (!method || typeof method !== "function") {
1052
+ throw new Error(`Function ${params.functionName} not found in contract ABI`);
1053
+ }
1054
+ const result = await method(...params.args || []).call();
1055
+ return result;
1056
+ } catch (method1Error) {
1057
+ console.warn("\u26A0\uFE0F [\u65B9\u6CD51] TronWeb\u6807\u51C6\u65B9\u6CD5\u5931\u8D25\uFF0C\u5C1D\u8BD5\u65B9\u6CD52:", method1Error.message);
1058
+ try {
1059
+ const contract2 = await tronWeb.contract().at(params.address);
1060
+ const method2 = contract2[params.functionName];
1061
+ if (!method2 || typeof method2 !== "function") {
1062
+ throw new Error(`Function ${params.functionName} not found in contract`);
1063
+ }
1064
+ const result = await method2(...params.args || []).call();
1065
+ return result;
1066
+ } catch (method2Error) {
1067
+ console.error("\u26A0\uFE0F [\u65B9\u6CD52] \u4E5F\u5931\u8D25:", method2Error.message);
1068
+ throw method1Error;
1069
+ }
1070
+ }
962
1071
  } catch (error) {
963
1072
  console.error("Read contract error:", error);
964
- throw new Error(`Failed to read contract: ${error.message}`);
1073
+ throw new Error(`Failed to read contract: ${error.message || "Unknown error"}`);
965
1074
  }
966
1075
  }
967
1076
  /**
@@ -1006,26 +1115,38 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1006
1115
  }));
1007
1116
  console.log("[TronLink] Transaction options:", options);
1008
1117
  console.log("[TronLink] Parameters:", parameter);
1009
- const transaction = await tronWeb.transactionBuilder.triggerSmartContract(
1118
+ const functionSelector = params.functionName + "(" + functionAbi.inputs.map((i) => i.type).join(",") + ")";
1119
+ console.log("[TronLink] Function selector:", functionSelector);
1120
+ console.log("[TronLink] Transaction options:", options);
1121
+ console.log("[TronLink] Parameters:", parameter);
1122
+ const tx = await tronWeb.transactionBuilder.triggerSmartContract(
1010
1123
  params.address,
1011
- params.functionName + "(" + functionAbi.inputs.map((i) => i.type).join(",") + ")",
1124
+ functionSelector,
1012
1125
  options,
1013
1126
  parameter,
1014
1127
  this.currentAccount.nativeAddress
1015
1128
  );
1016
- console.log("[TronLink] Transaction built:", transaction);
1017
- if (!transaction || !transaction.transaction) {
1129
+ console.log("[TronLink] Transaction built:", tx);
1130
+ if (!tx || !tx.transaction) {
1018
1131
  throw new Error("Failed to build transaction");
1019
1132
  }
1020
- const signedTx = await tronWeb.trx.sign(transaction.transaction);
1133
+ console.log("[TronLink] Requesting user signature...");
1134
+ const signedTx = await tronWeb.trx.sign(tx.transaction);
1135
+ console.log("[TronLink] Transaction signed:", signedTx);
1136
+ const txID = signedTx.txID;
1137
+ console.log("[TronLink] Transaction hash (txID):", txID);
1138
+ console.log("[TronLink] Broadcasting transaction...");
1021
1139
  const broadcast = await tronWeb.trx.sendRawTransaction(signedTx);
1022
1140
  console.log("[TronLink] Broadcast result:", broadcast);
1023
- if (!broadcast.result) {
1024
- throw new Error(broadcast.message || "Transaction broadcast failed");
1141
+ if (broadcast && broadcast.result === true) {
1142
+ return txID || broadcast.txid || "";
1143
+ } else {
1144
+ if (txID) {
1145
+ console.warn("[TronLink] Broadcast returned false but txID exists:", txID);
1146
+ return txID;
1147
+ }
1148
+ throw new Error(broadcast?.message || "Transaction broadcast failed");
1025
1149
  }
1026
- const txHash = broadcast.txid || broadcast.transaction?.txID;
1027
- console.log("[TronLink] Transaction hash:", txHash);
1028
- return txHash || "";
1029
1150
  } catch (error) {
1030
1151
  console.error("Write contract error:", error);
1031
1152
  if (error.message?.includes("User rejected") || error.message?.includes("Confirmation declined")) {