soroban.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. Soroban
  2. ========
  3. .. note::
  4. Soroban target is currently in Pre-Alpha stage, a lot of features are not yet implemented.
  5. You can find supported examples in the `examples directory <https://github.com/hyperledger-solang/solang/tree/main/examples>`_.
  6. Soroban is the smart contracts platform on the Stellar network. The soroban environment is an interface that exposes the blockchain's facilities to the contract, such as the ability to read and write to the ledger, send and receive payments, and access account information.
  7. The environment has two sides: `Host and Guest <https://developers.stellar.org/docs/learn/fundamentals/contract-development/environment-concepts#host-and-guest>`_. Code in the host environment implements the environment interface; code in the guest environment uses the environment interface.
  8. The guest environment is an isolated WASM virtual machine, which means that smart contracts should constitue of a WASM module which may contain calls to the host environment.
  9. Values communicated between the contract WASM code and the host environment are all encoded as soroban `Vals`. A `Val` is a unsigned 64-bit integer, with a tag the represents the type of the Val. For cases where the type does not fir in the 64-bit integer, the Val contains a pointer to the data in the host environment.
  10. `CAP-0046-1 <https://github.com/stellar/stellar-protocol/blob/master/core/cap-0046-01.md>`_ goes into more detail about the Soroban environment and how it works.
  11. Solidity for Soroban: changes to note
  12. +++++++++++++++++++++++++++++++++++++
  13. Authentication and Authorization
  14. ________________________________
  15. Authentication in Solidity is mostly done using the `msg.sender` variable, which contains the address of the caller. In Soroban, this is not available, and instead, the contract authorizes the caller by an invokation of `multiple host functions <https://github.com/stellar/rs-soroban-env/blob/0ee19322795bd0ff9097a1984b39210d0c58a6ea/soroban-env-common/env.json#L2365>`_ which take an `Address` as an argument.
  16. For example, this is a simple contract that only allows the owner to call the `set` function:
  17. .. code-block:: solidity
  18. pragma solidity ^0.8.20;
  19. contract OnlyOwner {
  20. address owner;
  21. uint
  22. constructor() {
  23. owner = msg.sender;
  24. }
  25. function set(uint256 value) public {
  26. require(msg.sender == owner, "Only owner can call this function");
  27. // Set the value
  28. }
  29. }
  30. In Soroban, this would be written as:
  31. .. code-block:: solidity
  32. contract auth {
  33. address public owner;
  34. uint64 public counter;
  35. constructor(address _owner) public {
  36. owner = _owner;
  37. }
  38. function increment() public returns (uint64) {
  39. owner.requireAuth();
  40. counter = counter + 1;
  41. return counter;
  42. }
  43. }
  44. The `requireAuth()` Builtin function will result in a host function call to the Soroban host environment, which will check if the caller is the owner of the contract. If not, the contract will revert.
  45. Storage Types
  46. ______________
  47. In Soroban, there exists `three storage types: Persistent, Temporary and Instance <https://developers.stellar.org/docs/build/guides/storage/choosing-the-right-storage>`_.
  48. You can specify the storage type of a variable by using the `persistent`, `temporary` or `instance` keyword before the variable type.
  49. .. code-block:: solidity
  50. contract storage_types {
  51. uint64 public temporary counter = 1;
  52. uint64 public instance counter1 = 1;
  53. uint64 public persistent counter2 = 2;
  54. uint64 public counter3 = 2;
  55. function inc() public {
  56. counter++;
  57. counter1++;
  58. counter2++;
  59. counter3++;
  60. }
  61. function dec() public {
  62. counter--;
  63. counter1--;
  64. counter2--;
  65. counter3--;
  66. }
  67. }