contracts.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. Contracts
  2. =========
  3. Constructors and contract instantiation
  4. ---------------------------------------
  5. When a contract is deployed, the contract storage is initialized to the initializer values provided,
  6. and any constructor is called. A constructor is not required for a contract. A constructor is defined
  7. like so:
  8. .. include:: ../../examples/polkadot/flipper.sol
  9. :code: solidity
  10. A constructor can have any number of arguments.
  11. If a constructor has arguments, they must be supplied when the contract is deployed.
  12. If a contract is expected to receive value on instantiation, the constructor should be declared ``payable``.
  13. .. note::
  14. Solang allows naming constructors in the Polkadot target:
  15. .. include:: ../examples/polkadot/constructor_named.sol
  16. :code: solidity
  17. Constructors without a name will be called ``new`` in the metadata.
  18. Note that constructor names are only used in the generated metadata. For contract instantiation,
  19. the correct constructor matching the function signature will be selected automatically.
  20. Instantiation using new
  21. _______________________
  22. Contracts can be created using the ``new`` keyword. The contract that is being created might have
  23. constructor arguments, which need to be provided.
  24. .. include:: ../examples/polkadot/contract_new.sol
  25. :code: solidity
  26. The constructor might fail for various reasons, for example ``require()`` might fail here. This can
  27. be handled using the :ref:`try-catch` statement, else errors cause the transaction to fail.
  28. .. note::
  29. On Solana, the :ref:`try-catch` statement is not supported, as any failure will
  30. cause the entire transaction to fail.
  31. .. _sending_values:
  32. Sending value to the new contract
  33. _________________________________
  34. It is possible to send value to the new contract. This can be done with the ``{value: 500}``
  35. syntax, like so:
  36. .. include:: ../examples/polkadot/contract_payable.sol
  37. :code: solidity
  38. The constructor should be declared ``payable`` for this to work.
  39. .. note::
  40. If no value is specified, then on Polkadot the minimum balance (also know as the
  41. existential deposit) is sent.
  42. .. note::
  43. On Solana, this functionality is not available.
  44. Setting the salt, gas, and address for the new contract
  45. _______________________________________________________
  46. .. note::
  47. The gas or salt cannot be set on Solana. However, when creating a contract
  48. on Solana, the address of the new account must be set using ``address:``.
  49. When a new contract is created, the address for the new contract is a hash of the input
  50. (the encoded constructor arguments) to the new contract and the salt. A contract cannot be
  51. created twice with the same input and salt. By giving a different salt, the same input
  52. can be used twice for a new contract. The salt can be set using the
  53. ``{salt: hex"439d399ee3b5b0fae6c8d567a8cbfa22d59f8f2c5fe308fd0a92366c116e5f1a"}``
  54. syntax, or if it is omitted, then a random value is used.
  55. Specifying a salt will remove the need for generating a random value at runtime, however
  56. care must be taken to avoid using the same salt more than once. Creating a contract twice
  57. with the same salt and arguments will fail. The salt is of type ``bytes32``.
  58. If gas is specified, this limits the amount gas the constructor for the new contract
  59. can use. gas is a ``uint64``.
  60. .. include:: ../examples/polkadot/contract_gas_limit.sol
  61. :code: solidity
  62. .. _solana_constructor:
  63. Instantiating a contract on Solana
  64. __________________________________
  65. On Solana, the contract being created must have the ``@program_id()`` annotation that specifies the program account to
  66. which the contract code has been deployed. This account holds only the contract's executable binary.
  67. When calling a constructor, one needs to provide an address that will serve as the contract's data account,
  68. by using the call argument ``address``:
  69. .. include:: ../examples/solana/contract_address.sol
  70. :code: solidity
  71. When the contract's data account is passed through the ``address`` call argument, the compiler will automatically create
  72. the ``AccountMeta`` array the constructor call needs. Due to the impossibility to track account ordering in
  73. private, internal and public functions, such a call argument is only allowed in external functions.
  74. Alternatively, the data account to be initialized can be provided using the ``accounts`` call argument. In this case,
  75. one needs to instantiate a fixed length array of type ``AccountMeta`` to pass to the call. The array must contain all
  76. the accounts the transaction is going to need, in addition to the data account to be initialized.
  77. For the creation of a contract, the data account must the **first** element in such a vector and the system account
  78. ``11111111111111111111111111111111`` must also be present. If the constructor one is calling has the
  79. :ref:`@payer annotation <payer_seeds_bump>`, the payer account should appear in the array as well. Moreover, the
  80. ``is_signer`` and ``is_writable`` bool flags need to be properly set, according to the following example:
  81. .. include:: ../examples/solana/create_contract_with_metas.sol
  82. :code: solidity
  83. The sequence of the accounts in the ``AccountMeta`` array matters and must follow the
  84. :ref:`IDL ordering <account_management>`.
  85. Base contracts, abstract contracts and interfaces
  86. -------------------------------------------------
  87. Solidity contracts support object-oriented programming. The style Solidity is somewhat similar to C++,
  88. but there are many differences. In Solidity we are dealing with contracts, not classes.
  89. Specifying base contracts
  90. _________________________
  91. To inherit from another contract, you have to specify it as a base contract. Multiple contracts can
  92. be specified here.
  93. .. include:: ../examples/contract_inheritance.sol
  94. :code: solidity
  95. In this case, contract ``a`` inherits from both ``b`` and ``c``. Both ``func1()`` and ``func2()``
  96. are visible in contract ``a``, and will be part of its public interface if they are declared ``public`` or
  97. ``external``. In addition, the contract storage variables ``foo`` and ``bar`` are also availabe in ``a``.
  98. Inheriting contracts is recursive; this means that if you inherit a contract, you also inherit everything
  99. that that contract inherits. In this example, contract ``a`` inherits ``b`` directly, and inherits ``c``
  100. through ``b``. This means that contract ``b`` also has a variable ``bar``.
  101. .. include:: ../examples/contract_recursive_inheritance.sol
  102. :code: solidity
  103. Virtual Functions
  104. _________________
  105. When inheriting from a base contract, it is possible to override a function with a newer function with the same name.
  106. For this to be possible, the base contract must have specified the function as ``virtual``. The
  107. inheriting contract must then specify the same function with the same name, arguments and return values, and
  108. add the ``override`` keyword.
  109. .. include:: ../examples/virtual_functions.sol
  110. :code: solidity
  111. If the function is present in more than one base contract, the ``override`` attribute must list all the base
  112. contracts it is overriding.
  113. .. include:: ../examples/virtual_functions_override.sol
  114. :code: solidity
  115. Calling function in base contract
  116. _________________________________
  117. When a virtual function is called, the dispatch is *virtual*. If the function being called is
  118. overriden in another contract, then the overriding function is called. For example:
  119. .. include:: ../examples/base_contract_function_call.sol
  120. :code: solidity
  121. Rather than specifying the base contract, use ``super`` as the contract to call the base contract
  122. function.
  123. .. include:: ../examples/super_contract_function_call.sol
  124. :code: solidity
  125. If there are multiple base contracts which the define the same function, the function of the first base
  126. contract is called.
  127. .. include:: ../examples/contract_multiple_inheritance.sol
  128. :code: solidity
  129. Specifying constructor arguments
  130. ________________________________
  131. If a contract inherits another contract, then when it is instantiated or deployed, then the constructor for
  132. its inherited contracts is called. The constructor arguments can be specified on the base contract itself.
  133. .. include:: ../examples/inherited_constructor_arguments.sol
  134. :code: solidity
  135. When ``a`` is deployed, the constructor for ``c`` is executed first, then ``b``, and lastly ``a``. When the
  136. constructor arguments are specified on the base contract, the values must be constant. It is possible to specify
  137. the base arguments on the constructor for inheriting contract. Now we have access to the constructor arguments,
  138. which means we can have runtime-defined arguments to the inheriting constructors.
  139. .. include:: ../examples/inherited_constructor_runtime_arguments.sol
  140. :code: solidity
  141. The execution is not entirely intuitive in this case. When contract ``a`` is deployed with an int argument of 10,
  142. then first the constructor argument or contract ``b`` is calculated: 10+2, and that value is used as an
  143. argument to constructor ``b``. constructor ``b`` calculates the arguments for constructor ``c`` to be: 12+3. Now,
  144. with all the arguments for all the constructors established, constructor ``c`` is executed with argument 15, then
  145. constructor ``b`` with argument 12, and lastly constructor ``a`` with the original argument 10.
  146. Abstract Contracts
  147. __________________
  148. An ``abstract contract`` is one that cannot be instantiated, but it can be used as a base for another contract,
  149. which can be instantiated. A contract can be abstract because the functions it defines do not have a body,
  150. for example:
  151. .. include:: ../examples/abstract_contract.sol
  152. :code: solidity
  153. This contract cannot be instantiated, since there is no body or implementation for ``func2``. Another contract
  154. can define this contract as a base contract and override ``func2`` with a body.
  155. Another reason why a contract must be abstract is missing constructor arguments. In this case, if we were to
  156. instantiate contract ``a`` we would not know what the constructor arguments to its base ``b`` would have to be.
  157. Note that contract ``c`` does inherit from ``a`` and can specify the arguments for ``b`` on its constructor,
  158. even though ``c`` does not directly inherit ``b`` (but does indirectly).
  159. .. include:: ../examples/abstract_contract_inheritance.sol
  160. :code: solidity