managing_values.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. .. note::
  19. On Solana, checking the balance of an account different than the data account
  20. requires that it be passed as an AccountMeta during the transaction.
  21. .. code-block:: solidity
  22. function balance() public returns (uint128) {
  23. return address(this).balance;
  24. }
  25. Creating contracts with an initial value
  26. ________________________________________
  27. You can specify the value you want to be deposited in the new contract by
  28. specifying ``{value: 100 ether}`` before the constructor arguments. This is
  29. explained in :ref:`sending value to the new contract <sending_values>`.
  30. Sending value with an external call
  31. ___________________________________
  32. You can specify the value you want to be sent along with the function call by
  33. specifying ``{value: 100 ether}`` before the function arguments. This is
  34. explained in :ref:`passing value and gas with external calls <passing_value_gas>`.
  35. .. _send_transfer:
  36. Sending value using ``send()`` and ``transfer()``
  37. _________________________________________________
  38. The ``send()`` and ``transfer()`` functions are available as method on a
  39. ``address payable`` variable. The single arguments is the amount of value you
  40. would like to send. The difference between the two functions is what happens
  41. in the failure case: ``transfer()`` will revert the current call, ``send()``
  42. returns a ``bool`` which will be ``false``.
  43. In order for the receiving contract to receive the value, it needs a ``receive()``
  44. function, see :ref:`fallback() and receive() function <fallback_receive>`.
  45. Here is an example:
  46. .. code-block:: solidity
  47. contract A {
  48. B other;
  49. constructor() {
  50. other = new B();
  51. bool complete = payable(other).transfer(100);
  52. if (!complete) {
  53. // oops
  54. }
  55. // if the following fails, our transaction will fail
  56. other.send(100);
  57. }
  58. }
  59. contract B {
  60. receive() payable external {
  61. // ..
  62. }
  63. }
  64. .. note::
  65. On Subtrate, this uses the ``seal_transfer()`` mechanism rather than ``seal_call()``, since this
  66. does not come with gas overhead. This means the ``receive()`` function is not required in the
  67. receiving contract, and it will not be called if it is present. If you want the ``receive()``
  68. function to be called, use ``address.call{value: 100}("")`` instead.
  69. .. note::
  70. On Solana, these functions manage native token assets from a contract's data account when
  71. invoked from a contract variable. Normally, funds are not stored in a contract's account, as
  72. one can set a separate payer account to pay for all the transactions. In this case, it is more
  73. straightforward to use the :ref:`system instruction library <system_instruction_library>` to transfer
  74. native tokens between accounts.