Class ContractFactory

    Factory for deploying contracts

    Combines ABI and bytecode to enable contract deployment. The factory handles encoding constructor arguments and building the deployment transaction.

    • Contract for interacting with deployed contracts
    • Interface for ABI encoding

    Constructors

    Properties

    bytecode: Uint8Array
    interface: Interface
    provider?: Provider
    signer: Signer

    Methods

    • 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

      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)
      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)
    • 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

      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)
      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)
    • 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)

      Error if provider is not available

      Error if transaction signing fails

      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)
      // 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)

      The returned Contract instance has a placeholder address ('klv1pending'). You must wait for the deployment transaction to be mined and then use ContractFactory.getDeployedAddress(receipt) to get the actual address.

    • 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)

      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 transaction
      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')
    • 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

      Error if receipt does not contain contract deployment data

      // 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)