Tip: Using Remix IDE
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
This lesson preview is part of the Million Ether Homepage course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to Million Ether Homepage, plus 70+ \newline books, guides and courses with the \newline Pro subscription.
[00:00 - 00:07] I want to give you another tip that will save you tons of time. When developing your smart contracts, use an IDE like Remix.
[00:08 - 00:23] In our videos, we've been compiling our smart contracts by hand using Sulk, and this is a good idea for learning what's going on and it's a best practice for production contracts. That said, when we're in the early development stages, copying and pasting byte code and ABIs can be a real pain.
[00:24 - 00:30] Thankfully, there are editors like Remix that make this a lot easier. And I want to give a disclaimer first.
[00:31 - 00:41] In this video and in this course, I'm going to be using Remix as a tool to quickly compile, deploy and experiment with smart contracts. I want you to know that Remix is not essential.
[00:42 - 00:50] You can and should be able to do anything I'm doing in Remix on the command line. We've covered how to do it in earlier videos.
[00:51 - 00:59] Remix is a tool to make our lives easier, but it's not a requirement. Another thing I want you to keep in mind is that Remix is under active development.
[01:00 - 01:14] I'm using the most recent version of Remix, but by the time you're watching this video, the design might have changed a little bit. If you see me using a feature that doesn't appear in the same place in your version, check the comments section for updated instructions.
[01:15 - 01:22] With that in mind, let's take a look at Remix. There's a deployed version of Remix at remix.etherium.org.
[01:23 - 01:29] You can also find the code at GitHub. In the main pane, you can see some default Solidity code.
[01:30 - 01:38] On the left, we have our file explorer, and on the right, we have some tools. For our development, we need to connect this page in two ways to our local computer.
[01:39 - 01:52] One, we're going to connect the Remix file pane to our local file system using a tool called RemixD. And two, we can connect this page to our local Geth test network using Web3.
[01:53 - 02:01] Keep in mind that we need to be very careful about security here. It's probably fine in this case to connect to your local Geth instance as long as it doesn't hold your real private keys.
[02:02 - 02:19] That said, if you want to be extra careful with your security, I'd recommend downloading Remix from GitHub and running a local instance instead of using this hosted instance. The hack scenario here would be where an attacker hacks the hosted version of Remix and secretly configures it to steal your private keys.
[02:20 - 02:26] This hasn't happened yet that I know of, but better safe than sorry. Let's connect Remix to our local file system using RemixD.
[02:27 - 02:34] You can view the GitHub repo for RemixD here, which gives you installation instructions. Right now it uses NPM install.
[02:35 - 02:49] We use it by starting RemixD and passing the path to the directory we want to use for our contracts. Now, back in Remix, we can click the chain connect icon above the file browser.
[02:50 - 02:56] And if you're lucky, it will connect to your local RemixD. Here you can see that I'm working on the million ether page.soul.
[02:57 - 03:04] Remix can run our code in a variety of environments. The default environment is a JavaScript implementation of the EVM.
[03:05 - 03:13] The JavaScript version is really fast, but it all runs within our browser. Instead, what we're going to do is connect to our local Geth node.
[03:14 - 03:25] This will let us use Remix as an interface to our local durable blockchain. We can also perform actions on the Geth console that affects state, and we'll be able to create a web UI that also reads from that same Geth.
[03:26 - 03:34] By connecting to Geth, we can use it as a central access point. In order for this to work, you need to have started Geth with the correct RPC options.
[03:35 - 03:42] Here's one Geth command that would work, though we'll use others in other videos. To connect to our local node, we'll pick from the dropdown web3 provider.
[03:43 - 03:53] Right now the default says localhost port 8545. I have to use 127.0.0.1 to connect to my machine.
[03:54 - 04:04] If it worked, you should see your accounts dropdown change. Within this Geth instance, I already have some accounts, and so they appear in the dropdown along with the amount of ether in each account.
[04:05 - 04:17] If we click the contract details, you can see a number of helpful info panels. For example, we can see the compiled bytecode, the ABI, the gas estimates, and even the assembly.
[04:18 - 04:27] Deploying an instance of our contract is super easy. We can create an instance on our blockchain by hitting Create.
[04:28 - 04:37] This will submit a transaction to our Geth node. And remember, you'll need to mine this and any transaction you send there, so make sure that you have a minor running.
[04:38 - 04:47] After we deployed, you can see that we have an address for this instance. We can copy this address, save it, and use it in the Geth console or in our apps.
[04:48 - 04:57] Notice that Remix also reported the amount of gas required to create this contract. Another thing you might notice is that the methods on our contract object now have form fields.
[04:58 - 05:05] One of the best things about Remix is that now we can use these forms as a minimal UI to test our contracts. Let's test it now.
[05:06 - 05:16] In this contract, we're storing an array of arrays of pixels stored as bytes 3. We can get the value of any particular pixel by using the pixels function.
[05:17 - 05:30] If we wanted to get the pixels at say 74, you can see that it returned 0. To set the color of a pixel, we need three arguments, the x index, the y index, and three bytes.
[05:31 - 05:37] The x and y index are easy enough, we just pass the numbers themselves. But passing three bytes is a little awkward.
[05:38 - 05:47] What we have to do is pass each byte itself as a hex encoded string. So here we have an array with three bytes, each byte individually encoded as hex.
[05:48 - 06:05] After we submit that transaction in its mind, we're given back a transaction receipt, which we can inspect to find the block number of that transaction as well as the amount of gas used. Notice that the from field is our account address, and the to field is the address of our contract.
[06:06 - 06:13] Our contracts account address lives on the blockchain like any other address. Another thing you can do with remix is debug particular transactions.
[06:14 - 06:27] I'm not going to walk through it here, but click on the launch debugger button to walk through the EVM opcodes that happened during our transaction. Now we can check the value of the pixel 74 by using pixels again.
[06:28 - 06:30] Notice that it returned our color in hexadecimal. Nice.
[06:31 - 06:39] Let's say that you refresh the page and you want to get set up again. You'll need to reconnect to remix D and also to your web3 provider.
[06:40 - 06:51] If you want to reuse an old contract, you can click the at address button and then paste the address of an already deployed contract. This will let you interact with that contract and keep its state.
[06:52 - 06:56] So that's the basics of remix. It might change a little by the time you watch this, so check out their docs for the latest.
[06:57 - 07:03] And you don't have to use remix, but we're going to be using remix a lot in these videos and it's going to save us a ton of time.