managing_values.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. .. _balance:
  11. Checking your balance
  12. _____________________
  13. .. tabs::
  14. .. group-tab:: Polkadot
  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. .. code-block:: solidity
  19. function balance() public returns (uint128) {
  20. return address(this).balance;
  21. }
  22. .. group-tab:: Solana
  23. On Solana, the balance of an account can be accessed using the ``lamports`` member of the ``AccountInfo``
  24. struct. Every account whose value we want to check must be declared with an account annotation.
  25. .. code-block::
  26. @account(my_acc)
  27. function balance() external returns (uint64) {
  28. return tx.accounts.my_acc.lamports;
  29. }
  30. Creating contracts with an initial value
  31. ________________________________________
  32. You can specify the value you want to be deposited in the new contract by
  33. specifying ``{value: 100 ether}`` before the constructor arguments. This is
  34. explained in :ref:`sending value to the new contract <sending_values>`.
  35. Sending value with an external call
  36. ___________________________________
  37. You can specify the value you want to be sent along with the function call by
  38. specifying ``{value: 100 ether}`` before the function arguments. This is
  39. explained in :ref:`passing value and gas with external calls <passing_value_gas>`.
  40. .. _send_transfer:
  41. Sending value using ``send()`` and ``transfer()``
  42. _________________________________________________
  43. .. tabs::
  44. .. group-tab:: Polkadot
  45. The ``send()`` and ``transfer()`` functions are available as method on a
  46. ``address payable`` variable. The single arguments is the amount of value you
  47. would like to send. The difference between the two functions is what happens
  48. in the failure case: ``transfer()`` will revert the current call, ``send()``
  49. returns a ``bool`` which will be ``false``.
  50. In order for the receiving contract to receive the value, it needs a ``receive()``
  51. function, see :ref:`fallback() and receive() function <fallback_receive>`.
  52. Here is an example:
  53. .. code-block:: solidity
  54. contract A {
  55. B other;
  56. constructor() {
  57. other = new B();
  58. bool complete = payable(other).transfer(100);
  59. if (!complete) {
  60. // oops
  61. }
  62. // if the following fails, our transaction will fail
  63. other.send(100);
  64. }
  65. }
  66. contract B {
  67. receive() payable external {
  68. // ..
  69. }
  70. }
  71. .. note::
  72. On Subtrate, this uses the ``seal_transfer()`` mechanism rather than ``seal_call()``, since this
  73. does not come with gas overhead. This means the ``receive()`` function is not required in the
  74. receiving contract, and it will not be called if it is present. If you want the ``receive()``
  75. function to be called, use ``address.call{value: 100}("")`` instead.
  76. .. group-tab:: Solana
  77. On Solana, there are no ``transfer`` and ``send`` functions. In order to alter the balance of accounts,
  78. one might increment or decrement the ``lamports`` field from the ``AccountInfo`` struct directly. This
  79. is only possible if the accounts whose balance is being changed are owned by the program.
  80. .. code-block::
  81. @mutableAccount(acc1)
  82. @mutableAccount(acc2)
  83. function transfer(uint64 amount) external {
  84. tx.accounts.acc1.lamports += amount;
  85. tx.accounts.acc2.lamports -= amount;
  86. }