@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.d.mts CHANGED
@@ -76,6 +76,8 @@ interface ContractWriteParams extends ContractReadParams {
76
76
  value?: string;
77
77
  gas?: number;
78
78
  gasPrice?: string;
79
+ maxFeePerGas?: string;
80
+ maxPriorityFeePerGas?: string;
79
81
  }
80
82
  interface EVMTransaction {
81
83
  to: string;
@@ -195,6 +197,8 @@ declare class WalletManager extends TypedEventEmitter<WalletManagerEvents> {
195
197
  value?: string;
196
198
  gas?: number;
197
199
  gasPrice?: string;
200
+ maxFeePerGas?: string;
201
+ maxPriorityFeePerGas?: string;
198
202
  }, chainType?: ChainType$1): Promise<string>;
199
203
  estimateGas(address: string, abi: any[], functionName: string, args?: any[], chainType?: ChainType$1): Promise<bigint>;
200
204
  waitForTransaction(txHash: string, confirmations?: number, chainType?: ChainType$1): Promise<TransactionReceipt>;
package/dist/index.d.ts CHANGED
@@ -76,6 +76,8 @@ interface ContractWriteParams extends ContractReadParams {
76
76
  value?: string;
77
77
  gas?: number;
78
78
  gasPrice?: string;
79
+ maxFeePerGas?: string;
80
+ maxPriorityFeePerGas?: string;
79
81
  }
80
82
  interface EVMTransaction {
81
83
  to: string;
@@ -195,6 +197,8 @@ declare class WalletManager extends TypedEventEmitter<WalletManagerEvents> {
195
197
  value?: string;
196
198
  gas?: number;
197
199
  gasPrice?: string;
200
+ maxFeePerGas?: string;
201
+ maxPriorityFeePerGas?: string;
198
202
  }, chainType?: ChainType$1): Promise<string>;
199
203
  estimateGas(address: string, abi: any[], functionName: string, args?: any[], chainType?: ChainType$1): Promise<bigint>;
200
204
  waitForTransaction(txHash: string, confirmations?: number, chainType?: ChainType$1): Promise<TransactionReceipt>;
package/dist/index.js CHANGED
@@ -760,25 +760,69 @@ var MetaMaskAdapter = class extends BrowserWalletAdapter {
760
760
  throw new Error("Wallet client not initialized");
761
761
  }
762
762
  try {
763
- console.log("\u{1F50D} [MetaMask writeContract] params.gasPrice:", params.gasPrice, "type:", typeof params.gasPrice);
764
- let processedGasPrice;
765
- if (!params.gasPrice) {
766
- processedGasPrice = void 0;
767
- } else if (params.gasPrice === "auto") {
768
- processedGasPrice = void 0;
769
- } else {
770
- processedGasPrice = BigInt(params.gasPrice);
771
- }
772
- console.log("\u{1F50D} [MetaMask writeContract] processedGasPrice:", processedGasPrice);
773
- const txHash = await this.walletClient.writeContract({
763
+ console.log("\u{1F50D} [MetaMask writeContract] Gas params:", {
764
+ gasPrice: params.gasPrice,
765
+ maxFeePerGas: params.maxFeePerGas,
766
+ maxPriorityFeePerGas: params.maxPriorityFeePerGas
767
+ });
768
+ const txOptions = {
774
769
  address: params.address,
775
770
  abi: params.abi,
776
771
  functionName: params.functionName,
777
772
  ...params.args ? { args: params.args } : {},
778
773
  value: params.value ? BigInt(params.value) : void 0,
779
- gas: params.gas ? BigInt(params.gas) : void 0,
780
- gasPrice: processedGasPrice
781
- });
774
+ gas: params.gas ? BigInt(params.gas) : void 0
775
+ };
776
+ if (params.maxFeePerGas || params.maxPriorityFeePerGas) {
777
+ if (params.maxFeePerGas) {
778
+ txOptions.maxFeePerGas = BigInt(params.maxFeePerGas);
779
+ }
780
+ if (params.maxPriorityFeePerGas) {
781
+ txOptions.maxPriorityFeePerGas = BigInt(params.maxPriorityFeePerGas);
782
+ }
783
+ console.log("\u{1F50D} [MetaMask writeContract] Using EIP-1559 gas params");
784
+ } else if (params.gasPrice) {
785
+ if (params.gasPrice === "auto") {
786
+ console.log("\u{1F50D} [MetaMask writeContract] Auto gas price - letting viem decide");
787
+ } else {
788
+ txOptions.gasPrice = BigInt(params.gasPrice);
789
+ console.log("\u{1F50D} [MetaMask writeContract] Using legacy gasPrice");
790
+ }
791
+ } else {
792
+ console.log("\u{1F50D} [MetaMask writeContract] No gas params - fetching and setting reasonable gas fees");
793
+ if (this.publicClient) {
794
+ try {
795
+ const feesPerGas = await this.publicClient.estimateFeesPerGas().catch(() => null);
796
+ if (feesPerGas) {
797
+ const minPriorityFeeWei = BigInt(1e8);
798
+ const maxPriorityFeePerGas = feesPerGas.maxPriorityFeePerGas > minPriorityFeeWei ? feesPerGas.maxPriorityFeePerGas : minPriorityFeeWei;
799
+ const adjustedMaxFeePerGas = feesPerGas.maxFeePerGas > maxPriorityFeePerGas ? feesPerGas.maxFeePerGas : maxPriorityFeePerGas + BigInt(1e9);
800
+ txOptions.maxFeePerGas = adjustedMaxFeePerGas;
801
+ txOptions.maxPriorityFeePerGas = maxPriorityFeePerGas;
802
+ const maxFeePerGasGwei = Number(adjustedMaxFeePerGas) / 1e9;
803
+ const maxPriorityFeePerGasGwei = Number(maxPriorityFeePerGas) / 1e9;
804
+ console.log("\u{1F4B0} [MetaMask writeContract] Set gas fees (EIP-1559):", {
805
+ maxFeePerGas: `${maxFeePerGasGwei.toFixed(6)} Gwei`,
806
+ maxPriorityFeePerGas: `${maxPriorityFeePerGasGwei.toFixed(6)} Gwei`,
807
+ maxFeePerGasWei: adjustedMaxFeePerGas.toString(),
808
+ maxPriorityFeePerGasWei: maxPriorityFeePerGas.toString(),
809
+ note: "\u5DF2\u8BBE\u7F6E\u5408\u7406\u7684 gas \u8D39\u7528\uFF0C\u907F\u514D MetaMask \u4F7F\u7528\u9ED8\u8BA4\u503C"
810
+ });
811
+ } else {
812
+ const gasPrice = await this.publicClient.getGasPrice();
813
+ txOptions.gasPrice = gasPrice;
814
+ const gasPriceGwei = Number(gasPrice) / 1e9;
815
+ console.log("\u{1F4B0} [MetaMask writeContract] Set gas price (Legacy):", {
816
+ gasPrice: `${gasPriceGwei.toFixed(6)} Gwei`,
817
+ gasPriceWei: gasPrice.toString()
818
+ });
819
+ }
820
+ } catch (err) {
821
+ console.warn("\u26A0\uFE0F [MetaMask writeContract] Failed to estimate gas fees, letting viem auto-estimate:", err);
822
+ }
823
+ }
824
+ }
825
+ const txHash = await this.walletClient.writeContract(txOptions);
782
826
  return txHash;
783
827
  } catch (error) {
784
828
  if (error.code === 4001) {
@@ -965,16 +1009,57 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
965
1009
  await this.ensureAvailable();
966
1010
  try {
967
1011
  this.setState("connecting" /* CONNECTING */);
1012
+ const w = window;
968
1013
  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);
1014
+ if (tronWeb.ready) {
1015
+ await tronWeb.ready;
1016
+ }
1017
+ let address = tronWeb.defaultAddress?.base58;
1018
+ if (!address && tronWeb.defaultAddress?.hex && tronWeb.address && typeof tronWeb.address.fromHex === "function") {
1019
+ try {
1020
+ address = tronWeb.address.fromHex(tronWeb.defaultAddress.hex);
1021
+ } catch (e) {
1022
+ }
974
1023
  }
975
- const address = tronWeb.defaultAddress?.base58;
976
1024
  if (!address) {
977
- throw new Error("Failed to get Tron address");
1025
+ for (let i = 0; i < 20; i++) {
1026
+ await new Promise((resolve) => setTimeout(resolve, 100));
1027
+ address = tronWeb.defaultAddress?.base58;
1028
+ if (address) break;
1029
+ }
1030
+ }
1031
+ if (!address) {
1032
+ if (w.tronLink && typeof w.tronLink.request === "function") {
1033
+ try {
1034
+ const result = await w.tronLink.request({
1035
+ method: "tron_requestAccounts"
1036
+ });
1037
+ if (!result || result.code !== 200) {
1038
+ throw new ConnectionRejectedError(this.type);
1039
+ }
1040
+ } catch (error) {
1041
+ if (error.code === 4001 || error.message?.includes("User rejected") || error.message?.includes("rejected")) {
1042
+ throw new ConnectionRejectedError(this.type);
1043
+ }
1044
+ }
1045
+ }
1046
+ address = tronWeb.defaultAddress?.base58;
1047
+ if (!address && tronWeb.defaultAddress?.hex && tronWeb.address && typeof tronWeb.address.fromHex === "function") {
1048
+ try {
1049
+ address = tronWeb.address.fromHex(tronWeb.defaultAddress.hex);
1050
+ } catch (e) {
1051
+ }
1052
+ }
1053
+ if (!address) {
1054
+ for (let i = 0; i < 20; i++) {
1055
+ await new Promise((resolve) => setTimeout(resolve, 100));
1056
+ address = tronWeb.defaultAddress?.base58;
1057
+ if (address) break;
1058
+ }
1059
+ }
1060
+ }
1061
+ if (!address) {
1062
+ throw new Error("Failed to get Tron address. Please make sure your wallet is unlocked and try again.");
978
1063
  }
979
1064
  const tronChainId = chainId || _TronLinkAdapter.TRON_MAINNET_CHAIN_ID;
980
1065
  const account = {
@@ -1050,17 +1135,41 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1050
1135
  }
1051
1136
  /**
1052
1137
  * 读取合约
1138
+ * 参考 webserver 的实现,使用 TronWeb 合约实例的标准 call() 方法
1053
1139
  */
1054
1140
  async readContract(params) {
1055
1141
  this.ensureConnected();
1056
1142
  try {
1057
1143
  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;
1144
+ if (!this.currentAccount) {
1145
+ throw new Error("No account connected");
1146
+ }
1147
+ try {
1148
+ const contract = await tronWeb.contract(params.abi, params.address);
1149
+ const method = contract[params.functionName];
1150
+ if (!method || typeof method !== "function") {
1151
+ throw new Error(`Function ${params.functionName} not found in contract ABI`);
1152
+ }
1153
+ const result = await method(...params.args || []).call();
1154
+ return result;
1155
+ } catch (method1Error) {
1156
+ console.warn("\u26A0\uFE0F [\u65B9\u6CD51] TronWeb\u6807\u51C6\u65B9\u6CD5\u5931\u8D25\uFF0C\u5C1D\u8BD5\u65B9\u6CD52:", method1Error.message);
1157
+ try {
1158
+ const contract2 = await tronWeb.contract().at(params.address);
1159
+ const method2 = contract2[params.functionName];
1160
+ if (!method2 || typeof method2 !== "function") {
1161
+ throw new Error(`Function ${params.functionName} not found in contract`);
1162
+ }
1163
+ const result = await method2(...params.args || []).call();
1164
+ return result;
1165
+ } catch (method2Error) {
1166
+ console.error("\u26A0\uFE0F [\u65B9\u6CD52] \u4E5F\u5931\u8D25:", method2Error.message);
1167
+ throw method1Error;
1168
+ }
1169
+ }
1061
1170
  } catch (error) {
1062
1171
  console.error("Read contract error:", error);
1063
- throw new Error(`Failed to read contract: ${error.message}`);
1172
+ throw new Error(`Failed to read contract: ${error.message || "Unknown error"}`);
1064
1173
  }
1065
1174
  }
1066
1175
  /**
@@ -1105,26 +1214,38 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1105
1214
  }));
1106
1215
  console.log("[TronLink] Transaction options:", options);
1107
1216
  console.log("[TronLink] Parameters:", parameter);
1108
- const transaction = await tronWeb.transactionBuilder.triggerSmartContract(
1217
+ const functionSelector = params.functionName + "(" + functionAbi.inputs.map((i) => i.type).join(",") + ")";
1218
+ console.log("[TronLink] Function selector:", functionSelector);
1219
+ console.log("[TronLink] Transaction options:", options);
1220
+ console.log("[TronLink] Parameters:", parameter);
1221
+ const tx = await tronWeb.transactionBuilder.triggerSmartContract(
1109
1222
  params.address,
1110
- params.functionName + "(" + functionAbi.inputs.map((i) => i.type).join(",") + ")",
1223
+ functionSelector,
1111
1224
  options,
1112
1225
  parameter,
1113
1226
  this.currentAccount.nativeAddress
1114
1227
  );
1115
- console.log("[TronLink] Transaction built:", transaction);
1116
- if (!transaction || !transaction.transaction) {
1228
+ console.log("[TronLink] Transaction built:", tx);
1229
+ if (!tx || !tx.transaction) {
1117
1230
  throw new Error("Failed to build transaction");
1118
1231
  }
1119
- const signedTx = await tronWeb.trx.sign(transaction.transaction);
1232
+ console.log("[TronLink] Requesting user signature...");
1233
+ const signedTx = await tronWeb.trx.sign(tx.transaction);
1234
+ console.log("[TronLink] Transaction signed:", signedTx);
1235
+ const txID = signedTx.txID;
1236
+ console.log("[TronLink] Transaction hash (txID):", txID);
1237
+ console.log("[TronLink] Broadcasting transaction...");
1120
1238
  const broadcast = await tronWeb.trx.sendRawTransaction(signedTx);
1121
1239
  console.log("[TronLink] Broadcast result:", broadcast);
1122
- if (!broadcast.result) {
1123
- throw new Error(broadcast.message || "Transaction broadcast failed");
1240
+ if (broadcast && broadcast.result === true) {
1241
+ return txID || broadcast.txid || "";
1242
+ } else {
1243
+ if (txID) {
1244
+ console.warn("[TronLink] Broadcast returned false but txID exists:", txID);
1245
+ return txID;
1246
+ }
1247
+ throw new Error(broadcast?.message || "Transaction broadcast failed");
1124
1248
  }
1125
- const txHash = broadcast.txid || broadcast.transaction?.txID;
1126
- console.log("[TronLink] Transaction hash:", txHash);
1127
- return txHash || "";
1128
1249
  } catch (error) {
1129
1250
  console.error("Write contract error:", error);
1130
1251
  if (error.message?.includes("User rejected") || error.message?.includes("Confirmation declined")) {