@mento-protocol/mento-sdk 2.0.0-beta.7 → 2.0.0-beta.9

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.
Files changed (90) hide show
  1. package/README.md +177 -0
  2. package/dist/abis/broker.d.ts.map +1 -1
  3. package/dist/abis/broker.js +7 -0
  4. package/dist/abis/broker.js.map +1 -1
  5. package/dist/abis/index.d.ts +1 -0
  6. package/dist/abis/index.d.ts.map +1 -1
  7. package/dist/abis/index.js +1 -0
  8. package/dist/abis/index.js.map +1 -1
  9. package/dist/adapters/implementations/ethersAdapter.d.ts +5 -1
  10. package/dist/adapters/implementations/ethersAdapter.d.ts.map +1 -1
  11. package/dist/adapters/implementations/ethersAdapter.js +73 -0
  12. package/dist/adapters/implementations/ethersAdapter.js.map +1 -1
  13. package/dist/adapters/implementations/ethersV5Adapter.d.ts +5 -1
  14. package/dist/adapters/implementations/ethersV5Adapter.d.ts.map +1 -1
  15. package/dist/adapters/implementations/ethersV5Adapter.js +73 -0
  16. package/dist/adapters/implementations/ethersV5Adapter.js.map +1 -1
  17. package/dist/adapters/implementations/viemAdapter.d.ts +12 -3
  18. package/dist/adapters/implementations/viemAdapter.d.ts.map +1 -1
  19. package/dist/adapters/implementations/viemAdapter.js +101 -1
  20. package/dist/adapters/implementations/viemAdapter.js.map +1 -1
  21. package/dist/adapters/proxies/ethersAdapterProxy.d.ts +2 -1
  22. package/dist/adapters/proxies/ethersAdapterProxy.d.ts.map +1 -1
  23. package/dist/adapters/proxies/ethersAdapterProxy.js +7 -0
  24. package/dist/adapters/proxies/ethersAdapterProxy.js.map +1 -1
  25. package/dist/adapters/proxies/ethersV5AdapterProxy.d.ts +2 -1
  26. package/dist/adapters/proxies/ethersV5AdapterProxy.d.ts.map +1 -1
  27. package/dist/adapters/proxies/ethersV5AdapterProxy.js +7 -0
  28. package/dist/adapters/proxies/ethersV5AdapterProxy.js.map +1 -1
  29. package/dist/adapters/proxies/viemAdapterProxy.d.ts +7 -3
  30. package/dist/adapters/proxies/viemAdapterProxy.d.ts.map +1 -1
  31. package/dist/adapters/proxies/viemAdapterProxy.js +11 -4
  32. package/dist/adapters/proxies/viemAdapterProxy.js.map +1 -1
  33. package/dist/constants/aaveConfig.d.ts +7 -0
  34. package/dist/constants/aaveConfig.d.ts.map +1 -0
  35. package/dist/constants/aaveConfig.js +22 -0
  36. package/dist/constants/aaveConfig.js.map +1 -0
  37. package/dist/constants/index.d.ts +1 -0
  38. package/dist/constants/index.d.ts.map +1 -1
  39. package/dist/constants/index.js +1 -0
  40. package/dist/constants/index.js.map +1 -1
  41. package/dist/constants/mentoAddresses.d.ts +1 -0
  42. package/dist/constants/mentoAddresses.d.ts.map +1 -1
  43. package/dist/constants/mentoAddresses.js +1 -0
  44. package/dist/constants/mentoAddresses.js.map +1 -1
  45. package/dist/index.d.ts +66 -2
  46. package/dist/index.d.ts.map +1 -1
  47. package/dist/index.js +75 -3
  48. package/dist/index.js.map +1 -1
  49. package/dist/services/index.d.ts +1 -0
  50. package/dist/services/index.d.ts.map +1 -1
  51. package/dist/services/index.js +1 -0
  52. package/dist/services/index.js.map +1 -1
  53. package/dist/services/stableTokenService.d.ts.map +1 -1
  54. package/dist/services/stableTokenService.js +2 -1
  55. package/dist/services/stableTokenService.js.map +1 -1
  56. package/dist/services/supply/ISupplyCalculator.d.ts +10 -0
  57. package/dist/services/supply/ISupplyCalculator.d.ts.map +1 -1
  58. package/dist/services/supply/aaveSupplyCalculator.d.ts +21 -0
  59. package/dist/services/supply/aaveSupplyCalculator.d.ts.map +1 -0
  60. package/dist/services/supply/aaveSupplyCalculator.js +46 -0
  61. package/dist/services/supply/aaveSupplyCalculator.js.map +1 -0
  62. package/dist/services/supply/calculatorFactory.d.ts +8 -0
  63. package/dist/services/supply/calculatorFactory.d.ts.map +1 -0
  64. package/dist/services/supply/calculatorFactory.js +3 -0
  65. package/dist/services/supply/calculatorFactory.js.map +1 -0
  66. package/dist/services/supply/defaultCalculatorFactory.d.ts +9 -0
  67. package/dist/services/supply/defaultCalculatorFactory.d.ts.map +1 -0
  68. package/dist/services/supply/defaultCalculatorFactory.js +23 -0
  69. package/dist/services/supply/defaultCalculatorFactory.js.map +1 -0
  70. package/dist/services/supply/index.d.ts +4 -1
  71. package/dist/services/supply/index.d.ts.map +1 -1
  72. package/dist/services/supply/index.js +4 -1
  73. package/dist/services/supply/index.js.map +1 -1
  74. package/dist/services/supply/{UniV3SupplyCalculator.d.ts → uniV3SupplyCalculator.d.ts} +1 -1
  75. package/dist/services/supply/{UniV3SupplyCalculator.d.ts.map → uniV3SupplyCalculator.d.ts.map} +1 -1
  76. package/dist/services/supply/{UniV3SupplyCalculator.js → uniV3SupplyCalculator.js} +1 -1
  77. package/dist/services/supply/{UniV3SupplyCalculator.js.map → uniV3SupplyCalculator.js.map} +1 -1
  78. package/dist/services/supplyAdjustmentService.d.ts +4 -2
  79. package/dist/services/supplyAdjustmentService.d.ts.map +1 -1
  80. package/dist/services/supplyAdjustmentService.js +19 -8
  81. package/dist/services/supplyAdjustmentService.js.map +1 -1
  82. package/dist/services/swapService.d.ts +70 -0
  83. package/dist/services/swapService.d.ts.map +1 -0
  84. package/dist/services/swapService.js +134 -0
  85. package/dist/services/swapService.js.map +1 -0
  86. package/dist/types/provider.d.ts +18 -0
  87. package/dist/types/provider.d.ts.map +1 -1
  88. package/dist/types/token.d.ts +8 -1
  89. package/dist/types/token.d.ts.map +1 -1
  90. package/package.json +1 -1
package/README.md CHANGED
@@ -120,6 +120,183 @@ Retrieve collateral assets:
120
120
  const assets = await mento.getCollateralAssets()
121
121
  ```
122
122
 
123
+ ### Swap Functionality
124
+
125
+ The SDK provides comprehensive swap functionality, supporting exchanges between collateral assets and stable tokens:
126
+
127
+ #### Provider-Specific Setup for Write Operations
128
+
129
+ Before executing swaps, ensure your provider is set up with the appropriate signing capabilities:
130
+
131
+ **With Ethers v6:**
132
+ ```typescript
133
+ import { JsonRpcProvider, Wallet } from 'ethers'
134
+ import { Mento } from '@mento/sdk'
135
+
136
+ // Create a provider with a signer
137
+ const provider = new JsonRpcProvider('YOUR_RPC_URL')
138
+ const wallet = new Wallet('YOUR_PRIVATE_KEY', provider)
139
+
140
+ // Initialize Mento SDK with the provider+signer
141
+ const mento = await Mento.create({ provider: wallet })
142
+ ```
143
+
144
+ **With Ethers v5:**
145
+ ```typescript
146
+ import { providers, Wallet } from 'ethers-v5'
147
+ import { Mento } from '@mento/sdk'
148
+
149
+ // Create a provider with a signer
150
+ const provider = new providers.JsonRpcProvider('YOUR_RPC_URL')
151
+ const wallet = new Wallet('YOUR_PRIVATE_KEY', provider)
152
+
153
+ // Initialize Mento SDK with the provider+signer
154
+ const mento = await Mento.create({ provider: wallet })
155
+ ```
156
+
157
+ **With Viem:**
158
+ ```typescript
159
+ import { createPublicClient, createWalletClient, http } from 'viem'
160
+ import { privateKeyToAccount } from 'viem/accounts'
161
+ import { celo } from 'viem/chains'
162
+ import { Mento } from '@mento/sdk'
163
+
164
+ // Create public client
165
+ const publicClient = createPublicClient({
166
+ chain: celo,
167
+ transport: http('YOUR_RPC_URL')
168
+ })
169
+
170
+ // Create wallet client
171
+ const account = privateKeyToAccount('0x' + 'YOUR_PRIVATE_KEY')
172
+ const walletClient = createWalletClient({
173
+ account,
174
+ chain: celo,
175
+ transport: http('YOUR_RPC_URL')
176
+ })
177
+
178
+ // Initialize Mento SDK
179
+ const mento = await Mento.create({
180
+ provider: publicClient,
181
+ viemAdapterConfig: { walletClient, account }
182
+ })
183
+ ```
184
+
185
+ #### Swap In (Fixed Input)
186
+
187
+ Exchange a specific amount of input token for at least a minimum amount of output token:
188
+
189
+ ```typescript
190
+ // Get estimated swap amounts
191
+ const amountOut = await mento.getAmountOut(
192
+ celoAddress,
193
+ cUsdAddress,
194
+ '1000000000000000000' // 1 CELO with 18 decimals
195
+ )
196
+ console.log(`Expected amount out: ${amountOut}`)
197
+
198
+ // Calculate slippage (0.5% in this example)
199
+ const minAmountOut = (BigInt(amountOut) * BigInt(995)) / BigInt(1000)
200
+
201
+ // Execute the swap
202
+ const tx = await mento.swapIn(
203
+ celoAddress, // tokenIn (CELO)
204
+ cUsdAddress, // tokenOut (cUSD)
205
+ '1000000000000000000', // amountIn (fixed)
206
+ minAmountOut.toString(), // minimum acceptable output
207
+ { gasLimit: '300000' } // Optional gas settings
208
+ )
209
+
210
+ // Wait for confirmation
211
+ const receipt = await tx.wait()
212
+ console.log('Swap completed!', receipt)
213
+ ```
214
+
215
+ #### Swap Out (Fixed Output)
216
+
217
+ Get a specific amount of output token by spending at most a maximum amount of input token:
218
+
219
+ ```typescript
220
+ // Get required input amount
221
+ const amountIn = await mento.getAmountIn(
222
+ celoAddress,
223
+ cUsdAddress,
224
+ '1000000000000000000' // 1 cUSD with 18 decimals
225
+ )
226
+ console.log(`Required amount in: ${amountIn}`)
227
+
228
+ // Calculate slippage (0.5% in this example)
229
+ const maxAmountIn = (BigInt(amountIn) * BigInt(1005)) / BigInt(1000)
230
+
231
+ // Execute the swap
232
+ const tx = await mento.swapOut(
233
+ celoAddress, // tokenIn (CELO)
234
+ cUsdAddress, // tokenOut (cUSD)
235
+ '1000000000000000000', // amountOut (fixed)
236
+ maxAmountIn.toString(), // maximum acceptable input
237
+ { gasLimit: '350000' } // Optional gas settings
238
+ )
239
+
240
+ // Wait for confirmation
241
+ const receipt = await tx.wait()
242
+ console.log('Swap completed!', receipt)
243
+ ```
244
+
245
+ #### Gas Estimation
246
+
247
+ Estimate gas costs for a swap operation:
248
+
249
+ ```typescript
250
+ const estimatedGas = await mento.estimateGas(
251
+ 'swapIn', // or 'swapOut'
252
+ [celoAddress, cUsdAddress, '1000000000000000000', minAmountOut.toString()]
253
+ )
254
+ console.log(`Estimated gas: ${estimatedGas}`)
255
+
256
+ // Use the estimated gas in your transaction
257
+ const tx = await mento.swapIn(
258
+ celoAddress,
259
+ cUsdAddress,
260
+ '1000000000000000000',
261
+ minAmountOut.toString(),
262
+ { gasLimit: estimatedGas }
263
+ )
264
+ ```
265
+
266
+ ## Agentic Coding with Claude Code
267
+
268
+ This project supports agentic coding practices using Claude Code to accelerate development and maintain high-quality standards. Follow these steps to get started:
269
+
270
+ 1. **Prerequisites:**
271
+
272
+ - Ensure you have [Claude Code](https://docs.anthropic.com/en/docs/claude-code/overview) installed and configured globally.
273
+ - Familiarize yourself with the project's coding standards and architectural patterns documented in `ai_docs/project-patterns.md`.
274
+
275
+ 2. **Workflow:**
276
+
277
+ - **Start Claude Code:** Launch your Claude Code interface.
278
+ - **Prime Context:**
279
+ - In Claude Code, type `/prime`.
280
+ - When prompted (or if your tool allows inline commands), execute the command defined in `.claude/commands/context_prime`. This will provide the AI with essential background information about the Mento SDK project.
281
+ - **Generate Feature Specification:**
282
+ - To create a new feature, first generate a detailed specification.
283
+ - In Claude Code, type `/spec` and select the "generate spec" command/prompt.
284
+ - Provide a clear description of the feature you want to implement. The AI will use the templates and examples in the `specs/` directory as a guide.
285
+ - **Review and Refine:** Carefully review the generated spec. Edit and refine it to ensure it accurately and comprehensively captures the feature requirements, edge cases, and acceptance criteria.
286
+ - **Implement Feature from Specification:**
287
+ - Once the spec is finalized, instruct the AI to write the code.
288
+ - In Claude Code, type `/spec` again, but this time select the "generate code from spec" command/prompt.
289
+ - Provide the filename of the spec you just created (e.g., `specs/my-new-feature.md`). You might be able to provide this inline with the command or when prompted by the AI.
290
+ - **Review Generated Code:**
291
+ - Thoroughly review the code generated by the AI. Check for correctness, adherence to coding standards (`ai_docs/project-patterns.md`), and completeness against the spec.
292
+ - Iterate with the AI if necessary, providing feedback for corrections or improvements.
293
+ - **Create a Pull Request (PR):**
294
+ - Once you are satisfied with the code, ask Claude Code to help you create a Pull Request.
295
+ - Ensure the PR description is clear and links to the relevant spec.
296
+ - The PR should then be reviewed by another team member before merging.
297
+
298
+ By following this workflow, you can leverage AI to handle boilerplate, draft implementations, and ensure consistency, allowing you to focus on the more complex and strategic aspects of development.
299
+
123
300
  ## Contributing
124
301
 
125
302
  Contributions are welcome! Please read our contributing guidelines for details.
@@ -1 +1 @@
1
- {"version":3,"file":"broker.d.ts","sourceRoot":"","sources":["../../src/abis/broker.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,UAAU,UAGtB,CAAA"}
1
+ {"version":3,"file":"broker.d.ts","sourceRoot":"","sources":["../../src/abis/broker.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,UAAU,UAYtB,CAAA"}
@@ -2,7 +2,14 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BROKER_ABI = void 0;
4
4
  exports.BROKER_ABI = [
5
+ // Existing methods
5
6
  'function tradingLimitsConfig(bytes32) view returns (uint32 timestep0, uint32 timestep1, int48 limit0, int48 limit1, int48 limitGlobal, uint8 flags)',
6
7
  'function tradingLimitsState(bytes32) view returns (uint32 lastUpdated0, uint32 lastUpdated1, int48 netflow0, int48 netflow1, int48 netflowGlobal)',
8
+ // Swap methods
9
+ 'function swapIn(address tokenIn, address tokenOut, uint256 amountIn, uint256 minAmountOut) payable returns (uint256)',
10
+ 'function swapOut(address tokenIn, address tokenOut, uint256 amountOut, uint256 maxAmountIn) payable returns (uint256)',
11
+ // Rate methods
12
+ 'function getAmountOut(address tokenIn, address tokenOut, uint256 amountIn) view returns (uint256)',
13
+ 'function getAmountIn(address tokenIn, address tokenOut, uint256 amountOut) view returns (uint256)'
7
14
  ];
8
15
  //# sourceMappingURL=broker.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"broker.js","sourceRoot":"","sources":["../../src/abis/broker.ts"],"names":[],"mappings":";;;AAAa,QAAA,UAAU,GAAG;IACxB,qJAAqJ;IACrJ,mJAAmJ;CACpJ,CAAA"}
1
+ {"version":3,"file":"broker.js","sourceRoot":"","sources":["../../src/abis/broker.ts"],"names":[],"mappings":";;;AAAa,QAAA,UAAU,GAAG;IACxB,mBAAmB;IACnB,qJAAqJ;IACrJ,mJAAmJ;IAEnJ,eAAe;IACf,sHAAsH;IACtH,uHAAuH;IAEvH,eAAe;IACf,mGAAmG;IACnG,mGAAmG;CACpG,CAAA"}
@@ -2,4 +2,5 @@ export * from './erc20';
2
2
  export * from './reserve';
3
3
  export * from './bipoolmanager';
4
4
  export * from './uniswapV3';
5
+ export * from './broker';
5
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/abis/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA;AACzB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,aAAa,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/abis/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA;AACzB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,aAAa,CAAA;AAC3B,cAAc,UAAU,CAAA"}
@@ -18,4 +18,5 @@ __exportStar(require("./erc20"), exports);
18
18
  __exportStar(require("./reserve"), exports);
19
19
  __exportStar(require("./bipoolmanager"), exports);
20
20
  __exportStar(require("./uniswapV3"), exports);
21
+ __exportStar(require("./broker"), exports);
21
22
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/abis/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAuB;AACvB,4CAAyB;AACzB,kDAA+B;AAC/B,8CAA2B"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/abis/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAuB;AACvB,4CAAyB;AACzB,kDAA+B;AAC/B,8CAA2B;AAC3B,2CAAwB"}
@@ -1,9 +1,13 @@
1
1
  import { Provider as EthersProvider } from 'ethers';
2
- import { ContractCallOptions, ProviderAdapter } from '../../types';
2
+ import { ContractCallOptions, ContractWriteOptions, ProviderAdapter, TransactionResponse } from '../../types';
3
+ /**
4
+ * Adapter for Ethers.js v6 provider
5
+ */
3
6
  export declare class EthersAdapter implements ProviderAdapter {
4
7
  private provider;
5
8
  constructor(provider: EthersProvider);
6
9
  readContract(options: ContractCallOptions): Promise<unknown>;
10
+ writeContract(options: ContractWriteOptions): Promise<TransactionResponse>;
7
11
  getChainId(): Promise<number>;
8
12
  }
9
13
  //# sourceMappingURL=ethersAdapter.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ethersAdapter.d.ts","sourceRoot":"","sources":["../../../src/adapters/implementations/ethersAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,QAAQ,IAAI,cAAc,EAAE,MAAM,QAAQ,CAAA;AACxE,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAElE,qBAAa,aAAc,YAAW,eAAe;IACvC,OAAO,CAAC,QAAQ;gBAAR,QAAQ,EAAE,cAAc;IAEtC,YAAY,CAAC,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,OAAO,CAAC;IAS5D,UAAU,IAAI,OAAO,CAAC,MAAM,CAAC;CAIpC"}
1
+ {"version":3,"file":"ethersAdapter.d.ts","sourceRoot":"","sources":["../../../src/adapters/implementations/ethersAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,QAAQ,IAAI,cAAc,EAA2B,MAAM,QAAQ,CAAA;AACjG,OAAO,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAA;AAE7G;;GAEG;AACH,qBAAa,aAAc,YAAW,eAAe;IACvC,OAAO,CAAC,QAAQ;gBAAR,QAAQ,EAAE,cAAc;IAEtC,YAAY,CAAC,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,OAAO,CAAC;IAS5D,aAAa,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,mBAAmB,CAAC;IAmF1E,UAAU,IAAI,OAAO,CAAC,MAAM,CAAC;CAIpC"}
@@ -2,6 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.EthersAdapter = void 0;
4
4
  const ethers_1 = require("ethers");
5
+ /**
6
+ * Adapter for Ethers.js v6 provider
7
+ */
5
8
  class EthersAdapter {
6
9
  constructor(provider) {
7
10
  this.provider = provider;
@@ -10,6 +13,76 @@ class EthersAdapter {
10
13
  const contract = new ethers_1.Contract(options.address, options.abi, this.provider);
11
14
  return await contract[options.functionName](...(options.args || []));
12
15
  }
16
+ async writeContract(options) {
17
+ try {
18
+ // For write operations we need a signer
19
+ let signer;
20
+ // Check if the provider is already a signer
21
+ if ('sendTransaction' in this.provider) {
22
+ signer = this.provider;
23
+ }
24
+ // Check if it's a BrowserProvider (can get a signer)
25
+ else if (this.provider instanceof ethers_1.BrowserProvider) {
26
+ signer = await this.provider.getSigner();
27
+ }
28
+ // No signer available
29
+ else {
30
+ throw new Error('Ethers provider must be a Signer or BrowserProvider for write operations. Please initialize with a Wallet or BrowserProvider instance.');
31
+ }
32
+ const contract = new ethers_1.Contract(options.address, options.abi, signer);
33
+ // Prepare transaction options
34
+ const overrides = {};
35
+ if (options.value !== undefined) {
36
+ overrides.value = options.value;
37
+ }
38
+ if (options.gasLimit !== undefined) {
39
+ overrides.gasLimit = options.gasLimit;
40
+ }
41
+ if (options.gasPrice !== undefined) {
42
+ overrides.gasPrice = options.gasPrice;
43
+ }
44
+ if (options.maxFeePerGas !== undefined) {
45
+ overrides.maxFeePerGas = options.maxFeePerGas;
46
+ }
47
+ if (options.maxPriorityFeePerGas !== undefined) {
48
+ overrides.maxPriorityFeePerGas = options.maxPriorityFeePerGas;
49
+ }
50
+ // Try to simulate the transaction first (estimate gas)
51
+ try {
52
+ await contract[options.functionName].estimateGas(...(options.args || []), overrides);
53
+ }
54
+ catch (estimateError) {
55
+ // Format simulation errors nicely
56
+ console.error('Transaction simulation failed:', estimateError);
57
+ throw new Error(`Transaction simulation failed: ${estimateError instanceof Error ? estimateError.message : String(estimateError)}`);
58
+ }
59
+ // Execute transaction
60
+ const tx = await contract[options.functionName](...(options.args || []), overrides);
61
+ // Return a standardized TransactionResponse
62
+ return {
63
+ hash: tx.hash,
64
+ wait: async (confirmations = 1) => {
65
+ try {
66
+ const receipt = await tx.wait(confirmations);
67
+ return {
68
+ transactionHash: receipt.hash,
69
+ blockNumber: receipt.blockNumber,
70
+ status: receipt.status,
71
+ ...receipt
72
+ };
73
+ }
74
+ catch (waitError) {
75
+ console.error('Transaction confirmation failed:', waitError);
76
+ throw new Error(`Transaction failed during confirmation: ${waitError instanceof Error ? waitError.message : String(waitError)}`);
77
+ }
78
+ }
79
+ };
80
+ }
81
+ catch (error) {
82
+ console.error('Transaction execution failed:', error);
83
+ throw error;
84
+ }
85
+ }
13
86
  async getChainId() {
14
87
  const network = await this.provider.getNetwork();
15
88
  return Number(network.chainId);
@@ -1 +1 @@
1
- {"version":3,"file":"ethersAdapter.js","sourceRoot":"","sources":["../../../src/adapters/implementations/ethersAdapter.ts"],"names":[],"mappings":";;;AAAA,mCAAwE;AAGxE,MAAa,aAAa;IACxB,YAAoB,QAAwB;QAAxB,aAAQ,GAAR,QAAQ,CAAgB;IAAG,CAAC;IAEhD,KAAK,CAAC,YAAY,CAAC,OAA4B;QAC7C,MAAM,QAAQ,GAAG,IAAI,iBAAQ,CAC3B,OAAO,CAAC,OAAO,EACf,OAAO,CAAC,GAA2B,EACnC,IAAI,CAAC,QAAQ,CACd,CAAA;QACD,OAAO,MAAM,QAAQ,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,CAAA;IACtE,CAAC;IAED,KAAK,CAAC,UAAU;QACd,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,UAAU,EAAE,CAAA;QAChD,OAAO,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;IAChC,CAAC;CACF;AAhBD,sCAgBC"}
1
+ {"version":3,"file":"ethersAdapter.js","sourceRoot":"","sources":["../../../src/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;wBACH,MAAM,OAAO,GAAG,MAAM,EAAE,CAAC,IAAI,CAAC,aAAa,CAAC,CAAA;wBAC5C,OAAO;4BACL,eAAe,EAAE,OAAO,CAAC,IAAI;4BAC7B,WAAW,EAAE,OAAO,CAAC,WAAW;4BAChC,MAAM,EAAE,OAAO,CAAC,MAAM;4BACtB,GAAG,OAAO;yBACX,CAAA;oBACH,CAAC;oBAAC,OAAO,SAAS,EAAE,CAAC;wBACnB,OAAO,CAAC,KAAK,CAAC,kCAAkC,EAAE,SAAS,CAAC,CAAA;wBAC5D,MAAM,IAAI,KAAK,CAAC,2CAA2C,SAAS,YAAY,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC,CAAA;oBAClI,CAAC;gBACH,CAAC;aACF,CAAA;QACH,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,+BAA+B,EAAE,KAAK,CAAC,CAAA;YACrD,MAAM,KAAK,CAAA;QACb,CAAC;IACH,CAAC;IAED,KAAK,CAAC,UAAU;QACd,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,UAAU,EAAE,CAAA;QAChD,OAAO,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;IAChC,CAAC;CACF;AAnGD,sCAmGC"}
@@ -1,9 +1,13 @@
1
1
  import { providers } from 'ethers-v5';
2
- import { ContractCallOptions, ProviderAdapter } from '../../types';
2
+ import { ContractCallOptions, ContractWriteOptions, ProviderAdapter, TransactionResponse } from '../../types';
3
+ /**
4
+ * Adapter for Ethers.js v5 provider
5
+ */
3
6
  export declare class EthersV5Adapter implements ProviderAdapter {
4
7
  private provider;
5
8
  constructor(provider: providers.Provider);
6
9
  readContract(options: ContractCallOptions): Promise<unknown>;
10
+ writeContract(options: ContractWriteOptions): Promise<TransactionResponse>;
7
11
  getChainId(): Promise<number>;
8
12
  }
9
13
  //# sourceMappingURL=ethersV5Adapter.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ethersV5Adapter.d.ts","sourceRoot":"","sources":["../../../src/adapters/implementations/ethersV5Adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,SAAS,EAAqB,MAAM,WAAW,CAAA;AAClE,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAElE,qBAAa,eAAgB,YAAW,eAAe;IACzC,OAAO,CAAC,QAAQ;gBAAR,QAAQ,EAAE,SAAS,CAAC,QAAQ;IAE1C,YAAY,CAAC,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,OAAO,CAAC;IAS5D,UAAU,IAAI,OAAO,CAAC,MAAM,CAAC;CAIpC"}
1
+ {"version":3,"file":"ethersV5Adapter.d.ts","sourceRoot":"","sources":["../../../src/adapters/implementations/ethersV5Adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,SAAS,EAA6B,MAAM,WAAW,CAAA;AAC1E,OAAO,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAA;AAE7G;;GAEG;AACH,qBAAa,eAAgB,YAAW,eAAe;IACzC,OAAO,CAAC,QAAQ;gBAAR,QAAQ,EAAE,SAAS,CAAC,QAAQ;IAE1C,YAAY,CAAC,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,OAAO,CAAC;IAS5D,aAAa,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,mBAAmB,CAAC;IAmF1E,UAAU,IAAI,OAAO,CAAC,MAAM,CAAC;CAIpC"}
@@ -2,6 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.EthersV5Adapter = void 0;
4
4
  const ethers_v5_1 = require("ethers-v5");
5
+ /**
6
+ * Adapter for Ethers.js v5 provider
7
+ */
5
8
  class EthersV5Adapter {
6
9
  constructor(provider) {
7
10
  this.provider = provider;
@@ -10,6 +13,76 @@ class EthersV5Adapter {
10
13
  const contract = new ethers_v5_1.Contract(options.address, options.abi, this.provider);
11
14
  return await contract[options.functionName](...(options.args || []));
12
15
  }
16
+ async writeContract(options) {
17
+ try {
18
+ // For write operations we need a signer
19
+ let signer;
20
+ // Check if the provider is already a signer
21
+ if ('sendTransaction' in this.provider) {
22
+ signer = this.provider;
23
+ }
24
+ // Check if it's a Web3Provider or similar (has getSigner)
25
+ else if ('getSigner' in this.provider) {
26
+ signer = this.provider.getSigner();
27
+ }
28
+ // No signer available
29
+ else {
30
+ throw new Error('Ethers v5 provider must be a Signer or support getSigner method for write operations. Please initialize with a Wallet or Web3Provider instance.');
31
+ }
32
+ const contract = new ethers_v5_1.Contract(options.address, options.abi, signer);
33
+ // Prepare transaction options
34
+ const overrides = {};
35
+ if (options.value !== undefined) {
36
+ overrides.value = options.value;
37
+ }
38
+ if (options.gasLimit !== undefined) {
39
+ overrides.gasLimit = options.gasLimit;
40
+ }
41
+ if (options.gasPrice !== undefined) {
42
+ overrides.gasPrice = options.gasPrice;
43
+ }
44
+ if (options.maxFeePerGas !== undefined) {
45
+ overrides.maxFeePerGas = options.maxFeePerGas;
46
+ }
47
+ if (options.maxPriorityFeePerGas !== undefined) {
48
+ overrides.maxPriorityFeePerGas = options.maxPriorityFeePerGas;
49
+ }
50
+ // Try to simulate the transaction first (estimate gas)
51
+ try {
52
+ await contract.estimateGas[options.functionName](...(options.args || []), overrides);
53
+ }
54
+ catch (estimateError) {
55
+ // Format simulation errors nicely
56
+ console.error('Transaction simulation failed:', estimateError);
57
+ throw new Error(`Transaction simulation failed: ${estimateError instanceof Error ? estimateError.message : String(estimateError)}`);
58
+ }
59
+ // Execute transaction
60
+ const tx = await contract[options.functionName](...(options.args || []), overrides);
61
+ // Return a standardized TransactionResponse
62
+ return {
63
+ hash: tx.hash,
64
+ wait: async (confirmations = 1) => {
65
+ try {
66
+ const receipt = await tx.wait(confirmations);
67
+ return {
68
+ transactionHash: receipt.hash,
69
+ blockNumber: receipt.blockNumber,
70
+ status: receipt.status,
71
+ ...receipt
72
+ };
73
+ }
74
+ catch (waitError) {
75
+ console.error('Transaction confirmation failed:', waitError);
76
+ throw new Error(`Transaction failed during confirmation: ${waitError instanceof Error ? waitError.message : String(waitError)}`);
77
+ }
78
+ }
79
+ };
80
+ }
81
+ catch (error) {
82
+ console.error('Transaction execution failed:', error);
83
+ throw error;
84
+ }
85
+ }
13
86
  async getChainId() {
14
87
  const network = await this.provider.getNetwork();
15
88
  return Number(network.chainId);
@@ -1 +1 @@
1
- {"version":3,"file":"ethersV5Adapter.js","sourceRoot":"","sources":["../../../src/adapters/implementations/ethersV5Adapter.ts"],"names":[],"mappings":";;;AAAA,yCAAkE;AAGlE,MAAa,eAAe;IAC1B,YAAoB,QAA4B;QAA5B,aAAQ,GAAR,QAAQ,CAAoB;IAAG,CAAC;IAEpD,KAAK,CAAC,YAAY,CAAC,OAA4B;QAC7C,MAAM,QAAQ,GAAG,IAAI,oBAAQ,CAC3B,OAAO,CAAC,OAAO,EACf,OAAO,CAAC,GAAmC,EAC3C,IAAI,CAAC,QAAQ,CACd,CAAA;QACD,OAAO,MAAM,QAAQ,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,CAAA;IACtE,CAAC;IAED,KAAK,CAAC,UAAU;QACd,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,UAAU,EAAE,CAAA;QAChD,OAAO,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;IAChC,CAAC;CACF;AAhBD,0CAgBC"}
1
+ {"version":3,"file":"ethersV5Adapter.js","sourceRoot":"","sources":["../../../src/adapters/implementations/ethersV5Adapter.ts"],"names":[],"mappings":";;;AAAA,yCAA0E;AAG1E;;GAEG;AACH,MAAa,eAAe;IAC1B,YAAoB,QAA4B;QAA5B,aAAQ,GAAR,QAAQ,CAAoB;IAAG,CAAC;I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W;4BAChC,MAAM,EAAE,OAAO,CAAC,MAAM;4BACtB,GAAG,OAAO;yBACX,CAAA;oBACH,CAAC;oBAAC,OAAO,SAAS,EAAE,CAAC;wBACnB,OAAO,CAAC,KAAK,CAAC,kCAAkC,EAAE,SAAS,CAAC,CAAA;wBAC5D,MAAM,IAAI,KAAK,CAAC,2CAA2C,SAAS,YAAY,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC,CAAA;oBAClI,CAAC;gBACH,CAAC;aACF,CAAA;QACH,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,+BAA+B,EAAE,KAAK,CAAC,CAAA;YACrD,MAAM,KAAK,CAAA;QACb,CAAC;IACH,CAAC;IAED,KAAK,CAAC,UAAU;QACd,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,UAAU,EAAE,CAAA;QAChD,OAAO,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;IAChC,CAAC;CACF;AAnGD,0CAmGC"}
@@ -1,9 +1,18 @@
1
- import { type PublicClient } from 'viem';
2
- import type { ContractCallOptions, ProviderAdapter } from '../../types';
1
+ import { type PublicClient, type WalletClient, type Account } from 'viem';
2
+ import type { ContractCallOptions, ContractWriteOptions, ProviderAdapter, TransactionResponse } from '../../types';
3
+ /**
4
+ * Adapter for Viem provider
5
+ */
3
6
  export declare class ViemAdapter implements ProviderAdapter {
4
7
  private client;
5
- constructor(client: PublicClient);
8
+ private walletClient?;
9
+ private account?;
10
+ constructor(client: PublicClient, walletClientConfig?: {
11
+ walletClient: WalletClient;
12
+ account: Account;
13
+ });
6
14
  readContract(options: ContractCallOptions): Promise<unknown>;
15
+ writeContract(options: ContractWriteOptions): Promise<TransactionResponse>;
7
16
  getChainId(): Promise<number>;
8
17
  }
9
18
  //# sourceMappingURL=viemAdapter.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"viemAdapter.d.ts","sourceRoot":"","sources":["../../../src/adapters/implementations/viemAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,YAAY,EAAE,MAAM,MAAM,CAAA;AAClD,OAAO,KAAK,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAEvE,qBAAa,WAAY,YAAW,eAAe;IACrC,OAAO,CAAC,MAAM;gBAAN,MAAM,EAAE,YAAY;IAElC,YAAY,CAAC,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,OAAO,CAAC;IAe5D,UAAU,IAAI,OAAO,CAAC,MAAM,CAAC;CAGpC"}
1
+ {"version":3,"file":"viemAdapter.d.ts","sourceRoot":"","sources":["../../../src/adapters/implementations/viemAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,OAAO,EAEb,MAAM,MAAM,CAAA;AACb,OAAO,KAAK,EACV,mBAAmB,EACnB,oBAAoB,EACpB,eAAe,EACf,mBAAmB,EAEpB,MAAM,aAAa,CAAA;AAEpB;;GAEG;AACH,qBAAa,WAAY,YAAW,eAAe;IAK/C,OAAO,CAAC,MAAM;IAJhB,OAAO,CAAC,YAAY,CAAC,CAAc;IACnC,OAAO,CAAC,OAAO,CAAC,CAAS;gBAGf,MAAM,EAAE,YAAY,EAC5B,kBAAkB,CAAC,EAAE;QAAE,YAAY,EAAE,YAAY,CAAC;QAAC,OAAO,EAAE,OAAO,CAAA;KAAE;IAQjE,YAAY,CAAC,OAAO,EAAE,mBAAmB,GAAG,OAAO,CAAC,OAAO,CAAC;IAe5D,aAAa,CACjB,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,mBAAmB,CAAC;IA6GzB,UAAU,IAAI,OAAO,CAAC,MAAM,CAAC;CAGpC"}
@@ -2,9 +2,16 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ViemAdapter = void 0;
4
4
  const viem_1 = require("viem");
5
+ /**
6
+ * Adapter for Viem provider
7
+ */
5
8
  class ViemAdapter {
6
- constructor(client) {
9
+ constructor(client, walletClientConfig) {
7
10
  this.client = client;
11
+ if (walletClientConfig) {
12
+ this.walletClient = walletClientConfig.walletClient;
13
+ this.account = walletClientConfig.account;
14
+ }
8
15
  }
9
16
  async readContract(options) {
10
17
  // Check if the abi is a string array(human readable format) or an array of objects(the normal format)
@@ -18,6 +25,99 @@ class ViemAdapter {
18
25
  args: options.args,
19
26
  });
20
27
  }
28
+ async writeContract(options) {
29
+ if (!this.walletClient || !this.account) {
30
+ throw new Error('WalletClient and account are required for write operations with Viem');
31
+ }
32
+ // Check if the abi is a string array(human readable format) or an array of objects(the normal format)
33
+ const abi = Array.isArray(options.abi) && typeof options.abi[0] === 'string'
34
+ ? (0, viem_1.parseAbi)(options.abi)
35
+ : options.abi;
36
+ // Prepare common parameters
37
+ const baseParams = {
38
+ address: options.address,
39
+ abi: abi,
40
+ functionName: options.functionName,
41
+ args: options.args,
42
+ };
43
+ // Add optional parameters
44
+ const txParams = {};
45
+ if (options.value !== undefined) {
46
+ txParams.value =
47
+ typeof options.value === 'string'
48
+ ? BigInt(options.value)
49
+ : options.value;
50
+ }
51
+ if (options.gasLimit !== undefined) {
52
+ txParams.gas =
53
+ typeof options.gasLimit === 'string'
54
+ ? BigInt(options.gasLimit)
55
+ : options.gasLimit;
56
+ }
57
+ if (options.gasPrice !== undefined) {
58
+ txParams.gasPrice =
59
+ typeof options.gasPrice === 'string'
60
+ ? BigInt(options.gasPrice)
61
+ : options.gasPrice;
62
+ }
63
+ if (options.maxFeePerGas !== undefined) {
64
+ txParams.maxFeePerGas =
65
+ typeof options.maxFeePerGas === 'string'
66
+ ? BigInt(options.maxFeePerGas)
67
+ : options.maxFeePerGas;
68
+ }
69
+ if (options.maxPriorityFeePerGas !== undefined) {
70
+ txParams.maxPriorityFeePerGas =
71
+ typeof options.maxPriorityFeePerGas === 'string'
72
+ ? BigInt(options.maxPriorityFeePerGas)
73
+ : options.maxPriorityFeePerGas;
74
+ }
75
+ // Simulate first to check for errors
76
+ try {
77
+ // For simulation, we need the account but not the chain
78
+ await this.client.simulateContract({
79
+ ...baseParams,
80
+ ...txParams,
81
+ account: this.account,
82
+ });
83
+ }
84
+ catch (error) {
85
+ console.error('Contract simulation failed:', error);
86
+ throw error;
87
+ }
88
+ // Execute transaction
89
+ // For write operations, we need both account and chain
90
+ const hash = await this.walletClient.writeContract({
91
+ ...baseParams,
92
+ ...txParams,
93
+ account: this.account,
94
+ chain: null, // Required by Viem type system
95
+ });
96
+ // Return a standardized TransactionResponse
97
+ return {
98
+ hash: hash,
99
+ wait: async (confirmations = 1) => {
100
+ const viemReceipt = await this.client.waitForTransactionReceipt({
101
+ hash,
102
+ confirmations,
103
+ });
104
+ // Convert Viem receipt to our standardized TransactionReceipt
105
+ const receipt = {
106
+ transactionHash: viemReceipt.transactionHash,
107
+ blockNumber: Number(viemReceipt.blockNumber),
108
+ status: viemReceipt.status === 'success' ? 1 : 0,
109
+ gasUsed: String(viemReceipt.gasUsed),
110
+ cumulativeGasUsed: String(viemReceipt.cumulativeGasUsed),
111
+ logs: viemReceipt.logs,
112
+ effectiveGasPrice: viemReceipt.effectiveGasPrice
113
+ ? String(viemReceipt.effectiveGasPrice)
114
+ : undefined,
115
+ contractAddress: viemReceipt.contractAddress,
116
+ };
117
+ return receipt;
118
+ },
119
+ };
120
+ }
21
121
  async getChainId() {
22
122
  return await this.client.getChainId();
23
123
  }
@@ -1 +1 @@
1
- {"version":3,"file":"viemAdapter.js","sourceRoot":"","sources":["../../../src/adapters/implementations/viemAdapter.ts"],"names":[],"mappings":";;;AAAA,+BAAkD;AAGlD,MAAa,WAAW;IACtB,YAAoB,MAAoB;QAApB,WAAM,GAAN,MAAM,CAAc;IAAG,CAAC;IAE5C,KAAK,CAAC,YAAY,CAAC,OAA4B;QAC7C,sGAAsG;QACtG,MAAM,GAAG,GACP,KAAK,CAAC,OAAO,CAA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U;YACb,GAAG,QAAQ;YACX,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,KAAK,EAAE,IAAI,EAAE,+BAA+B;SAC7C,CAAC,CAAA;QAEF,4CAA4C;QAC5C,OAAO;YACL,IAAI,EAAE,IAAI;YACV,IAAI,EAAE,KAAK,EAAE,aAAa,GAAG,CAAC,EAAE,EAAE;gBAChC,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,yBAAyB,CAAC;oBAC9D,IAAI;oBACJ,aAAa;iBACd,CAAC,CAAA;gBAEF,8DAA8D;gBAC9D,MAAM,OAAO,GAAuB;oBAClC,eAAe,EAAE,WAAW,CAAC,eAAe;oBAC5C,WAAW,EAAE,MAAM,CAAC,WAAW,CAAC,WAAW,CAAC;oBAC5C,MAAM,EAAE,WAAW,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;oBAChD,OAAO,EAAE,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC;oBACpC,iBAAiB,EAAE,MAAM,CAAC,WAAW,CAAC,iBAAiB,CAAC;oBACxD,IAAI,EAAE,WAAW,CAAC,IAAI;oBACtB,iBAAiB,EAAE,WAAW,CAAC,iBAAiB;wBAC9C,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,iBAAiB,CAAC;wBACvC,CAAC,CAAC,SAAS;oBACb,eAAe,EAAE,WAAW,CAAC,eAAe;iBAC7C,CAAA;gBAED,OAAO,OAAO,CAAA;YAChB,CAAC;SACF,CAAA;IACH,CAAC;IAED,KAAK,CAAC,UAAU;QACd,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,CAAA;IACvC,CAAC;CACF;AA/ID,kCA+IC"}
@@ -1,4 +1,4 @@
1
- import { ProviderAdapter } from '../../types';
1
+ import { ProviderAdapter, TransactionResponse } from '../../types';
2
2
  import type { Provider as EthersV6Provider } from 'ethers';
3
3
  /**
4
4
  * Proxy class that implements lazy loading for ethers provider adapter.
@@ -20,6 +20,7 @@ export declare class EthersAdapterProxy implements ProviderAdapter {
20
20
  constructor(provider: EthersV6Provider);
21
21
  private initialize;
22
22
  readContract(...args: Parameters<ProviderAdapter['readContract']>): Promise<unknown>;
23
+ writeContract(...args: Parameters<ProviderAdapter['writeContract']>): Promise<TransactionResponse>;
23
24
  getChainId(): Promise<number>;
24
25
  }
25
26
  //# sourceMappingURL=ethersAdapterProxy.d.ts.map