NEO Smart Contracts: Workflow Improvements

in #neo7 years ago

In this post I'll be going over a couple of quick setup tips that can help to save you time by improving your dev setup workflow.

If you haven't already already gone though the hello world and the dev environment setup tutorials, I'd suggest you quickly run through those before continuing with this post.

Hello World Smart Contract Tutorial

Dev Environment Setup Tutorial

Let's face it, carpal tunnel is real, and as much as I want you to come back and ready my blog posts, it's more important that you have a fast and efficient workflow with minimal friction between you and your contract code.

Bash Scripts

This is where bash scripts can come in handy. If you haven't used them before, they are just shortcuts in the terminal that we can use to refer to larger commands. We can edit the list of these in out ~/.bashrc files in our root directories, both on local machine in in our docker container.

Local Machine

To access your .bashrc on your local computer you can open it at the root with your favorite text editor. In my case it will be:

atom ~/.bashrc


Here we can add our alias commands. The first one is for spinning up the docker container. While this is commonly done once, you will eventually run out of gas, and have to recreate the container. As such, having this on hand will have you a trip to the interwebs to copy pasta.

alias neodocker="docker run -d --name neo-privnet-with-gas -p 20333-20336:20333-20336/tcp -p 30333-30336:30333-30336/tcp -v ~/Documents/smartContracts:/opt/neo-python/smartContracts metachris/neo-privnet-with-gas"


It's important to note that this is a bit modified from the version found in the dev environment setup tutorial. Since it is now a part of our bashrc, we need to update the volume reference path to our local directory that we will be sharing. In my case, it was updated from "$(pwd)" to ~/Documents/smartContracts


The next alias is a short cut for when you need to SSH into your docker container.

alias neossh="docker exec -it neo-privnet-with-gas /bin/bash"


Now on our local machine we can simply use the calls neodocker to spin up the container if it's not already created, and neossh to jack in. Since recreting the container is very much less common, you will probably just be using neossh for the majority of the time.

Docker Container

Within the container we can edit the bashrc file at the root in command line, or simply create one in the shared folder on our local machine and copy it over.

Once running neossh, you will be placed in the root directory of the docker container, but most of the fun happens in the opt/neo-python directory. This command gets you from root to fun times in 4 chars.

alias cdnp='cd opt/neo-python'


Once in the neo-python directory, we always want to quickly jump into our neo-python cli. With this command we can now do so by just typing neocli

alias neocli='python3 prompt.py -c protocol.privnet.json'

Preset Docker Image

If you are too lazy to set up the bashrc commands in the docker container, you are in luck. I made a docker image of my own which is exactly the same as neo-privnet-with-gas, but with these queries already setup for you~!

neo-privnet-plus

As an added bonus, I've also pre-downloaded the main.wallet file from the s3 bucket into the docker container neo-python folder. Saves you one more step when recreating your docker container.


In the case that you do choose to use this alternative, please make sure that you go back and update your bash scripts on your local machine to the following:

neodocker

alias neodocker="docker run -d --name neo-privnet-plus -p 20333-20336:20333-20336/tcp -p 30333-30336:30333-30336/tcp -v ~/Documents/smartContracts:/opt/neo-python/smartContracts nickfujita/neo-privnet-plus"


neossh

alias neossh="docker exec -it neo-privnet-plus /bin/bash"


I you use this alternative docker image, and you dont go back and update your saved queries or bashrc...


General Docker Scripts

Here are a couple other random docker commands that I use:

alias dps="docker ps"
alias dpsa="docker ps -a"


Please use the following 2 with caution, because you WILL close containers that you didn't mean to if you are not careful

drmfa () {
  docker rm -f $(docker ps -aq)
}


Called with drmf {container_id}

drmf () {
  docker rm -f "$1"
}


Lastly, if you don't like the abbreviations for any of the commands, the greatest part is that since they are all in your bashrc files, you are free to update them to w/e works best for you.

Once you get all these set up, you should be flying around the terminal like a mad man, and will have no excuses to not get straight to work on your contract code, like I am doing right now by writing this post :p


If you found this tutorial helpful, please donate at:

NEO/GAS/NEP5 tokens: AR8rRBxgWw5siKsp1dUmfTLy6QQTjcqoqB
ETH/ERC20 tokens: 0x575970777095575fDd12d9cD3B105b41D8DD1344
NANO/XRB: xrb_3rpmim57tqbrb8hhjfzm1x5uk135yhmgqoj7j7wsufpo53e3ad5k93ggy1cd
LTC: M8nZ7FwJXSMAkAfiGmTKjbB7MWpdwsp52d
BTC: 3LcA1ztxFjghexRNWPyGXA59VFU3gF4PTz
BCH: 1LXLiF7SjWGday6gm61hzbYHKbSAMevwXo