Offline experiments with Remix and local RSKJ Node
The Remix IDE is a popular environment to experiment with and deploy smart contracts written in Solidity for Ethereum-like blockchains including RSK. In this post we go over a few simple steps to run Remix IDE locally along with a local RSKJ node. A local setup is useful for offline testing and can also serve as an alternative to the Ethereum foundation’s service.
Components
Remix users work with the following components
- A blockchain: This may be a local blockchain e.g.
ganache
,geth
,rskj
, or a public blockchain endpoint e.g. https://public-node.testnet.rsk.co. - A wallet to manage blockchain credentials (accounts, private keys, signatures). This is often a browser extension e.g. Nifty or Metamask. Users can also use a hardware wallet.
- Remix IDE in a Web Browser, typically https://remix.ethereum.org via Chrome or Firefox.
To use Remix, users connect their browser wallet to a relevant blockchain. This is only needed if users wish to deploy or administer smart contracts. Those using Remix IDE just to compile contracts (no deployment) do not have to use a wallet or connect to any blockchain.
In the current exercise we run two of the above components within separate docker containers: one to run a blockchain (a local RSKJ node) and another to serve Remix. We then connect to these services as before — through a browser (with a wallet) on the host machine. I used Google Chrome with Nifty wallet extension enabled.
A Ubuntu 20.04 LTS machine served as the host system. I used a docker image with Ubuntu 18.04 LTS base for both containers. The process is quite straightforward and largely follows instructions from respective docs (RSKJ and Remix).
Steps in brief
- Follow these instructions to install RSKJ. Start the node in regtest mode
- Follow these instructions to install Remix IDE. Start the service.
- Enable a wallet extension in your browser. Connect wallet to the local RSK blockchain (custom RPC endpoint, with CORS enabled)
- Use [JSON RPC] to fund the account in the wallet so we can pay for gas costs of deploying contracts from Remix
#use eth_accounts() to
#find out pre-funded accounts on the node (regtest)curl localhost:4444 -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_accounts","params": [],"id":1}' #use eth_sendTransaction() to fund the wallet account#change "to", "from", "value" as neededcurl localhost:4444 -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{"from":"0xcd2a3d9f938e13cd947ec05abc7fe734df8dd826","to": "0xf87594Cb9036302G1081753D2d6D4Fb7e9127Ac5","gas":"0x76c0","gasPrice": "0x9184e72a000","value": "0xb6255df5f50080000","data": "0x"}],"id":1}'
Now we can use our own Remix service to compile and deploy contracts on our own local (experimental) blockchain.
Steps in more detail
Note: Depending on your user account (user id), you may need to use sudo
when installing.
Install RSKJ
RSKJ is a Java implementation of an RSK node. Use the general steps described here or this reproducible build. Some steps are reproduced here for convenience. I suggest you follow those from source links as they are more likely to be up to date. Binaries are also available for some systems. An x64 system, with at least 2 cores and 8GB RAM is recommended.
apt-get update -y && \
apt-get install -y git curl gnupg openjdk-8-jdk && \
rm -rf /var/lib/apt/lists/* && \
apt-get autoremove -y && \
apt-get cleangpg --keyserver https://secchannel.rsk.co/release.asc --recv-keys 1A92D8942171AFA951A857365DECF4415E3B8FA4
gpg --finger 1A92D8942171AFA951A857365DECF4415E3B8FA4# clone the project into a local directory
git clone --single-branch --depth 1 --branch PAPYRUS-2.1.0 https://github.com/rsksmart/rskj.git /code/rskj# switch to the directory
cd /code/rskj
gpg --verify SHA256SUMS.asc
sha256sum --check SHA256SUMS.asc#run the config script, changing persmission with chmod +x as needed
./configure.sh# build the project, automated tests can be run later if desired
./gradlew clean build -x test
#The built JARS will be in the directory `rskj-core/build/libs/`
# For a quick check of the installation, we can start the node in `regtest` mode.java -cp rskj-core/build/libs/rskj-core-2.1.0-SNAPSHOT-all.jar co.rsk.Start --regtest
Cross Origin Resource Sharing: In order to connect to the node from a browser based wallet, we should run the node with CORS enabled. See node configuration for details. One way to do this is to start a node with this option (instead of the one above)
java -cp rskj-core/build/libs/rskj-core-2.1.0-SNAPSHOT-all.jar -Drpc.providers.web.cors=* co.rsk.Start --regtest
If the node starts normally, then there will be no messages. This can seem odd the first time. But the node is working! It can be accessed via http://localhost:4444
from another terminal attached to the container. This is also the URL to use for custom RPC endpoint for browser wallets.
Test the node with a JSON remote procedure call (RPC) using curl
such as
curl localhost:4444/1.1.0/ -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}'
the response should be something like
{"jsonrpc":"2.0","id":1,"result":"0x10b"}
For later use, forward the port from docker container to host machine. Using an environment such as Visual Studio Code — with extensions for docker — makes this really simple.
Install Remix IDE
Remix IDE is open source with installation instructions here. There is a desktop version available as well (that I have not tested).
Assuming we already have curl, git, wget, yarn
etc installed. Run commands with sudo as needed.
# Install a recent version of node from debsource ..
curl -sL https://deb.nodesource.com/setup_15.x -o nodesource_setup.sh# make it executable and execute
bash chmod +x node*.sh# Install the nx CLI
npm install -g @nrwl/cli # to enable nx executable commands# Install Yarn package manager
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.listapt-get update && sudo apt-get install yarn# Clone the Remix repo (needs wget)
apt-get install wgetgit clone https://github.com/ethereum/remix-project.gitcd remix-project# install and serve
npm install
nx build remix-ide --with-deps
nx serve
If using docker, forward the port so we can access the service from browser on host machine.