Class ContractFactory
Index
Constructors
Properties
Methods
Constructors
constructor
- new ContractFactory(
abi: string | ContractABI,
bytecode: string | Uint8Array<ArrayBufferLike>,
signer: Signer,
): ContractFactoryParameters
- abi: string | ContractABI
- bytecode: string | Uint8Array<ArrayBufferLike>
- signer: Signer
Returns ContractFactory
Properties
Readonlybytecode
Readonlyinterface
Optional Readonlyprovider
Readonlysigner
Methods
attach
Attach to existing deployed contract
Creates a new Contract instance pointing to an already-deployed contract at the specified address. This is useful when you have the ABI and bytecode for a contract type and want to interact with an existing instance.
Parameters
- address: string
The address of the deployed contract
Returns Contract
A new Contract instance connected to the deployed contract
Example
const factory = new ContractFactory(abi, bytecode, wallet)
// Attach to an existing contract
const existingContract = factory.attach('klv1contract...')
// Now you can interact with it
const result = await existingContract.balanceOf(address)
await existingContract.transfer(toAddress, amount)Example: Interacting with multiple instances
const tokenFactory = new ContractFactory(tokenABI, tokenBytecode, wallet)
// Attach to different token instances
const token1 = tokenFactory.attach('klv1token1...')
const token2 = tokenFactory.attach('klv1token2...')
// Check balances for both
const balance1 = await token1.balanceOf(myAddress)
const balance2 = await token2.balanceOf(myAddress)- address: string
connect
Connect to different signer
Creates a new ContractFactory instance with the same ABI and bytecode but connected to a different signer. This is useful when you want to deploy or interact with contracts using different accounts.
Parameters
- signer: Signer
The new signer to use
Returns ContractFactory
A new ContractFactory instance with the new signer
Example
const factory1 = new ContractFactory(abi, bytecode, wallet1)
// Switch to a different wallet
const factory2 = factory1.connect(wallet2)
// Deploy the same contract from wallet2
const contract = await factory2.deploy(arg1, arg2)Example: Deploying from multiple accounts
const baseFactory = new ContractFactory(abi, bytecode, adminWallet)
// Deploy instances from different wallets
const contract1 = await baseFactory.deploy(owner1)
const contract2 = await baseFactory.connect(wallet2).deploy(owner2)
const contract3 = await baseFactory.connect(wallet3).deploy(owner3)- signer: Signer
deploy
Deploy contract
Deploys a new instance of the contract to the Klever blockchain. This method encodes the constructor arguments, builds the deployment transaction, signs it, and broadcasts it to the network.
Parameters
- ...args: unknown[]
Constructor arguments (if any)
Returns Promise<Contract>
Contract instance (note: address will be 'klv1pending' until receipt is parsed)
Example: Deploy without constructor arguments
const factory = new ContractFactory(abi, bytecode, wallet)
const contract = await factory.deploy()
// Access deployment transaction
const tx = contract.deployTransaction
console.log('Deployment tx hash:', tx.hash)Example: Deploy with constructor arguments
// Deploy token contract with name, symbol, and supply
const factory = new ContractFactory(tokenABI, tokenBytecode, wallet)
const token = await factory.deploy('MyToken', 'MTK', 1000000000n)
// Wait for deployment
const receipt = await provider.waitForTransaction(token.deployTransaction.hash)
// Get deployed address
const address = ContractFactory.getDeployedAddress(receipt)
// Attach to deployed contract
const deployedToken = factory.attach(address)- ...args: unknown[]
getDeployTransaction
Get deployment bytecode
Generates the deployment bytecode (contract bytecode + encoded constructor arguments) without actually deploying the contract. This is useful for offline transaction building or inspecting the deployment data.
Parameters
- ...args: unknown[]
Constructor arguments (if any)
Returns { data: string }
Object containing the deployment data (bytecode + constructor args as hex string)
Example
const factory = new ContractFactory(abi, bytecode, wallet)
// Get deployment transaction data
const deployTx = factory.getDeployTransaction('MyToken', 'MTK', 1000000n)
console.log('Deployment data:', deployTx.data)
// This is the hex string that would be included in the deployment transactionExample: Inspecting deployment data
const factory = new ContractFactory(tokenABI, tokenBytecode, wallet)
// Get deployment data for different configurations
const deploy1 = factory.getDeployTransaction('Token1', 'TK1', 1000n)
const deploy2 = factory.getDeployTransaction('Token2', 'TK2', 2000n)
console.log('Deploy 1 size:', deploy1.data.length / 2, 'bytes')
console.log('Deploy 2 size:', deploy2.data.length / 2, 'bytes')- ...args: unknown[]
StaticgetDeployedAddress
Extract deployed contract address from transaction receipt
Parses the deployment transaction receipt to extract the address where the contract was deployed. This is a static method that can be used without a ContractFactory instance.
Parameters
- receipt: TransactionReceipt
Transaction receipt from deployment
Returns string
The deployed contract address
Example
// Deploy contract
const factory = new ContractFactory(abi, bytecode, wallet)
const contract = await factory.deploy(arg1, arg2)
// Wait for deployment
const tx = contract.deployTransaction
const receipt = await provider.waitForTransaction(tx.hash)
// Extract deployed address
const address = ContractFactory.getDeployedAddress(receipt)
console.log('Contract deployed at:', address)
// Create new contract instance with real address
const deployedContract = factory.attach(address)- receipt: TransactionReceipt
Factory for deploying contracts
Combines ABI and bytecode to enable contract deployment. The factory handles encoding constructor arguments and building the deployment transaction.
See