Class Contract
Indexable
- [key: string]: unknown
Index
Constructors
Properties
Methods
Constructors
constructor
- new Contract(
address: string,
abi: string | ContractABI,
signerOrProvider?: Signer | Provider,
): ContractParameters
- address: string
- abi: string | ContractABI
Optional
signerOrProvider: Signer | Provider
Returns Contract
Properties
Readonly
address
Readonly
interface
Readonly
populateTransaction
Optional
Readonly
provider
Optional
Readonly
signer
Methods
attach
Attach contract to a different address
Creates a new Contract instance with the same ABI but pointing to a different contract address. This is useful when you have multiple instances of the same contract deployed at different addresses.
Parameters
- address: string
The new contract address
Returns Contract
New Contract instance with the same ABI but different address
Example
const tokenABI = [...] // ERC20-like token ABI
const token1 = new Contract('klv1token1...', tokenABI, wallet)
// Create a new instance pointing to a different token
const token2 = token1.attach('klv1token2...')
// Both use the same ABI but interact with different contracts
const balance1 = await token1.balanceOf(address)
const balance2 = await token2.balanceOf(address)- address: string
call
Call a contract function dynamically Useful when function name is determined at runtime
Supports passing CallOptions as the last argument for transaction customization (value, nonce, fees, chainId, permissionId, data, etc.)
Type Parameters
Parameters
- functionName: string
The name of the function to call
- ...args: unknown[]
Arguments to pass to the function (last arg can be CallOptions)
Returns Promise<T>
- functionName: string
callRaw
Query a contract function and return raw result (unparsed)
This method is useful when you need access to the raw returnData from a contract query, including metadata like returnCode, returnMessage, and gas information.
Parameters
- functionName: string
The name of the readonly function to query
- ...args: unknown[]
Arguments to pass to the function
Returns Promise<IContractQueryResult>
Raw query result with returnData, returnCode, returnMessage, and gas info
Example
// Query a contract and get raw results
const result = await contract.callRaw('getBalance', address)
console.log(result.data?.returnData) // ['AQ==', 'BQ==']
console.log(result.data?.returnCode) // 'ok'
console.log(result.data?.gasRemaining) // 500000n
console.log(result.data?.returnMessage) // 'Success'- functionName: string
connect
Connect contract to a different signer or provider
Creates a new Contract instance with the same ABI and address but connected to a different signer or provider. This is useful when you want to interact with the same contract using different accounts.
Parameters
- signerOrProvider: Signer | Provider
The signer or provider to connect to
Returns Contract
New Contract instance with the same ABI and address
- signerOrProvider: Signer | Provider
getEventIdentifiers
Get unique event identifiers from transaction logs
Parameters
Optional
logs: TransactionLogTransaction logs
Returns string[]
Array of unique event identifiers
getFunctions
Get all available function names
Returns an array of all function names defined in the contract's ABI, including both readonly (query) and mutable (transaction) functions.
Returns string[]
Array of function names
hasFunction
Check if contract has a function
Verifies whether the contract's ABI includes a function with the specified name.
Parameters
- functionName: string
The name of the function to check
Returns boolean
True if the function exists in the contract's ABI, false otherwise
- functionName: string
invoke
Invoke a mutable contract function (state-changing transaction) This method explicitly requires the function to be mutable.
Supports passing CallOptions as the last argument for transaction customization (value, nonce, fees, chainId, permissionId, data, etc.)
Parameters
- functionName: string
The name of the mutable function to invoke
- ...args: unknown[]
Arguments to pass to the function (last arg can be CallOptions)
Returns Promise<TransactionSubmitResult>
Transaction submit result with hash and wait() method
Example
// Basic invocation with individual args
const result = await contract.invoke('bet', betType, betValue)
await result.wait() // Wait for confirmation
// With array of args (will be spread automatically)
const params = [betType, betValue]
const result = await contract.invoke('bet', ...params)
// With CallOptions (value, nonce, fees, etc.)
const result = await contract.invoke('bet', betType, betValue, {
value: parseKLV('10'), // Send 10 KLV with call
nonce: 123,
fees: { kAppFee: 100 }
})
// With array of args AND CallOptions
const result = await contract.invoke('bet', ...params, {
value: parseKLV('10')
})- functionName: string
parseEvents
Parse events from transaction logs
Parameters
Optional
logs: TransactionLogTransaction logs from a transaction response
Optional
filter: ContractEventFilterOptional filter to apply (identifier, address, topics)
Returns ContractEvent[]
Array of parsed events
Example
// Parse all events from a transaction
const tx = await provider.getTransaction(hash)
const events = contract.parseEvents(tx.logs)
// Filter events by identifier
const betEvents = contract.parseEvents(tx.logs, {
identifier: 'BetPlaced'
})
// Filter events from this contract only
const contractEvents = contract.parseEvents(tx.logs, {
address: contract.address
})
toString
Get contract info
Returns string
Contract class with dynamic methods
Remarks
The Contract class provides two types of interactions:
Query (Readonly): Functions marked as 'readonly' in the ABI
call()
Invoke (Mutable): Functions that modify contract state
wait()
methodinvoke()
See