1. 程式人生 > >Create Ethereum API Services with Parity & Django Rest Framework

Create Ethereum API Services with Parity & Django Rest Framework

Create Ethereum API Services with Parity & Django Rest Framework

Making a REST API Services will help you to connect any clients of choice to the Ether Network: Chrome Extensions, Mac App, iOS App or Android App.

I am personally building Swiftly — an iMessage Extensions for Ethereum so I need a REST API to interact with the network.

1. Install Parity and Its Dependencies

Install Rust

Install and Build Parity inside Digital Ocean Droplet

$ bash <(curl https://get.parity.io -Lk)

Sync with the Ethereum network

$ parity

2. Config Parity for Digital Ocean

Create Parity .service file to start and restart when Ethereum node is down.

$ cd /etc/systemmd/system$ vim parity.service

Parity.service should look like this

If you want to use additional configuration for Parity, you can use Parity Generator and put your config.toml this directory

$ cd /root/.local/share/io.parity.ethereum$ vim config.toml

The ‘config.toml’ should look like this with your own additional configuration.

Now lets update our changes and start parity.service

$ systemctl daemon-reload      // Reload Daemon$ service parity start         // Start Parity$ service parity status        // Check if the Service running$ top                          // Check CPU & Memory Usage of Parity

I also put all my Parity Ethereum Node inside a separate 100G Digital Ocean Block Storage since you need a least 40G and some additional disk space in future.

Now lets test interacting with the main network using RPC APIs using Curl and the eth_getBalance API. You can read more about other available APIs here.

$ curl --data '{"method":"eth_getBalance","params":["0x407d73d8a49eeb85d32cf465507dd71d507100c1"],"id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST localhost:8545

3. Write REST API Services using Django and Django Rest Framework

$ cd home/django/django_project$ pip install djangorestframework coreapi$ python manage.py startapp api$ python manage.py migrate

And then we need to edit settings.py file inside django_project folder, add our app api, rest_framework into INSTALLED_APPS.

Create Transaction models inside our Database

We need a Serializer class to convert our data into json format for our APIs.

Now we can write our Views to display APIs endpoints and accept parameters from our future requests.

Edit our urls.py inside django_project folder to create our APIs endpoints.

OK now we just need our views to interact with RPC APIs. After this, we need migrate our database, test APIs with Postman and we are ready to go.

$ python manage.py makemigrations$ python manage.py migrate$ service nginx restart

References:

About the author: Troy DO is an iOS and Machine Learning Consultant. He has more than 4 years experience building and launching mobile and web products. Recently, he has been working with a lot of AI-related projects in the area of Healthcare and Financial Technology with his team at Top Flight Apps.