Using the Iota EVM with Remix to run a Solidity Contract (1)

IMPORTANT. The Iota EVM development is subject to rapid change so please check the Iota Wiki for the latest information.

author: @dumdave (Iota and Assembly Discord tag)

Feedback on typos, corrections and improvements needed welcome.

last updated: 19th Jan 2022


Article summary: Deploying and then accessing a simple Solidity contract on an Iota EVM using the tool Remix in a Chrome browser with the MetaMask extension.

See also Article 2 in this series which extends the idea to an example Olympus DAO contract.


[1] Introduction.

EVM stands for "Ethereum Virtual Machine.

Solidity is a programming language widely used with the EVM.

There are a large number of tested and proven Solidity Smart Contracts for EVM, and many developers already use Solidity and EVM.

Iota now has a Smart Contracts platform in development that provides support for EVM/Solidity smart contracts as well as Wasm and other forms of smart contract.

It is expected that Solidity developers will find the Iota network of interest because of the possibility of fee free/low fee execution of EVM Smart Contracts.

This is the first in a series of articles providing a simple introduction to the Iota EVM initiative.


[2] The Solidity Contract

The particular Solidity contract used in this article will be very familiar to anyone who has already used Remix (see [3] below) as it is one of their three initial demo contracts. It looks as follows:

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

/**
 * @title Storage
 * @dev Store & retrieve value in a variable
 */
contract Storage {

    uint256 number;

    /**
     * @dev Store value in variable
     * @param num value to store
     */
    function store(uint256 num) public {
        number = num;
    }

    /**
     * @dev Return value 
     * @return value of 'number'
     */
    function retrieve() public view returns (uint256){
        return number;
    }
}                
            

The important thing here is that the contract lets you call it to set a numerical value and then call it again to retrieve the numerical value.

The aim of this article is to allow the reader to deploy this contract to an Iota EVM and then use it to set and retrieve numbers. After all, if you can do that, everything else just needs a little more code!


[3] Remix

The Remix IDE (Integrated Development Environment) is an open source tool that helps you write Solidity contracts within a browser. Throughout this article the browser used is Chrome.

remix.ethereum.org

Once Remix is opened in Chrome, there is usually a brief 'tutorial' and then the lefthand panel will look as follows (once the 'contracts' folder is selected). Note that Remix provides three default contracts, one of which will be used here.

Remix lefthand panel

When the contract "1. Storage.sol" is selected the screen should look as follows, with the contract discussed in [2] above on the righthand screen.

Remix both panels

Before a contract can be deployed to an Ethereum Virtual Machine it has to be compiled, which will be discussed below. There also has to be an EVM to deploy the compiled contract to. The next section briefly explains the Iota approach.


[4] The Iota Networks

For the purposes of this article, an EVM chain provided by an Iota Community Member will be used, and the reader here only needs to note the details provided below in [5] to set up Metamask - so the next two paragraphs can be skipped unless of interest. There is also more information in section 10 below when the EVM Block is inspected on the Iota WASP node.

For those who like to know a little more, the chain is running on an Iota WASP 'Smart Contracts' node, which in turn uses an Iota GoShimmer node to connect to the network. The WASP node runs one or more 'chains', one of which is the EVM chain.

The following linked guide is a useful way in to exploring the WASP and GoShimmer nodes if that is of interest. it in turn links back to installation of those nodes using a tool called SWARM. Guide to the Iota Wasp CLI Tool, GoShimmer and Smart Contracts (for SWARM users) - Part 1.


[5] MetaMask

For the purposes of this article, MetaMask will be used as a Chrome browser extension. MetaMask is designed to make accessing the Ethereum ecosystem easy, and it will work on any compatible network. Please make sure that you fully inform yourself of the security aspects of MetaMask before you use it for transactions with value.

If you do not already have Metamask set up on Chrome, the steps are to add the extension to your browser, and then create your account. Once that has been done, you have an account number (essentially your public address) and a connection (by default) to the main Ethereum network. You do not need to buy anything on that or elsewhere to complete this article.

Go to the main Remix page on your Chrome browser. At the top right of your browser you will see the icon for Metamask. When clicked, the following message should show:

Metamask reload message

Once the page is reloaded, a password may be requested.

Metamask password request

Note that at the top it shows the main Ethereum Network. Click on that to produce a dropdown box with an option Add Network. Select that to get a page that looks like this:

Metamask new network

There are only 3 boxes that you need to complete. Do so as follows:


Network Name: IOTA-DEFI1

New RPC URL: https://evm.wasp.sc.iota-defi.com

Chain ID: 1075

If you want to enter a currency token then use: IOTA (but it does not matter). Once you have saved your new network, ensure that it is selected at the top of the Metamask screen whenever you use Metamask in the remainder of this exercise.

RPC URL. The important part is the URL for the RPC Server. JSON-RPC is a stateless, light-weight remote procedure call protocol. The RPC URL above provides the server connection to an IOTA WASP node that controls an EVM chain.

Essentially, Remix lets you build and compile a Solidity contract and then, by using the MetaMask browser extension and your Account connect to the IOTA WASP Smart Contract node and deploy your contract on the EVM Chain there.


[6] Compile the contract on Remix

Returning to Remix, and being sure to select the 1.Storage.sol contract, click the second down of the four menu icons on the very left of the screen. You should see the following:

Remix compile panel 1

Click 'Compile' and the display should change to the one below. This is a simple contract so it should not take more than a few seconds.

Remix compile panel 2

If you have time, look at the compilation details - in particular, the ABI and the Bytecode. You can cut and paste them directly from the tiny icons seen on the image above, or go to Compilation Details to see lots more options.

The ABI (Application Binary Interface) provides details on how functions are called etc.

The Bytecode is what actually gets deployed on the Ethereum blockchain.


[7] Deploying the contract using Remix

Still in Remix, select the third down of the four menu icons on the very left of the screen. You should see the following:

Remix deploy panel 1

Towards the top there is Environment: JavaScript VM etc. Change that by using the dropdown box to 'Injected Web 3'. If you don't see that, then your MetaMask is not yet active on the page.

Remix deploy panel 2

Note that when you change to Injected Web 3, your Metamask account details are used in the page including 'Custom 1075 network'. Check that your Contract box still refers to your 1.Storage.sol contract and press Deploy.

Your Metamask should open and ask for you to confirm the transaction. As always with Metamask, check the details very carefully in case someone is trying to get you to click on a fraudulent transaction!

NB. Note the time that you deployed the contract.

Metamask confirm transaction

Assuming that you have been successful (and that may take a few minutes) the bottom of the Remix screen should look as follows. You may have to expand the bottom right window to see this.

Remix deployed contract info

Notice at the bottom left, the area below 'Deployed Contracts'. There is one contract shown, with 'Storage at' showing a value you can copy and retain. It is the address on the EVM chain for the contract. It will be something like: 0x605D651BC777638a408a7B5B39AE666391Fbb2Bb.


[8] Accessing the deployed contract

Still in Remix, if you look to the left of the 'Storage at' line there is a symbol. Clicking on it opens up the following options, which relate of course to the original contract - the option to Store and Retrieve values.

Remix contract interaction 1

Enter 42 into the box to the right of the 'store' button and then press it. Metamask should pop up, asking you to confirm the transaction.

If successful, you should see a message similar to the following on the Remix page. Note the 'decoded input' of 42!

Remix contract interaction 2

To complete the logic, now press the 'retrieve' button. You should see an output like the following, with 42 in the 'decoded output'. Note that there was no need to confirm the transaction in Metamask this time as no changes were being made to the chain.

Remix contract interaction 3

Repeat the storage and retrieval activity one more time with the number of your own choice, and once again record the time. We will find the Block on Wasp that records what you did, and the easiest way to locate it is with the Timestamp.


[9] Tracking activity in Metamask

As deploying the contract and storing a number both needed Metamask approval, Metamask has kept a record. If you open it you should see a screen similar to this:

Metamask records of activity 1

Click on the three dots at the top right, and select "expand screen". You can then see all activity.

Metamask records of activity 2

Expand 'Contract Deployment' and you will obtain a little more information including the Timestamps.

Metamask records of activity 3

If you do the same with the Store activity then you will be able to see the From To fields recording the 'To' which should be the same as the Contract Address on the chain recorded earlier from Remix.


[10] Viewing Blocks in the Iota WASP node.

As explained in [4] above, the Iota EVM chain we are using runs on an Iota WASP node. The particular node it runs on can be accessed at the following address with UN and Password wasp and wasp.

wasp.sc.iota-defi.com

It is run by community member (Iota Discord handle: @unrecognized_User) as a service to others. To find out more join the Iota Discord and go to the Smart Contracts or EVM Smart Contract channels. When you get to the initial dashboard screen it should look similar to the following:

There is also an 'official' EVM test chain if this chain happens to be unavailable. Check the Iota Wiki for more (but better still, join the Iota Discord).

Wasp dashboard

Select the CHAINS menu at the top to arrive at:

Wasp Chains

Click on the ID of the 'Test EVM' chain to get to a page full of detail. Towards the bottom you will see a mention of the Block Index. As the chain is regularly reset, this will probably be a low number.

Wasp EVM link to Block

Use the link to view the latest block, and to link to earlier blocks. There should be one block for your Contract Deployment, and one for each time you used Store to store a number (check the timestamps). There will in time be a better Block Explorer to use with more useful information.

Wasp EVM Block

Current limitations: As the Iota Wiki says: "The current experimental EVM support for IOTA Smart Contracts allows developers to get a preview of EVM based smart contract solutions on top of IOTA. There are some limitations you should be aware of ..., which we will be addressing in the months to come." Please read the Iota Wiki for more.


[11] Conclusion

If you completed the activity successfully then you have seen how Solidity Contracts can be deployed to the Iota EVM chain, and then their functions can be called. If you can do this in Remix then it is only a small step to implement this in code outside of Remix.

Future articles in this series will look at more complex contracts within Remix and then move on to more general ways to use the Iota EVM chain.