From c669cb9ffef1529bd84fdb6a02d122b97ae76598 Mon Sep 17 00:00:00 2001 From: riccardo <106812074+riccardo-gnosis@users.noreply.github.com> Date: Mon, 9 Sep 2024 17:00:36 +0200 Subject: [PATCH 1/6] update: more gitignore --- .gitignore | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 6769e21..45483d0 100644 --- a/.gitignore +++ b/.gitignore @@ -127,6 +127,7 @@ venv/ ENV/ env.bak/ venv.bak/ +*/.env # Spyder project settings .spyderproject @@ -157,4 +158,12 @@ cython_debug/ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ \ No newline at end of file +#.idea/ + + +*/*.whl +*/*.tar.gz + +*/bin + +*/pyvenv.cfg \ No newline at end of file From aa65751502a0f1f568e413e0f0e7fcc98d4e3af4 Mon Sep 17 00:00:00 2001 From: riccardo <106812074+riccardo-gnosis@users.noreply.github.com> Date: Mon, 9 Sep 2024 17:01:03 +0200 Subject: [PATCH 2/6] add: docker compose and more documentation for easier local testing --- README.md | 37 ++++++++++++++++++++++++++++++++++- docker-compose.yaml | 47 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 docker-compose.yaml diff --git a/README.md b/README.md index 0a754ba..284ebb4 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,8 @@ cd api python3 -m venv .venv . .venv/bin/activate pip3 install -r requirements-dev.txt + +pip install setuptools ``` ### Run application @@ -66,6 +68,19 @@ flask -A api create_enabled_token xDAI 10200 0x000000000000000000000000000000000 Once enabled, the token will appear in the list of enabled tokens on the endpoint `api/v1/info`. +#### Create access keys + +To create access keys on the API just run the command `create_access_keys`. +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_access_keys +``` + + #### Change maximum daily amounts per user If you want to change the amount you are giving out for a specific token, make sure you have sqlite @@ -96,4 +111,24 @@ yarn ``` cd app yarn start -``` \ No newline at end of file +``` + + +### Docker Compose Up and create Access keys + +``` + +docker-compose up --build -d + +docker ps + +docker exec -it /bin/bash + +docker exec -it faee3118d09e flask -A api create_access_keys + +docker exec -it faee3118d09e flask -A api create_enabled_token xDAI 100 0x0000000000000000000000000000000000000000 0.01 native + +docker logs -f faee3118d09e + +``` + diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..c4d31b4 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,47 @@ +version: '3.8' + +services: + api: + build: + context: ./api + dockerfile: Dockerfile + # image: "ghcr.io/gnosischain/faucet-py-api:v0.4.9@sha256:58761f4fa91274dc393fbaf3a61c434f6b6627ada8a1cfe92a9b8bff265fe5f5" + container_name: api + command: ["sh", "/api/scripts/production_run_api.sh"] + env_file: "./api/.env" + environment: + - FAUCET_DATABASE_URI=sqlite:////db/gc_faucet.db + ports: + - "8000:8000" + volumes: + - db-volume:/db:rw + deploy: + resources: + limits: + cpus: '0.5' + memory: 500M + reservations: + cpus: '0.25' + memory: 250M + + # ui: + # build: + # context: ./app + # dockerfile: Dockerfile + # # image: "ghcr.io/gnosischain/faucet-py-ui:v0.4.9-gc@sha256:819f44e801d69d847c8866ff717281a2e989cb5e9076aad56c7a410b7a552b06" + # container_name: ui + # ports: + # - "80:80" + # env_file: "./app/.env" + # deploy: + # resources: + # limits: + # cpus: '0.1' + # memory: 50M + # reservations: + # cpus: '0.05' + # memory: 25M + +volumes: + db-volume: + driver: local \ No newline at end of file From c7a61ce6264709ecba1e4d3d62be3f1e790506ab Mon Sep 17 00:00:00 2001 From: riccardo <106812074+riccardo-gnosis@users.noreply.github.com> Date: Mon, 9 Sep 2024 17:01:34 +0200 Subject: [PATCH 3/6] add: a bit more logging during ask --- api/api/routes.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/api/api/routes.py b/api/api/routes.py index 1f6f927..eea1d5f 100644 --- a/api/api/routes.py +++ b/api/api/routes.py @@ -69,6 +69,7 @@ def _ask(request_data, request_headers, validate_captcha=True, validate_csrf=Tru try: # convert recipient address to checksum address recipient = Web3.to_checksum_address(validator.recipient) + print(f"will try to send {amount_wei} to {recipient}") w3 = Web3Singleton(current_app.config['FAUCET_RPC_URL'], current_app.config['FAUCET_PRIVATE_KEY']) @@ -78,11 +79,13 @@ def _ask(request_data, request_headers, validate_captcha=True, validate_csrf=Tru current_app.config['FAUCET_ADDRESS'], recipient, amount_wei) + print(f"native token txn: {tx_hash}") else: tx_hash = claim_token(w3, current_app.config['FAUCET_ADDRESS'], recipient, amount_wei, validator.token.address) + print(f"token with address txn: {validator.token.address}") # save transaction data on DB transaction = Transaction() From 5d20283adfbd3a26daa2c269a79151e4ab1dbc3e Mon Sep 17 00:00:00 2001 From: riccardo <106812074+riccardo-gnosis@users.noreply.github.com> Date: Mon, 9 Sep 2024 17:02:18 +0200 Subject: [PATCH 4/6] update: fetch nonce before sending txn and use nonce in txn to avoid situation where nonce is not up to speed --- api/api/services/transaction.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/api/api/services/transaction.py b/api/api/services/transaction.py index d5c1f04..794ebfa 100644 --- a/api/api/services/transaction.py +++ b/api/api/services/transaction.py @@ -28,12 +28,26 @@ def claim_native(w3, sender, recipient, amount): - recipient: String - amount: integer in wei format """ + + nonce = w3.eth.get_transaction_count(sender) + tx_dict = { 'from': sender, 'to': recipient, - 'value': amount + 'value': amount, + 'nonce': nonce } - return w3.eth.send_transaction(tx_dict).hex() + + tx_hash = w3.eth.send_transaction(tx_dict).hex() + + # this may cause a timeout, keep here for testing purposes + # receipt = w3.eth.wait_for_transaction_receipt(tx_hash) + # if receipt.status == 1: + # print(f"transaction successful {tx_hash}") + # else: + # print(f"transaction failed {tx_hash}") + + return tx_hash def claim_token(w3, sender, recipient, amount, token_address): From dc8b07af3cf3cb17b9241d4abed387e513dd097a Mon Sep 17 00:00:00 2001 From: riccardo <106812074+riccardo-gnosis@users.noreply.github.com> Date: Tue, 10 Sep 2024 10:52:44 +0200 Subject: [PATCH 5/6] update: add supertools as dependency and update readme --- README.md | 8 +++++--- api/requirements-dev.txt | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 284ebb4..7a4b05e 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,8 @@ yarn start ### Docker Compose Up and create Access keys +If you do not reset the volume you will be able to reuse the sqlite database with latest data (access keys and enabled tokens) + ``` docker-compose up --build -d @@ -124,11 +126,11 @@ docker ps docker exec -it /bin/bash -docker exec -it faee3118d09e flask -A api create_access_keys +docker exec -it flask -A api create_access_keys -docker exec -it faee3118d09e flask -A api create_enabled_token xDAI 100 0x0000000000000000000000000000000000000000 0.01 native +docker exec -it flask -A api create_enabled_token xDAI 100 0x0000000000000000000000000000000000000000 0.01 native -docker logs -f faee3118d09e +docker logs -f ``` diff --git a/api/requirements-dev.txt b/api/requirements-dev.txt index 246729a..7b854df 100644 --- a/api/requirements-dev.txt +++ b/api/requirements-dev.txt @@ -1,4 +1,5 @@ -r requirements.txt flake8==7.0.0 -isort==5.13.2 \ No newline at end of file +isort==5.13.2 +setuptools==1.0.1 \ No newline at end of file From 64849712858a0ff26952be9157a981ecdbe669ea Mon Sep 17 00:00:00 2001 From: riccardo <106812074+riccardo-gnosis@users.noreply.github.com> Date: Tue, 10 Sep 2024 10:59:45 +0200 Subject: [PATCH 6/6] use logging lib --- api/api/routes.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/api/api/routes.py b/api/api/routes.py index eea1d5f..b8afab0 100644 --- a/api/api/routes.py +++ b/api/api/routes.py @@ -1,3 +1,5 @@ +import logging + from flask import Blueprint, current_app, jsonify, request from web3 import Web3 @@ -69,7 +71,7 @@ def _ask(request_data, request_headers, validate_captcha=True, validate_csrf=Tru try: # convert recipient address to checksum address recipient = Web3.to_checksum_address(validator.recipient) - print(f"will try to send {amount_wei} to {recipient}") + logging.info(f'will try to send {amount_wei} to {recipient}') w3 = Web3Singleton(current_app.config['FAUCET_RPC_URL'], current_app.config['FAUCET_PRIVATE_KEY']) @@ -79,13 +81,13 @@ def _ask(request_data, request_headers, validate_captcha=True, validate_csrf=Tru current_app.config['FAUCET_ADDRESS'], recipient, amount_wei) - print(f"native token txn: {tx_hash}") + logging.info(f'native token txn: {tx_hash}') else: tx_hash = claim_token(w3, current_app.config['FAUCET_ADDRESS'], recipient, amount_wei, validator.token.address) - print(f"token with address txn: {validator.token.address}") + logging.info(f'token with address {validator.token.address} txn: {tx_hash}') # save transaction data on DB transaction = Transaction()