sdk-triggerx 0.1.30 → 0.1.31

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.
@@ -22,6 +22,7 @@ const topupTg = async (tgAmount, signer) => {
22
22
  let gasRegistryContractAddress;
23
23
  let contract;
24
24
  let contractWithSigner;
25
+ let rpcProvider;
25
26
  let resolvedChainId;
26
27
  let signerAddress;
27
28
  try {
@@ -35,6 +36,7 @@ const topupTg = async (tgAmount, signer) => {
35
36
  const contractInstances = await (0, contractUtils_1.createContractWithSdkRpcAndSigner)(gasRegistryContractAddress, GasRegistry_json_1.default, signer, resolvedChainId);
36
37
  contract = contractInstances.contract;
37
38
  contractWithSigner = contractInstances.contractWithSigner;
39
+ rpcProvider = contractInstances.rpcProvider;
38
40
  }
39
41
  catch (configError) {
40
42
  if (configError instanceof errors_1.ConfigurationError) {
@@ -67,14 +69,14 @@ const topupTg = async (tgAmount, signer) => {
67
69
  value: amountInEthWei,
68
70
  gasLimit: gasWithBuffer
69
71
  });
70
- await tx.wait();
72
+ await (0, contractUtils_1.waitForTransactionReceiptWithRpcFallback)(tx, rpcProvider);
71
73
  return { success: true, data: tx };
72
74
  }
73
75
  catch (gasEstimateError) {
74
76
  // If gas estimation fails, try without gas limit (let provider estimate)
75
77
  console.warn('Gas estimation failed (using SDK RPC), proceeding without gas limit:', gasEstimateError);
76
78
  const tx = await contractWithSigner.purchaseTG(amountInEthWei, { value: amountInEthWei });
77
- await tx.wait();
79
+ await (0, contractUtils_1.waitForTransactionReceiptWithRpcFallback)(tx, rpcProvider);
78
80
  return { success: true, data: tx };
79
81
  }
80
82
  }
@@ -25,15 +25,21 @@ const withdrawTg = async (signer, amountTG) => {
25
25
  try {
26
26
  // Resolve chain ID and create contract instances with SDK RPC provider
27
27
  let resolvedChainId;
28
+ let contract;
28
29
  let contractWithSigner;
30
+ let rpcProvider;
31
+ let signerAddress;
29
32
  try {
30
33
  // Resolve chain ID from signer
34
+ signerAddress = await signer.getAddress();
31
35
  resolvedChainId = await (0, contractUtils_1.resolveChainId)(signer);
32
36
  // Get contract address
33
37
  const gasRegistryContractAddress = (0, contractUtils_1.getContractAddress)(resolvedChainId, 'gasRegistry');
34
38
  // Create contract instances with SDK RPC provider
35
39
  const contractInstances = await (0, contractUtils_1.createContractWithSdkRpcAndSigner)(gasRegistryContractAddress, GasRegistry_json_1.default, signer, resolvedChainId);
40
+ contract = contractInstances.contract;
36
41
  contractWithSigner = contractInstances.contractWithSigner;
42
+ rpcProvider = contractInstances.rpcProvider;
37
43
  }
38
44
  catch (configError) {
39
45
  if (configError instanceof errors_1.ConfigurationError) {
@@ -43,8 +49,24 @@ const withdrawTg = async (signer, amountTG) => {
43
49
  }
44
50
  // Assumes the contract has a function: claimEthForTg(uint256 amount)
45
51
  const amountTGWei = ethers_1.ethers.parseEther(amountTG.toString());
46
- const tx = await contractWithSigner.claimETHForTG(amountTGWei);
47
- await tx.wait();
52
+ let tx;
53
+ try {
54
+ console.log('Estimating gas for claimETHForTG using SDK RPC provider...');
55
+ const estimatedGas = await contract.claimETHForTG.estimateGas(amountTGWei, {
56
+ from: signerAddress,
57
+ });
58
+ console.log('Estimated gas (claimETHForTG):', estimatedGas.toString());
59
+ const gasWithBuffer = (estimatedGas * BigInt(110)) / BigInt(100);
60
+ console.log('Gas with 10% buffer (claimETHForTG):', gasWithBuffer.toString());
61
+ tx = await contractWithSigner.claimETHForTG(amountTGWei, {
62
+ gasLimit: gasWithBuffer,
63
+ });
64
+ }
65
+ catch (gasEstimateError) {
66
+ console.warn('Gas estimation failed for claimETHForTG (using SDK RPC), proceeding without explicit gas limit:', gasEstimateError);
67
+ tx = await contractWithSigner.claimETHForTG(amountTGWei);
68
+ }
69
+ await (0, contractUtils_1.waitForTransactionReceiptWithRpcFallback)(tx, rpcProvider);
48
70
  return { success: true, data: tx };
49
71
  }
50
72
  catch (error) {
@@ -6,7 +6,7 @@ const contractUtils_1 = require("./contractUtils");
6
6
  async function createJobOnChain({ jobTitle, jobType, timeFrame, targetContractAddress, encodedData, contractAddress, abi, signer, }) {
7
7
  // Resolve chain ID and create contract with SDK RPC provider
8
8
  const chainId = await (0, contractUtils_1.resolveChainId)(signer);
9
- const { contract, contractWithSigner } = await (0, contractUtils_1.createContractWithSdkRpcAndSigner)(contractAddress, abi, signer, chainId);
9
+ const { contract, contractWithSigner, rpcProvider } = await (0, contractUtils_1.createContractWithSdkRpcAndSigner)(contractAddress, abi, signer, chainId);
10
10
  const signerAddress = await signer.getAddress();
11
11
  let tx;
12
12
  try {
@@ -27,7 +27,7 @@ async function createJobOnChain({ jobTitle, jobType, timeFrame, targetContractAd
27
27
  console.warn('Gas estimation failed for createJob (using SDK RPC), proceeding without explicit gas limit:', gasEstimateError);
28
28
  tx = await contractWithSigner.createJob(jobTitle, jobType, timeFrame, targetContractAddress, encodedData);
29
29
  }
30
- const receipt = await tx.wait();
30
+ const receipt = await (0, contractUtils_1.waitForTransactionReceiptWithRpcFallback)(tx, rpcProvider);
31
31
  // Try to extract jobId from event logs (assume event is JobCreated(jobId,...))
32
32
  // Use contract (with SDK RPC) for parsing logs
33
33
  const event = receipt.logs
@@ -48,7 +48,7 @@ async function createJobOnChain({ jobTitle, jobType, timeFrame, targetContractAd
48
48
  async function deleteJobOnChain({ jobId, contractAddress, abi, signer, }) {
49
49
  // Resolve chain ID and create contract with SDK RPC provider
50
50
  const chainId = await (0, contractUtils_1.resolveChainId)(signer);
51
- const { contract, contractWithSigner } = await (0, contractUtils_1.createContractWithSdkRpcAndSigner)(contractAddress, abi.abi || abi, signer, chainId);
51
+ const { contract, contractWithSigner, rpcProvider } = await (0, contractUtils_1.createContractWithSdkRpcAndSigner)(contractAddress, abi.abi || abi, signer, chainId);
52
52
  const signerAddress = await signer.getAddress();
53
53
  try {
54
54
  console.log('Estimating gas for deleteJob using SDK RPC provider...');
@@ -61,11 +61,11 @@ async function deleteJobOnChain({ jobId, contractAddress, abi, signer, }) {
61
61
  const tx = await contractWithSigner.deleteJob(jobId, {
62
62
  gasLimit: gasWithBuffer,
63
63
  });
64
- await tx.wait();
64
+ await (0, contractUtils_1.waitForTransactionReceiptWithRpcFallback)(tx, rpcProvider);
65
65
  }
66
66
  catch (gasEstimateError) {
67
67
  console.warn('Gas estimation failed for deleteJob (using SDK RPC), proceeding without explicit gas limit:', gasEstimateError);
68
68
  const tx = await contractWithSigner.deleteJob(jobId);
69
- await tx.wait();
69
+ await (0, contractUtils_1.waitForTransactionReceiptWithRpcFallback)(tx, rpcProvider);
70
70
  }
71
71
  }
@@ -43,7 +43,20 @@ export declare function createContractWithSdkRpcAndSigner(contractAddress: strin
43
43
  contract: Contract;
44
44
  contractWithSigner: Contract;
45
45
  chainId: string;
46
+ rpcProvider: ethers.JsonRpcProvider;
46
47
  }>;
48
+ /**
49
+ * Wait for a transaction receipt, falling back to eth_getTransactionReceipt if tx.wait fails
50
+ * This helps when the user's wallet/provider does not implement getTransactionReceipt properly.
51
+ * @param tx - Transaction response returned from contract call
52
+ * @param rpcProvider - SDK RPC provider that supports eth_getTransactionReceipt
53
+ * @param options - Optional polling configuration
54
+ * @returns Transaction receipt once available
55
+ */
56
+ export declare function waitForTransactionReceiptWithRpcFallback(tx: ethers.ContractTransactionResponse, rpcProvider: ethers.JsonRpcProvider, options?: {
57
+ pollIntervalMs?: number;
58
+ maxAttempts?: number;
59
+ }): Promise<ethers.TransactionReceipt>;
47
60
  /**
48
61
  * Get contract address for a given chain ID
49
62
  * @param chainId - Chain ID as string or number
@@ -4,6 +4,7 @@ exports.getSdkRpcProvider = getSdkRpcProvider;
4
4
  exports.resolveChainId = resolveChainId;
5
5
  exports.createContractWithSdkRpc = createContractWithSdkRpc;
6
6
  exports.createContractWithSdkRpcAndSigner = createContractWithSdkRpcAndSigner;
7
+ exports.waitForTransactionReceiptWithRpcFallback = waitForTransactionReceiptWithRpcFallback;
7
8
  exports.getContractAddress = getContractAddress;
8
9
  const ethers_1 = require("ethers");
9
10
  const config_1 = require("../config");
@@ -103,8 +104,48 @@ async function createContractWithSdkRpcAndSigner(contractAddress, abi, signer, c
103
104
  contract,
104
105
  contractWithSigner,
105
106
  chainId: resolvedChainId,
107
+ rpcProvider,
106
108
  };
107
109
  }
110
+ /**
111
+ * Wait for a transaction receipt, falling back to eth_getTransactionReceipt if tx.wait fails
112
+ * This helps when the user's wallet/provider does not implement getTransactionReceipt properly.
113
+ * @param tx - Transaction response returned from contract call
114
+ * @param rpcProvider - SDK RPC provider that supports eth_getTransactionReceipt
115
+ * @param options - Optional polling configuration
116
+ * @returns Transaction receipt once available
117
+ */
118
+ async function waitForTransactionReceiptWithRpcFallback(tx, rpcProvider, options) {
119
+ const pollIntervalMs = options?.pollIntervalMs ?? 3000;
120
+ const maxAttempts = options?.maxAttempts ?? 40;
121
+ try {
122
+ const directReceipt = await tx.wait();
123
+ if (directReceipt) {
124
+ return directReceipt;
125
+ }
126
+ console.warn(`tx.wait() returned null receipt for ${tx.hash}; falling back to eth_getTransactionReceipt via SDK RPC provider.`);
127
+ }
128
+ catch (waitError) {
129
+ console.warn('tx.wait() failed; falling back to eth_getTransactionReceipt via SDK RPC provider.', waitError);
130
+ }
131
+ const txHash = tx.hash;
132
+ if (!txHash) {
133
+ throw new Error('Transaction hash missing; cannot fetch receipt');
134
+ }
135
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
136
+ const receipt = await rpcProvider
137
+ .send('eth_getTransactionReceipt', [txHash])
138
+ .catch((rpcError) => {
139
+ console.warn('eth_getTransactionReceipt RPC call failed:', rpcError);
140
+ return null;
141
+ });
142
+ if (receipt) {
143
+ return receipt;
144
+ }
145
+ await new Promise((resolve) => setTimeout(resolve, pollIntervalMs));
146
+ }
147
+ throw new Error(`Transaction receipt not found for ${txHash} after ${maxAttempts} attempts using eth_getTransactionReceipt`);
148
+ }
108
149
  /**
109
150
  * Get contract address for a given chain ID
110
151
  * @param chainId - Chain ID as string or number
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sdk-triggerx",
3
- "version": "0.1.30",
3
+ "version": "0.1.31",
4
4
  "description": "SDK for interacting with the TriggerX backend and smart contracts.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",