managing_values.rst 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. Managing values
  2. ===============
  3. Sending and receiving value
  4. ___________________________
  5. Value in Solidity is represented by ``uint128``.
  6. .. note::
  7. Parity Substrate can be compiled with a different type for ``T::Balance``. If you
  8. need support for a different type, please raise an
  9. `issue <https://github.com/hyperledger/solang/issues>`_.
  10. Checking your balance
  11. _____________________
  12. The balance of a contract can be checked with `address` ``.balance``, so your own balance
  13. is ``address(this).balance``.
  14. .. note::
  15. Parity Substrate cannot check the balance for contracts other than the current
  16. one. If you need to check the balance of another contract, then add a balance
  17. function to that contract like the one below, and call that function instead.
  18. .. code-block:: solidity
  19. function balance() public returns (uint128) {
  20. return address(this).balance;
  21. }
  22. Creating contracts with an initial value
  23. ________________________________________
  24. You can specify the value you want to be deposited in the new contract by
  25. specifying ``{value: 100 ether}`` before the constructor arguments. This is
  26. explained in :ref:`sending value to the new contract <sending_values>`.
  27. Sending value with an external call
  28. ___________________________________
  29. You can specify the value you want to be sent along with the function call by
  30. specifying ``{value: 100 ether}`` before the function arguments. This is
  31. explained in :ref:`passing value and gas with external calls <passing_value_gas>`.
  32. .. _send_transfer:
  33. Sending value using ``send()`` and ``transfer()``
  34. _________________________________________________
  35. The ``send()`` and ``transfer()`` functions are available as method on a
  36. ``address payable`` variable. The single arguments is the amount of value you
  37. would like to send. The difference between the two functions is what happens
  38. in the failure case: ``transfer()`` will revert the current call, ``send()``
  39. returns a ``bool`` which will be ``false``.
  40. In order for the receiving contract to receive the value, it needs a ``receive()``
  41. function, see :ref:`fallback() and receive() function <fallback_receive>`.
  42. Here is an example:
  43. .. code-block:: solidity
  44. contract A {
  45. B other;
  46. constructor() {
  47. other = new B();
  48. bool complete = payable(other).transfer(100);
  49. if (!complete) {
  50. // oops
  51. }
  52. // if the following fails, our transaction will fail
  53. other.send(100);
  54. }
  55. }
  56. contract B {
  57. receive() payable external {
  58. // ..
  59. }
  60. }
  61. .. note::
  62. On Subtrate, this uses the ``seal_transfer()`` mechanism rather than ``seal_call()``, since this
  63. does not come with gas overhead. This means the ``receive()`` function is not required in the
  64. receiving contract, and it will not be called if it is present. If you want the ``receive()``
  65. function to be called, use ``address.call{value: 100}("")`` instead.