@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.
@@ -75,6 +75,8 @@ interface ContractWriteParams extends ContractReadParams {
75
75
  value?: string;
76
76
  gas?: number;
77
77
  gasPrice?: string;
78
+ maxFeePerGas?: string;
79
+ maxPriorityFeePerGas?: string;
78
80
  }
79
81
  interface EVMTransaction {
80
82
  to: string;
@@ -172,6 +174,8 @@ declare class WalletManager extends TypedEventEmitter<WalletManagerEvents> {
172
174
  value?: string;
173
175
  gas?: number;
174
176
  gasPrice?: string;
177
+ maxFeePerGas?: string;
178
+ maxPriorityFeePerGas?: string;
175
179
  }, chainType?: ChainType): Promise<string>;
176
180
  estimateGas(address: string, abi: any[], functionName: string, args?: any[], chainType?: ChainType): Promise<bigint>;
177
181
  waitForTransaction(txHash: string, confirmations?: number, chainType?: ChainType): Promise<TransactionReceipt>;
@@ -75,6 +75,8 @@ interface ContractWriteParams extends ContractReadParams {
75
75
  value?: string;
76
76
  gas?: number;
77
77
  gasPrice?: string;
78
+ maxFeePerGas?: string;
79
+ maxPriorityFeePerGas?: string;
78
80
  }
79
81
  interface EVMTransaction {
80
82
  to: string;
@@ -172,6 +174,8 @@ declare class WalletManager extends TypedEventEmitter<WalletManagerEvents> {
172
174
  value?: string;
173
175
  gas?: number;
174
176
  gasPrice?: string;
177
+ maxFeePerGas?: string;
178
+ maxPriorityFeePerGas?: string;
175
179
  }, chainType?: ChainType): Promise<string>;
176
180
  estimateGas(address: string, abi: any[], functionName: string, args?: any[], chainType?: ChainType): Promise<bigint>;
177
181
  waitForTransaction(txHash: string, confirmations?: number, chainType?: ChainType): Promise<TransactionReceipt>;
@@ -668,25 +668,69 @@ var MetaMaskAdapter = class extends BrowserWalletAdapter {
668
668
  throw new Error("Wallet client not initialized");
669
669
  }
670
670
  try {
671
- console.log("\u{1F50D} [MetaMask writeContract] params.gasPrice:", params.gasPrice, "type:", typeof params.gasPrice);
672
- let processedGasPrice;
673
- if (!params.gasPrice) {
674
- processedGasPrice = void 0;
675
- } else if (params.gasPrice === "auto") {
676
- processedGasPrice = void 0;
677
- } else {
678
- processedGasPrice = BigInt(params.gasPrice);
679
- }
680
- console.log("\u{1F50D} [MetaMask writeContract] processedGasPrice:", processedGasPrice);
681
- const txHash = await this.walletClient.writeContract({
671
+ console.log("\u{1F50D} [MetaMask writeContract] Gas params:", {
672
+ gasPrice: params.gasPrice,
673
+ maxFeePerGas: params.maxFeePerGas,
674
+ maxPriorityFeePerGas: params.maxPriorityFeePerGas
675
+ });
676
+ const txOptions = {
682
677
  address: params.address,
683
678
  abi: params.abi,
684
679
  functionName: params.functionName,
685
680
  ...params.args ? { args: params.args } : {},
686
681
  value: params.value ? BigInt(params.value) : void 0,
687
- gas: params.gas ? BigInt(params.gas) : void 0,
688
- gasPrice: processedGasPrice
689
- });
682
+ gas: params.gas ? BigInt(params.gas) : void 0
683
+ };
684
+ if (params.maxFeePerGas || params.maxPriorityFeePerGas) {
685
+ if (params.maxFeePerGas) {
686
+ txOptions.maxFeePerGas = BigInt(params.maxFeePerGas);
687
+ }
688
+ if (params.maxPriorityFeePerGas) {
689
+ txOptions.maxPriorityFeePerGas = BigInt(params.maxPriorityFeePerGas);
690
+ }
691
+ console.log("\u{1F50D} [MetaMask writeContract] Using EIP-1559 gas params");
692
+ } else if (params.gasPrice) {
693
+ if (params.gasPrice === "auto") {
694
+ console.log("\u{1F50D} [MetaMask writeContract] Auto gas price - letting viem decide");
695
+ } else {
696
+ txOptions.gasPrice = BigInt(params.gasPrice);
697
+ console.log("\u{1F50D} [MetaMask writeContract] Using legacy gasPrice");
698
+ }
699
+ } else {
700
+ console.log("\u{1F50D} [MetaMask writeContract] No gas params - fetching and setting reasonable gas fees");
701
+ if (this.publicClient) {
702
+ try {
703
+ const feesPerGas = await this.publicClient.estimateFeesPerGas().catch(() => null);
704
+ if (feesPerGas) {
705
+ const minPriorityFeeWei = BigInt(1e8);
706
+ const maxPriorityFeePerGas = feesPerGas.maxPriorityFeePerGas > minPriorityFeeWei ? feesPerGas.maxPriorityFeePerGas : minPriorityFeeWei;
707
+ const adjustedMaxFeePerGas = feesPerGas.maxFeePerGas > maxPriorityFeePerGas ? feesPerGas.maxFeePerGas : maxPriorityFeePerGas + BigInt(1e9);
708
+ txOptions.maxFeePerGas = adjustedMaxFeePerGas;
709
+ txOptions.maxPriorityFeePerGas = maxPriorityFeePerGas;
710
+ const maxFeePerGasGwei = Number(adjustedMaxFeePerGas) / 1e9;
711
+ const maxPriorityFeePerGasGwei = Number(maxPriorityFeePerGas) / 1e9;
712
+ console.log("\u{1F4B0} [MetaMask writeContract] Set gas fees (EIP-1559):", {
713
+ maxFeePerGas: `${maxFeePerGasGwei.toFixed(6)} Gwei`,
714
+ maxPriorityFeePerGas: `${maxPriorityFeePerGasGwei.toFixed(6)} Gwei`,
715
+ maxFeePerGasWei: adjustedMaxFeePerGas.toString(),
716
+ maxPriorityFeePerGasWei: maxPriorityFeePerGas.toString(),
717
+ note: "\u5DF2\u8BBE\u7F6E\u5408\u7406\u7684 gas \u8D39\u7528\uFF0C\u907F\u514D MetaMask \u4F7F\u7528\u9ED8\u8BA4\u503C"
718
+ });
719
+ } else {
720
+ const gasPrice = await this.publicClient.getGasPrice();
721
+ txOptions.gasPrice = gasPrice;
722
+ const gasPriceGwei = Number(gasPrice) / 1e9;
723
+ console.log("\u{1F4B0} [MetaMask writeContract] Set gas price (Legacy):", {
724
+ gasPrice: `${gasPriceGwei.toFixed(6)} Gwei`,
725
+ gasPriceWei: gasPrice.toString()
726
+ });
727
+ }
728
+ } catch (err) {
729
+ console.warn("\u26A0\uFE0F [MetaMask writeContract] Failed to estimate gas fees, letting viem auto-estimate:", err);
730
+ }
731
+ }
732
+ }
733
+ const txHash = await this.walletClient.writeContract(txOptions);
690
734
  return txHash;
691
735
  } catch (error) {
692
736
  if (error.code === 4001) {
@@ -873,16 +917,57 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
873
917
  await this.ensureAvailable();
874
918
  try {
875
919
  this.setState("connecting" /* CONNECTING */);
920
+ const w = window;
876
921
  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);
922
+ if (tronWeb.ready) {
923
+ await tronWeb.ready;
924
+ }
925
+ let address = tronWeb.defaultAddress?.base58;
926
+ if (!address && tronWeb.defaultAddress?.hex && tronWeb.address && typeof tronWeb.address.fromHex === "function") {
927
+ try {
928
+ address = tronWeb.address.fromHex(tronWeb.defaultAddress.hex);
929
+ } catch (e) {
930
+ }
882
931
  }
883
- const address = tronWeb.defaultAddress?.base58;
884
932
  if (!address) {
885
- throw new Error("Failed to get Tron address");
933
+ for (let i = 0; i < 20; i++) {
934
+ await new Promise((resolve) => setTimeout(resolve, 100));
935
+ address = tronWeb.defaultAddress?.base58;
936
+ if (address) break;
937
+ }
938
+ }
939
+ if (!address) {
940
+ if (w.tronLink && typeof w.tronLink.request === "function") {
941
+ try {
942
+ const result = await w.tronLink.request({
943
+ method: "tron_requestAccounts"
944
+ });
945
+ if (!result || result.code !== 200) {
946
+ throw new ConnectionRejectedError(this.type);
947
+ }
948
+ } catch (error) {
949
+ if (error.code === 4001 || error.message?.includes("User rejected") || error.message?.includes("rejected")) {
950
+ throw new ConnectionRejectedError(this.type);
951
+ }
952
+ }
953
+ }
954
+ address = tronWeb.defaultAddress?.base58;
955
+ if (!address && tronWeb.defaultAddress?.hex && tronWeb.address && typeof tronWeb.address.fromHex === "function") {
956
+ try {
957
+ address = tronWeb.address.fromHex(tronWeb.defaultAddress.hex);
958
+ } catch (e) {
959
+ }
960
+ }
961
+ if (!address) {
962
+ for (let i = 0; i < 20; i++) {
963
+ await new Promise((resolve) => setTimeout(resolve, 100));
964
+ address = tronWeb.defaultAddress?.base58;
965
+ if (address) break;
966
+ }
967
+ }
968
+ }
969
+ if (!address) {
970
+ throw new Error("Failed to get Tron address. Please make sure your wallet is unlocked and try again.");
886
971
  }
887
972
  const tronChainId = chainId || _TronLinkAdapter.TRON_MAINNET_CHAIN_ID;
888
973
  const account = {
@@ -958,17 +1043,41 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
958
1043
  }
959
1044
  /**
960
1045
  * 读取合约
1046
+ * 参考 webserver 的实现,使用 TronWeb 合约实例的标准 call() 方法
961
1047
  */
962
1048
  async readContract(params) {
963
1049
  this.ensureConnected();
964
1050
  try {
965
1051
  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;
1052
+ if (!this.currentAccount) {
1053
+ throw new Error("No account connected");
1054
+ }
1055
+ try {
1056
+ const contract = await tronWeb.contract(params.abi, params.address);
1057
+ const method = contract[params.functionName];
1058
+ if (!method || typeof method !== "function") {
1059
+ throw new Error(`Function ${params.functionName} not found in contract ABI`);
1060
+ }
1061
+ const result = await method(...params.args || []).call();
1062
+ return result;
1063
+ } catch (method1Error) {
1064
+ console.warn("\u26A0\uFE0F [\u65B9\u6CD51] TronWeb\u6807\u51C6\u65B9\u6CD5\u5931\u8D25\uFF0C\u5C1D\u8BD5\u65B9\u6CD52:", method1Error.message);
1065
+ try {
1066
+ const contract2 = await tronWeb.contract().at(params.address);
1067
+ const method2 = contract2[params.functionName];
1068
+ if (!method2 || typeof method2 !== "function") {
1069
+ throw new Error(`Function ${params.functionName} not found in contract`);
1070
+ }
1071
+ const result = await method2(...params.args || []).call();
1072
+ return result;
1073
+ } catch (method2Error) {
1074
+ console.error("\u26A0\uFE0F [\u65B9\u6CD52] \u4E5F\u5931\u8D25:", method2Error.message);
1075
+ throw method1Error;
1076
+ }
1077
+ }
969
1078
  } catch (error) {
970
1079
  console.error("Read contract error:", error);
971
- throw new Error(`Failed to read contract: ${error.message}`);
1080
+ throw new Error(`Failed to read contract: ${error.message || "Unknown error"}`);
972
1081
  }
973
1082
  }
974
1083
  /**
@@ -1013,26 +1122,38 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1013
1122
  }));
1014
1123
  console.log("[TronLink] Transaction options:", options);
1015
1124
  console.log("[TronLink] Parameters:", parameter);
1016
- const transaction = await tronWeb.transactionBuilder.triggerSmartContract(
1125
+ const functionSelector = params.functionName + "(" + functionAbi.inputs.map((i) => i.type).join(",") + ")";
1126
+ console.log("[TronLink] Function selector:", functionSelector);
1127
+ console.log("[TronLink] Transaction options:", options);
1128
+ console.log("[TronLink] Parameters:", parameter);
1129
+ const tx = await tronWeb.transactionBuilder.triggerSmartContract(
1017
1130
  params.address,
1018
- params.functionName + "(" + functionAbi.inputs.map((i) => i.type).join(",") + ")",
1131
+ functionSelector,
1019
1132
  options,
1020
1133
  parameter,
1021
1134
  this.currentAccount.nativeAddress
1022
1135
  );
1023
- console.log("[TronLink] Transaction built:", transaction);
1024
- if (!transaction || !transaction.transaction) {
1136
+ console.log("[TronLink] Transaction built:", tx);
1137
+ if (!tx || !tx.transaction) {
1025
1138
  throw new Error("Failed to build transaction");
1026
1139
  }
1027
- const signedTx = await tronWeb.trx.sign(transaction.transaction);
1140
+ console.log("[TronLink] Requesting user signature...");
1141
+ const signedTx = await tronWeb.trx.sign(tx.transaction);
1142
+ console.log("[TronLink] Transaction signed:", signedTx);
1143
+ const txID = signedTx.txID;
1144
+ console.log("[TronLink] Transaction hash (txID):", txID);
1145
+ console.log("[TronLink] Broadcasting transaction...");
1028
1146
  const broadcast = await tronWeb.trx.sendRawTransaction(signedTx);
1029
1147
  console.log("[TronLink] Broadcast result:", broadcast);
1030
- if (!broadcast.result) {
1031
- throw new Error(broadcast.message || "Transaction broadcast failed");
1148
+ if (broadcast && broadcast.result === true) {
1149
+ return txID || broadcast.txid || "";
1150
+ } else {
1151
+ if (txID) {
1152
+ console.warn("[TronLink] Broadcast returned false but txID exists:", txID);
1153
+ return txID;
1154
+ }
1155
+ throw new Error(broadcast?.message || "Transaction broadcast failed");
1032
1156
  }
1033
- const txHash = broadcast.txid || broadcast.transaction?.txID;
1034
- console.log("[TronLink] Transaction hash:", txHash);
1035
- return txHash || "";
1036
1157
  } catch (error) {
1037
1158
  console.error("Write contract error:", error);
1038
1159
  if (error.message?.includes("User rejected") || error.message?.includes("Confirmation declined")) {