Skip to content

Commit

Permalink
Merge pull request #4 from dipdup-io/dipdup-7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
droserasprout authored Nov 30, 2023
2 parents f7f377c + 38cfe49 commit 6c90e2b
Show file tree
Hide file tree
Showing 24 changed files with 243 additions and 328 deletions.
2 changes: 1 addition & 1 deletion api/tzprofiles/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Add metadata and build files
!tzprofiles
!pyproject.toml
!pdm.lock
!*.lock
!README.md

# Add Python code
Expand Down
2 changes: 1 addition & 1 deletion api/tzprofiles/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
!**/Dockerfile
!**/Makefile
!**/pyproject.toml
!**/pdm.lock
!**/*.lock
!**/README.md
!**/.keep

Expand Down
46 changes: 46 additions & 0 deletions api/tzprofiles/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
.ONESHELL:
.PHONY: $(MAKECMDGOALS)
MAKEFLAGS += --no-print-directory
##
## 🚧 DipDup developer tools
##
PACKAGE=tzprofiles
TAG=latest
COMPOSE=deploy/compose.yaml

help: ## Show this help (default)
@grep -Fh "##" $(MAKEFILE_LIST) | grep -Fv grep -F | sed -e 's/\\$$//' | sed -e 's/##//'

all: ## Run an entire CI pipeline
make format lint

format: ## Format with all tools
make black

lint: ## Lint with all tools
make ruff mypy

##

black: ## Format with black
black .

ruff: ## Lint with ruff
ruff check --fix .

mypy: ## Lint with mypy
mypy --no-incremental --exclude ${PACKAGE} .

##

image: ## Build Docker image
docker buildx build . -t ${PACKAGE}:${TAG}

up: ## Run Compose stack
docker-compose -f ${COMPOSE} up -d --build
docker-compose -f ${COMPOSE} logs -f

down: ## Stop Compose stack
docker-compose -f ${COMPOSE} down

##
35 changes: 24 additions & 11 deletions api/tzprofiles/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,48 @@ DipDup indexer for Tezos Profiles

This project is based on [DipDup](https://dipdup.io), a framework for building featureful dapps.

You need a Linux/macOS system with Python 3.11 installed. Use our installer for easy setup:
You need a Linux/macOS system with Python 3.11 installed. To install DipDup with pipx for the current user:

```bash
```shell
curl -Lsf https://dipdup.io/install.py | python3
```

See the [Installation](https://dipdup.io/docs/installation) page for all options.

## Usage

Run the indexer in-memory:
Run the indexer in memory:

```bash
```shell
dipdup run
```

Store data in SQLite database:

```bash
dipdup -c . -c configs/dipdup.sqlite.yml run
```shell
dipdup -c . -c configs/dipdup.sqlite.yaml run
```

Or spawn a docker-compose stack:
Or spawn a Compose stack with PostgreSQL and Hasura:

```bash
cp deploy/.env.default deploy/.env
# Edit .env before running
docker-compose -f deploy/compose.yaml up
```shell
cd deploy
cp .env.default .env
# Edit .env file before running
docker-compose up
```

## Development setup

To set up the development environment:

```shell
pdm install
$(pdm venv activate)
```

Run `make all` to run full CI check or `make help` to see other available commands.

## Usage

For now, only a GraphQL API is available at `/v1/graphql` (to be used with
Expand Down
4 changes: 1 addition & 3 deletions api/tzprofiles/configs/dipdup.compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ sentry:
environment: ${SENTRY_ENVIRONMENT:-""}

prometheus:
host: 0.0.0.0

logging: ${LOGLEVEL:-INFO}
host: 0.0.0.0
4 changes: 1 addition & 3 deletions api/tzprofiles/configs/dipdup.sqlite.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
database:
kind: sqlite
path: ${SQLITE_PATH:-/tmp/tzprofiles.sqlite}

logging: ${LOGLEVEL:-INFO}
path: ${SQLITE_PATH:-/tmp/tzprofiles.sqlite}
4 changes: 1 addition & 3 deletions api/tzprofiles/configs/dipdup.swarm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ sentry:
environment: ${SENTRY_ENVIRONMENT:-""}

prometheus:
host: 0.0.0.0

logging: ${LOGLEVEL:-INFO}
host: 0.0.0.0
5 changes: 4 additions & 1 deletion api/tzprofiles/configs/replay.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Run `dipdup new --replay configs/replay.yaml` to generate new project from this replay
# To refresh existing project run `dipdup init --base --force` after modifying this file.
# To generate a new project from this replay run `dipdup new --replay <path_to_file>`.
#
spec_version: 2.0
replay:
dipdup_version: 7
Expand All @@ -13,3 +15,4 @@ replay:
postgres_data_path: /var/lib/postgresql/data
hasura_image: hasura/graphql-engine:latest
line_length: 120
package_manager: pdm
1 change: 0 additions & 1 deletion api/tzprofiles/deploy/.env.default
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#
HASURA_HOST=hasura
HASURA_SECRET=
LOGLEVEL=INFO
POSTGRES_DB=dipdup
POSTGRES_HOST=db
POSTGRES_PASSWORD=
Expand Down
1 change: 0 additions & 1 deletion api/tzprofiles/deploy/sqlite.env.default
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# This env file was generated automatically by DipDup. Do not edit it!
# Create a copy with .env extension, fill it with your values and run DipDup with `--env-file` option.
#
LOGLEVEL=INFO
SQLITE_PATH=/tmp/tzprofiles.sqlite
1 change: 0 additions & 1 deletion api/tzprofiles/deploy/swarm.env.default
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#
HASURA_HOST=tzprofiles_hasura
HASURA_SECRET=
LOGLEVEL=INFO
POSTGRES_DB=dipdup
POSTGRES_HOST=tzprofiles_db
POSTGRES_PASSWORD=
Expand Down
5 changes: 2 additions & 3 deletions api/tzprofiles/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def __init__(self, message="Credential doesn't exist in Kepler anymore"):
after=after_log(LOGGER, logging.WARNING), # type: ignore[arg-type]
)
async def retrieve_claim(kepler_link: str) -> str:
orbit_id, file_hash = tuple(kepler_link.replace("kepler://", "").replace('v0:', '').split("/"))
orbit_id, file_hash = tuple(kepler_link.replace("kepler://", "").replace("v0:", "").split("/"))
url = urljoin(KEPLER_ENDPOINT, orbit_id + "/" + file_hash)
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
Expand Down Expand Up @@ -116,8 +116,7 @@ async def resolve_claim(kepler_link: str, checksum: str) -> dict[str, Any]:
if len(errors) > 0:
raise FailedVerification(claim, str(errors))

claim_json = json.loads(claim)
return claim_json
return json.loads(claim)


def validate_vc(vc: dict[str, Any], address: str) -> None:
Expand Down
2 changes: 1 addition & 1 deletion api/tzprofiles/hooks/on_index_rollback.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async def on_index_rollback(
from_level: int,
to_level: int,
) -> None:
await ctx.execute_sql('on_index_rollback')
await ctx.execute_sql("on_index_rollback")
await ctx.rollback(
index=index.name,
from_level=from_level,
Expand Down
2 changes: 1 addition & 1 deletion api/tzprofiles/hooks/on_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
async def on_reindex(
ctx: HookContext,
) -> None:
await ctx.execute_sql('on_reindex')
await ctx.execute_sql("on_reindex")
2 changes: 1 addition & 1 deletion api/tzprofiles/hooks/on_restart.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
async def on_restart(
ctx: HookContext,
) -> None:
await ctx.execute_sql('on_restart')
await ctx.execute_sql("on_restart")
2 changes: 1 addition & 1 deletion api/tzprofiles/hooks/on_synchronized.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
async def on_synchronized(
ctx: HookContext,
) -> None:
await ctx.execute_sql('on_synchronized')
await ctx.execute_sql("on_synchronized")
22 changes: 11 additions & 11 deletions api/tzprofiles/hooks/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
from tzprofiles.models import TZProfile

SLEEP = 5
_ENV_BATCH = os.getenv('BATCH')
_ENV_BATCH = os.getenv("BATCH")
BATCH = int(_ENV_BATCH) if _ENV_BATCH is not None else 100
IGNORED_PROFILES = (
'KT1G6jaUQkRcxJcnrNLjCTn7xgD686PM2mEd',
"KT1G6jaUQkRcxJcnrNLjCTn7xgD686PM2mEd",
)

async def _resolve(ctx: HookContext, profile: TZProfile):
ctx.logger.info(f'Resolving profile {profile.contract}')
ctx.logger.info("Resolving profile %s", profile.contract)

if profile.contract in IGNORED_PROFILES:
profile.failed = True
Expand All @@ -39,12 +39,12 @@ async def _resolve(ctx: HookContext, profile: TZProfile):

assert profile.account is not None
await ctx.update_contract_metadata(
network='mainnet',
network="mainnet",
address=profile.account,
metadata=profile.metadata,
)
ctx.logger.debug(
'Resolved in %.2f, saved in %.2f seconds',
"Resolved in %.2f, saved in %.2f seconds",
resolved_at - started_at,
time.perf_counter() - resolved_at,
)
Expand All @@ -57,24 +57,24 @@ async def resolver(
) -> None:
ctx.logger.logger.level = logging.INFO # config not being used here
set_logger(ctx.logger)
ctx.logger.info('Starting resolver daemon')
ctx.logger.info("Starting resolver daemon")
while True:
ctx.logger.info('Starting loop')
ctx.logger.info("Starting loop")
profiles = await TZProfile.filter(resolved=False).limit(BATCH).all()
# .only('account') doesn't with dipdup wrapper of versioned data
if not profiles:
ctx.logger.info('No profiles to resolve, sleeping %s seconds', SLEEP)
ctx.logger.info("No profiles to resolve, sleeping %s seconds", SLEEP)
await asyncio.sleep(SLEEP)
continue

start = time.time()

ctx.logger.info('Starting batch')
ctx.logger.info("Starting batch")
await asyncio.gather(*[_resolve(ctx, profile) for profile in profiles])
ctx.logger.info('Finished batch')
ctx.logger.info("Finished batch")

end = time.time()
remain = start + 1 - end
if remain > 0:
await asyncio.sleep(remain)
ctx.logger.info('Finishing loop')
ctx.logger.info("Finishing loop")
23 changes: 11 additions & 12 deletions api/tzprofiles/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typing import Any
from typing import Dict

from dipdup import fields
from dipdup.models import Model
Expand Down Expand Up @@ -42,18 +41,18 @@ def reset(self) -> None:
self.ethereum = None # type: ignore[assignment]

@property
def metadata(self) -> Dict[str, Any]:
def metadata(self) -> dict[str, Any]:
return {
'alias': self.alias,
'description': self.description,
'logo': self.logo,
'website': self.website,
'twitter': self.twitter,
'domain_name': self.domain_name,
'discord': self.discord,
'github': self.github,
'ethereum': self.ethereum,
"alias": self.alias,
"description": self.description,
"logo": self.logo,
"website": self.website,
"twitter": self.twitter,
"domain_name": self.domain_name,
"discord": self.discord,
"github": self.github,
"ethereum": self.ethereum,
}

class Meta:
table = 'tzprofiles'
table = "tzprofiles"
Loading

0 comments on commit 6c90e2b

Please sign in to comment.