Skip to content

Commit

Permalink
Merge pull request #49 from gnosischain/fix-ask
Browse files Browse the repository at this point in the history
Fix ask
  • Loading branch information
riccardo-gnosis authored Sep 10, 2024
2 parents eb444b3 + 6484971 commit 198d32e
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 5 deletions.
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ venv/
ENV/
env.bak/
venv.bak/
*/.env

# Spyder project settings
.spyderproject
Expand Down Expand Up @@ -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/
#.idea/


*/*.whl
*/*.tar.gz

*/bin

*/pyvenv.cfg
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ cd api
python3 -m venv .venv
. .venv/bin/activate
pip3 install -r requirements-dev.txt
pip install setuptools
```

### Run application
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -96,4 +111,26 @@ yarn
```
cd app
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
docker ps
docker exec -it <container_name_or_id> /bin/bash
docker exec -it <container_name_or_id> flask -A api create_access_keys
docker exec -it <container_name_or_id> flask -A api create_enabled_token xDAI 100 0x0000000000000000000000000000000000000000 0.01 native
docker logs -f <container_name_or_id>
```

5 changes: 5 additions & 0 deletions api/api/routes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

from flask import Blueprint, current_app, jsonify, request
from web3 import Web3

Expand Down Expand Up @@ -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'])
Expand All @@ -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()
Expand Down
18 changes: 16 additions & 2 deletions api/api/services/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion api/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
-r requirements.txt

flake8==7.0.0
isort==5.13.2
isort==5.13.2
setuptools==1.0.1
47 changes: 47 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 198d32e

Please sign in to comment.