Class ContractCreateTransaction


public final class ContractCreateTransaction extends Transaction<ContractCreateTransaction>
Start a new smart contract instance. After the instance is created, the ContractID for it is in the receipt.

The instance will exist for autoRenewPeriod seconds. When that is reached, it will renew itself for another autoRenewPeriod seconds by charging its associated cryptocurrency account (which it creates here). If it has insufficient cryptocurrency to extend that long, it will extend as long as it can. If its balance is zero, the instance will be deleted.

A smart contract instance normally enforces rules, so "the code is law". For example, an ERC-20 contract prevents a transfer from being undone without a signature by the recipient of the transfer. This is always enforced if the contract instance was created with the adminKeys being null. But for some uses, it might be desirable to create something like an ERC-20 contract that has a specific group of trusted individuals who can act as a "supreme court" with the ability to override the normal operation, when a sufficient number of them agree to do so. If adminKeys is not null, then they can sign a transaction that can change the state of the smart contract in arbitrary ways, such as to reverse a transaction that violates some standard of behavior that is not covered by the code itself. The admin keys can also be used to change the autoRenewPeriod, and change the adminKeys field itself. The API currently does not implement this ability. But it does allow the adminKeys field to be set and queried, and will in the future implement such admin abilities for any instance that has a non-null adminKeys.

If this constructor stores information, it is charged gas to store it. There is a fee in hbars to maintain that storage until the expiration time, and that fee is added as part of the transaction fee.

An entity (account, file, or smart contract instance) must be created in a particular realm. If the realmID is left null, then a new realm will be created with the given admin key. If a new realm has a null adminKey, then anyone can create/modify/delete entities in that realm. But if an admin key is given, then any transaction to create/modify/delete an entity in that realm must be signed by that key, though anyone can still call functions on smart contract instances that exist in that realm. A realm ceases to exist when everything within it has expired and no longer exists.

The current API ignores shardID, realmID, and newRealmAdminKey, and creates everything in shard 0 and realm 0, with a null key. Future versions of the API will support multiple realms and multiple shards.

The optional memo field can contain a string whose length is up to 100 bytes. That is the size after Unicode NFD then UTF-8 conversion. This field can be used to describe the smart contract. It could also be used for other purposes. One recommended purpose is to hold a hexadecimal string that is the SHA-384 hash of a PDF file containing a human-readable legal contract. Then, if the admin keys are the public keys of human arbitrators, they can use that legal document to guide their decisions during a binding arbitration tribunal, convened to consider any changes to the smart contract in the future. The memo field can only be changed using the admin keys. If there are no admin keys, then it cannot be changed after the smart contract is created.

  • Constructor Details

    • ContractCreateTransaction

      public ContractCreateTransaction()
      Constructor.
    • ContractCreateTransaction

      ContractCreateTransaction(LinkedHashMap<TransactionId,LinkedHashMap<AccountId,Transaction>> txs) throws com.google.protobuf.InvalidProtocolBufferException
      Constructor.
      Parameters:
      txs - Compound list of transaction id's list of (AccountId, Transaction) records
      Throws:
      com.google.protobuf.InvalidProtocolBufferException - when there is an issue with the protobuf
    • ContractCreateTransaction

      ContractCreateTransaction(TransactionBody txBody)
      Constructor.
      Parameters:
      txBody - protobuf TransactionBody
  • Method Details

    • getBytecodeFileId

      @Nullable public FileId getBytecodeFileId()
      Extract the file id.
      Returns:
      the file id as a byte code
    • setBytecodeFileId

      public ContractCreateTransaction setBytecodeFileId(FileId bytecodeFileId)
      Sets the file containing the smart contract byte code.

      A copy will be made and held by the contract instance, and have the same expiration time as the instance.

      The file must be the ASCII hexadecimal representation of the smart contract bytecode. The contract bytecode is limited in size only by the network file size limit.

      Parameters:
      bytecodeFileId - The FileId to be set
      Returns:
      this
    • getBytecode

      @Nullable public byte[] getBytecode()
      Extract the bytecode.
      Returns:
      the bytecode
    • setBytecode

      public ContractCreateTransaction setBytecode(byte[] bytecode)
      Sets the source for the smart contract EVM bytecode.

      The bytes of the smart contract initCode. A copy of the contents SHALL be made and held as `bytes` in smart contract state.
      This value is limited in length by the network transaction size limit. This entire transaction, including all fields and signatures, MUST be less than the network transaction size limit.

      Parameters:
      bytecode - The bytecode
      Returns:
      this
    • getAdminKey

      @Nullable public Key getAdminKey()
      Get the admin key
      Returns:
      the adminKey
    • setAdminKey

      public ContractCreateTransaction setAdminKey(Key adminKey)
      Access control for modification of the smart contract after it is created.

      If this field is set, that key MUST sign this transaction.
      If this field is set, that key MUST sign each future transaction to update or delete the contract.
      An updateContract transaction that _only_ extends the topic expirationTime (a "manual renewal" transaction) SHALL NOT require admin key signature.

      A contract without an admin key SHALL be immutable, except for expiration and renewal.

      Parameters:
      adminKey - The Key to be set
      Returns:
      this
    • getGas

      public long getGas()
      Extract the gas.
      Returns:
      the gas amount that was set
    • setGas

      public ContractCreateTransaction setGas(long gas)
      A maximum limit to the amount of gas to use for the constructor call.

      The network SHALL charge the greater of the following, but SHALL NOT charge more than the value of this field.

      1. The actual gas consumed by the smart contract constructor call.
      2. `80%` of this value.
      The `80%` factor encourages reasonable estimation, while allowing for some overage to ensure successful execution.
      Parameters:
      gas - The long to be set as gas
      Returns:
      this
    • getInitialBalance

      public Hbar getInitialBalance()
      Extract the initial balance.
      Returns:
      the initial balance in hbar
    • setInitialBalance

      public ContractCreateTransaction setInitialBalance(Hbar initialBalance)
      The amount of HBAR to use as an initial balance for the account representing the new smart contract.

      This value is presented in tinybar (10-8 HBAR).
      The HBAR provided here will be withdrawn from the payer account that signed this transaction.

      Parameters:
      initialBalance - The Hbar to be set as the initial balance
      Returns:
      this
    • getProxyAccountId

      @Deprecated @Nullable public AccountId getProxyAccountId()
      Deprecated.
      with no replacement Extract the proxy account id.
      Returns:
      the proxy account id
    • setProxyAccountId

      @Deprecated public ContractCreateTransaction setProxyAccountId(AccountId proxyAccountId)
      Deprecated.
      with no replacement Sets the ID of the account to which this account is proxy staked.

      If proxyAccountID is null, or is an invalid account, or is an account that isn't a node, then this account is automatically proxy staked to a node chosen by the network, but without earning payments.

      If the proxyAccountID account refuses to accept proxy staking , or if it is not currently running a node, then it will behave as if proxyAccountID was null.

      Parameters:
      proxyAccountId - The AccountId to be set
      Returns:
      this
    • getMaxAutomaticTokenAssociations

      public int getMaxAutomaticTokenAssociations()
      Get the maximum number of tokens that this contract can be automatically associated with (i.e., receive air-drops from).
      Returns:
      the maxAutomaticTokenAssociations
    • setMaxAutomaticTokenAssociations

      public ContractCreateTransaction setMaxAutomaticTokenAssociations(int maxAutomaticTokenAssociations)
      The maximum number of tokens that can be auto-associated with this smart contract.

      If this is less than or equal to `used_auto_associations` (or 0), then this contract MUST manually associate with a token before transacting in that token.
      Following HIP-904 This value may also be `-1` to indicate no limit.
      This value MUST NOT be less than `-1`.

      Parameters:
      maxAutomaticTokenAssociations - The maximum automatic token associations
      Returns:
      this
    • getAutoRenewPeriod

      @Nullable public Duration getAutoRenewPeriod()
      Extract the auto renew period.
      Returns:
      the auto renew period
    • setAutoRenewPeriod

      public ContractCreateTransaction setAutoRenewPeriod(Duration autoRenewPeriod)
      The initial lifetime, in seconds, for the smart contract, and the number of seconds for which the smart contract will be automatically renewed upon expiration.

      This value MUST be set.
      This value MUST be greater than the configured MIN_AUTORENEW_PERIOD.
      This value MUST be less than the configured MAX_AUTORENEW_PERIOD.

      Parameters:
      autoRenewPeriod - The Duration to be set for auto renewal
      Returns:
      this
    • getConstructorParameters

      public com.google.protobuf.ByteString getConstructorParameters()
      Extract the constructor parameters.
      Returns:
      the byte string representation
    • setConstructorParameters

      public ContractCreateTransaction setConstructorParameters(byte[] constructorParameters)
      Sets the constructor parameters as their raw bytes.

      Use this instead of setConstructorParameters(ContractFunctionParameters) if you have already pre-encoded a solidity function call.

      Parameters:
      constructorParameters - The constructor parameters
      Returns:
      this
    • setConstructorParameters

      public ContractCreateTransaction setConstructorParameters(ContractFunctionParameters constructorParameters)
      Sets the parameters to pass to the constructor.
      Parameters:
      constructorParameters - The contructor parameters
      Returns:
      this
    • getContractMemo

      public String getContractMemo()
      Extract the contract memo.
      Returns:
      the contract's memo
    • setContractMemo

      public ContractCreateTransaction setContractMemo(String memo)
      A short memo for this smart contract.

      This value, if set, MUST NOT exceed `transaction.maxMemoUtf8Bytes` (default 100) bytes when encoded as UTF-8.

      Parameters:
      memo - The String to be set as the memo
      Returns:
      this
    • getStakedAccountId

      @Nullable public AccountId getStakedAccountId()
      ID of the account to which this contract will stake
      Returns:
      ID of the account to which this contract will stake.
    • setStakedAccountId

      public ContractCreateTransaction setStakedAccountId(@Nullable AccountId stakedAccountId)
      Set the account to which this contract will stake
      Parameters:
      stakedAccountId - ID of the account to which this contract will stake.
      Returns:
      this
    • getStakedNodeId

      @Nullable public Long getStakedNodeId()
      The node to which this contract will stake
      Returns:
      ID of the node this contract will be staked to.
    • setStakedNodeId

      public ContractCreateTransaction setStakedNodeId(@Nullable Long stakedNodeId)
      The ID of a network node.

      This smart contract SHALL stake its HBAR to this node.

      Note: node IDs do fluctuate as node operators change. Most contracts are immutable, and a contract staking to an invalid node ID SHALL NOT participate in staking. Immutable contracts MAY find it more reliable to use a proxy account for staking (via `staked_account_id`) to enable updating the _effective_ staking node ID when necessary through updating the proxy account.
      Parameters:
      stakedNodeId - ID of the node this contract will be staked to.
      Returns:
      this
    • getDeclineStakingReward

      public boolean getDeclineStakingReward()
      If true, the contract declines receiving a staking reward. The default value is false.
      Returns:
      If true, the contract declines receiving a staking reward. The default value is false.
    • setDeclineStakingReward

      public ContractCreateTransaction setDeclineStakingReward(boolean declineStakingReward)
      A flag indicating that this smart contract declines to receive any reward for staking its HBAR balance to help secure the network.

      If set to true, this smart contract SHALL NOT receive any reward for staking its HBAR balance to help secure the network, regardless of staking configuration, but MAY stake HBAR to support the network without reward.

      Parameters:
      declineStakingReward - - If true, the contract declines receiving a staking reward. The default value is false.
      Returns:
      this
    • getAutoRenewAccountId

      @Nullable public AccountId getAutoRenewAccountId()
      Get the auto renew accountId.
      Returns:
      the auto renew accountId
    • setAutoRenewAccountId

      public ContractCreateTransaction setAutoRenewAccountId(AccountId autoRenewAccountId)
      The id of an account, in the same shard and realm as this smart contract, that has signed this transaction, allowing the network to use its balance, when needed, to automatically extend this contract's expiration time.

      If this field is set, that key MUST sign this transaction.
      If this field is set, then the network SHALL deduct the necessary fees from the designated auto-renew account, if that account has sufficient balance. If the auto-renewal account does not have sufficient balance, then the fees for contract renewal SHALL be deducted from the HBAR balance held by the smart contract.
      If this field is not set, then all renewal fees SHALL be deducted from the HBAR balance held by this contract.

      Parameters:
      autoRenewAccountId - The AccountId to be set for auto-renewal
      Returns:
      this
    • getHooks

      public List<HookCreationDetails> getHooks()
      Get the hook creation details for this contract.
      Returns:
      a copy of the hook creation details list
    • addHook

      public ContractCreateTransaction addHook(HookCreationDetails hookDetails)
      Add a hook creation detail to this contract.
      Parameters:
      hookDetails - the hook creation details to add
      Returns:
      this
    • setHooks

      public ContractCreateTransaction setHooks(List<HookCreationDetails> hookDetails)
      Set the hook creation details for this contract.
      Parameters:
      hookDetails - the list of hook creation details
      Returns:
      this
    • build

      Build the transaction body.
      Returns:
      ContractCreateTransactionBody
    • validateChecksums

      void validateChecksums(Client client) throws BadEntityIdException
      Specified by:
      validateChecksums in class Transaction<ContractCreateTransaction>
      Throws:
      BadEntityIdException
    • initFromTransactionBody

      void initFromTransactionBody()
      Initialize from the transaction body.
    • getMethodDescriptor

      io.grpc.MethodDescriptor<Transaction,TransactionResponse> getMethodDescriptor()
      Description copied from class: Executable
      Called to direct the invocation of the query to the appropriate gRPC service.
      Specified by:
      getMethodDescriptor in class Executable<ContractCreateTransaction,Transaction,TransactionResponse,TransactionResponse>
    • onFreeze

      void onFreeze(TransactionBody.Builder bodyBuilder)
      Description copied from class: Transaction
      Called in Transaction.freezeWith(Client) just before the transaction body is built. The intent is for the derived class to assign their data variant to the transaction body.
      Specified by:
      onFreeze in class Transaction<ContractCreateTransaction>
    • onScheduled

      void onScheduled(SchedulableTransactionBody.Builder scheduled)
      Description copied from class: Transaction
      Called in Transaction.schedule() when converting transaction into a scheduled version.
      Specified by:
      onScheduled in class Transaction<ContractCreateTransaction>