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