@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.
package/dist/index.mjs CHANGED
@@ -752,25 +752,69 @@ var MetaMaskAdapter = class extends BrowserWalletAdapter {
752
752
  throw new Error("Wallet client not initialized");
753
753
  }
754
754
  try {
755
- console.log("\u{1F50D} [MetaMask writeContract] params.gasPrice:", params.gasPrice, "type:", typeof params.gasPrice);
756
- let processedGasPrice;
757
- if (!params.gasPrice) {
758
- processedGasPrice = void 0;
759
- } else if (params.gasPrice === "auto") {
760
- processedGasPrice = void 0;
761
- } else {
762
- processedGasPrice = BigInt(params.gasPrice);
763
- }
764
- console.log("\u{1F50D} [MetaMask writeContract] processedGasPrice:", processedGasPrice);
765
- const txHash = await this.walletClient.writeContract({
755
+ console.log("\u{1F50D} [MetaMask writeContract] Gas params:", {
756
+ gasPrice: params.gasPrice,
757
+ maxFeePerGas: params.maxFeePerGas,
758
+ maxPriorityFeePerGas: params.maxPriorityFeePerGas
759
+ });
760
+ const txOptions = {
766
761
  address: params.address,
767
762
  abi: params.abi,
768
763
  functionName: params.functionName,
769
764
  ...params.args ? { args: params.args } : {},
770
765
  value: params.value ? BigInt(params.value) : void 0,
771
- gas: params.gas ? BigInt(params.gas) : void 0,
772
- gasPrice: processedGasPrice
773
- });
766
+ gas: params.gas ? BigInt(params.gas) : void 0
767
+ };
768
+ if (params.maxFeePerGas || params.maxPriorityFeePerGas) {
769
+ if (params.maxFeePerGas) {
770
+ txOptions.maxFeePerGas = BigInt(params.maxFeePerGas);
771
+ }
772
+ if (params.maxPriorityFeePerGas) {
773
+ txOptions.maxPriorityFeePerGas = BigInt(params.maxPriorityFeePerGas);
774
+ }
775
+ console.log("\u{1F50D} [MetaMask writeContract] Using EIP-1559 gas params");
776
+ } else if (params.gasPrice) {
777
+ if (params.gasPrice === "auto") {
778
+ console.log("\u{1F50D} [MetaMask writeContract] Auto gas price - letting viem decide");
779
+ } else {
780
+ txOptions.gasPrice = BigInt(params.gasPrice);
781
+ console.log("\u{1F50D} [MetaMask writeContract] Using legacy gasPrice");
782
+ }
783
+ } else {
784
+ console.log("\u{1F50D} [MetaMask writeContract] No gas params - fetching and setting reasonable gas fees");
785
+ if (this.publicClient) {
786
+ try {
787
+ const feesPerGas = await this.publicClient.estimateFeesPerGas().catch(() => null);
788
+ if (feesPerGas) {
789
+ const minPriorityFeeWei = BigInt(1e8);
790
+ const maxPriorityFeePerGas = feesPerGas.maxPriorityFeePerGas > minPriorityFeeWei ? feesPerGas.maxPriorityFeePerGas : minPriorityFeeWei;
791
+ const adjustedMaxFeePerGas = feesPerGas.maxFeePerGas > maxPriorityFeePerGas ? feesPerGas.maxFeePerGas : maxPriorityFeePerGas + BigInt(1e9);
792
+ txOptions.maxFeePerGas = adjustedMaxFeePerGas;
793
+ txOptions.maxPriorityFeePerGas = maxPriorityFeePerGas;
794
+ const maxFeePerGasGwei = Number(adjustedMaxFeePerGas) / 1e9;
795
+ const maxPriorityFeePerGasGwei = Number(maxPriorityFeePerGas) / 1e9;
796
+ console.log("\u{1F4B0} [MetaMask writeContract] Set gas fees (EIP-1559):", {
797
+ maxFeePerGas: `${maxFeePerGasGwei.toFixed(6)} Gwei`,
798
+ maxPriorityFeePerGas: `${maxPriorityFeePerGasGwei.toFixed(6)} Gwei`,
799
+ maxFeePerGasWei: adjustedMaxFeePerGas.toString(),
800
+ maxPriorityFeePerGasWei: maxPriorityFeePerGas.toString(),
801
+ note: "\u5DF2\u8BBE\u7F6E\u5408\u7406\u7684 gas \u8D39\u7528\uFF0C\u907F\u514D MetaMask \u4F7F\u7528\u9ED8\u8BA4\u503C"
802
+ });
803
+ } else {
804
+ const gasPrice = await this.publicClient.getGasPrice();
805
+ txOptions.gasPrice = gasPrice;
806
+ const gasPriceGwei = Number(gasPrice) / 1e9;
807
+ console.log("\u{1F4B0} [MetaMask writeContract] Set gas price (Legacy):", {
808
+ gasPrice: `${gasPriceGwei.toFixed(6)} Gwei`,
809
+ gasPriceWei: gasPrice.toString()
810
+ });
811
+ }
812
+ } catch (err) {
813
+ console.warn("\u26A0\uFE0F [MetaMask writeContract] Failed to estimate gas fees, letting viem auto-estimate:", err);
814
+ }
815
+ }
816
+ }
817
+ const txHash = await this.walletClient.writeContract(txOptions);
774
818
  return txHash;
775
819
  } catch (error) {
776
820
  if (error.code === 4001) {
@@ -957,16 +1001,57 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
957
1001
  await this.ensureAvailable();
958
1002
  try {
959
1003
  this.setState("connecting" /* CONNECTING */);
1004
+ const w = window;
960
1005
  const tronWeb = this.getTronWeb();
961
- const result = await tronWeb.request({
962
- method: "tron_requestAccounts"
963
- });
964
- if (!result || !result.code || result.code !== 200) {
965
- throw new ConnectionRejectedError(this.type);
1006
+ if (tronWeb.ready) {
1007
+ await tronWeb.ready;
1008
+ }
1009
+ let address = tronWeb.defaultAddress?.base58;
1010
+ if (!address && tronWeb.defaultAddress?.hex && tronWeb.address && typeof tronWeb.address.fromHex === "function") {
1011
+ try {
1012
+ address = tronWeb.address.fromHex(tronWeb.defaultAddress.hex);
1013
+ } catch (e) {
1014
+ }
966
1015
  }
967
- const address = tronWeb.defaultAddress?.base58;
968
1016
  if (!address) {
969
- throw new Error("Failed to get Tron address");
1017
+ for (let i = 0; i < 20; i++) {
1018
+ await new Promise((resolve) => setTimeout(resolve, 100));
1019
+ address = tronWeb.defaultAddress?.base58;
1020
+ if (address) break;
1021
+ }
1022
+ }
1023
+ if (!address) {
1024
+ if (w.tronLink && typeof w.tronLink.request === "function") {
1025
+ try {
1026
+ const result = await w.tronLink.request({
1027
+ method: "tron_requestAccounts"
1028
+ });
1029
+ if (!result || result.code !== 200) {
1030
+ throw new ConnectionRejectedError(this.type);
1031
+ }
1032
+ } catch (error) {
1033
+ if (error.code === 4001 || error.message?.includes("User rejected") || error.message?.includes("rejected")) {
1034
+ throw new ConnectionRejectedError(this.type);
1035
+ }
1036
+ }
1037
+ }
1038
+ address = tronWeb.defaultAddress?.base58;
1039
+ if (!address && tronWeb.defaultAddress?.hex && tronWeb.address && typeof tronWeb.address.fromHex === "function") {
1040
+ try {
1041
+ address = tronWeb.address.fromHex(tronWeb.defaultAddress.hex);
1042
+ } catch (e) {
1043
+ }
1044
+ }
1045
+ if (!address) {
1046
+ for (let i = 0; i < 20; i++) {
1047
+ await new Promise((resolve) => setTimeout(resolve, 100));
1048
+ address = tronWeb.defaultAddress?.base58;
1049
+ if (address) break;
1050
+ }
1051
+ }
1052
+ }
1053
+ if (!address) {
1054
+ throw new Error("Failed to get Tron address. Please make sure your wallet is unlocked and try again.");
970
1055
  }
971
1056
  const tronChainId = chainId || _TronLinkAdapter.TRON_MAINNET_CHAIN_ID;
972
1057
  const account = {
@@ -1042,17 +1127,41 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1042
1127
  }
1043
1128
  /**
1044
1129
  * 读取合约
1130
+ * 参考 webserver 的实现,使用 TronWeb 合约实例的标准 call() 方法
1045
1131
  */
1046
1132
  async readContract(params) {
1047
1133
  this.ensureConnected();
1048
1134
  try {
1049
1135
  const tronWeb = this.getTronWeb();
1050
- const contract = await tronWeb.contract(params.abi, params.address);
1051
- const result = await contract[params.functionName](...params.args || []).call();
1052
- return result;
1136
+ if (!this.currentAccount) {
1137
+ throw new Error("No account connected");
1138
+ }
1139
+ try {
1140
+ const contract = await tronWeb.contract(params.abi, params.address);
1141
+ const method = contract[params.functionName];
1142
+ if (!method || typeof method !== "function") {
1143
+ throw new Error(`Function ${params.functionName} not found in contract ABI`);
1144
+ }
1145
+ const result = await method(...params.args || []).call();
1146
+ return result;
1147
+ } catch (method1Error) {
1148
+ console.warn("\u26A0\uFE0F [\u65B9\u6CD51] TronWeb\u6807\u51C6\u65B9\u6CD5\u5931\u8D25\uFF0C\u5C1D\u8BD5\u65B9\u6CD52:", method1Error.message);
1149
+ try {
1150
+ const contract2 = await tronWeb.contract().at(params.address);
1151
+ const method2 = contract2[params.functionName];
1152
+ if (!method2 || typeof method2 !== "function") {
1153
+ throw new Error(`Function ${params.functionName} not found in contract`);
1154
+ }
1155
+ const result = await method2(...params.args || []).call();
1156
+ return result;
1157
+ } catch (method2Error) {
1158
+ console.error("\u26A0\uFE0F [\u65B9\u6CD52] \u4E5F\u5931\u8D25:", method2Error.message);
1159
+ throw method1Error;
1160
+ }
1161
+ }
1053
1162
  } catch (error) {
1054
1163
  console.error("Read contract error:", error);
1055
- throw new Error(`Failed to read contract: ${error.message}`);
1164
+ throw new Error(`Failed to read contract: ${error.message || "Unknown error"}`);
1056
1165
  }
1057
1166
  }
1058
1167
  /**
@@ -1097,26 +1206,38 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1097
1206
  }));
1098
1207
  console.log("[TronLink] Transaction options:", options);
1099
1208
  console.log("[TronLink] Parameters:", parameter);
1100
- const transaction = await tronWeb.transactionBuilder.triggerSmartContract(
1209
+ const functionSelector = params.functionName + "(" + functionAbi.inputs.map((i) => i.type).join(",") + ")";
1210
+ console.log("[TronLink] Function selector:", functionSelector);
1211
+ console.log("[TronLink] Transaction options:", options);
1212
+ console.log("[TronLink] Parameters:", parameter);
1213
+ const tx = await tronWeb.transactionBuilder.triggerSmartContract(
1101
1214
  params.address,
1102
- params.functionName + "(" + functionAbi.inputs.map((i) => i.type).join(",") + ")",
1215
+ functionSelector,
1103
1216
  options,
1104
1217
  parameter,
1105
1218
  this.currentAccount.nativeAddress
1106
1219
  );
1107
- console.log("[TronLink] Transaction built:", transaction);
1108
- if (!transaction || !transaction.transaction) {
1220
+ console.log("[TronLink] Transaction built:", tx);
1221
+ if (!tx || !tx.transaction) {
1109
1222
  throw new Error("Failed to build transaction");
1110
1223
  }
1111
- const signedTx = await tronWeb.trx.sign(transaction.transaction);
1224
+ console.log("[TronLink] Requesting user signature...");
1225
+ const signedTx = await tronWeb.trx.sign(tx.transaction);
1226
+ console.log("[TronLink] Transaction signed:", signedTx);
1227
+ const txID = signedTx.txID;
1228
+ console.log("[TronLink] Transaction hash (txID):", txID);
1229
+ console.log("[TronLink] Broadcasting transaction...");
1112
1230
  const broadcast = await tronWeb.trx.sendRawTransaction(signedTx);
1113
1231
  console.log("[TronLink] Broadcast result:", broadcast);
1114
- if (!broadcast.result) {
1115
- throw new Error(broadcast.message || "Transaction broadcast failed");
1232
+ if (broadcast && broadcast.result === true) {
1233
+ return txID || broadcast.txid || "";
1234
+ } else {
1235
+ if (txID) {
1236
+ console.warn("[TronLink] Broadcast returned false but txID exists:", txID);
1237
+ return txID;
1238
+ }
1239
+ throw new Error(broadcast?.message || "Transaction broadcast failed");
1116
1240
  }
1117
- const txHash = broadcast.txid || broadcast.transaction?.txID;
1118
- console.log("[TronLink] Transaction hash:", txHash);
1119
- return txHash || "";
1120
1241
  } catch (error) {
1121
1242
  console.error("Write contract error:", error);
1122
1243
  if (error.message?.includes("User rejected") || error.message?.includes("Confirmation declined")) {