A Jedi’s Guide to Ethereum — Part 3b(Remix)

Mark Mathis
8 min readJan 15, 2018

In our last article, Master Yoda learned how to create a smart contract for a betting app in Solidity and write tests for it using the popular framework truffle. Truffle uses a private non-mining version of the Ethereum blockchain in order to simplify and speed up development. Yoda has plenty of time on his hands in swampy Dagobah and wants to deploy the contract to the private Ethereum network that Obi-Wan and he built in Part 1 and Part 2 of this series.

The Network

Please refer to the previous articles if you have any questions about the steps below. We are starting Obi-Wan and Yoda’s respective nodes on Tatooine and Dagobah with a few additional parameters that will become useful later in the article.

Dagobah

Run the following commands to start up the dagobah ethereum node in a new terminal. Note the new rpc parameters. These are to be used to connect the node with our Remix IDE later in the article.

geth --datadir "~/ethereum/dagobah" --networkid 5555 --rpc --rpcport 8545 --rpccorsdomain "http://remix.ethereum.org"

Open a new tab in the terminal and run the command below to open a geth console to issue commands to the dagobah node.

geth attach ~/ethereum/dagobah/geth.ipc

Tatooine

Open a new tab in the terminal and start the Tatooine ethereum node. Note that the — rpcport value is different than the Dagobah value. This is because the port must be unique as we will connect a separate Remix IDE to each ethereum node. Also, note the — rpccorsdomain is http: NOT https:

geth --datadir "~/ethereum/tatooine" --networkid 5555 --rpc --rpcport 8546 --rpccorsdomain "http://remix.ethereum.org"

Open another tab in the terminal and run the command below to open a geth console to issue commands to the tatooine node.

geth attach ~/ethereum/dagobah/geth.ipc

Connect the Nodes as Peers

Add the Dagobah node as a peer to the Tattoine node by running the following command in the Tattoine console.

admin.addPeer("enode://bffe12a6809d1e6664c9fffe8c5193cdb4e9d70640b0fbd729e1a5fd57f612674450486b1444fcdf6e7f91f42df0bb0a6707f8ae6637ec9b756075a8767dcde2@[::]:30303")

Open up the Dagobah console and add Tatooine as a peer to Dagobah as well

admin.addPeer("enode://af2293f6ef0cd9d253ed53850bdfc8b494539b1fb143e7df9269b6de7f67d4200a28beb0e4b2d63368cdc7e60fe1688413fe2f3423eabbba92f930224d26ed15@[::]:30304")

Check both the nodes by running the following with the expected output shown below

> net.listening
true
> net.peerCount
1

Start Mining

With our truffle network, deployment and transactions to the contract did not have to be mined. Now that we are using an actual ethereum node, we need to have miners running on both nodes. Note that in the real world, we probably would rely on external miners to process the transactions, but in this case we can simply run the following command in each console.

> miner.start()

This should begin the mining process and each node should begin showing logs like this as mining takes place

INFO [01-11|12:10:24] Updated mining threads                   threads=0
INFO [01-11|12:10:24] Transaction pool price threshold updated price=18000000000
INFO [01-11|12:10:24] Starting mining operation
INFO [01-11|12:10:24] Commit new mining work number=296 txs=1 uncles=0 elapsed=1.040ms

Remix

Remix is an IDE(Integrated Development Environment) provided by the Ethereum Foundation to help with Solidity development and deployment to the blockchain. It comes as a standalone app or as a web app. For our purposes we will use the web app in Chrome. Go ahead and open up two new tabs in Chrome pointed here: http://remix.ethereum.org/ (Note the http: protocol and not https:)

Compile the Contract

Go ahead and open up the first Remix tab and click the plus icon in the upper left corner.

Name the new file, Bet.sol, copy/paste our contract code in, and let it auto-compile. It may have some warnings, but it should compile and display in green as Bet below

Connect Remix to Dagobah and Tatooine Nodes

Before we can deploy the contract anywhere, we need to connect our Remix IDE(s) to a respective ethereum node. We will connect our first Remix instance to Dagobah and our second Remix instance to Tatooine. Since we gave the nodes unique rpc ports upon starting them, we should be able to connect each remix ide to a separate ethereum node on the same machine. The process for connecting each Remix instance is the same besides the port number, so we will simply show Dagobah’s setup for brevity.

Remix Setup(For Dagobah AND Tattoine)

  • Go to the Run tab in the right hand side of the Remix IDE and select the Web3 Provider from the Environment drop down.
  • It will prompt you to confirm you want to connect to an ethereum node and then present an input box for you to put the ethereum node’s address in. Put http://localhost:8545 in for Dagobah.
  • Click OK, and you should be connected to the node that you input.
  • Repeat the above process to connect the Tatooine node to the second Remix IDE using http://localhost:8546

Deploy Contract

Open up the Dagobah connected Remix instance. We will use this instance to deploy the contract. Our Remix should look like this, though the amount of eth we have would vary. We only have one account created on the Dagobah node and that is defaulted as shown here

If we click the Create button it will fail because we have not logged into the account defaulted to in Remix.

Let’s go ahead and login now by entering the following command into the Dagobah console in terminal

> personal.unlockAccount("0x0be3899c3e84e2015fe93788dee96b30fc48f0a6","yoda")

Now we can click the Create button and observe the transaction eventually get mined and go through.

Place Bet

Now that we have deployed our Bet.sol contract, let’s go ahead and place our bet. For this we need to do a couple things

  • Set our bet amount in the value input — making sure that ether is selected as the denomination. In our case we bet 5 ether.
  • Set our guess in the input box to the right of the createBet button. In our case we guessed the number was 4.

Click createBet and watch the transaction be processed in the Remix console(note: you may need to unlock the account again)

Go ahead and copy the contract address by clicking the copy icon. We will use it in the next step.

Take Bet

Go ahead and open the Tatooine Remix instance and paste the copied contract address into the At Address input box and click the At Address button. You should see something similar to the following:

Since Dagobah and Tatooine are peers, the attached miners have been keeping the blockchains in sync with each other. For this reason, we are able to open up the contract address that was originally created on the Dagobah node on the Tatooine node as well.

Let’s go ahead and take Yoda’s bet. For this we need to do a couple things

  • Set our bet amount in the value input — making sure that ether is selected as the denomination. This value needs to match Yoda’s bet in order to be taken. If you recall, Yoda bet 5 ether in our example.
  • Set our guess in the input box to the right of the takeBet button. In our case we guessed the number was 6.
  • We need to login to our Obi-Wan account or the transaction will fail — do so in the Tatooine console as follows
personal.unlockAccount("0x8d17ee6f8171a1d8cc26851213d5dc3b1f08cc40","vader")

Click takeBet and watch the transaction get mined and go through

Results

The Bet has been completed, so let’s see the results and compare the contracts. Keep the Tatooine remix window open and examine the outcome of the bet. A couple things to note

  • The getPot is twice the getBetAmount
  • The getTakerGuess and getOriginatorGuess have propagated between the contracts

Let’s do the same on the Dagobah Remix instance. The contract attributes are the same! Pretty cool…

It looks like the taker one this one(Obi-Wan), so there is nothing left to do but payout the bet results. Either Yoda or Obi-Wan can execute the payout, but Yoda has elected to do it to prove that he is a good sport.

With the Dagobah remix instance still open, click the payout function button at the bottom and watch the transaction go through.

Now note that in both the contract instances in the separate nodes that the value returned from getPot is 0 as the contract’s balance has been paid out to the winner.

You should also see Obi-Wan’s balance go up ~10 ether as well, but it may be hard to tell since the miner is continually adding to the ether balance as well.

Conclusion

Remix is an awesome tool for Solidity development. It is very helpful to use Remix alongside a tool like truffle to gain more insight into the code itself and into the contract’s integration into the ecosystem. I would actually recommend writing your code in Remix first to get the bulk of it correct and then tune it within the truffle framework.

What we did in this part of our series was not trivial, so you are to be congratulated! Hopefully, the integration of Remix into your arsenal has deepened your understanding of how to interact and think in Ethereum. In our next article we will begin using web3 to interact with our smart contract.

--

--