@@ -67,6 +67,7 @@ import type {
6767 TestConfig ,
6868 TransactionEvent ,
6969 TransactionReceipt ,
70+ TxOptions ,
7071 WalletClient as UnifiedWalletClient ,
7172 UnwatchFunction ,
7273 WalletConfig ,
@@ -197,11 +198,24 @@ export class CoreClient implements ChainClient {
197198 ) ;
198199 }
199200
200- async waitForTransaction ( hash : string ) : Promise < TransactionReceipt > {
201+ /**
202+ * Wait for a transaction to be confirmed.
203+ * @param hash - Transaction hash.
204+ * @param timeout - Timeout in milliseconds (default: 30 000). Pass `0` for no timeout.
205+ */
206+ async waitForTransaction (
207+ hash : string ,
208+ timeout ?: number
209+ ) : Promise < TransactionReceipt > {
201210 try {
202- const receipt = await this . publicClient . waitForTransactionReceipt ( {
211+ const opts : Record < string , unknown > = {
203212 hash : hash as `0x${string } `,
204- } ) ;
213+ } ;
214+ const t = timeout ?? 30_000 ;
215+ if ( t > 0 ) opts . timeout = t ;
216+ const receipt = await this . publicClient . waitForTransactionReceipt (
217+ opts as { hash : `0x${string } `; timeout ?: number }
218+ ) ;
205219
206220 return {
207221 hash : receipt . transactionHash ,
@@ -231,6 +245,75 @@ export class CoreClient implements ChainClient {
231245 }
232246 }
233247
248+ /**
249+ * Get the raw balance as a bigint (in Drip, 1 CFX = 10^18 Drip).
250+ * Use this when you need the exact value for arithmetic.
251+ */
252+ async getBalanceRaw ( address : Address ) : Promise < bigint > {
253+ if ( ! isCoreAddress ( address ) ) {
254+ throw new NodeError (
255+ 'Invalid Core address format' ,
256+ 'INVALID_ADDRESS' ,
257+ 'core'
258+ ) ;
259+ }
260+ try {
261+ return await this . publicClient . getBalance ( { address } ) ;
262+ } catch ( error ) {
263+ throw new NodeError (
264+ `Failed to get balance: ${ error instanceof Error ? error . message : String ( error ) } ` ,
265+ 'BALANCE_ERROR' ,
266+ 'core' ,
267+ { address, originalError : error }
268+ ) ;
269+ }
270+ }
271+
272+ /**
273+ * Get the chain ID from the connected node.
274+ */
275+ async getChainId ( ) : Promise < number > {
276+ try {
277+ const status = await this . publicClient . getStatus ( ) ;
278+ return Number ( status . chainId ) ;
279+ } catch ( error ) {
280+ throw new NodeError (
281+ `Failed to get chain ID: ${ error instanceof Error ? error . message : String ( error ) } ` ,
282+ 'CHAIN_ID_ERROR' ,
283+ 'core' ,
284+ { originalError : error }
285+ ) ;
286+ }
287+ }
288+
289+ /**
290+ * Read a contract function (view/pure). Available on the public client —
291+ * no wallet / private key required.
292+ */
293+ async callContract < T = unknown > (
294+ address : string ,
295+ abi : unknown [ ] ,
296+ functionName : string ,
297+ args : unknown [ ] = [ ]
298+ ) : Promise < T > {
299+ try {
300+ const result = await this . publicClient . readContract ( {
301+ address : address as Address ,
302+ abi,
303+ functionName,
304+ args,
305+ } ) ;
306+ return result as T ;
307+ } catch ( error ) {
308+ throw new NodeError (
309+ `Failed to call contract: ${ error instanceof Error ? error . message : String ( error ) } ` ,
310+ 'CONTRACT_CALL_ERROR' ,
311+ 'core' ,
312+ { address, functionName, args, originalError : error }
313+ ) ;
314+ }
315+ }
316+
234317 async getTokenBalance (
235318 tokenAddress : Address ,
236319 holderAddress ?: Address
@@ -510,16 +593,19 @@ export class CoreWalletClient implements UnifiedWalletClient {
510593 }
511594 }
512595
513- /** @internal */
514- getInternalClient ( ) : WalletClient {
515- return this . walletClient ;
516- }
517-
518- async waitForTransaction ( hash : string ) : Promise < TransactionReceipt > {
596+ /**
597+ * Wait for a transaction to be confirmed.
598+ * @param hash - Transaction hash.
599+ * @param timeout - Timeout in milliseconds (default: 30 000).
600+ */
601+ async waitForTransaction (
602+ hash : string ,
603+ timeout ?: number
604+ ) : Promise < TransactionReceipt > {
519605 try {
520606 const receipt = await this . publicClient . waitForTransactionReceipt ( {
521607 hash : hash as `0x${string } `,
522- timeout : 5_000 , // 5 second timeout for faster response
608+ timeout : timeout ?? 30_000 ,
523609 } ) ;
524610
525611 return {
@@ -668,7 +754,8 @@ export class CoreWalletClient implements UnifiedWalletClient {
668754 async deployContract (
669755 abi : unknown [ ] ,
670756 bytecode : string ,
671- constructorArgs : unknown [ ] = [ ]
757+ constructorArgs : unknown [ ] = [ ] ,
758+ options ?: TxOptions
672759 ) : Promise < string > {
673760 try {
674761 const hash = await this . walletClient . deployContract ( {
@@ -677,10 +764,12 @@ export class CoreWalletClient implements UnifiedWalletClient {
677764 abi,
678765 bytecode : bytecode as `0x${string } `,
679766 args : constructorArgs ,
767+ gas : options ?. gasLimit ,
768+ gasPrice : options ?. gasPrice ,
680769 } ) ;
681770
682771 // Wait for transaction to be mined and get the contract address
683- const receipt = await this . waitForTransaction ( hash ) ;
772+ const receipt = await this . waitForTransaction ( hash , options ?. timeout ) ;
684773
685774 if ( ! receipt . contractAddress ) {
686775 throw new Error ( 'Contract address not found in transaction receipt' ) ;
@@ -757,6 +846,43 @@ export class CoreWalletClient implements UnifiedWalletClient {
757846 ) ;
758847 }
759848 }
849+
850+ /**
851+ * Write to a contract and wait for the transaction to be confirmed.
852+ * Convenience wrapper around `writeContract` + `waitForTransaction`.
853+ */
854+ async writeAndWait (
855+ address : string ,
856+ abi : unknown [ ] ,
857+ functionName : string ,
858+ args : unknown [ ] = [ ] ,
859+ value ?: bigint ,
860+ options ?: TxOptions
861+ ) : Promise < TransactionReceipt > {
862+ const hash = await this . writeContract (
863+ address ,
864+ abi ,
865+ functionName ,
866+ args ,
867+ value
868+ ) ;
869+ return this . waitForTransaction ( hash , options ?. timeout ) ;
870+ }
871+
872+ /** @internal */
873+ getInternalClient ( ) : WalletClient {
874+ return this . walletClient ;
875+ }
876+
877+ /** Access the underlying cive WalletClient for advanced operations. */
878+ get civeWalletClient ( ) : WalletClient {
879+ return this . walletClient ;
880+ }
881+
882+ /** Access the underlying cive PublicClient for advanced read operations. */
883+ get civePublicClient ( ) : PublicClient {
884+ return this . publicClient ;
885+ }
760886}
761887
762888/**
0 commit comments