[Ethereum] How to quickly test a Solidity function


There are snippets and functions of Solidity code provided on this Ethereum Stack Exchange and other sites. What are ways to quickly test them, possibly debug a little, and play around?

Best Answer

One way is to use Remix, the browser-based Solidity realtime compiler and runtime Solidity (formerly named browser-solidity).

  1. Paste the code in Remix.
  2. Wrap the function inside a contract if needed. (Example: contract C { ... }.)
  3. Click Contract tab and then Create to instantiate the contract.
  4. Enter the desired input next to the function.
    • For bytes1 ... bytes32, use a quoted hex string, example "0xabcdef" for bytes3.
    • For bytes, use an array of hex strings, example: ["0xab", "0xcd", "0xef"].
    • For strings, make sure they are quoted.
    • For large numbers, make sure they are quoted too.
    • For an array, use [], example: ["hello", 42, "0xabcd"]
  5. Click the function to get the Result. The Result is ABI encoded.

Here is an example screenshot:

enter image description here