@enclave-hq/wallet-sdk 1.2.0 → 1.2.1

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.
@@ -866,16 +866,57 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
866
866
  await this.ensureAvailable();
867
867
  try {
868
868
  this.setState("connecting" /* CONNECTING */);
869
+ const w = window;
869
870
  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);
871
+ if (tronWeb.ready) {
872
+ await tronWeb.ready;
873
+ }
874
+ let address = tronWeb.defaultAddress?.base58;
875
+ if (!address && tronWeb.defaultAddress?.hex && tronWeb.address && typeof tronWeb.address.fromHex === "function") {
876
+ try {
877
+ address = tronWeb.address.fromHex(tronWeb.defaultAddress.hex);
878
+ } catch (e) {
879
+ }
880
+ }
881
+ if (!address) {
882
+ for (let i = 0; i < 20; i++) {
883
+ await new Promise((resolve) => setTimeout(resolve, 100));
884
+ address = tronWeb.defaultAddress?.base58;
885
+ if (address) break;
886
+ }
887
+ }
888
+ if (!address) {
889
+ if (w.tronLink && typeof w.tronLink.request === "function") {
890
+ try {
891
+ const result = await w.tronLink.request({
892
+ method: "tron_requestAccounts"
893
+ });
894
+ if (!result || result.code !== 200) {
895
+ throw new ConnectionRejectedError(this.type);
896
+ }
897
+ } catch (error) {
898
+ if (error.code === 4001 || error.message?.includes("User rejected") || error.message?.includes("rejected")) {
899
+ throw new ConnectionRejectedError(this.type);
900
+ }
901
+ }
902
+ }
903
+ address = tronWeb.defaultAddress?.base58;
904
+ if (!address && tronWeb.defaultAddress?.hex && tronWeb.address && typeof tronWeb.address.fromHex === "function") {
905
+ try {
906
+ address = tronWeb.address.fromHex(tronWeb.defaultAddress.hex);
907
+ } catch (e) {
908
+ }
909
+ }
910
+ if (!address) {
911
+ for (let i = 0; i < 20; i++) {
912
+ await new Promise((resolve) => setTimeout(resolve, 100));
913
+ address = tronWeb.defaultAddress?.base58;
914
+ if (address) break;
915
+ }
916
+ }
875
917
  }
876
- const address = tronWeb.defaultAddress?.base58;
877
918
  if (!address) {
878
- throw new Error("Failed to get Tron address");
919
+ throw new Error("Failed to get Tron address. Please make sure your wallet is unlocked and try again.");
879
920
  }
880
921
  const tronChainId = chainId || _TronLinkAdapter.TRON_MAINNET_CHAIN_ID;
881
922
  const account = {
@@ -951,17 +992,41 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
951
992
  }
952
993
  /**
953
994
  * 读取合约
995
+ * 参考 webserver 的实现,使用 TronWeb 合约实例的标准 call() 方法
954
996
  */
955
997
  async readContract(params) {
956
998
  this.ensureConnected();
957
999
  try {
958
1000
  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;
1001
+ if (!this.currentAccount) {
1002
+ throw new Error("No account connected");
1003
+ }
1004
+ try {
1005
+ const contract = await tronWeb.contract(params.abi, params.address);
1006
+ const method = contract[params.functionName];
1007
+ if (!method || typeof method !== "function") {
1008
+ throw new Error(`Function ${params.functionName} not found in contract ABI`);
1009
+ }
1010
+ const result = await method(...params.args || []).call();
1011
+ return result;
1012
+ } catch (method1Error) {
1013
+ console.warn("\u26A0\uFE0F [\u65B9\u6CD51] TronWeb\u6807\u51C6\u65B9\u6CD5\u5931\u8D25\uFF0C\u5C1D\u8BD5\u65B9\u6CD52:", method1Error.message);
1014
+ try {
1015
+ const contract2 = await tronWeb.contract().at(params.address);
1016
+ const method2 = contract2[params.functionName];
1017
+ if (!method2 || typeof method2 !== "function") {
1018
+ throw new Error(`Function ${params.functionName} not found in contract`);
1019
+ }
1020
+ const result = await method2(...params.args || []).call();
1021
+ return result;
1022
+ } catch (method2Error) {
1023
+ console.error("\u26A0\uFE0F [\u65B9\u6CD52] \u4E5F\u5931\u8D25:", method2Error.message);
1024
+ throw method1Error;
1025
+ }
1026
+ }
962
1027
  } catch (error) {
963
1028
  console.error("Read contract error:", error);
964
- throw new Error(`Failed to read contract: ${error.message}`);
1029
+ throw new Error(`Failed to read contract: ${error.message || "Unknown error"}`);
965
1030
  }
966
1031
  }
967
1032
  /**
@@ -1006,26 +1071,38 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1006
1071
  }));
1007
1072
  console.log("[TronLink] Transaction options:", options);
1008
1073
  console.log("[TronLink] Parameters:", parameter);
1009
- const transaction = await tronWeb.transactionBuilder.triggerSmartContract(
1074
+ const functionSelector = params.functionName + "(" + functionAbi.inputs.map((i) => i.type).join(",") + ")";
1075
+ console.log("[TronLink] Function selector:", functionSelector);
1076
+ console.log("[TronLink] Transaction options:", options);
1077
+ console.log("[TronLink] Parameters:", parameter);
1078
+ const tx = await tronWeb.transactionBuilder.triggerSmartContract(
1010
1079
  params.address,
1011
- params.functionName + "(" + functionAbi.inputs.map((i) => i.type).join(",") + ")",
1080
+ functionSelector,
1012
1081
  options,
1013
1082
  parameter,
1014
1083
  this.currentAccount.nativeAddress
1015
1084
  );
1016
- console.log("[TronLink] Transaction built:", transaction);
1017
- if (!transaction || !transaction.transaction) {
1085
+ console.log("[TronLink] Transaction built:", tx);
1086
+ if (!tx || !tx.transaction) {
1018
1087
  throw new Error("Failed to build transaction");
1019
1088
  }
1020
- const signedTx = await tronWeb.trx.sign(transaction.transaction);
1089
+ console.log("[TronLink] Requesting user signature...");
1090
+ const signedTx = await tronWeb.trx.sign(tx.transaction);
1091
+ console.log("[TronLink] Transaction signed:", signedTx);
1092
+ const txID = signedTx.txID;
1093
+ console.log("[TronLink] Transaction hash (txID):", txID);
1094
+ console.log("[TronLink] Broadcasting transaction...");
1021
1095
  const broadcast = await tronWeb.trx.sendRawTransaction(signedTx);
1022
1096
  console.log("[TronLink] Broadcast result:", broadcast);
1023
- if (!broadcast.result) {
1024
- throw new Error(broadcast.message || "Transaction broadcast failed");
1097
+ if (broadcast && broadcast.result === true) {
1098
+ return txID || broadcast.txid || "";
1099
+ } else {
1100
+ if (txID) {
1101
+ console.warn("[TronLink] Broadcast returned false but txID exists:", txID);
1102
+ return txID;
1103
+ }
1104
+ throw new Error(broadcast?.message || "Transaction broadcast failed");
1025
1105
  }
1026
- const txHash = broadcast.txid || broadcast.transaction?.txID;
1027
- console.log("[TronLink] Transaction hash:", txHash);
1028
- return txHash || "";
1029
1106
  } catch (error) {
1030
1107
  console.error("Write contract error:", error);
1031
1108
  if (error.message?.includes("User rejected") || error.message?.includes("Confirmation declined")) {