@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.
package/dist/index.mjs CHANGED
@@ -957,16 +957,57 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
957
957
  await this.ensureAvailable();
958
958
  try {
959
959
  this.setState("connecting" /* CONNECTING */);
960
+ const w = window;
960
961
  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);
962
+ if (tronWeb.ready) {
963
+ await tronWeb.ready;
964
+ }
965
+ let address = tronWeb.defaultAddress?.base58;
966
+ if (!address && tronWeb.defaultAddress?.hex && tronWeb.address && typeof tronWeb.address.fromHex === "function") {
967
+ try {
968
+ address = tronWeb.address.fromHex(tronWeb.defaultAddress.hex);
969
+ } catch (e) {
970
+ }
971
+ }
972
+ if (!address) {
973
+ for (let i = 0; i < 20; i++) {
974
+ await new Promise((resolve) => setTimeout(resolve, 100));
975
+ address = tronWeb.defaultAddress?.base58;
976
+ if (address) break;
977
+ }
978
+ }
979
+ if (!address) {
980
+ if (w.tronLink && typeof w.tronLink.request === "function") {
981
+ try {
982
+ const result = await w.tronLink.request({
983
+ method: "tron_requestAccounts"
984
+ });
985
+ if (!result || result.code !== 200) {
986
+ throw new ConnectionRejectedError(this.type);
987
+ }
988
+ } catch (error) {
989
+ if (error.code === 4001 || error.message?.includes("User rejected") || error.message?.includes("rejected")) {
990
+ throw new ConnectionRejectedError(this.type);
991
+ }
992
+ }
993
+ }
994
+ address = tronWeb.defaultAddress?.base58;
995
+ if (!address && tronWeb.defaultAddress?.hex && tronWeb.address && typeof tronWeb.address.fromHex === "function") {
996
+ try {
997
+ address = tronWeb.address.fromHex(tronWeb.defaultAddress.hex);
998
+ } catch (e) {
999
+ }
1000
+ }
1001
+ if (!address) {
1002
+ for (let i = 0; i < 20; i++) {
1003
+ await new Promise((resolve) => setTimeout(resolve, 100));
1004
+ address = tronWeb.defaultAddress?.base58;
1005
+ if (address) break;
1006
+ }
1007
+ }
966
1008
  }
967
- const address = tronWeb.defaultAddress?.base58;
968
1009
  if (!address) {
969
- throw new Error("Failed to get Tron address");
1010
+ throw new Error("Failed to get Tron address. Please make sure your wallet is unlocked and try again.");
970
1011
  }
971
1012
  const tronChainId = chainId || _TronLinkAdapter.TRON_MAINNET_CHAIN_ID;
972
1013
  const account = {
@@ -1042,17 +1083,41 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1042
1083
  }
1043
1084
  /**
1044
1085
  * 读取合约
1086
+ * 参考 webserver 的实现,使用 TronWeb 合约实例的标准 call() 方法
1045
1087
  */
1046
1088
  async readContract(params) {
1047
1089
  this.ensureConnected();
1048
1090
  try {
1049
1091
  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;
1092
+ if (!this.currentAccount) {
1093
+ throw new Error("No account connected");
1094
+ }
1095
+ try {
1096
+ const contract = await tronWeb.contract(params.abi, params.address);
1097
+ const method = contract[params.functionName];
1098
+ if (!method || typeof method !== "function") {
1099
+ throw new Error(`Function ${params.functionName} not found in contract ABI`);
1100
+ }
1101
+ const result = await method(...params.args || []).call();
1102
+ return result;
1103
+ } catch (method1Error) {
1104
+ console.warn("\u26A0\uFE0F [\u65B9\u6CD51] TronWeb\u6807\u51C6\u65B9\u6CD5\u5931\u8D25\uFF0C\u5C1D\u8BD5\u65B9\u6CD52:", method1Error.message);
1105
+ try {
1106
+ const contract2 = await tronWeb.contract().at(params.address);
1107
+ const method2 = contract2[params.functionName];
1108
+ if (!method2 || typeof method2 !== "function") {
1109
+ throw new Error(`Function ${params.functionName} not found in contract`);
1110
+ }
1111
+ const result = await method2(...params.args || []).call();
1112
+ return result;
1113
+ } catch (method2Error) {
1114
+ console.error("\u26A0\uFE0F [\u65B9\u6CD52] \u4E5F\u5931\u8D25:", method2Error.message);
1115
+ throw method1Error;
1116
+ }
1117
+ }
1053
1118
  } catch (error) {
1054
1119
  console.error("Read contract error:", error);
1055
- throw new Error(`Failed to read contract: ${error.message}`);
1120
+ throw new Error(`Failed to read contract: ${error.message || "Unknown error"}`);
1056
1121
  }
1057
1122
  }
1058
1123
  /**
@@ -1097,26 +1162,38 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1097
1162
  }));
1098
1163
  console.log("[TronLink] Transaction options:", options);
1099
1164
  console.log("[TronLink] Parameters:", parameter);
1100
- const transaction = await tronWeb.transactionBuilder.triggerSmartContract(
1165
+ const functionSelector = params.functionName + "(" + functionAbi.inputs.map((i) => i.type).join(",") + ")";
1166
+ console.log("[TronLink] Function selector:", functionSelector);
1167
+ console.log("[TronLink] Transaction options:", options);
1168
+ console.log("[TronLink] Parameters:", parameter);
1169
+ const tx = await tronWeb.transactionBuilder.triggerSmartContract(
1101
1170
  params.address,
1102
- params.functionName + "(" + functionAbi.inputs.map((i) => i.type).join(",") + ")",
1171
+ functionSelector,
1103
1172
  options,
1104
1173
  parameter,
1105
1174
  this.currentAccount.nativeAddress
1106
1175
  );
1107
- console.log("[TronLink] Transaction built:", transaction);
1108
- if (!transaction || !transaction.transaction) {
1176
+ console.log("[TronLink] Transaction built:", tx);
1177
+ if (!tx || !tx.transaction) {
1109
1178
  throw new Error("Failed to build transaction");
1110
1179
  }
1111
- const signedTx = await tronWeb.trx.sign(transaction.transaction);
1180
+ console.log("[TronLink] Requesting user signature...");
1181
+ const signedTx = await tronWeb.trx.sign(tx.transaction);
1182
+ console.log("[TronLink] Transaction signed:", signedTx);
1183
+ const txID = signedTx.txID;
1184
+ console.log("[TronLink] Transaction hash (txID):", txID);
1185
+ console.log("[TronLink] Broadcasting transaction...");
1112
1186
  const broadcast = await tronWeb.trx.sendRawTransaction(signedTx);
1113
1187
  console.log("[TronLink] Broadcast result:", broadcast);
1114
- if (!broadcast.result) {
1115
- throw new Error(broadcast.message || "Transaction broadcast failed");
1188
+ if (broadcast && broadcast.result === true) {
1189
+ return txID || broadcast.txid || "";
1190
+ } else {
1191
+ if (txID) {
1192
+ console.warn("[TronLink] Broadcast returned false but txID exists:", txID);
1193
+ return txID;
1194
+ }
1195
+ throw new Error(broadcast?.message || "Transaction broadcast failed");
1116
1196
  }
1117
- const txHash = broadcast.txid || broadcast.transaction?.txID;
1118
- console.log("[TronLink] Transaction hash:", txHash);
1119
- return txHash || "";
1120
1197
  } catch (error) {
1121
1198
  console.error("Write contract error:", error);
1122
1199
  if (error.message?.includes("User rejected") || error.message?.includes("Confirmation declined")) {