Skip to content

Commit

Permalink
Merge pull request #15 from gnosischain/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
giacomognosis authored Mar 7, 2024
2 parents 6905502 + 4b3c889 commit a715ee8
Show file tree
Hide file tree
Showing 27 changed files with 657 additions and 234 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/publish-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ jobs:
labels: ${{ steps.meta.outputs.labels }}

eks-deployment-restart:
# Run job on branch dev only
if: github.ref == 'refs/heads/dev'
runs-on: ubuntu-latest
needs: build-and-push-image
permissions:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/publish-ui.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ jobs:
"REACT_APP_FAUCET_API_URL=${{ secrets.PROD_REACT_APP_FAUCET_API_URL}}"
eks-deployment-restart:
# Run job on branch dev only
if: github.ref == 'refs/heads/dev'
runs-on: ubuntu-latest
needs: build-and-push-image
permissions:
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ isort **/*.py --atomic
python3 -m flake8
```

### Operations

#### Add enabled tokens

To enable tokens on the API just run the command `create_enabled_token`.
Accepted parameters: token name, chain ID, token address, maximum amount per day per user, whether native or erc20

Samples below:

```
cd /api
flask -A api create_enabled_token GNO 10200 0x19C653Da7c37c66208fbfbE8908A5051B57b4C70 0.01 erc20
flask -A api create_enabled_token GNO 10200 0x0000000000000000000000000000000000000000 0.01 native
```

Once enabled, the token wil appear in the list of enabled tokens on the endpoint `api/v1/info`.

## ReactJS Frontend

### Requirements
Expand Down
30 changes: 8 additions & 22 deletions api/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
# Python
__pycache__
*.pyc
*.pyo
*.pyd
.Python
env
.env
pip-log.txt
pip-delete-this-directory.txt
.tox
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.log
.git
.mypy_cache
.pytest_cache
.hypothesis
# Ignore everything
*

# Allow files and directories
!/api
!/scripts
!/migrations
!/requirements.txt
3 changes: 1 addition & 2 deletions api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ FAUCET_AMOUNT=0.1
FAUCET_PRIVATE_KEY=0x0000000000000000000000000000000000000000000000000000000000000000
FAUCET_RPC_URL=https://rpc.chiadochain.net
FAUCET_CHAIN_ID=10200
FAUCET_ENABLED_TOKENS="[{\"address\": \"0x19C653Da7c37c66208fbfbE8908A5051B57b4C70\", \"name\":\"GNO\", \"maximumAmount\": 0.5}]"
# FAUCET_ENABLED_TOKENS=
FAUCET_DATABASE_URI=sqlite://
CAPTCHA_VERIFY_ENDPOINT=https://api.hcaptcha.com/siteverify
CAPTCHA_SECRET_KEY=0x0000000000000000000000000000000000000000
23 changes: 17 additions & 6 deletions api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
FROM python:3.8-alpine
FROM python:3.10-alpine

COPY requirements.txt /tmp/requirements.txt
RUN pip install --no-cache-dir -r /tmp/requirements.txt

COPY . /api
ENV PYTHONUNBUFFERED 1
WORKDIR /api

CMD ["gunicorn", "--bind", "0.0.0.0:8000", "api:create_app()"]
COPY requirements.txt ./

# Signal handling for PID1 https://github.com/krallin/tini
RUN apk add --update --no-cache tini libpq && \
apk add --no-cache --virtual .build-dependencies alpine-sdk libffi-dev && \
pip install --no-cache-dir -r requirements.txt && \
apk del .build-dependencies && \
find /usr/local \
\( -type d -a -name test -o -name tests \) \
-o \( -type f -a -name '*.pyc' -o -name '*.pyo' \) \
-exec rm -rf '{}' +

COPY . .

ENTRYPOINT ["/sbin/tini", "--"]
16 changes: 9 additions & 7 deletions api/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

from flask import Flask
from flask_cors import CORS
from flask_migrate import Migrate

from .manage import create_access_keys_cmd, create_enabled_token_cmd
from .routes import apiv1
from .services import Cache, Web3Singleton
from .services.database import db, migrate
from .services import Web3Singleton
from .services.database import db


def setup_logger(log_level):
Expand All @@ -32,17 +34,17 @@ def create_app():
app = Flask(__name__)
# Initialize main settings
app.config.from_object('api.settings')
# Initialize Cache
app.config['FAUCET_CACHE'] = Cache(app.config['FAUCET_RATE_LIMIT_TIME_LIMIT_SECONDS'])
# Initialize API Routes
app.register_blueprint(apiv1, url_prefix="/api/v1")
# Add cli commands
app.cli.add_command(create_access_keys_cmd)
app.cli.add_command(create_enabled_token_cmd)

with app.app_context():
db.init_app(app)
migrate.init_app(app, db)
db.create_all() # Create database tables for our data models
Migrate(app, db)

# Initialize Web3 class
# Initialize Web3 class for latter usage
w3 = Web3Singleton(app.config['FAUCET_RPC_URL'], app.config['FAUCET_PRIVATE_KEY'])

setup_cors(app)
Expand Down
17 changes: 16 additions & 1 deletion api/api/const.py
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
NATIVE_TOKEN_ADDRESS='native'
from enum import Enum

NATIVE_TOKEN_ADDRESS = 'native'
DEFAULT_NATIVE_MAX_AMOUNT_PER_DAY = 0.01
DEFAULT_ERC20_MAX_AMOUNT_PER_DAY = 0.01

CHAIN_NAMES = {
1: 'ETHEREUM MAINNET',
100: 'GNOSIS CHAIN',
10200: 'CHIADO CHAIN'
}


class FaucetRequestType(Enum):
web = 'web'
cli = 'cli'
64 changes: 64 additions & 0 deletions api/api/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import logging

import click
from flask import current_app
from flask.cli import with_appcontext

from .services import Web3Singleton
from .services.database import AccessKey, AccessKeyConfig, Token
from .utils import generate_access_key


@click.command(name='create_access_keys')
@with_appcontext
def create_access_keys_cmd():
access_key_id, secret_access_key = generate_access_key()
access_key = AccessKey()
access_key.access_key_id = access_key_id
access_key.secret_access_key = secret_access_key
access_key.save()

config = AccessKeyConfig()
config.access_key_id = access_key.access_key_id
config.chain_id = current_app.config["FAUCET_CHAIN_ID"]
config.save()

logging.info(f'Access Key ID : ${access_key_id}')
logging.info(f'Secret access key: ${secret_access_key}')


@click.command(name='create_enabled_token')
@click.argument('name')
@click.argument('chain_id')
@click.argument('address')
@click.argument('max_amount_day')
@click.argument('type')
@with_appcontext
def create_enabled_token_cmd(name, chain_id, address, max_amount_day, type):
w3 = Web3Singleton(
current_app.config['FAUCET_RPC_URL'],
current_app.config['FAUCET_PRIVATE_KEY']
)

# check if Token already exists
check_token = Token.get_by_address(w3.to_checksum_address(address))

if check_token:
raise Exception('Token %s already exists' % address)

# Checks
if type.lower() not in ('native', 'erc20'):
raise Exception('Type must be any of (erc20, native) got %s' % type)
if float(max_amount_day) <= 0:
raise Exception('Max amount per day must be greater than 0, got %d' % float(max_amount_day))

token = Token()
token.name = name
token.chain_id = chain_id
token.address = w3.to_checksum_address(address)
token.max_amount_day = float(max_amount_day)
token.type = type
token.enabled = True
token.save()

logging.info('Token created successfully')
Loading

0 comments on commit a715ee8

Please sign in to comment.