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 diff --git a/README.md b/README.md index 0a754ba..7a4b05e 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,26 @@ yarn ``` cd app yarn start -``` \ No newline at end of file +``` + + +### 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 + +docker ps + +docker exec -it /bin/bash + +docker exec -it flask -A api create_access_keys + +docker exec -it flask -A api create_enabled_token xDAI 100 0x0000000000000000000000000000000000000000 0.01 native + +docker logs -f + +``` + diff --git a/api/api/routes.py b/api/api/routes.py index 1f6f927..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,6 +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) + 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']) @@ -78,11 +81,13 @@ def _ask(request_data, request_headers, validate_captcha=True, validate_csrf=Tru current_app.config['FAUCET_ADDRESS'], recipient, amount_wei) + 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) + logging.info(f'token with address {validator.token.address} txn: {tx_hash}') # save transaction data on DB transaction = Transaction() 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): 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 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