solana.rst 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. Solana
  2. ======
  3. Solana Overview
  4. _______________
  5. As the underlying Solana environment is different than that of Ethereum, Solidity inner workings
  6. have been modified to function properly. For example, A Solidity contract on Solana utilizes two accounts: a
  7. data account and a program account. The program account stores the contract's executable binary and owns
  8. the data account, which holds all the storage variables. On Ethereum a single account can store executable
  9. code and data.
  10. Contract upgrades
  11. +++++++++++++++++
  12. Provided that the data layout from a new contract is compatible with that of an old one, it is possible
  13. to update the binary in the program account and retain the same data, rendering
  14. contract upgrades implemented in Solidity unnecessary. Solana's CLI tool provides a command to both do
  15. an initial deploy of a program, and redeploy it later.:
  16. .. code-block:: bash
  17. solana program deploy --program-id <KEYPAIR_FILEPATH> <PROGRAM_FILEPATH>
  18. where ``<KEYPAIR_FILEPATH>`` is the program's keypair json file and ``<PROGRAM_FILEPATH>``
  19. is the program binary ``.so`` file. For more information about redeploying a program,
  20. check `Solana's documentation <https://docs.solana.com/cli/deploy-a-program#redeploy-a-program>`_.
  21. Data types
  22. ++++++++++
  23. - An account address consists of a 32-bytes key, which is represented by the ``address`` type. This data model
  24. differs from Ethereum 20-bytes addresses.
  25. - Solana's virtual machine registers are 64-bit wide, so 64-bit integers ``uint64`` and ``int64`` are preferable over
  26. ``uint256`` and ``int256``. An operation with types wider than 64-bits is split into multiple operations, making
  27. it slower and consuming more compute units. This is the case, for instance, with multiplication, division and modulo
  28. using `uint256`.
  29. - Likewise, all balances and values on Solana are 64-bit wide, so the builtin functions for
  30. *address* ``.balance``, ``.transfer()`` and ``.send()`` use 64-bit integers.
  31. - An address literal has to be specified using the ``address"36VtvSbE6jVGGQytYWSaDPG7uZphaxEjpJHUUpuUbq4D"`` syntax.
  32. - Ethereum syntax for addresses ``0xE0f5206BBD039e7b0592d8918820024e2a7437b9`` is not supported.
  33. Runtime
  34. +++++++
  35. - The Solana target requires `Solana <https://www.solana.com/>`_ v1.8.1.
  36. - Function selectors are eight bytes wide and known as *discriminators*.
  37. - Solana provides different builtins, e.g. ``block.slot`` and ``tx.accounts``.
  38. - When calling an external function or invoking a contract's constructor, one
  39. :ref:`needs to provide <solana_cpi_accounts>` the necessary accounts for the transaction.
  40. - The keyword ``this`` returns the contract's program account, also know as program id.
  41. - Contracts :ref:`cannot be types <contracts_not_types>` on Solana and :ref:`calls to contracts <solana_contract_call>`
  42. follow a different syntax.
  43. - Accounts can be declared on functions using :ref:`annotations <account_management>`.
  44. - :ref:`Retrieving the balance <balance>` and :ref:`transferring values <send_transfer>`
  45. utilize the ``AccountInfo`` struct.
  46. Compute budget
  47. ++++++++++++++
  48. On Ethereum, when calling a smart contract function, one needs to specify the amount of gas the operation is allowed
  49. to use. Gas serves to pay for a contract execution on chain and can be a way for giving a contract priority execution
  50. when extra gas is offered in a transaction. Each EVM instruction has an associated gas value, which translates to real
  51. ETH cost. Provided that one can afford all the gas expenses, there is no upper boundary for the amount of gas limit
  52. one can provide in a transaction, so Solidity for Ethereum has gas builtins, like ``gasleft``, ``block.gaslimit``,
  53. ``tx.gasprice`` or the Yul ``gas()`` builtin, which returns the amount of gas left for execution.
  54. On the other hand, Solana is optimized for low latency and high transaction throughput and has an equivalent concept to
  55. gas: compute unit. Every smart contract function is allowed the same quantity of compute units (currently that
  56. value is 200k), and every instruction of a contract consumes exactly one compute unit. There is no need to provide
  57. an amount of compute units for a transaction and they are not charged, except when one wants priority execution on
  58. chain, in which case one would pay per compute unit consumed. Therefore, functions for gas are not available on
  59. Solidity for Solana.
  60. Solidity for Solana incompatibilities with Solidity for Ethereum
  61. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  62. - ``msg.sender`` is :ref:`not available on Solana <msg_sender_solana>`.
  63. - There is no ``ecrecover()`` builtin function because Solana does not use the ECDSA algorithm, but there
  64. is a ``signatureVerify()`` function, which can check ed25519 signatures. As a consequence, it is not possible to
  65. recover a signer from a signature.
  66. - Try-catch statements do not work on Solana. If any external call or contract creation fails, the runtime will
  67. halt execution and revert the entire transaction.
  68. - Error definitions and reverts with error messages are not yet working for Solana.
  69. - Value transfer with function call :ref:`does not work <value_transfer>`.
  70. - Many Yul builtins are not available, as specified in the :ref:`availability table <yul_builtins>`.
  71. - External calls on Solana require that accounts be specified, as in :ref:`this example <solana_external_call>`.
  72. - The ERC-20 interface is not compatible with Solana at the moment.
  73. Getting started on Solana
  74. _________________________
  75. Please follow the `Solang Getting Started Guide <https://solana.com/developers/guides/solang/getting-started>`_.
  76. For more examples, see the
  77. `solang's integration tests <https://github.com/hyperledger-solang/solang/tree/main/integration/solana>`_.
  78. Using the Anchor client library
  79. _______________________________
  80. Some notes on using the anchor javascript npm library.
  81. * Solidity function names are converted to camelCase. This means that if in Solidity a function is called ``foo_bar()``,
  82. you must write ``fooBar()`` in your javascript.
  83. * Anchor only allows you to call ``.view()`` on Solidity functions which are declared ``view`` or ``pure``.
  84. * Named return values in Solidity are also converted to camelCase. Unnamed returned are given the name ``return0``, ``return1``, etc,
  85. depending on the position in the returns values.
  86. * Only return values from ``view`` and ``pure`` functions can be decoded. Return values from other functions and are not accessible.
  87. This is a limitation in the Anchor library. Possibly this can be fixed.
  88. * In the case of an error, no return data is decoded. This means that the reason provided in ``revert('reason');`` is not
  89. available as a return value.
  90. * Number arguments for functions are expressed as ``BN`` values and not plain javascript ``Number`` or ``BigInt``.
  91. .. _call_anchor:
  92. Calling Anchor Programs from Solidity
  93. _____________________________________
  94. It is possible to call `Anchor Programs <https://github.com/coral-xyz/anchor>`_
  95. from Solidity. You first have to generate a Solidity interface file from the IDL file using
  96. the :ref:`idl_command`. Then, import the Solidity file in your Solidity using the
  97. ``import "...";`` syntax. Say you have an anchor program called ``bobcat`` with a
  98. function ``pounce``, you can call it like so:
  99. .. include:: ../examples/solana/call_anchor.sol
  100. :code: solidity
  101. Setting the program_id for a contract
  102. _____________________________________
  103. When developing contracts for Solana, programs are usually deployed to a well
  104. known account. The account can be specified in the source code using an annotation
  105. ``@program_id`` if it is known beforehand. If you want to call a contract via an external call,
  106. either the contract must have a ``@program_id`` annotation or the ``{program_id: ..}`` call argument must be present.
  107. .. include:: ../examples/solana/program_id.sol
  108. :code: solidity
  109. .. note::
  110. The program_id ``Foo5mMfYo5RhRcWa4NZ2bwFn4Kdhe8rNK5jchxsKrivA`` was generated using
  111. the command line:
  112. .. code-block:: bash
  113. solana-keygen grind --starts-with Foo:1
  114. .. _payer_seeds_bump:
  115. Setting the payer, seeds, bump, and space for a contract
  116. _________________________________________________________
  117. When a contract is instantiated, there are two accounts required: the program account to hold
  118. the executable code and the data account to save the state variables of the contract. The
  119. program account is deployed once and can be reused for updating the contract. When each
  120. Solidity contract is instantiated (also known as deployed), the data account has to be
  121. created. This can be done by the client-side code, and then the created blank account
  122. is passed to the transaction that runs the constructor code.
  123. Alternatively, the data account can be created by the constructor, on chain. When
  124. this method is used, some parameters must be specified for the account
  125. using annotations. Annotations placed above a constructor can only contain literals or
  126. constant expressions, as is the case for first ``@seed`` and ``@space`` in the following example.
  127. Annotations can also refer to constructor arguments when placed next to them, as the second ``@seed`` and
  128. the ``@bump`` examples below. The ``@payer`` annotation is a special annotation that
  129. :ref:`declares an account <account_management>`.
  130. If the contract has no constructor, annotations can be paired with an empty constructor.
  131. .. include:: ../examples/solana/constructor_annotations.sol
  132. :code: solidity
  133. Creating an account needs a payer, so at a minimum the ``@payer`` annotation must be
  134. specified. If it is missing, then the data account must be created client-side.
  135. The ``@payer`` annotation :ref:`declares a Solana account <account_management>` that must be passed in the transaction.
  136. The size of the data account can be specified with ``@space``. This is a
  137. ``uint64`` expression which can either be a constant or use one of the constructor
  138. arguments. The ``@space`` should at least be the size given when you run ``solang -v``:
  139. .. code-block:: bash
  140. $ solang compile --target solana -v examples/solana/flipper.sol
  141. ...
  142. info: contract flipper uses at least 17 bytes account data
  143. ...
  144. If the data account is going to be a
  145. `program derived address <https://docs.solana.com/developing/programming-model/calling-between-programs#program-derived-addresses>`_,
  146. then the seeds and bump have to be provided. There can be multiple seeds, and an optional
  147. single bump. If the bump is not provided, then the seeds must not create an
  148. account that falls on the curve. When placed above the constructor, the ``@seed`` can be a string literal,
  149. or a hex string with the format ``hex"4142"``. If before an argument, the seed annotation must refer to an argument
  150. of type ``bytes``, ``address``, or fixed length byte array of ``bytesN``. The ``@bump`` must a single byte of type ``bytes1``.
  151. .. _value_transfer:
  152. Transferring native value with a function call
  153. ______________________________________________
  154. The Solidity language on Ethereum allows value transfers with an external call
  155. or constructor, using the ``auction.bid{value: 501}()`` syntax.
  156. Solana Cross Program Invocation (CPI) does not support this, which means that:
  157. - Specifying ``value:`` on an external call or constructor is not permitted
  158. - The ``payable`` keyword has no effect
  159. - ``msg.value`` is not supported
  160. .. note::
  161. A naive way to implement this is to let the caller transfer
  162. native balance and then inform the callee about the amount transferred by
  163. specifying this in the instruction data. However, it would be trivial to
  164. forge such an operation.
  165. Receive function
  166. ________________
  167. In Solidity the ``receive()`` function, when defined, is called whenever the native
  168. balance for an account gets credited, for example through a contract calling
  169. ``account.transfer(value);``. On Solana, there is no method that implements
  170. this. The balance of an account can be credited without any code being executed.
  171. ``receive()`` functions are not permitted on the Solana target.
  172. .. _msg_sender_solana:
  173. ``msg.sender`` not available on Solana
  174. ______________________________________
  175. On Ethereum, ``msg.sender`` is used to identify either the account that submitted
  176. the transaction, or the caller when one contract calls another. On Ethereum, each
  177. contract execution can only use a single account, which provides the code and data.
  178. On Solana, each contract execution uses many accounts. Consider a rust contract which
  179. calls a Solidity contract: the rust contract can access a few data accounts, and which
  180. of those would be considered the caller? So in many cases there is not a single account
  181. which can be identified as a caller. In addition to that, the Solana VM has no
  182. mechanism for fetching the caller accounts. This means there is no way to implement
  183. ``msg.sender``.
  184. The way to implement this on Solana is to have an authority account for the contract
  185. that must be a signer for the transaction (note that on Solana there
  186. can be many signers too). This is a common construct on Solana contracts.
  187. .. include:: ../examples/solana/use_authority.sol
  188. :code: solidity
  189. Builtin Imports
  190. ________________
  191. Some builtin functionality is only available after importing. The following structs
  192. can be imported via the special builtin import file ``solana``.
  193. .. code-block:: solidity
  194. import {AccountMeta, AccountInfo} from 'solana';
  195. Note that ``{AccountMeta, AccountInfo}`` can be omitted, renamed or imported via
  196. import object.
  197. .. code-block:: solidity
  198. // Now AccountMeta will be known as AM
  199. import {AccountMeta as AM} from 'solana';
  200. // Now AccountMeta will be available as solana.AccountMeta
  201. import 'solana' as solana;
  202. .. note::
  203. The import file ``solana`` is only available when compiling for the Solana
  204. target.
  205. .. _account_info:
  206. Builtin AccountInfo
  207. +++++++++++++++++++
  208. The account info of all the accounts passed into the transaction. ``AccountInfo`` is a builtin
  209. structure with the following fields:
  210. address ``key``
  211. The address (or public key) of the account
  212. uint64 ``lamports``
  213. The lamports of the accounts. This field can be modified, however the lamports need to be
  214. balanced for all accounts by the end of the transaction.
  215. bytes ``data``
  216. The account data. This field can be modified, but use with caution.
  217. address ``owner``
  218. The program that owns this account
  219. uint64 ``rent_epoch``
  220. The next epoch when rent is due.
  221. bool ``is_signer``
  222. Did this account sign the transaction
  223. bool ``is_writable``
  224. Is this account writable in this transaction
  225. bool ``executable``
  226. Is this account a program
  227. .. _account_meta:
  228. Builtin AccountMeta
  229. +++++++++++++++++++
  230. When doing an external call (aka CPI), ``AccountMeta`` specifies which accounts
  231. should be passed to the callee.
  232. address ``pubkey``
  233. The address (or public key) of the account
  234. bool ``is_writable``
  235. Can the callee write to this account
  236. bool ``is_signer``
  237. Can the callee assume this account signed the transaction
  238. Builtin create_program_address
  239. ++++++++++++++++++++++++++++++
  240. This function returns the program derived address for a program address and
  241. the provided seeds. See the Solana documentation on
  242. `program derived addresses <https://edge.docs.solana.com/developing/programming-model/calling-between-programs#program-derived-addresses>`_.
  243. .. include:: ../examples/solana/builtin_create_program_address.sol
  244. :code: solidity
  245. Builtin try_find_program_address
  246. ++++++++++++++++++++++++++++++++
  247. This function returns the program derived address for a program address and
  248. the provided seeds, along with a seed bump. See the Solana documentation on
  249. `program derived addresses <https://edge.docs.solana.com/developing/programming-model/calling-between-programs#program-derived-addresses>`_.
  250. .. include:: ../examples/solana/builtin_try_find_program_address.sol
  251. :code: solidity
  252. Solana Library
  253. ______________
  254. In Solang's Github repository, there is a directory called ``solana-library``. It contains libraries for Solidity contracts
  255. to interact with Solana specific instructions. We provide two libraries: one for SPL tokens and another
  256. for Solana's system instructions. In order to use those functionalities, copy the correspondent library
  257. file to your project and import it.
  258. SPL-token
  259. +++++++++
  260. `spl-token <https://spl.solana.com/token>`_ is the Solana native way of creating tokens, minting, burning and
  261. transferring token. This is the Solana equivalent of
  262. `ERC-20 <https://ethereum.org/en/developers/docs/standards/tokens/erc-20/>`_ and
  263. `ERC-721 <https://ethereum.org/en/developers/docs/standards/tokens/erc-721/>`_. Solang's repository contains
  264. a library ``SplToken`` to use spl-token from Solidity. The file
  265. `spl_token.sol <https://github.com/hyperledger-solang/solang/blob/main/solana-library/spl_token.sol>`_ should be copied into
  266. your source tree, and then imported in your solidity files where it is required. The ``SplToken`` library has doc
  267. comments explaining how it should be used.
  268. There is an example in our integration tests of how this should be used. See
  269. `token.sol <https://github.com/hyperledger-solang/solang/blob/main/integration/solana/token.sol>`_ and
  270. `token.spec.ts <https://github.com/hyperledger-solang/solang/blob/main/integration/solana/token.spec.ts>`_.
  271. .. _system_instruction_library:
  272. System Instructions
  273. +++++++++++++++++++
  274. Solana's system instructions enable developers to interact with Solana's System Program. There are functions to
  275. create new accounts, allocate account data, assign accounts to owning programs, transfer lamports from System Program
  276. owned accounts and pay transaction fees. More information about the functions offered can be found both on
  277. `Solana documentation <https://docs.rs/solana-program/1.11.10/solana_program/system_instruction/enum.SystemInstruction.html>`_
  278. and on Solang's `system_instruction.sol <https://github.com/hyperledger-solang/solang/blob/main/solana-library/system_instruction.sol>`_ file.
  279. The usage of system instructions needs the correct setting of writable and signer accounts when interacting with Solidity
  280. contracts on chain. Examples are available on Solang's integration tests.
  281. See `system_instruction_example.sol <https://github.com/hyperledger-solang/solang/blob/main/integration/solana/system_instruction_example.sol>`_
  282. and `system_instruction.spec.ts <https://github.com/hyperledger-solang/solang/blob/main/integration/solana/system_instruction.spec.ts>`_
  283. Minimum balance
  284. +++++++++++++++
  285. In order to instantiate a contract, you need the minimum balance required for a Solana account of a given
  286. size. There is a function ``minimum_balance(uint64 space)`` defined in
  287. `minimum_balance.sol <https://github.com/hyperledger-solang/solang/blob/main/solana-library/minimum_balance.sol>`_
  288. to calculate this.
  289. .. _account_management:
  290. Solana Account Management
  291. _________________________
  292. In a contract constructor, one can optionally write the ``@payer`` annotation, which receives a character sequence as
  293. an argument. This annotation defines a Solana account that is going to pay for the initialization of the contract's data
  294. account. The syntax ``@payer(my_account)`` declares an account named ``my_account``, which will be
  295. required for every call to the constructor.
  296. Similarly, for external functions in a contract, one can declare the necessary accounts, using function annotations.
  297. ``@account(myAcc)`` declares a read only account ``myAcc``, while ``@mutableAccount(otherAcc)`` declares a mutable
  298. account ``otherAcc``. For signer accounts, the annotations follow the syntax ``@signer(mySigner)`` and
  299. ``@mutableSigner(myOtherSigner)``.
  300. Accessing accounts' data
  301. ++++++++++++++++++++++++
  302. Accounts declared on a constructor using the ``@payer`` annotation are available for access inside it. Likewise,
  303. accounts declared on external functions with any of the aforementioned annotations are also available in the
  304. ``tx.accounts`` vector for easy access. For an account declared as ``@account(funder)``, the access follows the
  305. syntax ``tx.accounts.funder``, which returns the :ref:`AccountInfo builtin struct <account_info>`.
  306. .. include:: ../examples/solana/account_access.sol
  307. :code: solidity
  308. .. _solana_cpi_accounts:
  309. External calls with accounts
  310. ++++++++++++++++++++++++++++
  311. In any Solana cross program invocation, including constructor calls, all the accounts a transaction needs must be
  312. informed. If the ``{accounts: ...}`` call argument is missing from an external call, the compiler will automatically
  313. generate the ``AccountMeta`` array that satisfies such a requirement. Currently, that only works if the call is done
  314. in an function declared external, as shown in the example below. In any other case, the ``AccountMeta`` array must
  315. be manually created, following the account ordering the IDL file specifies. If a certain call does not need any accounts,
  316. an empty vector must be passed ``{accounts: []}``.
  317. The following example shows two correct ways of calling a contract. Note that the IDL for the ``BeingBuilt`` contract
  318. has an instruction called ``new``, representing the contract's constructor, whose accounts are specified in the
  319. following order: ``dataAccount``, ``payer_account``, ``systemAccount``. That is the order one must follow when invoking
  320. such a constructor.
  321. .. include:: ../examples/solana/payer_annotation.sol
  322. :code: solidity