Skip to content

moerphous/moerphous-server

Repository files navigation

Moerphous Server

pre-commit.ci status linting: pylint Code style: black Static typing: mypy License: MIT Codeql

architecture

A Fully Async-based backend for Moerphous.

Note: MongoDB is being used to store only the wallet classic_address and its seed to make transactions work. This project is just a POC for an NFT marketplace backend. In the future, once deployed on the mainnet, MetaMask will be utilized to connect the wallet.

Table of Contents

Development Requirements

  • Make (GNU command)
  • Python (>= 3.8)
  • Poetry (1.2)

Project Structure

❯ tree app
.
├── auth          # Package contains different config files for the `auth` app.
│   ├── crud.py       # Module contains different CRUD operations performed on the database.
│   ├── router.py     # Module contains different routes for this api.
│   └── schemas.py    # Module contains different schemas for this api for validation purposes.
├── wallets      # Package contains different config files for the `wallets` app.
│   ├── crud.py       # Module contains different CRUD operations performed on the database.
│   ├── models.py     # Module contains different models for ODMs to inteact with database.
│   ├── router.py     # Module contains different routes for this api.
│   └── schemas.py    # Module contains different schemas for this api for validation purposes.
├── nfts      # Package contains different config files for the `nfts` app.
│   ├── crud.py       # Module contains different CRUD operations performed on the database.
│   ├── models.py     # Module contains different models for ODMs to inteact with database.
│   ├── router.py     # Module contains different routes for this api.
│   └── schemas.py    # Module contains different schemas for this api for validation purposes.
├── utils         # Package contains different common utility modules for the whole project.
│   ├── dependencies.py     # A utility script that yield a session for each request to make the crud call work.
│   ├── engine.py           # A utility script that initializes an ODMantic engine and client and set them as app state variables.
│   ├── jwt.py              # A utility script for JWT.
│   ├── mixins.py           # A utility script that contains common mixins for different models.
├── config.py     # Module contains the main configuration settings for project.
├── __init__.py
├── main.py       # Startup script. Starts uvicorn.
└── py.typed      # mypy related file.

Installation with Make

The best way to configure, install main dependencies, and run the project is by using make. So, ensure you have make installed and configured on your machine. If it is not the case, head over to this thread on StackOverflow to install it on windows, or this thread to install it on Mac OS.

Having make installed and configured on your machine, you can now run make under the root directory of this project to explore different available commands to run:

make

Please use 'make <target>' where <target> is one of:

venv                     Create a virtual environment
install                  Install the package and all required core dependencies
run                      Running the app locally
deploy-deta              Deploy the app on a Deta Micro
clean                    Remove all build, test, coverage and Python artifacts
lint                     Check style with pre-commit
test                     Run tests quickly with pytest
test-all                 Run tests on every Python version with tox
coverage                 Check code coverage quickly with the default Python
build                    Build docker containers services
up                       Spin up the containers
down                     Stop all running containers

1. Create a virtualenv

make venv

2. Activate the virtualenv

source .venv/bin/activate

3. Install dependencies

make install

Note: This command will automatically generate a .env file from .env.example, uninstall the old version of poetry on your machine, then install latest version 1.2.2, and install the required main dependencies.

4. Setup a MongoDB Atlas account

Head over to the official website to create a MongoDB account and a cluster.

5. Set your MongoDB Credentials

Fill in the following environment variables in your .env file accordingly:

# Database
MONGODB_USERNAME=
MONGODB_PASSWORD=
MONGODB_HOST=cluster_name.example.mongodb.net
MONGODB_DATABASE=xrpl

6. Create a Pinata Account

Register an account on pinata, and create a new API under the development tab.

7. Set your Pinata Credentials

Set the following environment variable in your .env file according to your credentials:

# Pinata Cloud
PINATA_API_KEY=
PINATA_API_SECRET=

8. Generate a secret key

Generate a secret key using OpenSSL and update its env var in the .env file.

openssl rand -hex 128

afa1639545d53ecf83c9f8acf4704abe1382f9a9dbf76d2fd229d4795a4748712dbfe7cf1f0a812f1c0fad2d47c8343cd1017b22fc3bf43d052307137f6ba68cd2cb69748b561df846873a6257e3569d6307a7e022b82b79cb3d6e0fee00553d80913c1dcf946e2e91e1dfcbba1ed9f34c9250597c1f70f572744e91c68cbe76
# App config:
JWT_SECRET_KEY=afa1639545d53ecf83c9f8acf4704abe1382f9a9dbf76d2fd229d4795a4748712dbfe7cf1f0a812f1c0fad2d47c8343cd1017b22fc3bf43d052307137f6ba68cd2cb69748b561df846873a6257e3569d6307a7e022b82b79cb3d6e0fee00553d80913c1dcf946e2e91e1dfcbba1ed9f34c9250597c1f70f572744e91c68cbe76
DEBUG=info

9. Run The Project Locally

make run

Note: You have to set DEBUG=info to access the docs.

Access Swagger Documentation

http://localhost:8000/docs

Access Redocs Documentation

http://localhost:8000/redocs

Authentication and Authorization

For development purposes, you can use openapi to interact with the endpoints. To do so, you have to follow the steps below in order:

  • Generate a faucet wallet through the /api/v1/wallet post endpoint.

create wallet

  • Copy the access token from the response, and use it to authorize openapi with the backend.

create wallet

authorize button

authorize token

As you can notice, the default openapi authorization form has been customized to authenticate wallets using only an access token.

  • Now you can interact with any endpoint that require authentication.

wallet info

Deployments

Deploy locally with Compose v2

To run the entire platform, you have to clone the moerphous submodule using the following command:

git submodule update --init --recursive

Once that is done, make sure you have compose v2 installed and configured on your machine, and run the following command to build the predefined docker services(make sure you have a .env file beforehand):

Using Make

make build

or simply running:

docker compose build

Once that is done, you can spin up the containers:

Using Make

make up

or running:

docker compose up

Wait until the client service becomes available:

moerphous-client-1      | Starting the development server...

You can stop the running containers but issuing the following command on a separate terminal session:

make down

Deta Micros (Endpoints not working)

You'll need to create a Deta account to use the Deta version of the APIs.

Deploy on Deta

Deta CLI

Make sure you have Deta CLI installed on your machine. If it is not the case, just run the following command(on a Linux distro or Mac):

curl -fsSL https://get.deta.dev/cli.sh | sh

Manually add /home/<user_name>/.deta/bin/deta to your path:

PATH="/home/<user_name>/.deta/bin:$PATH"

Now you can deploy the app on a Deta Micro:

make deploy-deta

You can then use the Deta UI to check the logs and the URL the API is hosted on.

Notes:

  • Make sure your .env file is being provided with valid env vars values accordingly.

  • The main.py file is used as an entry point for Deta. The same goes for requirements.txt.

  • Deta Micros are limited to 512MB per deployment.

Heroku

This button will only deploy the server.

Deploy on Heroku

Heroku CLI (Experimental: Deploy the entire stack)

Note that this approach is not perfect because in the docker world, you should only have one service for each container, and you should use docker-compose to build and run more than two containers(e.g. one for the server and the other one for the client). However, Heroku doesn't support docker-compose with multiple services(except databases and such.). Hence running both services in one container.

To do so, ensure you have already installed and configured the Heroku CLI on your machine. If it is not the case, you can install it on Ubuntu using the following command:

sudo wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh

Now, you need to install the Heroku container registry plugin:

heroku plugins:install heroku-container-registry

Once that is completed, log in to your registry:

heroku container:login

Now, create a Heroku app:

heroku create <a unique app name>

You can list all your apps to verify that your recent app has been created:

heroku apps

Set your env variables in the .env file.

Build your container image:

docker compose -f heroku-compose.yml build

Deploy to Heroku:

heroku container:push web --app <your heroku app name>; heroku logs --tail

Once the build and push are completed, you can run the following command in a separate shell to interact with the app:

heroku open --app=<your app name>

You can refer to heroku dev center for more info. Happy Herokuing!

Render

Deploy to Render

Core Dependencies

The following packages are the main dependencies used to build this project:

License

This project and the accompanying materials are made available under the terms and conditions of the MIT LICENSE.

About

Moerphous server powered by FastAPI, ODMantic, MongoDB, PinataPY, XRPL-PY and friends.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published