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