contracts.rst 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/substrate/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 Substrate target:
  15. .. include:: ../examples/substrate/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/substrate/contract_new.sol
  25. :code: solidity
  26. On Solana, the contract being created must have the ``@program_id()`` annotation, and the address
  27. of the new account must be specified using the ``{address: new_address}`` syntax.
  28. .. include:: ../examples/substrate/contract_new.sol
  29. :code: solidity
  30. The constructor might fail for various reasons, for example ``require()`` might fail here. This can
  31. be handled using the :ref:`try-catch` statement, else errors cause the transaction to fail.
  32. .. note::
  33. On Solana, the :ref:`try-catch` statement is not supported, as any failure will
  34. cause the entire transaction to fail.
  35. .. _sending_values:
  36. Sending value to the new contract
  37. _________________________________
  38. It is possible to send value to the new contract. This can be done with the ``{value: 500}``
  39. syntax, like so:
  40. .. include:: ../examples/substrate/contract_payable.sol
  41. :code: solidity
  42. The constructor should be declared ``payable`` for this to work.
  43. .. note::
  44. If no value is specified, then on Parity Substrate the minimum balance (also know as the
  45. existential deposit) is sent.
  46. .. note::
  47. On Solana, this functionality is not available.
  48. Setting the salt, gas, and address for the new contract
  49. _______________________________________________________
  50. .. note::
  51. The gas or salt cannot be set on Solana. However, when creating a contract
  52. on Solana, the address of the new account must be set using ``address:``.
  53. When a new contract is created, the address for the new contract is a hash of the input
  54. (the constructor arguments) to the new contract. So, a contract cannot be created twice
  55. with the same input. This is why the salt is concatenated to the input. The salt is
  56. either a random value or it can be explicitly set using the ``{salt: 2}`` syntax. A
  57. constant will remove the need for the runtime random generation, however creating
  58. a contract twice with the same salt and arguments will fail. The salt is of type
  59. ``uint256``.
  60. If gas is specified, this limits the amount gas the constructor for the new contract
  61. can use. gas is a ``uint64``.
  62. .. include:: ../examples/substrate/contract_gas_limit.sol
  63. :code: solidity
  64. When creating a contract on Solana, the address of the new account must be
  65. specified using ```address:``.
  66. .. include:: ../examples/solana/contract_address.sol
  67. :code: solidity
  68. Base contracts, abstract contracts and interfaces
  69. -------------------------------------------------
  70. Solidity contracts support object-oriented programming. The style Solidity is somewhat similar to C++,
  71. but there are many differences. In Solidity we are dealing with contracts, not classes.
  72. Specifying base contracts
  73. _________________________
  74. To inherit from another contract, you have to specify it as a base contract. Multiple contracts can
  75. be specified here.
  76. .. include:: ../examples/contract_inheritance.sol
  77. :code: solidity
  78. In this case, contract ``a`` inherits from both ``b`` and ``c``. Both ``func1()`` and ``func2()``
  79. are visible in contract ``a``, and will be part of its public interface if they are declared ``public`` or
  80. ``external``. In addition, the contract storage variables ``foo`` and ``bar`` are also availabe in ``a``.
  81. Inheriting contracts is recursive; this means that if you inherit a contract, you also inherit everything
  82. that that contract inherits. In this example, contract ``a`` inherits ``b`` directly, and inherits ``c``
  83. through ``b``. This means that contract ``b`` also has a variable ``bar``.
  84. .. include:: ../examples/contract_recursive_inheritance.sol
  85. :code: solidity
  86. Virtual Functions
  87. _________________
  88. When inheriting from a base contract, it is possible to override a function with a newer function with the same name.
  89. For this to be possible, the base contract must have specified the function as ``virtual``. The
  90. inheriting contract must then specify the same function with the same name, arguments and return values, and
  91. add the ``override`` keyword.
  92. .. include:: ../examples/virtual_functions.sol
  93. :code: solidity
  94. If the function is present in more than one base contract, the ``override`` attribute must list all the base
  95. contracts it is overriding.
  96. .. include:: ../examples/virtual_functions_override.sol
  97. :code: solidity
  98. Calling function in base contract
  99. _________________________________
  100. When a virtual function is called, the dispatch is *virtual*. If the function being called is
  101. overriden in another contract, then the overriding function is called. For example:
  102. .. include:: ../examples/base_contract_function_call.sol
  103. :code: solidity
  104. Rather than specifying the base contract, use ``super`` as the contract to call the base contract
  105. function.
  106. .. include:: ../examples/super_contract_function_call.sol
  107. :code: solidity
  108. If there are multiple base contracts which the define the same function, the function of the first base
  109. contract is called.
  110. .. include:: ../examples/contract_multiple_inheritance.sol
  111. :code: solidity
  112. Specifying constructor arguments
  113. ________________________________
  114. If a contract inherits another contract, then when it is instantiated or deployed, then the constructor for
  115. its inherited contracts is called. The constructor arguments can be specified on the base contract itself.
  116. .. include:: ../examples/inherited_constructor_arguments.sol
  117. :code: solidity
  118. When ``a`` is deployed, the constructor for ``c`` is executed first, then ``b``, and lastly ``a``. When the
  119. constructor arguments are specified on the base contract, the values must be constant. It is possible to specify
  120. the base arguments on the constructor for inheriting contract. Now we have access to the constructor arguments,
  121. which means we can have runtime-defined arguments to the inheriting constructors.
  122. .. include:: ../examples/inherited_constructor_runtime_arguments.sol
  123. :code: solidity
  124. The execution is not entirely intuitive in this case. When contract ``a`` is deployed with an int argument of 10,
  125. then first the constructor argument or contract ``b`` is calculated: 10+2, and that value is used as an
  126. argument to constructor ``b``. constructor ``b`` calculates the arguments for constructor ``c`` to be: 12+3. Now,
  127. with all the arguments for all the constructors established, constructor ``c`` is executed with argument 15, then
  128. constructor ``b`` with argument 12, and lastly constructor ``a`` with the original argument 10.
  129. Abstract Contracts
  130. __________________
  131. An ``abstract contract`` is one that cannot be instantiated, but it can be used as a base for another contract,
  132. which can be instantiated. A contract can be abstract because the functions it defines do not have a body,
  133. for example:
  134. .. include:: ../examples/abstract_contract.sol
  135. :code: solidity
  136. This contract cannot be instantiated, since there is no body or implementation for ``func2``. Another contract
  137. can define this contract as a base contract and override ``func2`` with a body.
  138. Another reason why a contract must be abstract is missing constructor arguments. In this case, if we were to
  139. instantiate contract ``a`` we would not know what the constructor arguments to its base ``b`` would have to be.
  140. Note that contract ``c`` does inherit from ``a`` and can specify the arguments for ``b`` on its constructor,
  141. even though ``c`` does not directly inherit ``b`` (but does indirectly).
  142. .. include:: ../examples/abstract_contract_inheritance.sol
  143. :code: solidity