Talking to Ethereum Smart Contracts 1/2
Tim Cotten
AI, Gaming & Web3. Building the Inori Network for Autonomous AI Agents. Founder of Scrypted. Pre-seed: @a16zcrypto CSX. Ex-EA, Ex-Mythic. cottenio.eth ??
Server Side Implementation of a Simple Update Routine
Original article: https://blog.cotten.io/talking-to-ethereum-smart-contracts-1-2-af88e8f57508
This article is part of a two-part step-by-step guide to creating a simple smart contract update service on an Amazon Web Services EC2 instance running Node.JS with the Ethereum JavaScript API (web3.js). Part two is available at: https://blog.cotten.io/talking-to-ethereum-smart-contracts-2-2-1e9737b9da59
Simple Goal: A One Way Update
As part of an ongoing effort to develop prototype oracle services I decided to test out the functionality of an off-chain server communicating with an on-chain smart contract.
To do so I’ll be creating an EC2 instance in the Amazon cloud and configuring it with Node.JS and the Ethereum Javascript API (web3.js). Additionally I’ll be deploying a simple smart contract in the Ethereum Rinkeby test network for the server to communicate with.
The goal will be updating a single uint in the smart contract at regular intervals while rejecting any external attempts (e.g. not my server) to execute an update.
Server Setup
We’ll follow a standard setup on EC2 using the Amazon Linux AMI. After initiating an instance and logging in the system let’s us know we need to run updates.
As the message suggests we’ll run sudo yum update -y to auto-install all the updates that have been added since the last major revision of Amazon Linux 2.
After we’ve done this we also want to install the dependencies for interacting with the Ethereum network, and we’ll start with Node.JS.
Amazon already has a helpful guide for install Node.JS on an Amazon server, and we’ll condense the instructions here:
1. Install nvm (node version manager)
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.0/install.sh | bash
Note that it recommends restarting your terminal (logging out/back in) so the proper export directive is applied. I prefer to just re-source.
source .bashrc
If we run export at the command line we’ll see the list of environment variables and that the ones referenced above have been properly added to our session:
We’ll then actually execute nvm so we can install Node.JS.
. ~/.nvm/nvm.sh
An aside about the command above and the Amazon documentation: the preceding dot in the command is same as the source command, and all this command is doing is sourcing the file. I only point this out as weird since earlier the documentation recommends restarting your shell session instead of running source on .bashrc — a bit of a disconnect in style.
Meanwhile, the above command has no output but we’re ready to go.
2. Let’s install Node.JS using nvm
We’ll just install the latest version of Node.JS:
nvm install node
That was easy.
3. Let’s install Web3.js
Node.JS is installed now and we have the node package manager (npm). So let’s install the Ethereum JavaScript API (web3.js):
npm install web3
Again, that was easy.
4. Let’s install the other dependencies
For the purposes of this project we are not going to run a full Ethereum node. I deployed this setup on a small 8GB disk (t2-micro default), and since we’re not going to be monitoring the smart contract state live (we’re just sending updates) we can get away with using a lite wallet and a connection to a remote node.
I learned this (mostly) headless method from a great CodeTract article: https://medium.com/@codetractio/try-out-ethereum-using-only-nodejs-and-npm-eabaaaf97c80
Before we do anything else though, let’s install Git, since some of the dependencies will need it for syncing up.
sudo yum install git -y
Also, a C++ compiler:
sudo yum install gcc gcc-c++ -y
And now we can install the other Ethereum dependencies:
npm install ethereumjs-util ethereumjs-tx eth-lightwallet
Ready to rock! Next time: deploying a simple contract and talking to it.