builtins.rst 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. Builtin Functions and Variables
  2. ===============================
  3. .. _builtins:
  4. The Solidity language has a number of built-in variables and functions which give
  5. access to the chain environment or pre-defined functions. Some of these functions will
  6. be different on different chains.
  7. Block and transaction
  8. _____________________
  9. The functions and variables give access to block properties like block
  10. number and transaction properties like gas used, and value sent.
  11. gasleft() returns (uint64)
  12. ++++++++++++++++++++++++++
  13. Returns the amount of gas remaining the current transaction.
  14. .. note::
  15. ``gasleft()`` is not available on Solana.
  16. Gasprice is not used on Solana. There is compute budget which may not be
  17. exceeded, but there is no charge based on compute units used.
  18. blockhash(uint64 block) returns (bytes32)
  19. +++++++++++++++++++++++++++++++++++++++++
  20. Returns the blockhash for a particular block. This not possible for the current
  21. block, or any block except for the most recent 256. Do not use this a source of
  22. randomness unless you know what you are doing.
  23. .. note::
  24. This function is not available on Solana. There is the
  25. `recent block hashes account <https://edge.docs.solana.com/developing/runtime-facilities/sysvars#recentblockhashes>`_
  26. that looks useful at first glance, however it is not usable because:
  27. - This account is `deprecated <https://github.com/solana-labs/solana/pull/18875>`_.
  28. - It does not give any slot of block number, so it is not possible to provide a matching
  29. function signature.
  30. ``msg`` properties
  31. ++++++++++++++++++
  32. uint128 ``msg.value``
  33. The amount of value sent with a transaction, or 0 if no value was sent.
  34. bytes ``msg.data``
  35. The raw ABI encoded arguments passed to the current call.
  36. bytes4 (Polkadot) or bytes8 (Solana) ``msg.sig``
  37. Function selector (or discriminator for Solana) from the encoded calldata,
  38. e.g. the first four or eight bytes. This might be 0 if no function selector was present.
  39. In Ethereum, constructor calls do not have function selectors but in Polkadot they do.
  40. On Solana, selectors are called discriminators.
  41. address ``msg.sender``
  42. The sender of the current call. This is either the address of the contract
  43. that called the current contract, or the address that started the transaction
  44. if it called the current contract directly.
  45. ``tx`` properties
  46. +++++++++++++++++
  47. .. _gasprice:
  48. uint128 ``tx.gasprice``
  49. The price of one unit of gas. This field cannot be used on Polkadot,
  50. see the warning box below.
  51. .. note::
  52. ``tx.gasprice`` is not available on Solana.
  53. gasprice is not used on Solana. There is compute budget which may not be
  54. exceeded, but there is no charge based on compute units used.
  55. uint128 ``tx.gasprice(uint64 gas)``
  56. The total price of `gas` units of gas.
  57. .. warning::
  58. On Polkadot, the cost of one gas unit may not be an exact whole round value. In fact,
  59. if the gas price is less than 1 it may round down to 0, giving the incorrect appearance gas is free.
  60. Therefore, avoid the ``tx.gasprice`` member in favour of the function ``tx.gasprice(uint64 gas)``.
  61. To avoid rounding errors, pass the total amount of gas into ``tx.gasprice(uint64 gas)`` rather than
  62. doing arithmetic on the result. As an example, **replace** this bad example:
  63. .. code-block:: solidity
  64. // BAD example
  65. uint128 cost = num_items * tx.gasprice(gas_per_item);
  66. with:
  67. .. code-block:: solidity
  68. uint128 cost = tx.gasprice(num_items * gas_per_item);
  69. Note this function is not available on the Ethereum Foundation Solidity compiler.
  70. address ``tx.origin``
  71. The address that started this transaction. Not available on Polkadot or Solana.
  72. AccountInfo[] ``tx.accounts``
  73. Only available on Solana. See :ref:`account_info`. Here is an example:
  74. .. include:: ../examples/solana/accountinfo.sol
  75. :code: solidity
  76. ``block`` properties
  77. ++++++++++++++++++++++
  78. Some block properties are always available:
  79. uint64 ``block.number``
  80. The current block number.
  81. uint64 ``block.timestamp``
  82. The time in unix epoch, i.e. seconds since the beginning of 1970.
  83. Do not use either of these two fields as a source of randomness unless you know what
  84. you are doing.
  85. The other block properties depend on which chain is being used.
  86. .. note::
  87. Solana requires the `clock account <https://edge.docs.solana.com/developing/runtime-facilities/sysvars#clock>`_
  88. to present in the account for the instruction to use any of the ``block`` fields.
  89. On Solana, ``block.number`` gives the slot number rather than the block height.
  90. For processing, you want to use the slot rather the block height. Slots
  91. include empty blocks, which do not count towards the block height.
  92. Solana
  93. ~~~~~~
  94. uint64 ``block.slot``
  95. The current slot. This is an alias for ``block.number``.
  96. Polkadot
  97. ~~~~~~~~
  98. uint128 ``block.minimum_deposit``
  99. The minimum amonut needed to create a contract. This does not include
  100. storage rent.
  101. Ethereum
  102. ~~~~~~~~
  103. uint64 ``block.gaslimit``
  104. The current block gas limit.
  105. address payable ``block.coinbase``
  106. The current block miner's address.
  107. uint256 ``block.difficulty``
  108. The current block's difficulty.
  109. Error handling
  110. ______________
  111. assert(bool)
  112. ++++++++++++
  113. Assert takes a boolean argument. If that evaluates to false, execution is aborted.
  114. .. include:: ../examples/revert.sol
  115. :code: solidity
  116. revert() or revert(string)
  117. ++++++++++++++++++++++++++
  118. revert aborts execution of the current contract, and returns to the caller. revert()
  119. can be called with no arguments, or a single `string` argument, which is called the
  120. `ReasonCode`. This function can be called at any point, either in a constructor or
  121. a function.
  122. If the caller is another contract, it can use the `ReasonCode` in a :ref:`try-catch`
  123. statement.
  124. .. include:: ../examples/assert.sol
  125. :code: solidity
  126. require(bool) or require(bool, string)
  127. ++++++++++++++++++++++++++++++++++++++
  128. This function is used to check that a condition holds true, or abort execution otherwise. So,
  129. if the first `bool` argument is `true`, this function does nothing, however
  130. if the `bool` arguments is `false`, then execution is aborted. There is an optional second
  131. `string` argument which is called the `ReasonCode`, which can be used by the caller
  132. to identify what the problem is.
  133. .. include:: ../examples/require.sol
  134. :code: solidity
  135. ABI encoding and decoding
  136. _________________________
  137. The ABI encoding depends on the target being compiled for. Polkadot uses the
  138. `SCALE Codec <https://docs.substrate.io/reference/scale-codec/>`_.
  139. abi.decode(bytes, (*type-list*))
  140. ++++++++++++++++++++++++++++++++
  141. This function decodes the first argument and returns the decoded fields. *type-list* is a comma-separated
  142. list of types. If multiple values are decoded, then a destructure statement must be used.
  143. .. code-block:: solidity
  144. uint64 foo = abi.decode(bar, (uint64));
  145. .. code-block:: solidity
  146. (uint64 foo1, bool foo2) = abi.decode(bar, (uint64, bool));
  147. If the arguments cannot be decoded, contract execution will abort. This can happen if the encoded
  148. length is too short, for example.
  149. abi.encode(...)
  150. +++++++++++++++
  151. ABI encodes the arguments to bytes. Any number of arguments can be provided.
  152. .. code-block:: solidity
  153. uint16 x = 241;
  154. bytes foo = abi.encode(x);
  155. On Polkadot, foo will be ``hex"f100"``. On Ethereum this will be ``hex"00000000000000000000000000000000000000000000000000000000000000f1"``.
  156. abi.encodeWithSelector(selector, ...)
  157. ++++++++++++++++++++++++++++++++++++++++++++
  158. ABI encodes the arguments with the function selector, which is known as the discriminator on Solana.
  159. After the selector, any number of arguments can be provided.
  160. .. code-block:: solidity
  161. // An eight-byte selector (discriminator) is exclusive for Solana.
  162. // On Polkadot, the selector contains four bytes. hex"01020304" is an example.
  163. bytes foo = abi.encodeWithSelector(hex"0102030405060708", uint16(0xff00));
  164. On Solana, foo will be ``hex"080706050403020100ff"``. In addition, a discriminator for a Solidity function on Solana
  165. are the first eight bytes of the sha-256 hash of its name converted to camel case and preceded
  166. by the prefix ``global:``, as the following:
  167. .. code-block:: solidity
  168. bytes8 discriminator = bytes8(sha256(bytes("global:myFunctionName")));
  169. abi.encodeWithSignature(string signature, ...)
  170. ++++++++++++++++++++++++++++++++++++++++++++++
  171. ABI encodes the arguments with the hash of the signature. After the signature, any number of arguments
  172. can be provided.
  173. On Polkadot, the signature is the name of the function followed by its arguments, for example:
  174. .. code-block:: solidity
  175. bytes foo = abi.encodeWithSignature("foo_bar(uint64)", uint64(257));
  176. ``foo`` will be ``hex"e934aa71_0101_0000__0000_0000"``. This is equivalent to ``abi.encodeWithSelector(bytes4(keccak256("test2(uint64)")), ...)``.
  177. On Solana, the signature is known as the discriminator image. It is the function name without any arguments,
  178. converted to camel case, and preceded by the prefix ``global:``.
  179. For example, if you had the function ``foo_bar(uint64)``, the discriminator image would be ``global:fooBar``.
  180. .. code-block:: solidity
  181. bytes foo = abi.encodeWithSignature("global:fooBar", uint64(257));
  182. This builtin is equivalent to
  183. ``abi.encodeWithSelector(bytes8(sha256(bytes("global:fooBar"))), ...)`` for Solana.
  184. abi.encodePacked(...)
  185. +++++++++++++++++++++
  186. ABI encodes the arguments to bytes. Any number of arguments can be provided. The packed encoding only
  187. encodes the raw data, not the lengths of strings and arrays. For example, when encoding ``string`` only the string
  188. bytes will be encoded, not the length. It is not possible to decode packed encoding.
  189. .. code-block:: solidity
  190. bytes foo = abi.encodePacked(uint16(0xff00), "ABCD");
  191. On Polkadot, foo will be ``hex"00ff41424344"``. On Ethereum this will be ``hex"ff0041424344"``.
  192. abi.encodeCall(function, ...)
  193. +++++++++++++++++++++++++++++
  194. ABI encodes the function call to the function which should be specified as ``ContractName.FunctionName``. The arguments
  195. are cast and checked against the function specified as the first argument.
  196. .. include:: ../examples/abi_encode_call.sol
  197. :code: solidity
  198. Hash
  199. ++++
  200. Only available on Polkadot, it represents the ``Hash`` type from ``ink_primitives`` via user type definition.
  201. Its underlying type is ``bytes32``, but it will be reported correctly as the ``Hash`` type in the metadata.
  202. .. include:: ../examples/polkadot/hash_type.sol
  203. :code: solidity
  204. chain_extension(uint32 ID, bytes input) returns (uint32, bytes)
  205. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  206. Only available on Polkadot. Call the chain extension with the given ``ID`` and ``input`` data.
  207. Returns the return value from the chain extension and the output data.
  208. This function is a low level interface.
  209. The caller is responsible for encoding the input and decoding the output correctly.
  210. We expect parachain authors to write their own higher level libraries on top.
  211. .. warning::
  212. This function calls the runtime API `call_chain_extension <https://docs.rs/pallet-contracts/latest/pallet_contracts/api_doc/trait.Version0.html#tymethod.call_chain_extension>`_.
  213. It assumes that the implementation of the chain extension
  214. - reads the input from the ``input_ptr`` parameter, used as a buffer pointer
  215. - writes potential output into the buffer found at the ``output_ptr`` pointer
  216. - respects the output buffer length in ``output_len_ptr`` to prevent OOB writes. The output buffer is 16KB in size.
  217. - writes the amount of bytes written to ``output_ptr`` into the buffer at ``output_len_ptr``
  218. Unlike with other runtime API calls, the contracts pallet can not guarantee this behaviour.
  219. Instead, it's specific to the targeted chain runtime. Hence, when using this builtin,
  220. you must be sure that the implementation being called underneath is compatible.
  221. The following example demonstrates the usage of this builtin function.
  222. It shows how the chain extension example from the `ink! documentation <https://use.ink/macros-attributes/chain-extension/>`_
  223. looks like in a solidity contract:
  224. .. include:: ../examples/polkadot/call_chain_extension.sol
  225. :code: solidity
  226. is_contract(address AccountId) returns (bool)
  227. +++++++++++++++++++++++++++++++++++++++++++++
  228. Only available on Polkadot. Checks whether the given address is a contract address.
  229. set_code_hash(uint8[32] hash) returns (uint32)
  230. ++++++++++++++++++++++++++++++++++++++++++++++
  231. Only available on Polkadot. Replace the contract's code with the code corresponding to ``hash``.
  232. Assumes that the new code was already uploaded, otherwise the operation fails.
  233. A return value of 0 indicates success; a return value of 7 indicates that there was no corresponding code found.
  234. .. note::
  235. This is a low level function. We strongly advise consulting the underlying
  236. `API documentation <https://docs.rs/pallet-contracts/latest/pallet_contracts/api_doc/trait.Version0.html#tymethod.set_code_hash>`_
  237. to obtain a full understanding of its implications.
  238. This functionality is intended to be used for implementing upgradeable contracts.
  239. Pitfalls generally applying to writing
  240. `upgradeable contracts <https://docs.openzeppelin.com/upgrades-plugins/1.x/writing-upgradeable>`_
  241. must be considered whenever using this builtin function, most notably:
  242. * The contract must safeguard access to this functionality, so that it is only callable by priviledged users.
  243. * The code you are upgrading to must be
  244. `storage compatible <https://docs.openzeppelin.com/upgrades-plugins/1.x/proxies#storage-collisions-between-implementation-versions>`_
  245. with the existing code.
  246. * Constructors and any other initializers, including initial storage value definitions, won't be executed.
  247. Cryptography
  248. ____________
  249. keccak256(bytes)
  250. ++++++++++++++++
  251. This returns the ``bytes32`` keccak256 hash of the bytes.
  252. ripemd160(bytes)
  253. ++++++++++++++++
  254. This returns the ``bytes20`` ripemd160 hash of the bytes.
  255. sha256(bytes)
  256. +++++++++++++
  257. This returns the ``bytes32`` sha256 hash of the bytes.
  258. blake2_128(bytes)
  259. +++++++++++++++++
  260. This returns the ``bytes16`` blake2_128 hash of the bytes.
  261. .. note::
  262. This function is only available on Polkadot.
  263. blake2_256(bytes)
  264. +++++++++++++++++
  265. This returns the ``bytes32`` blake2_256 hash of the bytes.
  266. .. note::
  267. This function is only available on Polkadot.
  268. signatureVerify(address public_key, bytes message, bytes signature)
  269. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  270. Verify the ed25519 signature given the public key, message, and signature. This
  271. function returns ``true`` if the signature matches, ``false`` otherwise.
  272. The transactions which executes this function, needs an
  273. `ed25519 program <https://edge.docs.solana.com/developing/runtime-facilities/programs#ed25519-program>`_
  274. instruction with matching public key, message, and signature.
  275. In order to examine the instruction, the
  276. `instructions sysvar <https://edge.docs.solana.com/developing/runtime-facilities/sysvars#instructions>`_
  277. needs be in the accounts for the Solidity instruction as well.
  278. .. note::
  279. This function is only available on Solana.
  280. Mathematical
  281. ____________
  282. addmod(uint x, uint y, uint, k) returns (uint)
  283. ++++++++++++++++++++++++++++++++++++++++++++++
  284. Add x to y, and then divides by k. x + y will not overflow.
  285. mulmod(uint x, uint y, uint, k) returns (uint)
  286. ++++++++++++++++++++++++++++++++++++++++++++++
  287. Multiply x with y, and then divides by k. x * y will not overflow.
  288. Encoding and decoding values from bytes buffer
  289. ______________________________________________
  290. The ``abi.encode()`` and friends functions do not allow you to write or read data
  291. from an arbitrary offset, so the Solang dialect has the following functions. These
  292. methods are available on a ``bytes`` type.
  293. These functions are inspired by the `node buffer api <https://nodejs.org/api/buffer.html>`_.
  294. .. code-block:: solidity
  295. contract c {
  296. function f() public returns (bytes) {
  297. bytes data = new bytes(10);
  298. data.writeUint32LE(102, 0);
  299. data.writeUint64LE(0xdeadcafe, 4);
  300. return data;
  301. }
  302. function g(bytes data) public returns (uint64) {
  303. return data.readUint64LE(1);
  304. }
  305. }
  306. readInt8(uint32 offset)
  307. +++++++++++++++++++++++
  308. Read a signed ``int8`` from the specified offset.
  309. readInt16LE(uint32 offset)
  310. ++++++++++++++++++++++++++
  311. Read a signed ``int16`` from the specified offset in little endian order.
  312. readInt32LE(uint32 offset)
  313. ++++++++++++++++++++++++++
  314. Read a signed ``int32`` from the specified offset in little endian order.
  315. readInt64LE(uint32 offset)
  316. ++++++++++++++++++++++++++
  317. Read a signed ``int64`` from the specified offset in little endian order.
  318. readInt128LE(uint32 offset)
  319. +++++++++++++++++++++++++++
  320. Read a signed ``int128`` from the specified offset in little endian order.
  321. readInt256LE(uint32 offset)
  322. +++++++++++++++++++++++++++
  323. Read a signed ``int256`` from the specified offset in little endian order.
  324. readUint16LE(uint32 offset)
  325. +++++++++++++++++++++++++++
  326. Read an unsigned ``uint16`` from the specified offset in little endian order.
  327. readUint32LE(uint32 offset)
  328. +++++++++++++++++++++++++++
  329. Read an unsigned ``uint32`` from the specified offset in little endian order.
  330. readUint64LE(uint32 offset)
  331. +++++++++++++++++++++++++++
  332. Read an unsigned ``uint64`` from the specified offset in little endian order.
  333. readUint128LE(uint32 offset)
  334. ++++++++++++++++++++++++++++
  335. Read an unsigned ``uint128`` from the specified offset in little endian order.
  336. readUint256LE(uint32 offset)
  337. ++++++++++++++++++++++++++++
  338. Read an unsigned ``uint256`` from the specified offset in little endian order.
  339. readAddress(uint32 offset)
  340. ++++++++++++++++++++++++++
  341. Read an ``address`` from the specified offset.
  342. writeInt8(int8 value, uint32 offset)
  343. ++++++++++++++++++++++++++++++++++++
  344. Write a signed ``int8`` to the specified offset.
  345. writeInt16LE(int16 value, uint32 offset)
  346. ++++++++++++++++++++++++++++++++++++++++
  347. Write a signed ``int16`` to the specified offset in little endian order.
  348. writeInt32LE(int32 value, uint32 offset)
  349. ++++++++++++++++++++++++++++++++++++++++
  350. Write a signed ``int32`` to the specified offset in little endian order.
  351. writeInt64LE(int64 value, uint32 offset)
  352. ++++++++++++++++++++++++++++++++++++++++
  353. Write a signed ``int64`` to the specified offset in little endian order.
  354. writeInt128LE(int128 value, uint32 offset)
  355. ++++++++++++++++++++++++++++++++++++++++++
  356. Write a signed ``int128`` to the specified offset in little endian order.
  357. writeInt256LE(int256 value, uint32 offset)
  358. ++++++++++++++++++++++++++++++++++++++++++
  359. Write a signed ``int256`` to the specified offset in little endian order.
  360. writeUint16LE(uint16 value, uint32 offset)
  361. ++++++++++++++++++++++++++++++++++++++++++
  362. Write an unsigned ``uint16`` to the specified offset in little endian order.
  363. writeUint32LE(uint32 value, uint32 offset)
  364. ++++++++++++++++++++++++++++++++++++++++++
  365. Write an unsigned ``uint32`` to the specified offset in little endian order.
  366. writeUint64LE(uint64 value, uint32 offset)
  367. ++++++++++++++++++++++++++++++++++++++++++
  368. Write an unsigned ``uint64`` to the specified offset in little endian order.
  369. writeUint128LE(uint128 value, uint32 offset)
  370. ++++++++++++++++++++++++++++++++++++++++++++
  371. Write an unsigned ``uint128`` to the specified offset in little endian order.
  372. writeUint256LE(uint256 value, uint32 offset)
  373. ++++++++++++++++++++++++++++++++++++++++++++
  374. Write an unsigned ``uint256`` to the specified offset in little endian order.
  375. writeAddress(address value, uint32 offset)
  376. ++++++++++++++++++++++++++++++++++++++++++
  377. Write an ``address`` to the specified offset.
  378. writeString(string value, uint32 offset)
  379. ++++++++++++++++++++++++++++++++++++++++++++
  380. Write the characters of a ``string`` to the specified offset. This function does not
  381. write the length of the string to the buffer.
  382. writeBytes(bytes value, uint32 offset)
  383. ++++++++++++++++++++++++++++++++++++++++++
  384. Write the bytes of a Solidity dynamic bytes type ``bytes`` to the specified offset.
  385. This function does not write the length of the byte array to the buffer.
  386. Miscellaneous
  387. _____________
  388. .. _print_function:
  389. print(string)
  390. +++++++++++++
  391. print() takes a string argument.
  392. .. include:: ../examples/print.sol
  393. :code: solidity
  394. .. note::
  395. print() is not available with the Ethereum Foundation Solidity compiler.
  396. When using Polkadot, this function is only available on development chains.
  397. If you use this function on a production chain, the contract will fail to load.
  398. .. _selfdestruct:
  399. selfdestruct(address payable recipient)
  400. +++++++++++++++++++++++++++++++++++++++
  401. The ``selfdestruct()`` function causes the current contract to be deleted, and any
  402. remaining balance to be sent to `recipient`. This functions does not return, as the
  403. contract no longer exists.
  404. .. note::
  405. This function does not exist on Solana.
  406. String formatting using ``"{}".format()``
  407. +++++++++++++++++++++++++++++++++++++++++
  408. Sometimes it is useful to convert an integer to a string, e.g. for debugging purposes. There is
  409. a format builtin function for this, which is a method on string literals. Each ``{}`` in the
  410. string will be replaced with the value of an argument to format().
  411. .. code-block:: solidity
  412. function foo(int arg1, bool arg2) public {
  413. print("foo entry arg1:{} arg2:{}".format(arg1, arg2));
  414. }
  415. Assuming `arg1` is 5355 and `arg2` is true, the output to the log will be ``foo entry arg1:5355 arg2:true``.
  416. The types accepted by format are ``bool``, ``uint``, ``int`` (any size, e.g. ``int128`` or ``uint64``), ``address``,
  417. ``bytes`` (fixed and dynamic), and ``string``. Enums are also supported, but will print the ordinal value
  418. of the enum. The ``uint`` and ``int`` types can have a format specifier. This allows you to convert to
  419. hexadecimal ``{:x}`` or binary ``{:b}``, rather than decimals. No other types
  420. have a format specifier. To include a literal ``{`` or ``}``, replace it with ``{{`` or ``}}``.
  421. .. code-block:: solidity
  422. function foo(int arg1, uint arg2) public {
  423. // print arg1 in hex, and arg2 in binary
  424. print("foo entry {{arg1:{:x},arg2:{:b}}}".format(arg1, arg2));
  425. }
  426. Assuming `arg1` is 512 and `arg2` is 196, the output to the log will be ``foo entry {arg1:0x200,arg2:0b11000100}``.
  427. .. warning::
  428. Each time you call the ``format()`` some specialized code is generated, to format the string at
  429. runtime. This requires loops and so on to do the conversion.
  430. When formatting integers in to decimals, types larger than 64 bits require expensive division.
  431. Be mindful this will increase the gas cost. Larger values will incur a higher gas cost.
  432. Alternatively, use a hexadecimal ``{:x}`` format specifier to reduce the cost.