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